blob: 96d27b8f111c16376ce932e49ec1f27f275cdb63 [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";
Zixuan Qufecb6062022-11-12 04:44:31 +00002766 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
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
Zixuan Qufecb6062022-11-12 04:44:31 +00003585TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3586 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3587 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3588 std::list<NotifyArgs> unused =
3589 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3590
3591 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3592
3593 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3594 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3595
3596 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3597 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3598 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3599 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3600 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3601}
3602
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003603// --- KeyboardInputMapperTest_ExternalDevice ---
3604
3605class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3606protected:
Chris Yea52ade12020-08-27 16:49:20 -07003607 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003608};
3609
3610TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003611 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3612 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003613
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003614 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3615 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3616 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3617 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003618
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003619 KeyboardInputMapper& mapper =
3620 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3621 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003622
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003624 NotifyKeyArgs args;
3625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3626 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3627
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003628 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3630 ASSERT_EQ(uint32_t(0), args.policyFlags);
3631
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3634 ASSERT_EQ(uint32_t(0), args.policyFlags);
3635
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3638 ASSERT_EQ(uint32_t(0), args.policyFlags);
3639
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3642 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3643
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003644 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3647}
3648
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003649TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003650 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003652 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003655
Powei Fengd041c5d2019-05-03 17:11:33 -07003656 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 KeyboardInputMapper& mapper =
3658 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3659 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003660
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003661 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003662 NotifyKeyArgs args;
3663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3664 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3665
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003666 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3668 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3669
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3672 ASSERT_EQ(uint32_t(0), args.policyFlags);
3673
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003674 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(uint32_t(0), args.policyFlags);
3677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3684 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3685}
3686
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687// --- CursorInputMapperTest ---
3688
3689class CursorInputMapperTest : public InputMapperTest {
3690protected:
3691 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3692
Michael Wright17db18e2020-06-26 20:51:44 +01003693 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003694
Chris Yea52ade12020-08-27 16:49:20 -07003695 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003696 InputMapperTest::SetUp();
3697
Michael Wright17db18e2020-06-26 20:51:44 +01003698 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003699 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003700 }
3701
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003702 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3703 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003704
Michael Wrighta9cf4192022-12-01 23:46:39 +00003705 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003706 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3707 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3708 }
3709
3710 void prepareSecondaryDisplay() {
3711 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003712 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003713 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003714 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003715
3716 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3717 float pressure) {
3718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3719 0.0f, 0.0f, 0.0f, EPSILON));
3720 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721};
3722
3723const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3724
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3726 int32_t originalY, int32_t rotatedX,
3727 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728 NotifyMotionArgs args;
3729
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003730 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003735 ASSERT_NO_FATAL_FAILURE(
3736 assertCursorPointerCoords(args.pointerCoords[0],
3737 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3738 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003739}
3740
3741TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003742 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003743 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003745 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746}
3747
3748TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003749 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003750 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003753}
3754
3755TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003757 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003758
3759 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003760 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003761
3762 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003763 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3764 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003765 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3766 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3767
3768 // When the bounds are set, then there should be a valid motion range.
3769 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3770
3771 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3775 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3776 1, 800 - 1, 0.0f, 0.0f));
3777 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3778 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3779 2, 480 - 1, 0.0f, 0.0f));
3780 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3781 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3782 0.0f, 1.0f, 0.0f, 0.0f));
3783}
3784
3785TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
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
3789 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003790 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003791
3792 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3793 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3794 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3795 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3796 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3797 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3798 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3799 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3800 0.0f, 1.0f, 0.0f, 0.0f));
3801}
3802
3803TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003804 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806
arthurhungdcef2dc2020-08-11 14:47:50 +08003807 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 NotifyMotionArgs args;
3810
3811 // Button press.
3812 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -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_DOWN, 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003829 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3830 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3831 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3832
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3834 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3835 ASSERT_EQ(DEVICE_ID, args.deviceId);
3836 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3837 ASSERT_EQ(uint32_t(0), args.policyFlags);
3838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3839 ASSERT_EQ(0, args.flags);
3840 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3841 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3842 ASSERT_EQ(0, args.edgeFlags);
3843 ASSERT_EQ(uint32_t(1), args.pointerCount);
3844 ASSERT_EQ(0, args.pointerProperties[0].id);
3845 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003846 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003847 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3848 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3849 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3850
Michael Wrightd02c5b62014-02-10 15:10:22 -08003851 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003852 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3853 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 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);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, 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));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -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 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3873 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3874 ASSERT_EQ(DEVICE_ID, args.deviceId);
3875 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3876 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003877 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3878 ASSERT_EQ(0, args.flags);
3879 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3880 ASSERT_EQ(0, args.buttonState);
3881 ASSERT_EQ(0, args.edgeFlags);
3882 ASSERT_EQ(uint32_t(1), args.pointerCount);
3883 ASSERT_EQ(0, args.pointerProperties[0].id);
3884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003885 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003886 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3887 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3888 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3889}
3890
3891TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003892 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003893 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003894
3895 NotifyMotionArgs args;
3896
3897 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003902 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3903 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3904 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003905
3906 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003907 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3908 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3910 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003911 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3912 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003913}
3914
3915TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003916 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003917 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003918
3919 NotifyMotionArgs args;
3920
3921 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3923 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3925 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003926 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3929 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003930 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003931
Michael Wrightd02c5b62014-02-10 15:10:22 -08003932 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003937 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003938
3939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003940 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003941 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942}
3943
3944TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003945 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003946 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003947
3948 NotifyMotionArgs args;
3949
3950 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003957 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3958 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3959 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003960
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003963 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3964 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3965 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003966
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003968 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
3969 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
3970 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003973 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3974 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3975 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003976
3977 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003982 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003983
3984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003986 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003987}
3988
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003989TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003990 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003991 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003992 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
3993 // need to be rotated.
3994 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996
Michael Wrighta9cf4192022-12-01 23:46:39 +00003997 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003998 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4006}
4007
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004008TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004009 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004011 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4012 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004013 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004014
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004015 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004016 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4018 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4019 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4020 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4021 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
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004026 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004027 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4029 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4030 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4031 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4032 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));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004036
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004037 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004038 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004039 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4040 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4041 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4042 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4043 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4044 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4045 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4046 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4047
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004048 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004049 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4056 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4057 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004058}
4059
4060TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004061 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004062 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004063
4064 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4065 mFakePointerController->setPosition(100, 200);
4066 mFakePointerController->setButtonState(0);
4067
4068 NotifyMotionArgs motionArgs;
4069 NotifyKeyArgs keyArgs;
4070
4071 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4075 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4076 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4077 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004078 ASSERT_NO_FATAL_FAILURE(
4079 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004080
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4082 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4083 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4084 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004085 ASSERT_NO_FATAL_FAILURE(
4086 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004087
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4089 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004091 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092 ASSERT_EQ(0, motionArgs.buttonState);
4093 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004094 ASSERT_NO_FATAL_FAILURE(
4095 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004096
4097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004098 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004099 ASSERT_EQ(0, motionArgs.buttonState);
4100 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004101 ASSERT_NO_FATAL_FAILURE(
4102 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004103
4104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_EQ(0, motionArgs.buttonState);
4107 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004108 ASSERT_NO_FATAL_FAILURE(
4109 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
4111 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4114 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4116 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4117 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4118 motionArgs.buttonState);
4119 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4120 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004121 ASSERT_NO_FATAL_FAILURE(
4122 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4127 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4128 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));
4133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4134 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4135 motionArgs.buttonState);
4136 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4137 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004138 ASSERT_NO_FATAL_FAILURE(
4139 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004140
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004141 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004144 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004145 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4146 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004147 ASSERT_NO_FATAL_FAILURE(
4148 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
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(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004152 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4153 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004154 ASSERT_NO_FATAL_FAILURE(
4155 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4161 ASSERT_EQ(0, motionArgs.buttonState);
4162 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004163 ASSERT_NO_FATAL_FAILURE(
4164 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004167
4168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004169 ASSERT_EQ(0, motionArgs.buttonState);
4170 ASSERT_EQ(0, mFakePointerController->getButtonState());
4171 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004172 ASSERT_NO_FATAL_FAILURE(
4173 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004174
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4176 ASSERT_EQ(0, motionArgs.buttonState);
4177 ASSERT_EQ(0, mFakePointerController->getButtonState());
4178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004181
4182 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4186 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4187 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4192 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004193 ASSERT_NO_FATAL_FAILURE(
4194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004195
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4198 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4199 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004200 ASSERT_NO_FATAL_FAILURE(
4201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004202
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004206 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004207 ASSERT_EQ(0, motionArgs.buttonState);
4208 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004209 ASSERT_NO_FATAL_FAILURE(
4210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
4212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004214 ASSERT_EQ(0, motionArgs.buttonState);
4215 ASSERT_EQ(0, mFakePointerController->getButtonState());
4216
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004217 ASSERT_NO_FATAL_FAILURE(
4218 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4220 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4221 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4222
4223 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4227 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4228 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004231 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004232 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4233 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004234 ASSERT_NO_FATAL_FAILURE(
4235 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004236
4237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4239 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4240 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004241 ASSERT_NO_FATAL_FAILURE(
4242 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004244 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4245 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004248 ASSERT_EQ(0, motionArgs.buttonState);
4249 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252
4253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4255 ASSERT_EQ(0, motionArgs.buttonState);
4256 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4261 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4262 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4263
4264 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4268 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4269 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4274 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004275 ASSERT_NO_FATAL_FAILURE(
4276 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004277
4278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4279 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4280 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4281 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004282 ASSERT_NO_FATAL_FAILURE(
4283 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004284
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4286 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004289 ASSERT_EQ(0, motionArgs.buttonState);
4290 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
4294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4296 ASSERT_EQ(0, motionArgs.buttonState);
4297 ASSERT_EQ(0, 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
Michael Wrightd02c5b62014-02-10 15:10:22 -08004301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4303 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4304
4305 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004306 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4307 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4309 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4310 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004313 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4315 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004316 ASSERT_NO_FATAL_FAILURE(
4317 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004318
4319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4321 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4322 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004323 ASSERT_NO_FATAL_FAILURE(
4324 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004325
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 ASSERT_EQ(0, motionArgs.buttonState);
4331 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004334
4335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4336 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4337 ASSERT_EQ(0, motionArgs.buttonState);
4338 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004339 ASSERT_NO_FATAL_FAILURE(
4340 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004341
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4343 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4344 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4345}
4346
4347TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004348 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004349 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004350
4351 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4352 mFakePointerController->setPosition(100, 200);
4353 mFakePointerController->setButtonState(0);
4354
4355 NotifyMotionArgs args;
4356
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004361 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4362 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4364 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 +00004365 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004366}
4367
4368TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004369 addConfigurationProperty("cursor.mode", "pointer");
4370 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004371 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004372
4373 NotifyDeviceResetArgs resetArgs;
4374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4375 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4376 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4377
4378 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4379 mFakePointerController->setPosition(100, 200);
4380 mFakePointerController->setButtonState(0);
4381
4382 NotifyMotionArgs args;
4383
4384 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4386 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4387 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4389 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4392 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 +00004393 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004394
4395 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4400 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4402 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4404 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4405 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4407 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4408
4409 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004410 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4411 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4413 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4414 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4416 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4418 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4419 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4421 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4422
4423 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004424 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4428 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4429 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4431 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 +00004432 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004433
4434 // Disable pointer capture and check that the device generation got bumped
4435 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004436 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004437 mFakePolicy->setPointerCapture(false);
4438 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004439 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004440
4441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004442 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4443
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4448 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4451 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 +00004452 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453}
4454
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004455/**
4456 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4457 * pointer acceleration or speed processing should not be applied.
4458 */
4459TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4460 addConfigurationProperty("cursor.mode", "pointer");
4461 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4462 100.f /*high threshold*/, 10.f /*acceleration*/);
4463 mFakePolicy->setVelocityControlParams(testParams);
4464 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4465
4466 NotifyDeviceResetArgs resetArgs;
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4468 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4469 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4470
4471 NotifyMotionArgs args;
4472
4473 // Move and verify scale is applied.
4474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4480 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4481 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4482 ASSERT_GT(relX, 10);
4483 ASSERT_GT(relY, 20);
4484
4485 // Enable Pointer Capture
4486 mFakePolicy->setPointerCapture(true);
4487 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4488 NotifyPointerCaptureChangedArgs captureArgs;
4489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4490 ASSERT_TRUE(captureArgs.request.enable);
4491
4492 // Move and verify scale is not applied.
4493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4497 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4499 ASSERT_EQ(10, args.pointerCoords[0].getX());
4500 ASSERT_EQ(20, args.pointerCoords[0].getY());
4501}
4502
Prabir Pradhan208360b2022-06-24 18:37:04 +00004503TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4504 addConfigurationProperty("cursor.mode", "pointer");
4505 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4506
4507 NotifyDeviceResetArgs resetArgs;
4508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4509 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4510 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4511
4512 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004513 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004514
4515 NotifyMotionArgs args;
4516
4517 // Verify that the coordinates are rotated.
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4522 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4524 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4525 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4526
4527 // Enable Pointer Capture.
4528 mFakePolicy->setPointerCapture(true);
4529 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4530 NotifyPointerCaptureChangedArgs captureArgs;
4531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4532 ASSERT_TRUE(captureArgs.request.enable);
4533
4534 // Move and verify rotation is not applied.
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4539 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4540 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4541 ASSERT_EQ(10, args.pointerCoords[0].getX());
4542 ASSERT_EQ(20, args.pointerCoords[0].getY());
4543}
4544
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004545TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004546 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004547
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004548 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004549 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004550
4551 // Set up the secondary display as the display on which the pointer should be shown.
4552 // The InputDevice is not associated with any display.
4553 prepareSecondaryDisplay();
4554 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004555 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4556
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004557 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004558 mFakePointerController->setPosition(100, 200);
4559 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004560
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004561 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004566 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4567 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4568 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004569 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004570}
4571
4572TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4573 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4574
4575 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004576 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004577
4578 // Set up the secondary display as the display on which the pointer should be shown,
4579 // and associate the InputDevice with the secondary display.
4580 prepareSecondaryDisplay();
4581 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4582 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4583 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4584
4585 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4586 mFakePointerController->setPosition(100, 200);
4587 mFakePointerController->setButtonState(0);
4588
4589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4594 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4595 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004596 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004597}
4598
4599TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4600 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4601
4602 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004603 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004604 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4605
4606 // Associate the InputDevice with the secondary display.
4607 prepareSecondaryDisplay();
4608 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4609 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4610
4611 // The mapper should not generate any events because it is associated with a display that is
4612 // different from the pointer display.
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004617}
4618
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004619// --- BluetoothCursorInputMapperTest ---
4620
4621class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4622protected:
4623 void SetUp() override {
4624 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4625
4626 mFakePointerController = std::make_shared<FakePointerController>();
4627 mFakePolicy->setPointerController(mFakePointerController);
4628 }
4629};
4630
4631TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4632 addConfigurationProperty("cursor.mode", "pointer");
4633 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4634
4635 nsecs_t kernelEventTime = ARBITRARY_TIME;
4636 nsecs_t expectedEventTime = ARBITRARY_TIME;
4637 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4638 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4640 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4641 WithEventTime(expectedEventTime))));
4642
4643 // Process several events that come in quick succession, according to their timestamps.
4644 for (int i = 0; i < 3; i++) {
4645 constexpr static nsecs_t delta = ms2ns(1);
4646 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4647 kernelEventTime += delta;
4648 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4649
4650 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4651 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4654 WithEventTime(expectedEventTime))));
4655 }
4656}
4657
4658TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4659 addConfigurationProperty("cursor.mode", "pointer");
4660 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4661
4662 nsecs_t expectedEventTime = ARBITRARY_TIME;
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4666 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4667 WithEventTime(expectedEventTime))));
4668
4669 // Process several events with the same timestamp from the kernel.
4670 // Ensure that we do not generate events too far into the future.
4671 constexpr static int32_t numEvents =
4672 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4673 for (int i = 0; i < numEvents; i++) {
4674 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4675
4676 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4680 WithEventTime(expectedEventTime))));
4681 }
4682
4683 // By processing more events with the same timestamp, we should not generate events with a
4684 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4685 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4686 for (int i = 0; i < 3; i++) {
4687 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4688 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4691 WithEventTime(cappedEventTime))));
4692 }
4693}
4694
4695TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4696 addConfigurationProperty("cursor.mode", "pointer");
4697 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4698
4699 nsecs_t kernelEventTime = ARBITRARY_TIME;
4700 nsecs_t expectedEventTime = ARBITRARY_TIME;
4701 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4702 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4705 WithEventTime(expectedEventTime))));
4706
4707 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4708 // smoothening is not needed, its timestamp is not affected.
4709 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4710 expectedEventTime = kernelEventTime;
4711
4712 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4713 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4715 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4716 WithEventTime(expectedEventTime))));
4717}
4718
Michael Wrightd02c5b62014-02-10 15:10:22 -08004719// --- TouchInputMapperTest ---
4720
4721class TouchInputMapperTest : public InputMapperTest {
4722protected:
4723 static const int32_t RAW_X_MIN;
4724 static const int32_t RAW_X_MAX;
4725 static const int32_t RAW_Y_MIN;
4726 static const int32_t RAW_Y_MAX;
4727 static const int32_t RAW_TOUCH_MIN;
4728 static const int32_t RAW_TOUCH_MAX;
4729 static const int32_t RAW_TOOL_MIN;
4730 static const int32_t RAW_TOOL_MAX;
4731 static const int32_t RAW_PRESSURE_MIN;
4732 static const int32_t RAW_PRESSURE_MAX;
4733 static const int32_t RAW_ORIENTATION_MIN;
4734 static const int32_t RAW_ORIENTATION_MAX;
4735 static const int32_t RAW_DISTANCE_MIN;
4736 static const int32_t RAW_DISTANCE_MAX;
4737 static const int32_t RAW_TILT_MIN;
4738 static const int32_t RAW_TILT_MAX;
4739 static const int32_t RAW_ID_MIN;
4740 static const int32_t RAW_ID_MAX;
4741 static const int32_t RAW_SLOT_MIN;
4742 static const int32_t RAW_SLOT_MAX;
4743 static const float X_PRECISION;
4744 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004745 static const float X_PRECISION_VIRTUAL;
4746 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
4748 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004749 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004750
4751 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4752
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004753 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004754 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004755
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 enum Axes {
4757 POSITION = 1 << 0,
4758 TOUCH = 1 << 1,
4759 TOOL = 1 << 2,
4760 PRESSURE = 1 << 3,
4761 ORIENTATION = 1 << 4,
4762 MINOR = 1 << 5,
4763 ID = 1 << 6,
4764 DISTANCE = 1 << 7,
4765 TILT = 1 << 8,
4766 SLOT = 1 << 9,
4767 TOOL_TYPE = 1 << 10,
4768 };
4769
Michael Wrighta9cf4192022-12-01 23:46:39 +00004770 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004771 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004772 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004774 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775 int32_t toRawX(float displayX);
4776 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004777 int32_t toRotatedRawX(float displayX);
4778 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004779 float toCookedX(float rawX, float rawY);
4780 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004781 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004782 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004783 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004784 float toDisplayY(int32_t rawY, int32_t displayHeight);
4785
Michael Wrightd02c5b62014-02-10 15:10:22 -08004786};
4787
4788const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4789const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4790const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4791const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4792const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4793const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4794const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4795const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004796const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4797const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4799const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4800const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4801const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4802const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4803const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4804const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4805const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4806const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4807const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4808const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4809const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004810const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4811 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4812const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4813 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004814const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4815 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816
4817const float TouchInputMapperTest::GEOMETRIC_SCALE =
4818 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4819 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4820
4821const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4822 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4823 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4824};
4825
Michael Wrighta9cf4192022-12-01 23:46:39 +00004826void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004827 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4828 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004829}
4830
4831void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4832 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004833 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004834}
4835
Michael Wrighta9cf4192022-12-01 23:46:39 +00004836void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004837 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4838 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4839 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004840}
4841
Michael Wrightd02c5b62014-02-10 15:10:22 -08004842void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004843 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4844 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4845 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4846 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004847}
4848
Jason Gerecke489fda82012-09-07 17:19:40 -07004849void TouchInputMapperTest::prepareLocationCalibration() {
4850 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4851}
4852
Michael Wrightd02c5b62014-02-10 15:10:22 -08004853int32_t TouchInputMapperTest::toRawX(float displayX) {
4854 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4855}
4856
4857int32_t TouchInputMapperTest::toRawY(float displayY) {
4858 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4859}
4860
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004861int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4862 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4863}
4864
4865int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4866 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4867}
4868
Jason Gerecke489fda82012-09-07 17:19:40 -07004869float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4870 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4871 return rawX;
4872}
4873
4874float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4875 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4876 return rawY;
4877}
4878
Michael Wrightd02c5b62014-02-10 15:10:22 -08004879float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004880 return toDisplayX(rawX, DISPLAY_WIDTH);
4881}
4882
4883float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4884 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004885}
4886
4887float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004888 return toDisplayY(rawY, DISPLAY_HEIGHT);
4889}
4890
4891float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4892 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004893}
4894
4895
4896// --- SingleTouchInputMapperTest ---
4897
4898class SingleTouchInputMapperTest : public TouchInputMapperTest {
4899protected:
4900 void prepareButtons();
4901 void prepareAxes(int axes);
4902
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004903 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4904 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4905 void processUp(SingleTouchInputMapper& mappery);
4906 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4907 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4908 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4909 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4910 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4911 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912};
4913
4914void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004915 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916}
4917
4918void SingleTouchInputMapperTest::prepareAxes(int axes) {
4919 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004920 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4921 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004922 }
4923 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004924 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4925 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004926 }
4927 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4929 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004930 }
4931 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4933 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 }
4935 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004936 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4937 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 }
4939}
4940
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004941void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4943 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004950}
4951
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004952void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004954}
4955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004956void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004958}
4959
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004960void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4961 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004962 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004963}
4964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004965void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004966 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967}
4968
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4970 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004971 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973}
4974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004975void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4976 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004977 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004978}
4979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004982}
4983
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004985 prepareButtons();
4986 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004987 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988
Harry Cutts16a24cc2022-10-26 15:22:19 +00004989 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990}
4991
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993 prepareButtons();
4994 prepareAxes(POSITION);
4995 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004996 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004998 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004999}
5000
5001TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005003 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004 prepareButtons();
5005 prepareAxes(POSITION);
5006 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008
5009 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005011
5012 // Virtual key is down.
5013 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5014 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5015 processDown(mapper, x, y);
5016 processSync(mapper);
5017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005020
5021 // Virtual key is up.
5022 processUp(mapper);
5023 processSync(mapper);
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5025
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005026 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005027}
5028
5029TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005031 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032 prepareButtons();
5033 prepareAxes(POSITION);
5034 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005035 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005036
5037 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005038 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039
5040 // Virtual key is down.
5041 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5042 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5043 processDown(mapper, x, y);
5044 processSync(mapper);
5045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5046
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005047 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048
5049 // Virtual key is up.
5050 processUp(mapper);
5051 processSync(mapper);
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5053
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005054 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055}
5056
5057TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005059 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060 prepareButtons();
5061 prepareAxes(POSITION);
5062 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005063 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005064
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005066 ASSERT_TRUE(
5067 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005068 ASSERT_TRUE(flags[0]);
5069 ASSERT_FALSE(flags[1]);
5070}
5071
5072TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005073 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005074 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005075 prepareButtons();
5076 prepareAxes(POSITION);
5077 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005078 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005079
arthurhungdcef2dc2020-08-11 14:47:50 +08005080 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081
5082 NotifyKeyArgs args;
5083
5084 // Press virtual key.
5085 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5086 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5087 processDown(mapper, x, y);
5088 processSync(mapper);
5089
5090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5091 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5092 ASSERT_EQ(DEVICE_ID, args.deviceId);
5093 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5094 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5095 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5096 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5097 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5098 ASSERT_EQ(KEY_HOME, args.scanCode);
5099 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5100 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5101
5102 // Release virtual key.
5103 processUp(mapper);
5104 processSync(mapper);
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5107 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5108 ASSERT_EQ(DEVICE_ID, args.deviceId);
5109 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5110 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5111 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5112 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5113 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5114 ASSERT_EQ(KEY_HOME, args.scanCode);
5115 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5116 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5117
5118 // Should not have sent any motions.
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5120}
5121
5122TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
arthurhungdcef2dc2020-08-11 14:47:50 +08005130 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131
5132 NotifyKeyArgs keyArgs;
5133
5134 // Press virtual key.
5135 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5136 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5137 processDown(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&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_DOWN, keyArgs.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5147 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5148 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5150 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5151
5152 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5153 // into the display area.
5154 y -= 100;
5155 processMove(mapper, x, y);
5156 processSync(mapper);
5157
5158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5159 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5160 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5161 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5162 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5163 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5164 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5165 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5166 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5167 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5168 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5169 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5170
5171 NotifyMotionArgs motionArgs;
5172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5173 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5174 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5175 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5176 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5178 ASSERT_EQ(0, motionArgs.flags);
5179 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5180 ASSERT_EQ(0, motionArgs.buttonState);
5181 ASSERT_EQ(0, motionArgs.edgeFlags);
5182 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5183 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5186 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5187 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5188 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5189 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5190
5191 // Keep moving out of bounds. Should generate a pointer move.
5192 y -= 50;
5193 processMove(mapper, x, y);
5194 processSync(mapper);
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5198 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5199 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5200 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5202 ASSERT_EQ(0, motionArgs.flags);
5203 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5204 ASSERT_EQ(0, motionArgs.buttonState);
5205 ASSERT_EQ(0, motionArgs.edgeFlags);
5206 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5207 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5208 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5209 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5210 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5211 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5212 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5214
5215 // Release out of bounds. Should generate a pointer up.
5216 processUp(mapper);
5217 processSync(mapper);
5218
5219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5220 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5221 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5222 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5223 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5224 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5225 ASSERT_EQ(0, motionArgs.flags);
5226 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5227 ASSERT_EQ(0, motionArgs.buttonState);
5228 ASSERT_EQ(0, motionArgs.edgeFlags);
5229 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5230 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5231 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5233 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5237
5238 // Should not have sent any more keys or motions.
5239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5241}
5242
5243TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005244 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005245 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005246 prepareButtons();
5247 prepareAxes(POSITION);
5248 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005249 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250
arthurhungdcef2dc2020-08-11 14:47:50 +08005251 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252
5253 NotifyMotionArgs motionArgs;
5254
5255 // Initially go down out of bounds.
5256 int32_t x = -10;
5257 int32_t y = -10;
5258 processDown(mapper, x, y);
5259 processSync(mapper);
5260
5261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5262
5263 // Move into the display area. Should generate a pointer down.
5264 x = 50;
5265 y = 75;
5266 processMove(mapper, x, y);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5275 ASSERT_EQ(0, motionArgs.flags);
5276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5277 ASSERT_EQ(0, motionArgs.buttonState);
5278 ASSERT_EQ(0, motionArgs.edgeFlags);
5279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Release. Should generate a pointer up.
5289 processUp(mapper);
5290 processSync(mapper);
5291
5292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5297 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5298 ASSERT_EQ(0, motionArgs.flags);
5299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5300 ASSERT_EQ(0, motionArgs.buttonState);
5301 ASSERT_EQ(0, motionArgs.edgeFlags);
5302 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5303 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5306 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5310
5311 // Should not have sent any more keys or motions.
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5314}
5315
Santos Cordonfa5cf462017-04-05 10:37:00 -07005316TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005317 addConfigurationProperty("touch.deviceType", "touchScreen");
5318 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5319
Michael Wrighta9cf4192022-12-01 23:46:39 +00005320 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005321 prepareButtons();
5322 prepareAxes(POSITION);
5323 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005324 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005325
arthurhungdcef2dc2020-08-11 14:47:50 +08005326 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005327
5328 NotifyMotionArgs motionArgs;
5329
5330 // Down.
5331 int32_t x = 100;
5332 int32_t y = 125;
5333 processDown(mapper, x, y);
5334 processSync(mapper);
5335
5336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5338 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5339 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5340 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5341 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5342 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5343 ASSERT_EQ(0, motionArgs.flags);
5344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5345 ASSERT_EQ(0, motionArgs.buttonState);
5346 ASSERT_EQ(0, motionArgs.edgeFlags);
5347 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5348 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5349 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5350 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5351 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5352 1, 0, 0, 0, 0, 0, 0, 0));
5353 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5354 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5356
5357 // Move.
5358 x += 50;
5359 y += 75;
5360 processMove(mapper, x, y);
5361 processSync(mapper);
5362
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5365 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5366 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5367 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5368 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5369 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5370 ASSERT_EQ(0, motionArgs.flags);
5371 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5372 ASSERT_EQ(0, motionArgs.buttonState);
5373 ASSERT_EQ(0, motionArgs.edgeFlags);
5374 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5375 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5378 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5379 1, 0, 0, 0, 0, 0, 0, 0));
5380 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5381 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5383
5384 // Up.
5385 processUp(mapper);
5386 processSync(mapper);
5387
5388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5390 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5391 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5392 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5393 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5394 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5395 ASSERT_EQ(0, motionArgs.flags);
5396 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5397 ASSERT_EQ(0, motionArgs.buttonState);
5398 ASSERT_EQ(0, motionArgs.edgeFlags);
5399 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5400 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5403 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5404 1, 0, 0, 0, 0, 0, 0, 0));
5405 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5406 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5408
5409 // Should not have sent any more keys or motions.
5410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5412}
5413
Michael Wrightd02c5b62014-02-10 15:10:22 -08005414TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005415 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005416 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005417 prepareButtons();
5418 prepareAxes(POSITION);
5419 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005420 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005421
arthurhungdcef2dc2020-08-11 14:47:50 +08005422 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005423
5424 NotifyMotionArgs motionArgs;
5425
5426 // Down.
5427 int32_t x = 100;
5428 int32_t y = 125;
5429 processDown(mapper, x, y);
5430 processSync(mapper);
5431
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5433 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5434 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5435 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5436 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5437 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5438 ASSERT_EQ(0, motionArgs.flags);
5439 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5440 ASSERT_EQ(0, motionArgs.buttonState);
5441 ASSERT_EQ(0, motionArgs.edgeFlags);
5442 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5443 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5446 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5447 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5448 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5450
5451 // Move.
5452 x += 50;
5453 y += 75;
5454 processMove(mapper, x, y);
5455 processSync(mapper);
5456
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5459 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5463 ASSERT_EQ(0, motionArgs.flags);
5464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5465 ASSERT_EQ(0, motionArgs.buttonState);
5466 ASSERT_EQ(0, motionArgs.edgeFlags);
5467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5468 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5471 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5475
5476 // Up.
5477 processUp(mapper);
5478 processSync(mapper);
5479
5480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5481 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5482 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5483 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5484 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5485 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5486 ASSERT_EQ(0, motionArgs.flags);
5487 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5488 ASSERT_EQ(0, motionArgs.buttonState);
5489 ASSERT_EQ(0, motionArgs.edgeFlags);
5490 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5491 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5493 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5494 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5495 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5496 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5497 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5498
5499 // Should not have sent any more keys or motions.
5500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5502}
5503
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005504TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 addConfigurationProperty("touch.deviceType", "touchScreen");
5506 prepareButtons();
5507 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005508 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5509 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005510 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
5512 NotifyMotionArgs args;
5513
5514 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005515 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005516 processDown(mapper, toRawX(50), toRawY(75));
5517 processSync(mapper);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5520 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5521 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5522
5523 processUp(mapper);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5526}
5527
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005528TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005529 addConfigurationProperty("touch.deviceType", "touchScreen");
5530 prepareButtons();
5531 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005532 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5533 // orientation-aware are affected by display rotation.
5534 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005535 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536
5537 NotifyMotionArgs args;
5538
5539 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005540 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005541 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005542 processDown(mapper, toRawX(50), toRawY(75));
5543 processSync(mapper);
5544
5545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5546 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5547 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5548
5549 processUp(mapper);
5550 processSync(mapper);
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5552
5553 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005554 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005555 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005556 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005557 processSync(mapper);
5558
5559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5560 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5561 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5562
5563 processUp(mapper);
5564 processSync(mapper);
5565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5566
5567 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005568 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005569 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005570 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5571 processSync(mapper);
5572
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5574 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5575 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5576
5577 processUp(mapper);
5578 processSync(mapper);
5579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5580
5581 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005582 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005583 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005584 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005585 processSync(mapper);
5586
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5588 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5589 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5590
5591 processUp(mapper);
5592 processSync(mapper);
5593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5594}
5595
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005596TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5597 addConfigurationProperty("touch.deviceType", "touchScreen");
5598 prepareButtons();
5599 prepareAxes(POSITION);
5600 addConfigurationProperty("touch.orientationAware", "1");
5601 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5602 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005603 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005604 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5605 NotifyMotionArgs args;
5606
5607 // Orientation 0.
5608 processDown(mapper, toRawX(50), toRawY(75));
5609 processSync(mapper);
5610
5611 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5612 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5613 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5614
5615 processUp(mapper);
5616 processSync(mapper);
5617 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5618}
5619
5620TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5621 addConfigurationProperty("touch.deviceType", "touchScreen");
5622 prepareButtons();
5623 prepareAxes(POSITION);
5624 addConfigurationProperty("touch.orientationAware", "1");
5625 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5626 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005627 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005628 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5629 NotifyMotionArgs args;
5630
5631 // Orientation 90.
5632 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5633 processSync(mapper);
5634
5635 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5636 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5637 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5638
5639 processUp(mapper);
5640 processSync(mapper);
5641 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5642}
5643
5644TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5645 addConfigurationProperty("touch.deviceType", "touchScreen");
5646 prepareButtons();
5647 prepareAxes(POSITION);
5648 addConfigurationProperty("touch.orientationAware", "1");
5649 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5650 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005651 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005652 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5653 NotifyMotionArgs args;
5654
5655 // Orientation 180.
5656 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5657 processSync(mapper);
5658
5659 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5660 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5661 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5662
5663 processUp(mapper);
5664 processSync(mapper);
5665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5666}
5667
5668TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5669 addConfigurationProperty("touch.deviceType", "touchScreen");
5670 prepareButtons();
5671 prepareAxes(POSITION);
5672 addConfigurationProperty("touch.orientationAware", "1");
5673 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5674 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005675 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005676 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5677 NotifyMotionArgs args;
5678
5679 // Orientation 270.
5680 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5681 processSync(mapper);
5682
5683 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5684 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5685 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5686
5687 processUp(mapper);
5688 processSync(mapper);
5689 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5690}
5691
5692TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5693 addConfigurationProperty("touch.deviceType", "touchScreen");
5694 prepareButtons();
5695 prepareAxes(POSITION);
5696 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5697 // orientation-aware are affected by display rotation.
5698 addConfigurationProperty("touch.orientationAware", "0");
5699 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5700 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5701
5702 NotifyMotionArgs args;
5703
5704 // Orientation 90, Rotation 0.
5705 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005706 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005707 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5708 processSync(mapper);
5709
5710 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5711 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5712 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5713
5714 processUp(mapper);
5715 processSync(mapper);
5716 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5717
5718 // Orientation 90, Rotation 90.
5719 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005720 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005721 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005722 processSync(mapper);
5723
5724 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5725 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5726 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5727
5728 processUp(mapper);
5729 processSync(mapper);
5730 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5731
5732 // Orientation 90, Rotation 180.
5733 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005734 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005735 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5736 processSync(mapper);
5737
5738 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5739 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5740 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5741
5742 processUp(mapper);
5743 processSync(mapper);
5744 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5745
5746 // Orientation 90, Rotation 270.
5747 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005748 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005749 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 -07005750 processSync(mapper);
5751
5752 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5753 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5754 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5755
5756 processUp(mapper);
5757 processSync(mapper);
5758 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5759}
5760
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005761TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5762 addConfigurationProperty("touch.deviceType", "touchScreen");
5763 prepareButtons();
5764 prepareAxes(POSITION);
5765 addConfigurationProperty("touch.orientationAware", "1");
5766 prepareDisplay(ui::ROTATION_0);
5767 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5768
5769 // Set a physical frame in the display viewport.
5770 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5771 viewport->physicalLeft = 20;
5772 viewport->physicalTop = 600;
5773 viewport->physicalRight = 30;
5774 viewport->physicalBottom = 610;
5775 mFakePolicy->updateViewport(*viewport);
5776 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5777
5778 // Start the touch.
5779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5780 processSync(mapper);
5781
5782 // Expect all input starting outside the physical frame to be ignored.
5783 const std::array<Point, 6> outsidePoints = {
5784 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5785 for (const auto& p : outsidePoints) {
5786 processMove(mapper, toRawX(p.x), toRawY(p.y));
5787 processSync(mapper);
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5789 }
5790
5791 // Move the touch into the physical frame.
5792 processMove(mapper, toRawX(25), toRawY(605));
5793 processSync(mapper);
5794 NotifyMotionArgs args;
5795 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5796 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5797 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5798 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5799
5800 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5801 for (const auto& p : outsidePoints) {
5802 processMove(mapper, toRawX(p.x), toRawY(p.y));
5803 processSync(mapper);
5804 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5805 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5806 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5807 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5808 }
5809
5810 processUp(mapper);
5811 processSync(mapper);
5812 EXPECT_NO_FATAL_FAILURE(
5813 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5814}
5815
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005818 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005819 prepareButtons();
5820 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005821 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822
5823 // These calculations are based on the input device calibration documentation.
5824 int32_t rawX = 100;
5825 int32_t rawY = 200;
5826 int32_t rawPressure = 10;
5827 int32_t rawToolMajor = 12;
5828 int32_t rawDistance = 2;
5829 int32_t rawTiltX = 30;
5830 int32_t rawTiltY = 110;
5831
5832 float x = toDisplayX(rawX);
5833 float y = toDisplayY(rawY);
5834 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5835 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5836 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5837 float distance = float(rawDistance);
5838
5839 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5840 float tiltScale = M_PI / 180;
5841 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5842 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5843 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5844 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5845
5846 processDown(mapper, rawX, rawY);
5847 processPressure(mapper, rawPressure);
5848 processToolMajor(mapper, rawToolMajor);
5849 processDistance(mapper, rawDistance);
5850 processTilt(mapper, rawTiltX, rawTiltY);
5851 processSync(mapper);
5852
5853 NotifyMotionArgs args;
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5856 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5857 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5858}
5859
Jason Gerecke489fda82012-09-07 17:19:40 -07005860TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005861 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005862 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005863 prepareLocationCalibration();
5864 prepareButtons();
5865 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005866 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005867
5868 int32_t rawX = 100;
5869 int32_t rawY = 200;
5870
5871 float x = toDisplayX(toCookedX(rawX, rawY));
5872 float y = toDisplayY(toCookedY(rawX, rawY));
5873
5874 processDown(mapper, rawX, rawY);
5875 processSync(mapper);
5876
5877 NotifyMotionArgs args;
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5880 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5881}
5882
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005885 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005886 prepareButtons();
5887 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005888 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889
5890 NotifyMotionArgs motionArgs;
5891 NotifyKeyArgs keyArgs;
5892
5893 processDown(mapper, 100, 200);
5894 processSync(mapper);
5895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5896 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5897 ASSERT_EQ(0, motionArgs.buttonState);
5898
5899 // press BTN_LEFT, release BTN_LEFT
5900 processKey(mapper, BTN_LEFT, 1);
5901 processSync(mapper);
5902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5903 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5904 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5905
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5908 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5909
Michael Wrightd02c5b62014-02-10 15:10:22 -08005910 processKey(mapper, BTN_LEFT, 0);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005914 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005915
5916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005918 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005919
5920 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5921 processKey(mapper, BTN_RIGHT, 1);
5922 processKey(mapper, BTN_MIDDLE, 1);
5923 processSync(mapper);
5924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5926 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5927 motionArgs.buttonState);
5928
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5931 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5932
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5935 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5936 motionArgs.buttonState);
5937
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 processKey(mapper, BTN_RIGHT, 0);
5939 processSync(mapper);
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005941 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 processKey(mapper, BTN_MIDDLE, 0);
5949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005951 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953
5954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 // press BTN_BACK, release BTN_BACK
5959 processKey(mapper, BTN_BACK, 1);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5962 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5963 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5968
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5971 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005972
5973 processKey(mapper, BTN_BACK, 0);
5974 processSync(mapper);
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005976 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978
5979 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(0, motionArgs.buttonState);
5982
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5984 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5985 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5986
5987 // press BTN_SIDE, release BTN_SIDE
5988 processKey(mapper, BTN_SIDE, 1);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5992 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5997
5998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6000 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001
6002 processKey(mapper, BTN_SIDE, 0);
6003 processSync(mapper);
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006010 ASSERT_EQ(0, motionArgs.buttonState);
6011
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6014 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6015
6016 // press BTN_FORWARD, release BTN_FORWARD
6017 processKey(mapper, BTN_FORWARD, 1);
6018 processSync(mapper);
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6021 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006022
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006025 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6026
6027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6029 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030
6031 processKey(mapper, BTN_FORWARD, 0);
6032 processSync(mapper);
6033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036
6037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(0, motionArgs.buttonState);
6040
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6043 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6044
6045 // press BTN_EXTRA, release BTN_EXTRA
6046 processKey(mapper, BTN_EXTRA, 1);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6050 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006054 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6055
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6058 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059
6060 processKey(mapper, BTN_EXTRA, 0);
6061 processSync(mapper);
6062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006064 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006068 ASSERT_EQ(0, motionArgs.buttonState);
6069
Michael Wrightd02c5b62014-02-10 15:10:22 -08006070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6071 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6072 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6073
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6075
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 // press BTN_STYLUS, release BTN_STYLUS
6077 processKey(mapper, BTN_STYLUS, 1);
6078 processSync(mapper);
6079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006081 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6082
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6085 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006086
6087 processKey(mapper, BTN_STYLUS, 0);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006090 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006092
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006096
6097 // press BTN_STYLUS2, release BTN_STYLUS2
6098 processKey(mapper, BTN_STYLUS2, 1);
6099 processSync(mapper);
6100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6101 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006102 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6103
6104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6106 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006107
6108 processKey(mapper, BTN_STYLUS2, 0);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006115 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006116 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117
6118 // release touch
6119 processUp(mapper);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6123 ASSERT_EQ(0, motionArgs.buttonState);
6124}
6125
6126TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006127 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006128 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006129 prepareButtons();
6130 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006131 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132
6133 NotifyMotionArgs motionArgs;
6134
6135 // default tool type is finger
6136 processDown(mapper, 100, 200);
6137 processSync(mapper);
6138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6139 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6140 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6141
6142 // eraser
6143 processKey(mapper, BTN_TOOL_RUBBER, 1);
6144 processSync(mapper);
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6148
6149 // stylus
6150 processKey(mapper, BTN_TOOL_RUBBER, 0);
6151 processKey(mapper, BTN_TOOL_PEN, 1);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6156
6157 // brush
6158 processKey(mapper, BTN_TOOL_PEN, 0);
6159 processKey(mapper, BTN_TOOL_BRUSH, 1);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6164
6165 // pencil
6166 processKey(mapper, BTN_TOOL_BRUSH, 0);
6167 processKey(mapper, BTN_TOOL_PENCIL, 1);
6168 processSync(mapper);
6169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6172
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006173 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006174 processKey(mapper, BTN_TOOL_PENCIL, 0);
6175 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6176 processSync(mapper);
6177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6179 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6180
6181 // mouse
6182 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6183 processKey(mapper, BTN_TOOL_MOUSE, 1);
6184 processSync(mapper);
6185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6188
6189 // lens
6190 processKey(mapper, BTN_TOOL_MOUSE, 0);
6191 processKey(mapper, BTN_TOOL_LENS, 1);
6192 processSync(mapper);
6193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6195 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6196
6197 // double-tap
6198 processKey(mapper, BTN_TOOL_LENS, 0);
6199 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6200 processSync(mapper);
6201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6204
6205 // triple-tap
6206 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6207 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6208 processSync(mapper);
6209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6211 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6212
6213 // quad-tap
6214 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6215 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6216 processSync(mapper);
6217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6219 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6220
6221 // finger
6222 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6223 processKey(mapper, BTN_TOOL_FINGER, 1);
6224 processSync(mapper);
6225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6228
6229 // stylus trumps finger
6230 processKey(mapper, BTN_TOOL_PEN, 1);
6231 processSync(mapper);
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6235
6236 // eraser trumps stylus
6237 processKey(mapper, BTN_TOOL_RUBBER, 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6241 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6242
6243 // mouse trumps eraser
6244 processKey(mapper, BTN_TOOL_MOUSE, 1);
6245 processSync(mapper);
6246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6249
6250 // back to default tool type
6251 processKey(mapper, BTN_TOOL_MOUSE, 0);
6252 processKey(mapper, BTN_TOOL_RUBBER, 0);
6253 processKey(mapper, BTN_TOOL_PEN, 0);
6254 processKey(mapper, BTN_TOOL_FINGER, 0);
6255 processSync(mapper);
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6259}
6260
6261TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006263 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 prepareButtons();
6265 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006266 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006267 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006268
6269 NotifyMotionArgs motionArgs;
6270
6271 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6272 processKey(mapper, BTN_TOOL_FINGER, 1);
6273 processMove(mapper, 100, 200);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6277 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6278 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6279
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6283 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6284
6285 // move a little
6286 processMove(mapper, 150, 250);
6287 processSync(mapper);
6288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6289 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6290 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6291 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6292
6293 // down when BTN_TOUCH is pressed, pressure defaults to 1
6294 processKey(mapper, BTN_TOUCH, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6300
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6305
6306 // up when BTN_TOUCH is released, hover restored
6307 processKey(mapper, BTN_TOUCH, 0);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6312 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6313
6314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6315 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6317 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6318
6319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6320 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6321 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6322 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6323
6324 // exit hover when pointer goes away
6325 processKey(mapper, BTN_TOOL_FINGER, 0);
6326 processSync(mapper);
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6329 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6330 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6331}
6332
6333TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006334 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006335 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 prepareButtons();
6337 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006338 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006339
6340 NotifyMotionArgs motionArgs;
6341
6342 // initially hovering because pressure is 0
6343 processDown(mapper, 100, 200);
6344 processPressure(mapper, 0);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6349 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6355
6356 // move a little
6357 processMove(mapper, 150, 250);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6363
6364 // down when pressure is non-zero
6365 processPressure(mapper, RAW_PRESSURE_MAX);
6366 processSync(mapper);
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6376
6377 // up when pressure becomes 0, hover restored
6378 processPressure(mapper, 0);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6383 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6384
6385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6388 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6389
6390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6391 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6393 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6394
6395 // exit hover when pointer goes away
6396 processUp(mapper);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6399 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6401 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6402}
6403
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006404TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6405 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006406 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006407 prepareButtons();
6408 prepareAxes(POSITION | PRESSURE);
6409 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6410
6411 // Touch down.
6412 processDown(mapper, 100, 200);
6413 processPressure(mapper, 1);
6414 processSync(mapper);
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6416 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6417
6418 // Reset the mapper. This should cancel the ongoing gesture.
6419 resetMapper(mapper, ARBITRARY_TIME);
6420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6421 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6422
6423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6424}
6425
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006426TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6427 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006428 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006429 prepareButtons();
6430 prepareAxes(POSITION | PRESSURE);
6431 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6432
6433 // Set the initial state for the touch pointer.
6434 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6435 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6436 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6437 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6438
6439 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006440 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6441 // does not generate any events.
6442 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006443
6444 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6445 // the recreated touch state to generate a down event.
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6448 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6449
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6451}
6452
lilinnan687e58f2022-07-19 16:00:50 +08006453TEST_F(SingleTouchInputMapperTest,
6454 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006456 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006457 prepareButtons();
6458 prepareAxes(POSITION);
6459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6460 NotifyMotionArgs motionArgs;
6461
6462 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006463 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006464 processSync(mapper);
6465
6466 // We should receive a down event
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6468 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6469
6470 // Change display id
6471 clearViewports();
6472 prepareSecondaryDisplay(ViewportType::INTERNAL);
6473
6474 // We should receive a cancel event
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6476 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6477 // Then receive reset called
6478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6479}
6480
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006481TEST_F(SingleTouchInputMapperTest,
6482 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6483 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006484 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006485 prepareButtons();
6486 prepareAxes(POSITION);
6487 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6489 NotifyMotionArgs motionArgs;
6490
6491 // Start a new gesture.
6492 processDown(mapper, 100, 200);
6493 processSync(mapper);
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6495 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6496
6497 // Make the viewport inactive. This will put the device in disabled mode.
6498 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6499 viewport->isActive = false;
6500 mFakePolicy->updateViewport(*viewport);
6501 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6502
6503 // We should receive a cancel event for the ongoing gesture.
6504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6505 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6506 // Then we should be notified that the device was reset.
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6508
6509 // No events are generated while the viewport is inactive.
6510 processMove(mapper, 101, 201);
6511 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006512 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6515
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006516 // Start a new gesture while the viewport is still inactive.
6517 processDown(mapper, 300, 400);
6518 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6519 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6520 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6521 processSync(mapper);
6522
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006523 // Make the viewport active again. The device should resume processing events.
6524 viewport->isActive = true;
6525 mFakePolicy->updateViewport(*viewport);
6526 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6527
6528 // The device is reset because it changes back to direct mode, without generating any events.
6529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6531
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006532 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006533 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6535 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006536
6537 // No more events.
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6540}
6541
Prabir Pradhan211ba622022-10-31 21:09:21 +00006542TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6543 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006544 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006545 prepareButtons();
6546 prepareAxes(POSITION);
6547 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6549
6550 // Press a stylus button.
6551 processKey(mapper, BTN_STYLUS, 1);
6552 processSync(mapper);
6553
6554 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6555 processDown(mapper, 100, 200);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6558 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6559 WithCoords(toDisplayX(100), toDisplayY(200)),
6560 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6562 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6563 WithCoords(toDisplayX(100), toDisplayY(200)),
6564 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6565
6566 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6567 // the button has not actually been released, since there will be no pointers through which the
6568 // button state can be reported. The event is generated at the location of the pointer before
6569 // it went up.
6570 processUp(mapper);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6574 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6576 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6577 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6578}
6579
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006580TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6581 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6582 prepareDisplay(ui::ROTATION_0);
6583 prepareButtons();
6584 prepareAxes(POSITION);
6585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6587
6588 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6589}
6590
Prabir Pradhan5632d622021-09-06 07:57:20 -07006591// --- TouchDisplayProjectionTest ---
6592
6593class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6594public:
6595 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6596 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6597 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006598 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006599 uint32_t inverseRotationFlags;
6600 auto width = DISPLAY_WIDTH;
6601 auto height = DISPLAY_HEIGHT;
6602 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006603 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006604 inverseRotationFlags = ui::Transform::ROT_270;
6605 std::swap(width, height);
6606 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006607 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006608 inverseRotationFlags = ui::Transform::ROT_180;
6609 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006610 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006611 inverseRotationFlags = ui::Transform::ROT_90;
6612 std::swap(width, height);
6613 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006614 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006615 inverseRotationFlags = ui::Transform::ROT_0;
6616 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006617 }
6618
6619 const ui::Transform rotation(inverseRotationFlags, width, height);
6620 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6621
6622 std::optional<DisplayViewport> internalViewport =
6623 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6624 DisplayViewport& v = *internalViewport;
6625 v.displayId = DISPLAY_ID;
6626 v.orientation = orientation;
6627
6628 v.logicalLeft = 0;
6629 v.logicalTop = 0;
6630 v.logicalRight = 100;
6631 v.logicalBottom = 100;
6632
6633 v.physicalLeft = rotatedPhysicalDisplay.left;
6634 v.physicalTop = rotatedPhysicalDisplay.top;
6635 v.physicalRight = rotatedPhysicalDisplay.right;
6636 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6637
6638 v.deviceWidth = width;
6639 v.deviceHeight = height;
6640
6641 v.isActive = true;
6642 v.uniqueId = UNIQUE_ID;
6643 v.type = ViewportType::INTERNAL;
6644 mFakePolicy->updateViewport(v);
6645 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6646 }
6647
6648 void assertReceivedMove(const Point& point) {
6649 NotifyMotionArgs motionArgs;
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6654 1, 0, 0, 0, 0, 0, 0, 0));
6655 }
6656};
6657
6658TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6659 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006660 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006661
6662 prepareButtons();
6663 prepareAxes(POSITION);
6664 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6665
6666 NotifyMotionArgs motionArgs;
6667
6668 // Configure the DisplayViewport such that the logical display maps to a subsection of
6669 // the display panel called the physical display. Here, the physical display is bounded by the
6670 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6671 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6672 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6673 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6674
Michael Wrighta9cf4192022-12-01 23:46:39 +00006675 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006676 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6677
6678 // Touches outside the physical display should be ignored, and should not generate any
6679 // events. Ensure touches at the following points that lie outside of the physical display
6680 // area do not generate any events.
6681 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6682 processDown(mapper, toRawX(point.x), toRawY(point.y));
6683 processSync(mapper);
6684 processUp(mapper);
6685 processSync(mapper);
6686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6687 << "Unexpected event generated for touch outside physical display at point: "
6688 << point.x << ", " << point.y;
6689 }
6690 }
6691}
6692
6693TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6694 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006695 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006696
6697 prepareButtons();
6698 prepareAxes(POSITION);
6699 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6700
6701 NotifyMotionArgs motionArgs;
6702
6703 // Configure the DisplayViewport such that the logical display maps to a subsection of
6704 // the display panel called the physical display. Here, the physical display is bounded by the
6705 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6706 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6707
Michael Wrighta9cf4192022-12-01 23:46:39 +00006708 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006709 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6710
6711 // Touches that start outside the physical display should be ignored until it enters the
6712 // physical display bounds, at which point it should generate a down event. Start a touch at
6713 // the point (5, 100), which is outside the physical display bounds.
6714 static const Point kOutsidePoint{5, 100};
6715 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6718
6719 // Move the touch into the physical display area. This should generate a pointer down.
6720 processMove(mapper, toRawX(11), toRawY(21));
6721 processSync(mapper);
6722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6723 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6724 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6725 ASSERT_NO_FATAL_FAILURE(
6726 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6727
6728 // Move the touch inside the physical display area. This should generate a pointer move.
6729 processMove(mapper, toRawX(69), toRawY(159));
6730 processSync(mapper);
6731 assertReceivedMove({69, 159});
6732
6733 // Move outside the physical display area. Since the pointer is already down, this should
6734 // now continue generating events.
6735 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6736 processSync(mapper);
6737 assertReceivedMove(kOutsidePoint);
6738
6739 // Release. This should generate a pointer up.
6740 processUp(mapper);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6744 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6745 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6746
6747 // Ensure no more events were generated.
6748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6750 }
6751}
6752
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006753// --- ExternalStylusFusionTest ---
6754
6755class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6756public:
6757 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6758 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006759 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006760 prepareButtons();
6761 prepareAxes(POSITION);
6762 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6763
6764 mStylusState.when = ARBITRARY_TIME;
6765 mStylusState.pressure = 0.f;
6766 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6767 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6768 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6769 processExternalStylusState(mapper);
6770 return mapper;
6771 }
6772
6773 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6774 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6775 for (const NotifyArgs& args : generatedArgs) {
6776 mFakeListener->notify(args);
6777 }
6778 // Loop the reader to flush the input listener queue.
6779 mReader->loopOnce();
6780 return generatedArgs;
6781 }
6782
6783protected:
6784 StylusState mStylusState{};
6785 static constexpr uint32_t EXPECTED_SOURCE =
6786 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6787
6788 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6789 auto toolTypeSource =
6790 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6791
6792 // The first pointer is withheld.
6793 processDown(mapper, 100, 200);
6794 processSync(mapper);
6795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6796 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6797 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6798
6799 // The external stylus reports pressure. The withheld finger pointer is released as a
6800 // stylus.
6801 mStylusState.pressure = 1.f;
6802 processExternalStylusState(mapper);
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6804 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6805 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6806
6807 // Subsequent pointer events are not withheld.
6808 processMove(mapper, 101, 201);
6809 processSync(mapper);
6810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6811 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6812
6813 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6815 }
6816
6817 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6818 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6819
6820 // Releasing the touch pointer ends the gesture.
6821 processUp(mapper);
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6824 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6825 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6826
6827 mStylusState.pressure = 0.f;
6828 processExternalStylusState(mapper);
6829 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6831 }
6832
6833 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6834 auto toolTypeSource =
6835 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6836
6837 // The first pointer is withheld when an external stylus is connected,
6838 // and a timeout is requested.
6839 processDown(mapper, 100, 200);
6840 processSync(mapper);
6841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6842 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6843 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6844
6845 // If the timeout expires early, it is requested again.
6846 handleTimeout(mapper, ARBITRARY_TIME + 1);
6847 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6848 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6849
6850 // When the timeout expires, the withheld touch is released as a finger pointer.
6851 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6853 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6854
6855 // Subsequent pointer events are not withheld.
6856 processMove(mapper, 101, 201);
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6859 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6860 processUp(mapper);
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6863 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6864
6865 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6867 }
6868
6869private:
6870 InputDeviceInfo mExternalStylusDeviceInfo{};
6871};
6872
6873TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6874 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6875 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6876}
6877
6878TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6879 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6880 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6881}
6882
6883TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6884 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6885 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6886}
6887
6888// Test a successful stylus fusion gesture where the pressure is reported by the external
6889// before the touch is reported by the touchscreen.
6890TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6891 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6892 auto toolTypeSource =
6893 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6894
6895 // The external stylus reports pressure first. It is ignored for now.
6896 mStylusState.pressure = 1.f;
6897 processExternalStylusState(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6899 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6900
6901 // When the touch goes down afterwards, it is reported as a stylus pointer.
6902 processDown(mapper, 100, 200);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6905 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6906 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6907
6908 processMove(mapper, 101, 201);
6909 processSync(mapper);
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6911 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6912 processUp(mapper);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6915 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6916
6917 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6919}
6920
6921TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6922 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6923
6924 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6925 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6926
6927 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6928 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6929 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6930 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6931}
6932
6933TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6934 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6935 auto toolTypeSource =
6936 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6937
6938 mStylusState.pressure = 0.8f;
6939 processExternalStylusState(mapper);
6940 processDown(mapper, 100, 200);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6943 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6944 WithPressure(0.8f))));
6945 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6946
6947 // The external stylus reports a pressure change. We wait for some time for a touch event.
6948 mStylusState.pressure = 0.6f;
6949 processExternalStylusState(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6951 ASSERT_NO_FATAL_FAILURE(
6952 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6953
6954 // If a touch is reported within the timeout, it reports the updated pressure.
6955 processMove(mapper, 101, 201);
6956 processSync(mapper);
6957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6958 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6959 WithPressure(0.6f))));
6960 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6961
6962 // There is another pressure change.
6963 mStylusState.pressure = 0.5f;
6964 processExternalStylusState(mapper);
6965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6966 ASSERT_NO_FATAL_FAILURE(
6967 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6968
6969 // If a touch is not reported within the timeout, a move event is generated to report
6970 // the new pressure.
6971 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6973 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6974 WithPressure(0.5f))));
6975
6976 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6977 // repeated indefinitely.
6978 mStylusState.pressure = 0.0f;
6979 processExternalStylusState(mapper);
6980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6981 ASSERT_NO_FATAL_FAILURE(
6982 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6983 processMove(mapper, 102, 202);
6984 processSync(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6986 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6987 WithPressure(0.5f))));
6988 processMove(mapper, 103, 203);
6989 processSync(mapper);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6991 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6992 WithPressure(0.5f))));
6993
6994 processUp(mapper);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6998 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6999
7000 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002}
7003
7004TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7005 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7006 auto source = WithSource(EXPECTED_SOURCE);
7007
7008 mStylusState.pressure = 1.f;
7009 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7010 processExternalStylusState(mapper);
7011 processDown(mapper, 100, 200);
7012 processSync(mapper);
7013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7014 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7015 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7016 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7017
7018 // The external stylus reports a tool change. We wait for some time for a touch event.
7019 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7020 processExternalStylusState(mapper);
7021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7022 ASSERT_NO_FATAL_FAILURE(
7023 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7024
7025 // If a touch is reported within the timeout, it reports the updated pressure.
7026 processMove(mapper, 101, 201);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7029 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7030 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7031 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7032
7033 // There is another tool type change.
7034 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7035 processExternalStylusState(mapper);
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7037 ASSERT_NO_FATAL_FAILURE(
7038 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7039
7040 // If a touch is not reported within the timeout, a move event is generated to report
7041 // the new tool type.
7042 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7044 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7045 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7046
7047 processUp(mapper);
7048 processSync(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7050 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7051 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7052
7053 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7055}
7056
7057TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7058 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7059 auto toolTypeSource =
7060 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7061
7062 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7063
7064 // The external stylus reports a button change. We wait for some time for a touch event.
7065 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7066 processExternalStylusState(mapper);
7067 ASSERT_NO_FATAL_FAILURE(
7068 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7069
7070 // If a touch is reported within the timeout, it reports the updated button state.
7071 processMove(mapper, 101, 201);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7074 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7075 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7077 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7078 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7079 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7080
7081 // The button is now released.
7082 mStylusState.buttons = 0;
7083 processExternalStylusState(mapper);
7084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7085 ASSERT_NO_FATAL_FAILURE(
7086 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7087
7088 // If a touch is not reported within the timeout, a move event is generated to report
7089 // the new button state.
7090 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7092 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7093 WithButtonState(0))));
7094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007095 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7096 WithButtonState(0))));
7097
7098 processUp(mapper);
7099 processSync(mapper);
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007101 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7102
7103 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7105}
7106
Michael Wrightd02c5b62014-02-10 15:10:22 -08007107// --- MultiTouchInputMapperTest ---
7108
7109class MultiTouchInputMapperTest : public TouchInputMapperTest {
7110protected:
7111 void prepareAxes(int axes);
7112
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007113 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7114 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7115 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7116 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7117 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7118 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7119 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7120 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7121 void processId(MultiTouchInputMapper& mapper, int32_t id);
7122 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7123 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7124 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007125 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007127 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7128 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129};
7130
7131void MultiTouchInputMapperTest::prepareAxes(int axes) {
7132 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7134 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007135 }
7136 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7138 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007140 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7141 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007142 }
7143 }
7144 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007145 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7146 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007148 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007149 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 }
7151 }
7152 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007153 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7154 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155 }
7156 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007157 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7158 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007159 }
7160 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007161 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7162 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163 }
7164 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007165 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7166 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167 }
7168 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007169 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7170 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171 }
7172 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007173 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174 }
7175}
7176
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007177void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7178 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7184 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186}
7187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007188void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7189 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191}
7192
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007193void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007194 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195}
7196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007197void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007198 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199}
7200
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007201void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7202 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204}
7205
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007206void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007208}
7209
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007210void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007211 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007212}
7213
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007214void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007216}
7217
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007218void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007220}
7221
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007222void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224}
7225
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007226void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7227 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229}
7230
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007231void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7232 int32_t value) {
7233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7235}
7236
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007237void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007238 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007239}
7240
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007241void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7242 nsecs_t readTime) {
7243 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007244}
7245
Michael Wrightd02c5b62014-02-10 15:10:22 -08007246TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007248 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249 prepareAxes(POSITION);
7250 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007251 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007252
arthurhungdcef2dc2020-08-11 14:47:50 +08007253 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007254
7255 NotifyMotionArgs motionArgs;
7256
7257 // Two fingers down at once.
7258 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7259 processPosition(mapper, x1, y1);
7260 processMTSync(mapper);
7261 processPosition(mapper, x2, y2);
7262 processMTSync(mapper);
7263 processSync(mapper);
7264
7265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7267 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7268 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7269 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7271 ASSERT_EQ(0, motionArgs.flags);
7272 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7273 ASSERT_EQ(0, motionArgs.buttonState);
7274 ASSERT_EQ(0, motionArgs.edgeFlags);
7275 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7276 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7279 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7280 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7281 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7282 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7283
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7286 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7287 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7288 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007289 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007290 ASSERT_EQ(0, motionArgs.flags);
7291 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7292 ASSERT_EQ(0, motionArgs.buttonState);
7293 ASSERT_EQ(0, motionArgs.edgeFlags);
7294 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7295 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7297 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7300 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7302 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7303 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7304 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7305 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7306
7307 // Move.
7308 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7309 processPosition(mapper, x1, y1);
7310 processMTSync(mapper);
7311 processPosition(mapper, x2, y2);
7312 processMTSync(mapper);
7313 processSync(mapper);
7314
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7316 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7317 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7318 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7319 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7320 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7321 ASSERT_EQ(0, motionArgs.flags);
7322 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7323 ASSERT_EQ(0, motionArgs.buttonState);
7324 ASSERT_EQ(0, motionArgs.edgeFlags);
7325 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7326 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7327 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7328 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7329 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7331 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
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 // First finger up.
7339 x2 += 15; y2 -= 20;
7340 processPosition(mapper, x2, y2);
7341 processMTSync(mapper);
7342 processSync(mapper);
7343
7344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7345 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7346 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7347 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7348 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007349 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007350 ASSERT_EQ(0, motionArgs.flags);
7351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7352 ASSERT_EQ(0, motionArgs.buttonState);
7353 ASSERT_EQ(0, motionArgs.edgeFlags);
7354 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7355 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7357 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7360 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7362 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7363 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7364 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7366
7367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7369 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7370 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7371 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7373 ASSERT_EQ(0, motionArgs.flags);
7374 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7375 ASSERT_EQ(0, motionArgs.buttonState);
7376 ASSERT_EQ(0, motionArgs.edgeFlags);
7377 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7378 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7381 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7382 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7383 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7384 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7385
7386 // Move.
7387 x2 += 20; y2 -= 25;
7388 processPosition(mapper, x2, y2);
7389 processMTSync(mapper);
7390 processSync(mapper);
7391
7392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7393 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7394 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7395 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7396 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7398 ASSERT_EQ(0, motionArgs.flags);
7399 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7400 ASSERT_EQ(0, motionArgs.buttonState);
7401 ASSERT_EQ(0, motionArgs.edgeFlags);
7402 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7403 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7404 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7405 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7406 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7407 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7408 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7409 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7410
7411 // New finger down.
7412 int32_t x3 = 700, y3 = 300;
7413 processPosition(mapper, x2, y2);
7414 processMTSync(mapper);
7415 processPosition(mapper, x3, y3);
7416 processMTSync(mapper);
7417 processSync(mapper);
7418
7419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7420 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7421 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7422 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7423 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007424 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 ASSERT_EQ(0, motionArgs.flags);
7426 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7427 ASSERT_EQ(0, motionArgs.buttonState);
7428 ASSERT_EQ(0, motionArgs.edgeFlags);
7429 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7430 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7432 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7437 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7438 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7439 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7441
7442 // Second finger up.
7443 x3 += 30; y3 -= 20;
7444 processPosition(mapper, x3, y3);
7445 processMTSync(mapper);
7446 processSync(mapper);
7447
7448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7449 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7450 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7451 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7452 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007453 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007454 ASSERT_EQ(0, motionArgs.flags);
7455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7456 ASSERT_EQ(0, motionArgs.buttonState);
7457 ASSERT_EQ(0, motionArgs.edgeFlags);
7458 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7459 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7461 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7462 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7463 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7464 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7466 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7467 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7468 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7469 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7470
7471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7472 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7473 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7474 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7475 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7477 ASSERT_EQ(0, motionArgs.flags);
7478 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7479 ASSERT_EQ(0, motionArgs.buttonState);
7480 ASSERT_EQ(0, motionArgs.edgeFlags);
7481 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7482 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7483 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7485 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7486 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7487 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7488 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7489
7490 // Last finger up.
7491 processMTSync(mapper);
7492 processSync(mapper);
7493
7494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7495 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7496 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7497 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7498 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7499 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7500 ASSERT_EQ(0, motionArgs.flags);
7501 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7502 ASSERT_EQ(0, motionArgs.buttonState);
7503 ASSERT_EQ(0, motionArgs.edgeFlags);
7504 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7505 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7506 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7508 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7509 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7510 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7511 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7512
7513 // Should not have sent any more keys or motions.
7514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7516}
7517
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007518TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7519 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007520 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007521
7522 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7523 /*fuzz*/ 0, /*resolution*/ 10);
7524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7525 /*fuzz*/ 0, /*resolution*/ 11);
7526 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7527 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7528 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7529 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7530 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7531 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7532 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7533 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7534
7535 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7536
7537 // X and Y axes
7538 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7539 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7540 // Touch major and minor
7541 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7542 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7543 // Tool major and minor
7544 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7545 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7546}
7547
7548TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7549 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007550 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007551
7552 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7553 /*fuzz*/ 0, /*resolution*/ 10);
7554 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7555 /*fuzz*/ 0, /*resolution*/ 11);
7556
7557 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7558
7559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7560
7561 // Touch major and minor
7562 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7563 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7564 // Tool major and minor
7565 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7566 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7567}
7568
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007571 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 prepareAxes(POSITION | ID);
7573 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007574 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575
arthurhungdcef2dc2020-08-11 14:47:50 +08007576 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007577
7578 NotifyMotionArgs motionArgs;
7579
7580 // Two fingers down at once.
7581 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7582 processPosition(mapper, x1, y1);
7583 processId(mapper, 1);
7584 processMTSync(mapper);
7585 processPosition(mapper, x2, y2);
7586 processId(mapper, 2);
7587 processMTSync(mapper);
7588 processSync(mapper);
7589
7590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7591 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7592 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7593 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7594 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7596 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7597
7598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007599 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7601 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7602 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7603 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7606 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7608 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7609
7610 // Move.
7611 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7612 processPosition(mapper, x1, y1);
7613 processId(mapper, 1);
7614 processMTSync(mapper);
7615 processPosition(mapper, x2, y2);
7616 processId(mapper, 2);
7617 processMTSync(mapper);
7618 processSync(mapper);
7619
7620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7622 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7624 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7625 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7626 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7628 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7631
7632 // First finger up.
7633 x2 += 15; y2 -= 20;
7634 processPosition(mapper, x2, y2);
7635 processId(mapper, 2);
7636 processMTSync(mapper);
7637 processSync(mapper);
7638
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007640 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007641 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7642 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7643 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7644 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7647 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7649 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7650
7651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7652 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7653 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7654 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7658
7659 // Move.
7660 x2 += 20; y2 -= 25;
7661 processPosition(mapper, x2, y2);
7662 processId(mapper, 2);
7663 processMTSync(mapper);
7664 processSync(mapper);
7665
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7668 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7669 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7671 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7672 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7673
7674 // New finger down.
7675 int32_t x3 = 700, y3 = 300;
7676 processPosition(mapper, x2, y2);
7677 processId(mapper, 2);
7678 processMTSync(mapper);
7679 processPosition(mapper, x3, y3);
7680 processId(mapper, 3);
7681 processMTSync(mapper);
7682 processSync(mapper);
7683
7684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007685 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007686 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7687 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7688 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7689 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7692 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7694 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7695
7696 // Second finger up.
7697 x3 += 30; y3 -= 20;
7698 processPosition(mapper, x3, y3);
7699 processId(mapper, 3);
7700 processMTSync(mapper);
7701 processSync(mapper);
7702
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007704 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007705 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7707 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7708 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7709 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7710 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7711 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7712 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7713 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7714
7715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7717 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7718 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7722
7723 // Last finger up.
7724 processMTSync(mapper);
7725 processSync(mapper);
7726
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7728 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7734
7735 // Should not have sent any more keys or motions.
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7738}
7739
7740TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007742 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007743 prepareAxes(POSITION | ID | SLOT);
7744 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007745 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007746
arthurhungdcef2dc2020-08-11 14:47:50 +08007747 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007748
7749 NotifyMotionArgs motionArgs;
7750
7751 // Two fingers down at once.
7752 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7753 processPosition(mapper, x1, y1);
7754 processId(mapper, 1);
7755 processSlot(mapper, 1);
7756 processPosition(mapper, x2, y2);
7757 processId(mapper, 2);
7758 processSync(mapper);
7759
7760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7762 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7763 ASSERT_EQ(0, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7767
7768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007769 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007770 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7771 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7772 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7773 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7776 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7778 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7779
7780 // Move.
7781 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7782 processSlot(mapper, 0);
7783 processPosition(mapper, x1, y1);
7784 processSlot(mapper, 1);
7785 processPosition(mapper, x2, y2);
7786 processSync(mapper);
7787
7788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7790 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7793 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7796 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7798 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7799
7800 // First finger up.
7801 x2 += 15; y2 -= 20;
7802 processSlot(mapper, 0);
7803 processId(mapper, -1);
7804 processSlot(mapper, 1);
7805 processPosition(mapper, x2, y2);
7806 processSync(mapper);
7807
7808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007809 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7813 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7816 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7818 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7819
7820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7822 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7823 ASSERT_EQ(1, 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(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7827
7828 // Move.
7829 x2 += 20; y2 -= 25;
7830 processPosition(mapper, x2, y2);
7831 processSync(mapper);
7832
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7835 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7836 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7839 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7840
7841 // New finger down.
7842 int32_t x3 = 700, y3 = 300;
7843 processPosition(mapper, x2, y2);
7844 processSlot(mapper, 0);
7845 processId(mapper, 3);
7846 processPosition(mapper, x3, y3);
7847 processSync(mapper);
7848
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007850 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007851 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7852 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7853 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7854 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7857 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7859 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7860
7861 // Second finger up.
7862 x3 += 30; y3 -= 20;
7863 processSlot(mapper, 1);
7864 processId(mapper, -1);
7865 processSlot(mapper, 0);
7866 processPosition(mapper, x3, y3);
7867 processSync(mapper);
7868
7869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007870 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007871 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7872 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7873 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7874 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7876 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7877 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7879 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7880
7881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7883 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7884 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7885 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7887 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7888
7889 // Last finger up.
7890 processId(mapper, -1);
7891 processSync(mapper);
7892
7893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7895 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7897 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7899 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7900
7901 // Should not have sent any more keys or motions.
7902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7904}
7905
7906TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007908 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007909 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007911
7912 // These calculations are based on the input device calibration documentation.
7913 int32_t rawX = 100;
7914 int32_t rawY = 200;
7915 int32_t rawTouchMajor = 7;
7916 int32_t rawTouchMinor = 6;
7917 int32_t rawToolMajor = 9;
7918 int32_t rawToolMinor = 8;
7919 int32_t rawPressure = 11;
7920 int32_t rawDistance = 0;
7921 int32_t rawOrientation = 3;
7922 int32_t id = 5;
7923
7924 float x = toDisplayX(rawX);
7925 float y = toDisplayY(rawY);
7926 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7927 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7928 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7929 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7930 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7931 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7932 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7933 float distance = float(rawDistance);
7934
7935 processPosition(mapper, rawX, rawY);
7936 processTouchMajor(mapper, rawTouchMajor);
7937 processTouchMinor(mapper, rawTouchMinor);
7938 processToolMajor(mapper, rawToolMajor);
7939 processToolMinor(mapper, rawToolMinor);
7940 processPressure(mapper, rawPressure);
7941 processOrientation(mapper, rawOrientation);
7942 processDistance(mapper, rawDistance);
7943 processId(mapper, id);
7944 processMTSync(mapper);
7945 processSync(mapper);
7946
7947 NotifyMotionArgs args;
7948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7949 ASSERT_EQ(0, args.pointerProperties[0].id);
7950 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7951 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7952 orientation, distance));
7953}
7954
7955TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007956 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007957 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007958 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7959 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007960 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961
7962 // These calculations are based on the input device calibration documentation.
7963 int32_t rawX = 100;
7964 int32_t rawY = 200;
7965 int32_t rawTouchMajor = 140;
7966 int32_t rawTouchMinor = 120;
7967 int32_t rawToolMajor = 180;
7968 int32_t rawToolMinor = 160;
7969
7970 float x = toDisplayX(rawX);
7971 float y = toDisplayY(rawY);
7972 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7973 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7974 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7975 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7976 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7977
7978 processPosition(mapper, rawX, rawY);
7979 processTouchMajor(mapper, rawTouchMajor);
7980 processTouchMinor(mapper, rawTouchMinor);
7981 processToolMajor(mapper, rawToolMajor);
7982 processToolMinor(mapper, rawToolMinor);
7983 processMTSync(mapper);
7984 processSync(mapper);
7985
7986 NotifyMotionArgs args;
7987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7989 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7990}
7991
7992TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007994 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 prepareAxes(POSITION | TOUCH | TOOL);
7996 addConfigurationProperty("touch.size.calibration", "diameter");
7997 addConfigurationProperty("touch.size.scale", "10");
7998 addConfigurationProperty("touch.size.bias", "160");
7999 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001
8002 // These calculations are based on the input device calibration documentation.
8003 // Note: We only provide a single common touch/tool value because the device is assumed
8004 // not to emit separate values for each pointer (isSummed = 1).
8005 int32_t rawX = 100;
8006 int32_t rawY = 200;
8007 int32_t rawX2 = 150;
8008 int32_t rawY2 = 250;
8009 int32_t rawTouchMajor = 5;
8010 int32_t rawToolMajor = 8;
8011
8012 float x = toDisplayX(rawX);
8013 float y = toDisplayY(rawY);
8014 float x2 = toDisplayX(rawX2);
8015 float y2 = toDisplayY(rawY2);
8016 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8017 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8018 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8019
8020 processPosition(mapper, rawX, rawY);
8021 processTouchMajor(mapper, rawTouchMajor);
8022 processToolMajor(mapper, rawToolMajor);
8023 processMTSync(mapper);
8024 processPosition(mapper, rawX2, rawY2);
8025 processTouchMajor(mapper, rawTouchMajor);
8026 processToolMajor(mapper, rawToolMajor);
8027 processMTSync(mapper);
8028 processSync(mapper);
8029
8030 NotifyMotionArgs args;
8031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8033
8034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008035 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 ASSERT_EQ(size_t(2), args.pointerCount);
8037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8038 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8040 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8041}
8042
8043TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008045 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 prepareAxes(POSITION | TOUCH | TOOL);
8047 addConfigurationProperty("touch.size.calibration", "area");
8048 addConfigurationProperty("touch.size.scale", "43");
8049 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051
8052 // These calculations are based on the input device calibration documentation.
8053 int32_t rawX = 100;
8054 int32_t rawY = 200;
8055 int32_t rawTouchMajor = 5;
8056 int32_t rawToolMajor = 8;
8057
8058 float x = toDisplayX(rawX);
8059 float y = toDisplayY(rawY);
8060 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8061 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8062 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8063
8064 processPosition(mapper, rawX, rawY);
8065 processTouchMajor(mapper, rawTouchMajor);
8066 processToolMajor(mapper, rawToolMajor);
8067 processMTSync(mapper);
8068 processSync(mapper);
8069
8070 NotifyMotionArgs args;
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8073 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8074}
8075
8076TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008078 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 prepareAxes(POSITION | PRESSURE);
8080 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8081 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083
Michael Wrightaa449c92017-12-13 21:21:43 +00008084 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008085 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008086 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8087 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8088 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8089
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 // These calculations are based on the input device calibration documentation.
8091 int32_t rawX = 100;
8092 int32_t rawY = 200;
8093 int32_t rawPressure = 60;
8094
8095 float x = toDisplayX(rawX);
8096 float y = toDisplayY(rawY);
8097 float pressure = float(rawPressure) * 0.01f;
8098
8099 processPosition(mapper, rawX, rawY);
8100 processPressure(mapper, rawPressure);
8101 processMTSync(mapper);
8102 processSync(mapper);
8103
8104 NotifyMotionArgs args;
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8107 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8108}
8109
8110TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008112 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008113 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008114 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115
8116 NotifyMotionArgs motionArgs;
8117 NotifyKeyArgs keyArgs;
8118
8119 processId(mapper, 1);
8120 processPosition(mapper, 100, 200);
8121 processSync(mapper);
8122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8124 ASSERT_EQ(0, motionArgs.buttonState);
8125
8126 // press BTN_LEFT, release BTN_LEFT
8127 processKey(mapper, BTN_LEFT, 1);
8128 processSync(mapper);
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8131 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8132
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8136
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 processKey(mapper, BTN_LEFT, 0);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008142
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146
8147 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8148 processKey(mapper, BTN_RIGHT, 1);
8149 processKey(mapper, BTN_MIDDLE, 1);
8150 processSync(mapper);
8151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8153 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8154 motionArgs.buttonState);
8155
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8158 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8162 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8163 motionArgs.buttonState);
8164
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 processKey(mapper, BTN_RIGHT, 0);
8166 processSync(mapper);
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174
8175 processKey(mapper, BTN_MIDDLE, 0);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008184
8185 // press BTN_BACK, release BTN_BACK
8186 processKey(mapper, BTN_BACK, 1);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8189 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8190 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8195
8196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8198 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008199
8200 processKey(mapper, BTN_BACK, 0);
8201 processSync(mapper);
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008203 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205
8206 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(0, motionArgs.buttonState);
8209
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8211 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8212 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8213
8214 // press BTN_SIDE, release BTN_SIDE
8215 processKey(mapper, BTN_SIDE, 1);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8218 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8219 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8224
8225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8226 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8227 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008228
8229 processKey(mapper, BTN_SIDE, 0);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008237 ASSERT_EQ(0, motionArgs.buttonState);
8238
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8240 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8241 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8242
8243 // press BTN_FORWARD, release BTN_FORWARD
8244 processKey(mapper, BTN_FORWARD, 1);
8245 processSync(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8247 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8248 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008249
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008252 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8253
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8255 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8256 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008257
8258 processKey(mapper, BTN_FORWARD, 0);
8259 processSync(mapper);
8260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263
8264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008265 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266 ASSERT_EQ(0, motionArgs.buttonState);
8267
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8269 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8270 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8271
8272 // press BTN_EXTRA, release BTN_EXTRA
8273 processKey(mapper, BTN_EXTRA, 1);
8274 processSync(mapper);
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8276 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8277 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008278
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8282
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8285 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008286
8287 processKey(mapper, BTN_EXTRA, 0);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008290 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008291 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008292
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295 ASSERT_EQ(0, motionArgs.buttonState);
8296
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8298 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8299 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8300
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8302
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 // press BTN_STYLUS, release BTN_STYLUS
8304 processKey(mapper, BTN_STYLUS, 1);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008308 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8309
8310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8312 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313
8314 processKey(mapper, BTN_STYLUS, 0);
8315 processSync(mapper);
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008317 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323
8324 // press BTN_STYLUS2, release BTN_STYLUS2
8325 processKey(mapper, BTN_STYLUS2, 1);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008329 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8330
8331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8332 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8333 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008334
8335 processKey(mapper, BTN_STYLUS2, 0);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008340
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008343 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008344
8345 // release touch
8346 processId(mapper, -1);
8347 processSync(mapper);
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8349 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8350 ASSERT_EQ(0, motionArgs.buttonState);
8351}
8352
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008353TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008355 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008356 prepareAxes(POSITION | ID | SLOT);
8357 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8358
8359 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8360 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8361
8362 // Touch down.
8363 processId(mapper, 1);
8364 processPosition(mapper, 100, 200);
8365 processSync(mapper);
8366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8368
8369 // Press and release button mapped to the primary stylus button.
8370 processKey(mapper, BTN_A, 1);
8371 processSync(mapper);
8372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8373 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8374 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8377 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8378
8379 processKey(mapper, BTN_A, 0);
8380 processSync(mapper);
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8382 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8385
8386 // Press and release the HID usage mapped to the secondary stylus button.
8387 processHidUsage(mapper, 0xabcd, 1);
8388 processSync(mapper);
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8390 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8391 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8393 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8394 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8395
8396 processHidUsage(mapper, 0xabcd, 0);
8397 processSync(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8399 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8401 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8402
8403 // Release touch.
8404 processId(mapper, -1);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8407 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8408}
8409
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008412 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008414 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008415
8416 NotifyMotionArgs motionArgs;
8417
8418 // default tool type is finger
8419 processId(mapper, 1);
8420 processPosition(mapper, 100, 200);
8421 processSync(mapper);
8422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8424 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8425
8426 // eraser
8427 processKey(mapper, BTN_TOOL_RUBBER, 1);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8432
8433 // stylus
8434 processKey(mapper, BTN_TOOL_RUBBER, 0);
8435 processKey(mapper, BTN_TOOL_PEN, 1);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8440
8441 // brush
8442 processKey(mapper, BTN_TOOL_PEN, 0);
8443 processKey(mapper, BTN_TOOL_BRUSH, 1);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8447 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8448
8449 // pencil
8450 processKey(mapper, BTN_TOOL_BRUSH, 0);
8451 processKey(mapper, BTN_TOOL_PENCIL, 1);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8454 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8455 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8456
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008457 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 processKey(mapper, BTN_TOOL_PENCIL, 0);
8459 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8460 processSync(mapper);
8461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8463 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8464
8465 // mouse
8466 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8467 processKey(mapper, BTN_TOOL_MOUSE, 1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8471 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8472
8473 // lens
8474 processKey(mapper, BTN_TOOL_MOUSE, 0);
8475 processKey(mapper, BTN_TOOL_LENS, 1);
8476 processSync(mapper);
8477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8479 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8480
8481 // double-tap
8482 processKey(mapper, BTN_TOOL_LENS, 0);
8483 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8484 processSync(mapper);
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8488
8489 // triple-tap
8490 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8491 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8492 processSync(mapper);
8493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8494 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8495 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8496
8497 // quad-tap
8498 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8499 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8503 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8504
8505 // finger
8506 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8507 processKey(mapper, BTN_TOOL_FINGER, 1);
8508 processSync(mapper);
8509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8510 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8511 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8512
8513 // stylus trumps finger
8514 processKey(mapper, BTN_TOOL_PEN, 1);
8515 processSync(mapper);
8516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8518 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8519
8520 // eraser trumps stylus
8521 processKey(mapper, BTN_TOOL_RUBBER, 1);
8522 processSync(mapper);
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8526
8527 // mouse trumps eraser
8528 processKey(mapper, BTN_TOOL_MOUSE, 1);
8529 processSync(mapper);
8530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8533
8534 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8535 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8540
8541 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8542 processToolType(mapper, MT_TOOL_PEN);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8546 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8547
8548 // back to default tool type
8549 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8550 processKey(mapper, BTN_TOOL_MOUSE, 0);
8551 processKey(mapper, BTN_TOOL_RUBBER, 0);
8552 processKey(mapper, BTN_TOOL_PEN, 0);
8553 processKey(mapper, BTN_TOOL_FINGER, 0);
8554 processSync(mapper);
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8557 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8558}
8559
8560TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008561 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008562 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008563 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008564 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008565 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566
8567 NotifyMotionArgs motionArgs;
8568
8569 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8570 processId(mapper, 1);
8571 processPosition(mapper, 100, 200);
8572 processSync(mapper);
8573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8574 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8576 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8577
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8581 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8582
8583 // move a little
8584 processPosition(mapper, 150, 250);
8585 processSync(mapper);
8586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8587 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8589 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8590
8591 // down when BTN_TOUCH is pressed, pressure defaults to 1
8592 processKey(mapper, BTN_TOUCH, 1);
8593 processSync(mapper);
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8598
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8602 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8603
8604 // up when BTN_TOUCH is released, hover restored
8605 processKey(mapper, BTN_TOUCH, 0);
8606 processSync(mapper);
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8610 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8613 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8614 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8615 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8616
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8620 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8621
8622 // exit hover when pointer goes away
8623 processId(mapper, -1);
8624 processSync(mapper);
8625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8626 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8628 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8629}
8630
8631TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008633 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008634 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008635 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008636
8637 NotifyMotionArgs motionArgs;
8638
8639 // initially hovering because pressure is 0
8640 processId(mapper, 1);
8641 processPosition(mapper, 100, 200);
8642 processPressure(mapper, 0);
8643 processSync(mapper);
8644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8645 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8646 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8647 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8648
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8650 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8652 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8653
8654 // move a little
8655 processPosition(mapper, 150, 250);
8656 processSync(mapper);
8657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8658 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8660 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8661
8662 // down when pressure becomes non-zero
8663 processPressure(mapper, RAW_PRESSURE_MAX);
8664 processSync(mapper);
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8668 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8669
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8673 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8674
8675 // up when pressure becomes 0, hover restored
8676 processPressure(mapper, 0);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8679 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8681 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8682
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8686 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8687
8688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8689 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8691 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8692
8693 // exit hover when pointer goes away
8694 processId(mapper, -1);
8695 processSync(mapper);
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8697 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8699 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8700}
8701
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008702/**
8703 * Set the input device port <--> display port associations, and check that the
8704 * events are routed to the display that matches the display port.
8705 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8706 */
8707TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008708 const std::string usb2 = "USB2";
8709 const uint8_t hdmi1 = 0;
8710 const uint8_t hdmi2 = 1;
8711 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008712 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008713
8714 addConfigurationProperty("touch.deviceType", "touchScreen");
8715 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008717
8718 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8719 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8720
8721 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8722 // for this input device is specified, and the matching viewport is not present,
8723 // the input device should be disabled (at the mapper level).
8724
8725 // Add viewport for display 2 on hdmi2
8726 prepareSecondaryDisplay(type, hdmi2);
8727 // Send a touch event
8728 processPosition(mapper, 100, 100);
8729 processSync(mapper);
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8731
8732 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008733 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008734 // Send a touch event again
8735 processPosition(mapper, 100, 100);
8736 processSync(mapper);
8737
8738 NotifyMotionArgs args;
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8740 ASSERT_EQ(DISPLAY_ID, args.displayId);
8741}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008743TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8744 addConfigurationProperty("touch.deviceType", "touchScreen");
8745 prepareAxes(POSITION);
8746 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8747
8748 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8749
Michael Wrighta9cf4192022-12-01 23:46:39 +00008750 prepareDisplay(ui::ROTATION_0);
8751 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008752
8753 // Send a touch event
8754 processPosition(mapper, 100, 100);
8755 processSync(mapper);
8756
8757 NotifyMotionArgs args;
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8760}
8761
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008762TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008763 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008764 std::shared_ptr<FakePointerController> fakePointerController =
8765 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008766 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008767 fakePointerController->setPosition(100, 200);
8768 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008769 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008770
Garfield Tan888a6a42020-01-09 11:39:16 -08008771 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008772 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008773
Michael Wrighta9cf4192022-12-01 23:46:39 +00008774 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008775 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008776 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008777
Harry Cutts16a24cc2022-10-26 15:22:19 +00008778 // Check source is a touchpad that would obtain the PointerController.
8779 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008780
8781 NotifyMotionArgs motionArgs;
8782 processPosition(mapper, 100, 100);
8783 processSync(mapper);
8784
8785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8786 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8787 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8788}
8789
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008790/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008791 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8792 */
8793TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8794 addConfigurationProperty("touch.deviceType", "touchScreen");
8795 prepareAxes(POSITION);
8796 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8797
Michael Wrighta9cf4192022-12-01 23:46:39 +00008798 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008799 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8800 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8801 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8802 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8803
8804 NotifyMotionArgs args;
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8806 ASSERT_EQ(26, args.readTime);
8807
8808 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8809 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8810 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8811
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8813 ASSERT_EQ(33, args.readTime);
8814}
8815
8816/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008817 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8818 * events should not be delivered to the listener.
8819 */
8820TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8821 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008822 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008823 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8824 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008825 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8826 prepareAxes(POSITION);
8827 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8828
8829 NotifyMotionArgs motionArgs;
8830 processPosition(mapper, 100, 100);
8831 processSync(mapper);
8832
8833 mFakeListener->assertNotifyMotionWasNotCalled();
8834}
8835
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008836/**
8837 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8838 * the touch mapper can process the events and the events can be delivered to the listener.
8839 */
8840TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8841 addConfigurationProperty("touch.deviceType", "touchScreen");
8842 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008843 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8844 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008845 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8846 prepareAxes(POSITION);
8847 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8848
8849 NotifyMotionArgs motionArgs;
8850 processPosition(mapper, 100, 100);
8851 processSync(mapper);
8852
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8854 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8855}
8856
Garfield Tanc734e4f2021-01-15 20:01:39 -08008857TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8858 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008859 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008860 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8861 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008862 std::optional<DisplayViewport> optionalDisplayViewport =
8863 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8864 ASSERT_TRUE(optionalDisplayViewport.has_value());
8865 DisplayViewport displayViewport = *optionalDisplayViewport;
8866
8867 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8868 prepareAxes(POSITION);
8869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8870
8871 // Finger down
8872 int32_t x = 100, y = 100;
8873 processPosition(mapper, x, y);
8874 processSync(mapper);
8875
8876 NotifyMotionArgs motionArgs;
8877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8878 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8879
8880 // Deactivate display viewport
8881 displayViewport.isActive = false;
8882 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8883 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8884
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008885 // The ongoing touch should be canceled immediately
8886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8888
8889 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008890 x += 10, y += 10;
8891 processPosition(mapper, x, y);
8892 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008894
8895 // Reactivate display viewport
8896 displayViewport.isActive = true;
8897 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8898 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8899
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008900 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008901 x += 10, y += 10;
8902 processPosition(mapper, x, y);
8903 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8905 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008906}
8907
Arthur Hung7c645402019-01-25 17:45:42 +08008908TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8909 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008910 prepareAxes(POSITION | ID | SLOT);
8911 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008913
8914 // Create the second touch screen device, and enable multi fingers.
8915 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008916 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008917 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008918 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008919 std::shared_ptr<InputDevice> device2 =
8920 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008921 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008922
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008923 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8924 0 /*flat*/, 0 /*fuzz*/);
8925 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8926 0 /*flat*/, 0 /*fuzz*/);
8927 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8928 0 /*flat*/, 0 /*fuzz*/);
8929 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8930 0 /*flat*/, 0 /*fuzz*/);
8931 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8932 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8933 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008934
8935 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008936 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008937 std::list<NotifyArgs> unused =
8938 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8939 0 /*changes*/);
8940 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008941
8942 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008943 std::shared_ptr<FakePointerController> fakePointerController =
8944 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008945 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008946
8947 // Setup policy for associated displays and show touches.
8948 const uint8_t hdmi1 = 0;
8949 const uint8_t hdmi2 = 1;
8950 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8951 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8952 mFakePolicy->setShowTouches(true);
8953
8954 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008955 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008956 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008957
8958 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008959 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8960 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8961 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008962
8963 // Two fingers down at default display.
8964 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8965 processPosition(mapper, x1, y1);
8966 processId(mapper, 1);
8967 processSlot(mapper, 1);
8968 processPosition(mapper, x2, y2);
8969 processId(mapper, 2);
8970 processSync(mapper);
8971
8972 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8973 fakePointerController->getSpots().find(DISPLAY_ID);
8974 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8975 ASSERT_EQ(size_t(2), iter->second.size());
8976
8977 // Two fingers down at second display.
8978 processPosition(mapper2, x1, y1);
8979 processId(mapper2, 1);
8980 processSlot(mapper2, 1);
8981 processPosition(mapper2, x2, y2);
8982 processId(mapper2, 2);
8983 processSync(mapper2);
8984
8985 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8986 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8987 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008988
8989 // Disable the show touches configuration and ensure the spots are cleared.
8990 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008991 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8992 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008993
8994 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008995}
8996
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008997TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008998 prepareAxes(POSITION);
8999 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009000 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009001 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009002
9003 NotifyMotionArgs motionArgs;
9004 // Unrotated video frame
9005 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9006 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009007 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009008 processPosition(mapper, 100, 200);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 ASSERT_EQ(frames, motionArgs.videoFrames);
9012
9013 // Subsequent touch events should not have any videoframes
9014 // This is implemented separately in FakeEventHub,
9015 // but that should match the behaviour of TouchVideoDevice.
9016 processPosition(mapper, 200, 200);
9017 processSync(mapper);
9018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9019 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9020}
9021
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009022TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009023 prepareAxes(POSITION);
9024 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009025 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009026 // Unrotated video frame
9027 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9028 NotifyMotionArgs motionArgs;
9029
9030 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009031 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009032 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9033 clearViewports();
9034 prepareDisplay(orientation);
9035 std::vector<TouchVideoFrame> frames{frame};
9036 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9037 processPosition(mapper, 100, 200);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(frames, motionArgs.videoFrames);
9041 }
9042}
9043
9044TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9045 prepareAxes(POSITION);
9046 addConfigurationProperty("touch.deviceType", "touchScreen");
9047 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9048 // orientation-aware are affected by display rotation.
9049 addConfigurationProperty("touch.orientationAware", "0");
9050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9051 // Unrotated video frame
9052 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9053 NotifyMotionArgs motionArgs;
9054
9055 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009056 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009057 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9058 clearViewports();
9059 prepareDisplay(orientation);
9060 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009061 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009062 processPosition(mapper, 100, 200);
9063 processSync(mapper);
9064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009065 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9066 // compared to the display. This is so that when the window transform (which contains the
9067 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9068 // window's coordinate space.
9069 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009070 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009071
9072 // Release finger.
9073 processSync(mapper);
9074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009075 }
9076}
9077
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009078TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009079 prepareAxes(POSITION);
9080 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009081 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009082 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9083 // so mix these.
9084 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9085 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9086 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9087 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9088 NotifyMotionArgs motionArgs;
9089
Michael Wrighta9cf4192022-12-01 23:46:39 +00009090 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009091 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009092 processPosition(mapper, 100, 200);
9093 processSync(mapper);
9094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009095 ASSERT_EQ(frames, motionArgs.videoFrames);
9096}
9097
9098TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9099 prepareAxes(POSITION);
9100 addConfigurationProperty("touch.deviceType", "touchScreen");
9101 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9102 // orientation-aware are affected by display rotation.
9103 addConfigurationProperty("touch.orientationAware", "0");
9104 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9105 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9106 // so mix these.
9107 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9108 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9109 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9110 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9111 NotifyMotionArgs motionArgs;
9112
Michael Wrighta9cf4192022-12-01 23:46:39 +00009113 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009114 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9115 processPosition(mapper, 100, 200);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9119 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9120 // compared to the display. This is so that when the window transform (which contains the
9121 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9122 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009123 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009124 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009125 ASSERT_EQ(frames, motionArgs.videoFrames);
9126}
9127
Arthur Hung9da14732019-09-02 16:16:58 +08009128/**
9129 * If we had defined port associations, but the viewport is not ready, the touch device would be
9130 * expected to be disabled, and it should be enabled after the viewport has found.
9131 */
9132TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009133 constexpr uint8_t hdmi2 = 1;
9134 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009135 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009136
9137 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9138
9139 addConfigurationProperty("touch.deviceType", "touchScreen");
9140 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009141 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009142
9143 ASSERT_EQ(mDevice->isEnabled(), false);
9144
9145 // Add display on hdmi2, the device should be enabled and can receive touch event.
9146 prepareSecondaryDisplay(type, hdmi2);
9147 ASSERT_EQ(mDevice->isEnabled(), true);
9148
9149 // Send a touch event.
9150 processPosition(mapper, 100, 100);
9151 processSync(mapper);
9152
9153 NotifyMotionArgs args;
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9155 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9156}
9157
Arthur Hung421eb1c2020-01-16 00:09:42 +08009158TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009159 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009160 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009161 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009162 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009163
9164 NotifyMotionArgs motionArgs;
9165
9166 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9167 // finger down
9168 processId(mapper, 1);
9169 processPosition(mapper, x1, y1);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9174
9175 // finger move
9176 processId(mapper, 1);
9177 processPosition(mapper, x2, y2);
9178 processSync(mapper);
9179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9181 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9182
9183 // finger up.
9184 processId(mapper, -1);
9185 processSync(mapper);
9186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9187 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9188 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9189
9190 // new finger down
9191 processId(mapper, 1);
9192 processPosition(mapper, x3, y3);
9193 processSync(mapper);
9194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9196 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9197}
9198
9199/**
arthurhungcc7f9802020-04-30 17:55:40 +08009200 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9201 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009202 */
arthurhungcc7f9802020-04-30 17:55:40 +08009203TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009204 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009205 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009206 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009208
9209 NotifyMotionArgs motionArgs;
9210
9211 // default tool type is finger
9212 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009213 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009214 processPosition(mapper, x1, y1);
9215 processSync(mapper);
9216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9217 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9218 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9219
9220 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9221 processToolType(mapper, MT_TOOL_PALM);
9222 processSync(mapper);
9223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9224 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9225
9226 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009227 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009228 processPosition(mapper, x2, y2);
9229 processSync(mapper);
9230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9231
9232 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009233 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9236
9237 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009238 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009239 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009240 processPosition(mapper, x3, y3);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9245}
9246
arthurhungbf89a482020-04-17 17:37:55 +08009247/**
arthurhungcc7f9802020-04-30 17:55:40 +08009248 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9249 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009250 */
arthurhungcc7f9802020-04-30 17:55:40 +08009251TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009252 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009253 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009254 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9255 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9256
9257 NotifyMotionArgs motionArgs;
9258
9259 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009260 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9261 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009262 processPosition(mapper, x1, y1);
9263 processSync(mapper);
9264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9265 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9266 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9267
9268 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009269 processSlot(mapper, SECOND_SLOT);
9270 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009271 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009272 processSync(mapper);
9273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009274 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9276
9277 // If the tool type of the first finger changes to MT_TOOL_PALM,
9278 // we expect to receive ACTION_POINTER_UP with cancel flag.
9279 processSlot(mapper, FIRST_SLOT);
9280 processId(mapper, FIRST_TRACKING_ID);
9281 processToolType(mapper, MT_TOOL_PALM);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009284 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009285 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9286
9287 // The following MOVE events of second finger should be processed.
9288 processSlot(mapper, SECOND_SLOT);
9289 processId(mapper, SECOND_TRACKING_ID);
9290 processPosition(mapper, x2 + 1, y2 + 1);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9294 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9295
9296 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9297 // it. Second finger receive move.
9298 processSlot(mapper, FIRST_SLOT);
9299 processId(mapper, INVALID_TRACKING_ID);
9300 processSync(mapper);
9301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9303 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9304
9305 // Second finger keeps moving.
9306 processSlot(mapper, SECOND_SLOT);
9307 processId(mapper, SECOND_TRACKING_ID);
9308 processPosition(mapper, x2 + 2, y2 + 2);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9312 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9313
9314 // Second finger up.
9315 processId(mapper, INVALID_TRACKING_ID);
9316 processSync(mapper);
9317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9318 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9319 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9320}
9321
9322/**
9323 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9324 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9325 */
9326TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9327 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009328 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009329 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9330 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9331
9332 NotifyMotionArgs motionArgs;
9333
9334 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9335 // First finger down.
9336 processId(mapper, FIRST_TRACKING_ID);
9337 processPosition(mapper, x1, y1);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9341 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9342
9343 // Second finger down.
9344 processSlot(mapper, SECOND_SLOT);
9345 processId(mapper, SECOND_TRACKING_ID);
9346 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009347 processSync(mapper);
9348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009349 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9351
arthurhungcc7f9802020-04-30 17:55:40 +08009352 // If the tool type of the first finger changes to MT_TOOL_PALM,
9353 // we expect to receive ACTION_POINTER_UP with cancel flag.
9354 processSlot(mapper, FIRST_SLOT);
9355 processId(mapper, FIRST_TRACKING_ID);
9356 processToolType(mapper, MT_TOOL_PALM);
9357 processSync(mapper);
9358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009359 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009360 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9361
9362 // Second finger keeps moving.
9363 processSlot(mapper, SECOND_SLOT);
9364 processId(mapper, SECOND_TRACKING_ID);
9365 processPosition(mapper, x2 + 1, y2 + 1);
9366 processSync(mapper);
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9368 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9369
9370 // second finger becomes palm, receive cancel due to only 1 finger is active.
9371 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009372 processToolType(mapper, MT_TOOL_PALM);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9376
arthurhungcc7f9802020-04-30 17:55:40 +08009377 // third finger down.
9378 processSlot(mapper, THIRD_SLOT);
9379 processId(mapper, THIRD_TRACKING_ID);
9380 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009381 processPosition(mapper, x3, y3);
9382 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009386 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9387
9388 // third finger move
9389 processId(mapper, THIRD_TRACKING_ID);
9390 processPosition(mapper, x3 + 1, y3 + 1);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9394
9395 // first finger up, third finger receive move.
9396 processSlot(mapper, FIRST_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 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9402
9403 // second finger up, third finger receive move.
9404 processSlot(mapper, SECOND_SLOT);
9405 processId(mapper, INVALID_TRACKING_ID);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9409 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9410
9411 // third finger up.
9412 processSlot(mapper, THIRD_SLOT);
9413 processId(mapper, INVALID_TRACKING_ID);
9414 processSync(mapper);
9415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9416 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9417 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9418}
9419
9420/**
9421 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9422 * and the active finger could still be allowed to receive the events
9423 */
9424TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9425 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009426 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009427 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9429
9430 NotifyMotionArgs motionArgs;
9431
9432 // default tool type is finger
9433 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9434 processId(mapper, FIRST_TRACKING_ID);
9435 processPosition(mapper, x1, y1);
9436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9438 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9440
9441 // Second finger down.
9442 processSlot(mapper, SECOND_SLOT);
9443 processId(mapper, SECOND_TRACKING_ID);
9444 processPosition(mapper, x2, y2);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009447 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9449
9450 // If the tool type of the second finger changes to MT_TOOL_PALM,
9451 // we expect to receive ACTION_POINTER_UP with cancel flag.
9452 processId(mapper, SECOND_TRACKING_ID);
9453 processToolType(mapper, MT_TOOL_PALM);
9454 processSync(mapper);
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009456 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009457 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9458
9459 // The following MOVE event should be processed.
9460 processSlot(mapper, FIRST_SLOT);
9461 processId(mapper, FIRST_TRACKING_ID);
9462 processPosition(mapper, x1 + 1, y1 + 1);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9465 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9466 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9467
9468 // second finger up.
9469 processSlot(mapper, SECOND_SLOT);
9470 processId(mapper, INVALID_TRACKING_ID);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9474
9475 // first finger keep moving
9476 processSlot(mapper, FIRST_SLOT);
9477 processId(mapper, FIRST_TRACKING_ID);
9478 processPosition(mapper, x1 + 2, y1 + 2);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9482
9483 // first finger up.
9484 processId(mapper, INVALID_TRACKING_ID);
9485 processSync(mapper);
9486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9487 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9488 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009489}
9490
Arthur Hung9ad18942021-06-19 02:04:46 +00009491/**
9492 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9493 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9494 * cause slot be valid again.
9495 */
9496TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9497 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009498 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009499 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9500 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9501
9502 NotifyMotionArgs motionArgs;
9503
9504 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9505 // First finger down.
9506 processId(mapper, FIRST_TRACKING_ID);
9507 processPosition(mapper, x1, y1);
9508 processPressure(mapper, RAW_PRESSURE_MAX);
9509 processSync(mapper);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9511 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9512 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9513
9514 // First finger move.
9515 processId(mapper, FIRST_TRACKING_ID);
9516 processPosition(mapper, x1 + 1, y1 + 1);
9517 processPressure(mapper, RAW_PRESSURE_MAX);
9518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9521 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9522
9523 // Second finger down.
9524 processSlot(mapper, SECOND_SLOT);
9525 processId(mapper, SECOND_TRACKING_ID);
9526 processPosition(mapper, x2, y2);
9527 processPressure(mapper, RAW_PRESSURE_MAX);
9528 processSync(mapper);
9529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009530 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009531 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9532
9533 // second finger up with some unexpected data.
9534 processSlot(mapper, SECOND_SLOT);
9535 processId(mapper, INVALID_TRACKING_ID);
9536 processPosition(mapper, x2, y2);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009539 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009540 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9541
9542 // first finger up with some unexpected data.
9543 processSlot(mapper, FIRST_SLOT);
9544 processId(mapper, INVALID_TRACKING_ID);
9545 processPosition(mapper, x2, y2);
9546 processPressure(mapper, RAW_PRESSURE_MAX);
9547 processSync(mapper);
9548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9549 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9550 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9551}
9552
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009553TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9554 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009555 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009556 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9557 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9558
9559 // First finger down.
9560 processId(mapper, FIRST_TRACKING_ID);
9561 processPosition(mapper, 100, 200);
9562 processPressure(mapper, RAW_PRESSURE_MAX);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9565 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9566
9567 // Second finger down.
9568 processSlot(mapper, SECOND_SLOT);
9569 processId(mapper, SECOND_TRACKING_ID);
9570 processPosition(mapper, 300, 400);
9571 processPressure(mapper, RAW_PRESSURE_MAX);
9572 processSync(mapper);
9573 ASSERT_NO_FATAL_FAILURE(
9574 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9575
9576 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009577 // preserved. Resetting should cancel the ongoing gesture.
9578 resetMapper(mapper, ARBITRARY_TIME);
9579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9580 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009581
9582 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9583 // the existing touch state to generate a down event.
9584 processPosition(mapper, 301, 302);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9587 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9589 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9590
9591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9592}
9593
9594TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9595 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009596 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009597 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9598 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9599
9600 // First finger touches down and releases.
9601 processId(mapper, FIRST_TRACKING_ID);
9602 processPosition(mapper, 100, 200);
9603 processPressure(mapper, RAW_PRESSURE_MAX);
9604 processSync(mapper);
9605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9606 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9607 processId(mapper, INVALID_TRACKING_ID);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(
9610 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9611
9612 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9613 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009614 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9616
9617 // Send an empty sync frame. Since there are no pointers, no events are generated.
9618 processSync(mapper);
9619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9620}
9621
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009622TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009623 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009624 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009625 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9626 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009628
9629 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9630 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9631 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9632 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9634
9635 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009636 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009637 processId(mapper, FIRST_TRACKING_ID);
9638 processToolType(mapper, MT_TOOL_PEN);
9639 processPosition(mapper, 100, 200);
9640 processPressure(mapper, RAW_PRESSURE_MAX);
9641 processSync(mapper);
9642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9644 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9645 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9646
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009647 // Now that we know the device supports styluses, ensure that the device is re-configured with
9648 // the stylus source.
9649 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9650 {
9651 const auto& devices = mReader->getInputDevices();
9652 auto deviceInfo =
9653 std::find_if(devices.begin(), devices.end(),
9654 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9655 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9657 }
9658
9659 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9661
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009662 processId(mapper, INVALID_TRACKING_ID);
9663 processSync(mapper);
9664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9666 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9667 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009668}
9669
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009670// --- MultiTouchInputMapperTest_ExternalDevice ---
9671
9672class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9673protected:
Chris Yea52ade12020-08-27 16:49:20 -07009674 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009675};
9676
9677/**
9678 * Expect fallback to internal viewport if device is external and external viewport is not present.
9679 */
9680TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9681 prepareAxes(POSITION);
9682 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009683 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009684 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9685
9686 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9687
9688 NotifyMotionArgs motionArgs;
9689
9690 // Expect the event to be sent to the internal viewport,
9691 // because an external viewport is not present.
9692 processPosition(mapper, 100, 100);
9693 processSync(mapper);
9694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9695 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9696
9697 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009698 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009699 processPosition(mapper, 100, 100);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9703}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009704
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009705TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9706 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9707 std::shared_ptr<FakePointerController> fakePointerController =
9708 std::make_shared<FakePointerController>();
9709 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9710 fakePointerController->setPosition(0, 0);
9711 fakePointerController->setButtonState(0);
9712
9713 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009714 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009715 prepareAxes(POSITION | ID | SLOT);
9716 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9717 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9718 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009719 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009720 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9721
9722 // captured touchpad should be a touchpad source
9723 NotifyDeviceResetArgs resetArgs;
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9725 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9726
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009727 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009728
9729 const InputDeviceInfo::MotionRange* relRangeX =
9730 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9731 ASSERT_NE(relRangeX, nullptr);
9732 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9733 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9734 const InputDeviceInfo::MotionRange* relRangeY =
9735 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9736 ASSERT_NE(relRangeY, nullptr);
9737 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9738 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9739
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009740 // run captured pointer tests - note that this is unscaled, so input listener events should be
9741 // identical to what the hardware sends (accounting for any
9742 // calibration).
9743 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009744 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009745 processId(mapper, 1);
9746 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9747 processKey(mapper, BTN_TOUCH, 1);
9748 processSync(mapper);
9749
9750 // expect coord[0] to contain initial location of touch 0
9751 NotifyMotionArgs args;
9752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9753 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9754 ASSERT_EQ(1U, args.pointerCount);
9755 ASSERT_EQ(0, args.pointerProperties[0].id);
9756 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9757 ASSERT_NO_FATAL_FAILURE(
9758 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9759
9760 // FINGER 1 DOWN
9761 processSlot(mapper, 1);
9762 processId(mapper, 2);
9763 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9764 processSync(mapper);
9765
9766 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009768 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009769 ASSERT_EQ(2U, args.pointerCount);
9770 ASSERT_EQ(0, args.pointerProperties[0].id);
9771 ASSERT_EQ(1, args.pointerProperties[1].id);
9772 ASSERT_NO_FATAL_FAILURE(
9773 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9774 ASSERT_NO_FATAL_FAILURE(
9775 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9776
9777 // FINGER 1 MOVE
9778 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9779 processSync(mapper);
9780
9781 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9782 // from move
9783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9785 ASSERT_NO_FATAL_FAILURE(
9786 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9787 ASSERT_NO_FATAL_FAILURE(
9788 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9789
9790 // FINGER 0 MOVE
9791 processSlot(mapper, 0);
9792 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9793 processSync(mapper);
9794
9795 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9798 ASSERT_NO_FATAL_FAILURE(
9799 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9800 ASSERT_NO_FATAL_FAILURE(
9801 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9802
9803 // BUTTON DOWN
9804 processKey(mapper, BTN_LEFT, 1);
9805 processSync(mapper);
9806
9807 // touchinputmapper design sends a move before button press
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9809 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9812
9813 // BUTTON UP
9814 processKey(mapper, BTN_LEFT, 0);
9815 processSync(mapper);
9816
9817 // touchinputmapper design sends a move after button release
9818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9819 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9822
9823 // FINGER 0 UP
9824 processId(mapper, -1);
9825 processSync(mapper);
9826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9827 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9828
9829 // FINGER 1 MOVE
9830 processSlot(mapper, 1);
9831 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9832 processSync(mapper);
9833
9834 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9837 ASSERT_EQ(1U, args.pointerCount);
9838 ASSERT_EQ(1, args.pointerProperties[0].id);
9839 ASSERT_NO_FATAL_FAILURE(
9840 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9841
9842 // FINGER 1 UP
9843 processId(mapper, -1);
9844 processKey(mapper, BTN_TOUCH, 0);
9845 processSync(mapper);
9846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9847 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9848
Harry Cutts16a24cc2022-10-26 15:22:19 +00009849 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009850 mFakePolicy->setPointerCapture(false);
9851 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009853 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009854}
9855
9856TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9857 std::shared_ptr<FakePointerController> fakePointerController =
9858 std::make_shared<FakePointerController>();
9859 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9860 fakePointerController->setPosition(0, 0);
9861 fakePointerController->setButtonState(0);
9862
9863 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009864 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009865 prepareAxes(POSITION | ID | SLOT);
9866 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9867 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009868 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009869 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9870 // run uncaptured pointer tests - pushes out generic events
9871 // FINGER 0 DOWN
9872 processId(mapper, 3);
9873 processPosition(mapper, 100, 100);
9874 processKey(mapper, BTN_TOUCH, 1);
9875 processSync(mapper);
9876
9877 // start at (100,100), cursor should be at (0,0) * scale
9878 NotifyMotionArgs args;
9879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9880 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9881 ASSERT_NO_FATAL_FAILURE(
9882 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9883
9884 // FINGER 0 MOVE
9885 processPosition(mapper, 200, 200);
9886 processSync(mapper);
9887
9888 // compute scaling to help with touch position checking
9889 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9890 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9891 float scale =
9892 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9893
9894 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9896 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9897 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9898 0, 0, 0, 0, 0, 0, 0));
9899}
9900
9901TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9902 std::shared_ptr<FakePointerController> fakePointerController =
9903 std::make_shared<FakePointerController>();
9904
Michael Wrighta9cf4192022-12-01 23:46:39 +00009905 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009906 prepareAxes(POSITION | ID | SLOT);
9907 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009908 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009909 mFakePolicy->setPointerCapture(false);
9910 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9911
Harry Cutts16a24cc2022-10-26 15:22:19 +00009912 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9913 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009914
Harry Cutts16a24cc2022-10-26 15:22:19 +00009915 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009916 mFakePolicy->setPointerCapture(true);
9917 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9918 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9919}
9920
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009921// --- BluetoothMultiTouchInputMapperTest ---
9922
9923class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9924protected:
9925 void SetUp() override {
9926 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9927 }
9928};
9929
9930TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9931 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009932 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009933 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9934 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9935
9936 nsecs_t kernelEventTime = ARBITRARY_TIME;
9937 nsecs_t expectedEventTime = ARBITRARY_TIME;
9938 // Touch down.
9939 processId(mapper, FIRST_TRACKING_ID);
9940 processPosition(mapper, 100, 200);
9941 processPressure(mapper, RAW_PRESSURE_MAX);
9942 processSync(mapper, ARBITRARY_TIME);
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9945
9946 // Process several events that come in quick succession, according to their timestamps.
9947 for (int i = 0; i < 3; i++) {
9948 constexpr static nsecs_t delta = ms2ns(1);
9949 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9950 kernelEventTime += delta;
9951 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9952
9953 processPosition(mapper, 101 + i, 201 + i);
9954 processSync(mapper, kernelEventTime);
9955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9956 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9957 WithEventTime(expectedEventTime))));
9958 }
9959
9960 // Release the touch.
9961 processId(mapper, INVALID_TRACKING_ID);
9962 processPressure(mapper, RAW_PRESSURE_MIN);
9963 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9965 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9966 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9967}
9968
9969// --- MultiTouchPointerModeTest ---
9970
HQ Liue6983c72022-04-19 22:14:56 +00009971class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9972protected:
9973 float mPointerMovementScale;
9974 float mPointerXZoomScale;
9975 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9976 addConfigurationProperty("touch.deviceType", "pointer");
9977 std::shared_ptr<FakePointerController> fakePointerController =
9978 std::make_shared<FakePointerController>();
9979 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9980 fakePointerController->setPosition(0, 0);
9981 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +00009982 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009983
9984 prepareAxes(POSITION);
9985 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9986 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9987 // needs to be disabled, and the pointer gesture needs to be enabled.
9988 mFakePolicy->setPointerCapture(false);
9989 mFakePolicy->setPointerGestureEnabled(true);
9990 mFakePolicy->setPointerController(fakePointerController);
9991
9992 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9993 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9994 mPointerMovementScale =
9995 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9996 mPointerXZoomScale =
9997 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9998 }
9999
10000 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10001 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10002 /*flat*/ 0,
10003 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10004 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10005 /*flat*/ 0,
10006 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10007 }
10008};
10009
10010/**
10011 * Two fingers down on a pointer mode touch pad. The width
10012 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10013 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10014 * be greater than the both value to be freeform gesture, so that after two
10015 * fingers start to move downwards, the gesture should be swipe.
10016 */
10017TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10018 // The min freeform gesture width is 25units/mm x 30mm = 750
10019 // which is greater than fraction of the diagnal length of the touchpad (349).
10020 // Thus, MaxSwipWidth is 750.
10021 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10022 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10023 NotifyMotionArgs motionArgs;
10024
10025 // Two fingers down at once.
10026 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10027 // Pointer's initial position is used the [0,0] coordinate.
10028 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10029
10030 processId(mapper, FIRST_TRACKING_ID);
10031 processPosition(mapper, x1, y1);
10032 processMTSync(mapper);
10033 processId(mapper, SECOND_TRACKING_ID);
10034 processPosition(mapper, x2, y2);
10035 processMTSync(mapper);
10036 processSync(mapper);
10037
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10039 ASSERT_EQ(1U, motionArgs.pointerCount);
10040 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10041 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010042 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010043 ASSERT_NO_FATAL_FAILURE(
10044 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10045
10046 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10047 // that there should be 1 pointer.
10048 int32_t movingDistance = 200;
10049 y1 += movingDistance;
10050 y2 += movingDistance;
10051
10052 processId(mapper, FIRST_TRACKING_ID);
10053 processPosition(mapper, x1, y1);
10054 processMTSync(mapper);
10055 processId(mapper, SECOND_TRACKING_ID);
10056 processPosition(mapper, x2, y2);
10057 processMTSync(mapper);
10058 processSync(mapper);
10059
10060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10061 ASSERT_EQ(1U, motionArgs.pointerCount);
10062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10063 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010064 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010065 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10066 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10067 0, 0, 0, 0));
10068}
10069
10070/**
10071 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10072 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10073 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10074 * value to be freeform gesture, so that after two fingers start to move downwards,
10075 * the gesture should be swipe.
10076 */
10077TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10078 // The min freeform gesture width is 5units/mm x 30mm = 150
10079 // which is greater than fraction of the diagnal length of the touchpad (349).
10080 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10081 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10083 NotifyMotionArgs motionArgs;
10084
10085 // Two fingers down at once.
10086 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10087 // Pointer's initial position is used the [0,0] coordinate.
10088 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10089
10090 processId(mapper, FIRST_TRACKING_ID);
10091 processPosition(mapper, x1, y1);
10092 processMTSync(mapper);
10093 processId(mapper, SECOND_TRACKING_ID);
10094 processPosition(mapper, x2, y2);
10095 processMTSync(mapper);
10096 processSync(mapper);
10097
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10099 ASSERT_EQ(1U, motionArgs.pointerCount);
10100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010102 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010103 ASSERT_NO_FATAL_FAILURE(
10104 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10105
10106 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10107 // and there should be 1 pointer.
10108 int32_t movingDistance = 200;
10109 y1 += movingDistance;
10110 y2 += movingDistance;
10111
10112 processId(mapper, FIRST_TRACKING_ID);
10113 processPosition(mapper, x1, y1);
10114 processMTSync(mapper);
10115 processId(mapper, SECOND_TRACKING_ID);
10116 processPosition(mapper, x2, y2);
10117 processMTSync(mapper);
10118 processSync(mapper);
10119
10120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10121 ASSERT_EQ(1U, motionArgs.pointerCount);
10122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010124 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010125 // New coordinate is the scaled relative coordinate from the initial coordinate.
10126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10127 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10128 0, 0, 0, 0));
10129}
10130
10131/**
10132 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10133 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10134 * freeform gestures after two fingers start to move downwards.
10135 */
10136TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10137 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10138 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10139
10140 NotifyMotionArgs motionArgs;
10141
10142 // Two fingers down at once. Wider than the max swipe width.
10143 // The gesture is expected to be PRESS, then transformed to FREEFORM
10144 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10145
10146 processId(mapper, FIRST_TRACKING_ID);
10147 processPosition(mapper, x1, y1);
10148 processMTSync(mapper);
10149 processId(mapper, SECOND_TRACKING_ID);
10150 processPosition(mapper, x2, y2);
10151 processMTSync(mapper);
10152 processSync(mapper);
10153
10154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10155 ASSERT_EQ(1U, motionArgs.pointerCount);
10156 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010158 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010159 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10160 ASSERT_NO_FATAL_FAILURE(
10161 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10162
10163 int32_t movingDistance = 200;
10164
10165 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10166 // then two down events for two pointers.
10167 y1 += movingDistance;
10168 y2 += movingDistance;
10169
10170 processId(mapper, FIRST_TRACKING_ID);
10171 processPosition(mapper, x1, y1);
10172 processMTSync(mapper);
10173 processId(mapper, SECOND_TRACKING_ID);
10174 processPosition(mapper, x2, y2);
10175 processMTSync(mapper);
10176 processSync(mapper);
10177
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10179 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10180 ASSERT_EQ(1U, motionArgs.pointerCount);
10181 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10184 ASSERT_EQ(1U, motionArgs.pointerCount);
10185 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10187 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010188 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010189 ASSERT_EQ(2U, motionArgs.pointerCount);
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010192 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010193 // Two pointers' scaled relative coordinates from their initial centroid.
10194 // Initial y coordinates are 0 as y1 and y2 have the same value.
10195 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10196 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10197 // When pointers move, the new coordinates equal to the initial coordinates plus
10198 // scaled moving distance.
10199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10200 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10201 0, 0, 0, 0));
10202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10203 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10204 0, 0, 0, 0));
10205
10206 // Move two fingers down again, expect one MOVE motion event.
10207 y1 += movingDistance;
10208 y2 += movingDistance;
10209
10210 processId(mapper, FIRST_TRACKING_ID);
10211 processPosition(mapper, x1, y1);
10212 processMTSync(mapper);
10213 processId(mapper, SECOND_TRACKING_ID);
10214 processPosition(mapper, x2, y2);
10215 processMTSync(mapper);
10216 processSync(mapper);
10217
10218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10219 ASSERT_EQ(2U, motionArgs.pointerCount);
10220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010222 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10224 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10225 0, 0, 0, 0, 0));
10226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10227 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10228 0, 0, 0, 0, 0));
10229}
10230
Harry Cutts39b7ca22022-10-05 15:55:48 +000010231TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10232 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10234 NotifyMotionArgs motionArgs;
10235
10236 // Place two fingers down.
10237 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10238
10239 processId(mapper, FIRST_TRACKING_ID);
10240 processPosition(mapper, x1, y1);
10241 processMTSync(mapper);
10242 processId(mapper, SECOND_TRACKING_ID);
10243 processPosition(mapper, x2, y2);
10244 processMTSync(mapper);
10245 processSync(mapper);
10246
10247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10248 ASSERT_EQ(1U, motionArgs.pointerCount);
10249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10250 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10251 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10252 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10253
10254 // Move the two fingers down and to the left.
10255 int32_t movingDistance = 200;
10256 x1 -= movingDistance;
10257 y1 += movingDistance;
10258 x2 -= movingDistance;
10259 y2 += movingDistance;
10260
10261 processId(mapper, FIRST_TRACKING_ID);
10262 processPosition(mapper, x1, y1);
10263 processMTSync(mapper);
10264 processId(mapper, SECOND_TRACKING_ID);
10265 processPosition(mapper, x2, y2);
10266 processMTSync(mapper);
10267 processSync(mapper);
10268
10269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10270 ASSERT_EQ(1U, motionArgs.pointerCount);
10271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10272 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10273 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10274 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10275}
10276
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010277TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10278 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10279 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10280 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10282
10283 // Start a stylus gesture.
10284 processKey(mapper, BTN_TOOL_PEN, 1);
10285 processId(mapper, FIRST_TRACKING_ID);
10286 processPosition(mapper, 100, 200);
10287 processSync(mapper);
10288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10289 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10290 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10291 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10292 // TODO(b/257078296): Pointer mode generates extra event.
10293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10295 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10296 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10298
10299 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10300 // gesture should be disabled.
10301 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10302 viewport->isActive = false;
10303 mFakePolicy->updateViewport(*viewport);
10304 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10306 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10307 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10308 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10309 // TODO(b/257078296): Pointer mode generates extra event.
10310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10311 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10312 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10313 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10315}
10316
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010317// --- JoystickInputMapperTest ---
10318
10319class JoystickInputMapperTest : public InputMapperTest {
10320protected:
10321 static const int32_t RAW_X_MIN;
10322 static const int32_t RAW_X_MAX;
10323 static const int32_t RAW_Y_MIN;
10324 static const int32_t RAW_Y_MAX;
10325
10326 void SetUp() override {
10327 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10328 }
10329 void prepareAxes() {
10330 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10332 }
10333
10334 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10336 }
10337
10338 void processSync(JoystickInputMapper& mapper) {
10339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10340 }
10341
Michael Wrighta9cf4192022-12-01 23:46:39 +000010342 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010343 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10344 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10345 NO_PORT, ViewportType::VIRTUAL);
10346 }
10347};
10348
10349const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10350const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10351const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10352const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10353
10354TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10355 prepareAxes();
10356 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10357
10358 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10359
Michael Wrighta9cf4192022-12-01 23:46:39 +000010360 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010361
10362 // Send an axis event
10363 processAxis(mapper, ABS_X, 100);
10364 processSync(mapper);
10365
10366 NotifyMotionArgs args;
10367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10368 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10369
10370 // Send another axis event
10371 processAxis(mapper, ABS_Y, 100);
10372 processSync(mapper);
10373
10374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10375 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10376}
10377
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010378// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010379
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010380class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010381protected:
10382 static const char* DEVICE_NAME;
10383 static const char* DEVICE_LOCATION;
10384 static const int32_t DEVICE_ID;
10385 static const int32_t DEVICE_GENERATION;
10386 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010387 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010388 static const int32_t EVENTHUB_ID;
10389
10390 std::shared_ptr<FakeEventHub> mFakeEventHub;
10391 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010392 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010393 std::unique_ptr<InstrumentedInputReader> mReader;
10394 std::shared_ptr<InputDevice> mDevice;
10395
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010396 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010397 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010398 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010399 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010400 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010401 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10403 }
10404
10405 void SetUp() override { SetUp(DEVICE_CLASSES); }
10406
10407 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010408 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010409 mFakePolicy.clear();
10410 }
10411
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010412 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010413 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10414 mReader->requestRefreshConfiguration(changes);
10415 mReader->loopOnce();
10416 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010417 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010418 }
10419
10420 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10421 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010422 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010423 InputDeviceIdentifier identifier;
10424 identifier.name = name;
10425 identifier.location = location;
10426 std::shared_ptr<InputDevice> device =
10427 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10428 identifier);
10429 mReader->pushNextDevice(device);
10430 mFakeEventHub->addDevice(eventHubId, name, classes);
10431 mReader->loopOnce();
10432 return device;
10433 }
10434
10435 template <class T, typename... Args>
10436 T& addControllerAndConfigure(Args... args) {
10437 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10438
10439 return controller;
10440 }
10441};
10442
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010443const char* PeripheralControllerTest::DEVICE_NAME = "device";
10444const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10445const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10446const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10447const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010448const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10449 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010450const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010451
10452// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010453class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010454protected:
10455 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010456 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010457 }
10458};
10459
10460TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010461 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010462
Harry Cuttsa5b71292022-11-28 12:56:17 +000010463 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10464 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10465 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010466}
10467
10468TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010469 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010470
Harry Cuttsa5b71292022-11-28 12:56:17 +000010471 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10472 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10473 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010474}
10475
10476// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010477class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010478protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010479 void SetUp() override {
10480 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10481 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010482};
10483
Chris Ye85758332021-05-16 23:05:17 -070010484TEST_F(LightControllerTest, MonoLight) {
10485 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010486 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010487 .maxBrightness = 255,
10488 .flags = InputLightClass::BRIGHTNESS,
10489 .path = ""};
10490 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010491
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010492 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010493 InputDeviceInfo info;
10494 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010495 std::vector<InputDeviceLightInfo> lights = info.getLights();
10496 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010497 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10498 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10499
10500 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10501 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10502}
10503
10504TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10505 RawLightInfo infoMono = {.id = 1,
10506 .name = "mono_keyboard_backlight",
10507 .maxBrightness = 255,
10508 .flags = InputLightClass::BRIGHTNESS |
10509 InputLightClass::KEYBOARD_BACKLIGHT,
10510 .path = ""};
10511 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10512
10513 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10514 InputDeviceInfo info;
10515 controller.populateDeviceInfo(&info);
10516 std::vector<InputDeviceLightInfo> lights = info.getLights();
10517 ASSERT_EQ(1U, lights.size());
10518 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10519 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010520
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010521 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10522 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010523}
10524
10525TEST_F(LightControllerTest, RGBLight) {
10526 RawLightInfo infoRed = {.id = 1,
10527 .name = "red",
10528 .maxBrightness = 255,
10529 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10530 .path = ""};
10531 RawLightInfo infoGreen = {.id = 2,
10532 .name = "green",
10533 .maxBrightness = 255,
10534 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10535 .path = ""};
10536 RawLightInfo infoBlue = {.id = 3,
10537 .name = "blue",
10538 .maxBrightness = 255,
10539 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10540 .path = ""};
10541 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10542 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10543 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10544
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010545 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010546 InputDeviceInfo info;
10547 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010548 std::vector<InputDeviceLightInfo> lights = info.getLights();
10549 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010550 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10551 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10552 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10553
10554 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10555 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10556}
10557
10558TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10559 RawLightInfo infoRed = {.id = 1,
10560 .name = "red_keyboard_backlight",
10561 .maxBrightness = 255,
10562 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10563 InputLightClass::KEYBOARD_BACKLIGHT,
10564 .path = ""};
10565 RawLightInfo infoGreen = {.id = 2,
10566 .name = "green_keyboard_backlight",
10567 .maxBrightness = 255,
10568 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10569 InputLightClass::KEYBOARD_BACKLIGHT,
10570 .path = ""};
10571 RawLightInfo infoBlue = {.id = 3,
10572 .name = "blue_keyboard_backlight",
10573 .maxBrightness = 255,
10574 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10575 InputLightClass::KEYBOARD_BACKLIGHT,
10576 .path = ""};
10577 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10578 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10579 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10580
10581 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10582 InputDeviceInfo info;
10583 controller.populateDeviceInfo(&info);
10584 std::vector<InputDeviceLightInfo> lights = info.getLights();
10585 ASSERT_EQ(1U, lights.size());
10586 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10587 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10588 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10589
10590 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10591 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10592}
10593
10594TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10595 RawLightInfo infoRed = {.id = 1,
10596 .name = "red",
10597 .maxBrightness = 255,
10598 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10599 .path = ""};
10600 RawLightInfo infoGreen = {.id = 2,
10601 .name = "green",
10602 .maxBrightness = 255,
10603 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10604 .path = ""};
10605 RawLightInfo infoBlue = {.id = 3,
10606 .name = "blue",
10607 .maxBrightness = 255,
10608 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10609 .path = ""};
10610 RawLightInfo infoGlobal = {.id = 3,
10611 .name = "global_keyboard_backlight",
10612 .maxBrightness = 255,
10613 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10614 InputLightClass::KEYBOARD_BACKLIGHT,
10615 .path = ""};
10616 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10617 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10618 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10619 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10620
10621 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10622 InputDeviceInfo info;
10623 controller.populateDeviceInfo(&info);
10624 std::vector<InputDeviceLightInfo> lights = info.getLights();
10625 ASSERT_EQ(1U, lights.size());
10626 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10627 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10628 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010629
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010630 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10631 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010632}
10633
10634TEST_F(LightControllerTest, MultiColorRGBLight) {
10635 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010636 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010637 .maxBrightness = 255,
10638 .flags = InputLightClass::BRIGHTNESS |
10639 InputLightClass::MULTI_INTENSITY |
10640 InputLightClass::MULTI_INDEX,
10641 .path = ""};
10642
10643 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10644
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010645 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010646 InputDeviceInfo info;
10647 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010648 std::vector<InputDeviceLightInfo> lights = info.getLights();
10649 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010650 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10651 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10652 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10653
10654 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10655 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10656}
10657
10658TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10659 RawLightInfo infoColor = {.id = 1,
10660 .name = "multi_color_keyboard_backlight",
10661 .maxBrightness = 255,
10662 .flags = InputLightClass::BRIGHTNESS |
10663 InputLightClass::MULTI_INTENSITY |
10664 InputLightClass::MULTI_INDEX |
10665 InputLightClass::KEYBOARD_BACKLIGHT,
10666 .path = ""};
10667
10668 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10669
10670 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10671 InputDeviceInfo info;
10672 controller.populateDeviceInfo(&info);
10673 std::vector<InputDeviceLightInfo> lights = info.getLights();
10674 ASSERT_EQ(1U, lights.size());
10675 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10676 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10677 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010678
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010679 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10680 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010681}
10682
10683TEST_F(LightControllerTest, PlayerIdLight) {
10684 RawLightInfo info1 = {.id = 1,
10685 .name = "player1",
10686 .maxBrightness = 255,
10687 .flags = InputLightClass::BRIGHTNESS,
10688 .path = ""};
10689 RawLightInfo info2 = {.id = 2,
10690 .name = "player2",
10691 .maxBrightness = 255,
10692 .flags = InputLightClass::BRIGHTNESS,
10693 .path = ""};
10694 RawLightInfo info3 = {.id = 3,
10695 .name = "player3",
10696 .maxBrightness = 255,
10697 .flags = InputLightClass::BRIGHTNESS,
10698 .path = ""};
10699 RawLightInfo info4 = {.id = 4,
10700 .name = "player4",
10701 .maxBrightness = 255,
10702 .flags = InputLightClass::BRIGHTNESS,
10703 .path = ""};
10704 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10705 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10706 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10707 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10708
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010709 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010710 InputDeviceInfo info;
10711 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010712 std::vector<InputDeviceLightInfo> lights = info.getLights();
10713 ASSERT_EQ(1U, lights.size());
10714 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010715 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10716 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010717
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010718 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10719 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10720 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010721}
10722
Michael Wrightd02c5b62014-02-10 15:10:22 -080010723} // namespace android