blob: 5b0185fc8ef51ce3ea5ae460bc4fc451076062ae [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));
Michael Wright17db18e2020-06-26 20:51:44 +01004365 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 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));
Michael Wright17db18e2020-06-26 20:51:44 +01004393 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 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));
Michael Wright17db18e2020-06-26 20:51:44 +01004432 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 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));
Michael Wright17db18e2020-06-26 20:51:44 +01004452 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 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))));
Michael Wright17db18e2020-06-26 20:51:44 +01004569 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 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))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004596 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
4597}
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 Pradhanc14266f2021-05-12 15:56:24 -07005556 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(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 Pradhanc14266f2021-05-12 15:56:24 -07005584 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(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 Pradhanac1c74f2021-08-20 16:09:32 -07005721 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5722 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 Pradhanac1c74f2021-08-20 16:09:32 -07005749 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5750 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5751 processSync(mapper);
5752
5753 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5754 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5755 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5756
5757 processUp(mapper);
5758 processSync(mapper);
5759 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5760}
5761
Michael Wrightd02c5b62014-02-10 15:10:22 -08005762TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005764 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765 prepareButtons();
5766 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005767 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005768
5769 // These calculations are based on the input device calibration documentation.
5770 int32_t rawX = 100;
5771 int32_t rawY = 200;
5772 int32_t rawPressure = 10;
5773 int32_t rawToolMajor = 12;
5774 int32_t rawDistance = 2;
5775 int32_t rawTiltX = 30;
5776 int32_t rawTiltY = 110;
5777
5778 float x = toDisplayX(rawX);
5779 float y = toDisplayY(rawY);
5780 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5781 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5782 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5783 float distance = float(rawDistance);
5784
5785 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5786 float tiltScale = M_PI / 180;
5787 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5788 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5789 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5790 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5791
5792 processDown(mapper, rawX, rawY);
5793 processPressure(mapper, rawPressure);
5794 processToolMajor(mapper, rawToolMajor);
5795 processDistance(mapper, rawDistance);
5796 processTilt(mapper, rawTiltX, rawTiltY);
5797 processSync(mapper);
5798
5799 NotifyMotionArgs args;
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5802 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5803 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5804}
5805
Jason Gerecke489fda82012-09-07 17:19:40 -07005806TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005807 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005808 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005809 prepareLocationCalibration();
5810 prepareButtons();
5811 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005812 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005813
5814 int32_t rawX = 100;
5815 int32_t rawY = 200;
5816
5817 float x = toDisplayX(toCookedX(rawX, rawY));
5818 float y = toDisplayY(toCookedY(rawX, rawY));
5819
5820 processDown(mapper, rawX, rawY);
5821 processSync(mapper);
5822
5823 NotifyMotionArgs args;
5824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5826 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5827}
5828
Michael Wrightd02c5b62014-02-10 15:10:22 -08005829TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005830 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005831 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005832 prepareButtons();
5833 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005834 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835
5836 NotifyMotionArgs motionArgs;
5837 NotifyKeyArgs keyArgs;
5838
5839 processDown(mapper, 100, 200);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5843 ASSERT_EQ(0, motionArgs.buttonState);
5844
5845 // press BTN_LEFT, release BTN_LEFT
5846 processKey(mapper, BTN_LEFT, 1);
5847 processSync(mapper);
5848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5850 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5851
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5854 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5855
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856 processKey(mapper, BTN_LEFT, 0);
5857 processSync(mapper);
5858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005859 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005861
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005864 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
5866 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5867 processKey(mapper, BTN_RIGHT, 1);
5868 processKey(mapper, BTN_MIDDLE, 1);
5869 processSync(mapper);
5870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5871 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5872 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5873 motionArgs.buttonState);
5874
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5876 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5877 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5878
5879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5881 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5882 motionArgs.buttonState);
5883
Michael Wrightd02c5b62014-02-10 15:10:22 -08005884 processKey(mapper, BTN_RIGHT, 0);
5885 processSync(mapper);
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005889
5890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005891 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005892 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
5894 processKey(mapper, BTN_MIDDLE, 0);
5895 processSync(mapper);
5896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005899
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005902 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903
5904 // press BTN_BACK, release BTN_BACK
5905 processKey(mapper, BTN_BACK, 1);
5906 processSync(mapper);
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5908 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5909 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005910
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005913 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5917 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918
5919 processKey(mapper, BTN_BACK, 0);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005922 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005927 ASSERT_EQ(0, motionArgs.buttonState);
5928
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5930 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5931 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5932
5933 // press BTN_SIDE, release BTN_SIDE
5934 processKey(mapper, BTN_SIDE, 1);
5935 processSync(mapper);
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5937 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5938 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005939
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005942 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5946 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
5948 processKey(mapper, BTN_SIDE, 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);
5957
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5959 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5960 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5961
5962 // press BTN_FORWARD, release BTN_FORWARD
5963 processKey(mapper, BTN_FORWARD, 1);
5964 processSync(mapper);
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5966 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5967 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005970 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005971 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5972
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5974 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5975 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976
5977 processKey(mapper, BTN_FORWARD, 0);
5978 processSync(mapper);
5979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005985 ASSERT_EQ(0, motionArgs.buttonState);
5986
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5988 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5989 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5990
5991 // press BTN_EXTRA, release BTN_EXTRA
5992 processKey(mapper, BTN_EXTRA, 1);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5995 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5996 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005997
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006000 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6001
6002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6003 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6004 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005
6006 processKey(mapper, BTN_EXTRA, 0);
6007 processSync(mapper);
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006010 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006011
6012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014 ASSERT_EQ(0, motionArgs.buttonState);
6015
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6017 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6018 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6019
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6021
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 // press BTN_STYLUS, release BTN_STYLUS
6023 processKey(mapper, BTN_STYLUS, 1);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6026 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006027 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6031 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006032
6033 processKey(mapper, BTN_STYLUS, 0);
6034 processSync(mapper);
6035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006037 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006038
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006041 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006042
6043 // press BTN_STYLUS2, release BTN_STYLUS2
6044 processKey(mapper, BTN_STYLUS2, 1);
6045 processSync(mapper);
6046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006048 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6049
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6051 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6052 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053
6054 processKey(mapper, BTN_STYLUS2, 0);
6055 processSync(mapper);
6056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006059
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006061 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006062 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006063
6064 // release touch
6065 processUp(mapper);
6066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6068 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6069 ASSERT_EQ(0, motionArgs.buttonState);
6070}
6071
6072TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006074 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 prepareButtons();
6076 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006077 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
6079 NotifyMotionArgs motionArgs;
6080
6081 // default tool type is finger
6082 processDown(mapper, 100, 200);
6083 processSync(mapper);
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6085 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6086 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6087
6088 // eraser
6089 processKey(mapper, BTN_TOOL_RUBBER, 1);
6090 processSync(mapper);
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6094
6095 // stylus
6096 processKey(mapper, BTN_TOOL_RUBBER, 0);
6097 processKey(mapper, BTN_TOOL_PEN, 1);
6098 processSync(mapper);
6099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6102
6103 // brush
6104 processKey(mapper, BTN_TOOL_PEN, 0);
6105 processKey(mapper, BTN_TOOL_BRUSH, 1);
6106 processSync(mapper);
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6110
6111 // pencil
6112 processKey(mapper, BTN_TOOL_BRUSH, 0);
6113 processKey(mapper, BTN_TOOL_PENCIL, 1);
6114 processSync(mapper);
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6117 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6118
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006119 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 processKey(mapper, BTN_TOOL_PENCIL, 0);
6121 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6122 processSync(mapper);
6123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6124 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6125 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6126
6127 // mouse
6128 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6129 processKey(mapper, BTN_TOOL_MOUSE, 1);
6130 processSync(mapper);
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6132 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6134
6135 // lens
6136 processKey(mapper, BTN_TOOL_MOUSE, 0);
6137 processKey(mapper, BTN_TOOL_LENS, 1);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6142
6143 // double-tap
6144 processKey(mapper, BTN_TOOL_LENS, 0);
6145 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6146 processSync(mapper);
6147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6150
6151 // triple-tap
6152 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6153 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6154 processSync(mapper);
6155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6157 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6158
6159 // quad-tap
6160 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6161 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6162 processSync(mapper);
6163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6166
6167 // finger
6168 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6169 processKey(mapper, BTN_TOOL_FINGER, 1);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6174
6175 // stylus trumps finger
6176 processKey(mapper, BTN_TOOL_PEN, 1);
6177 processSync(mapper);
6178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6180 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6181
6182 // eraser trumps stylus
6183 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
6188
6189 // mouse trumps eraser
6190 processKey(mapper, BTN_TOOL_MOUSE, 1);
6191 processSync(mapper);
6192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6195
6196 // back to default tool type
6197 processKey(mapper, BTN_TOOL_MOUSE, 0);
6198 processKey(mapper, BTN_TOOL_RUBBER, 0);
6199 processKey(mapper, BTN_TOOL_PEN, 0);
6200 processKey(mapper, BTN_TOOL_FINGER, 0);
6201 processSync(mapper);
6202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6205}
6206
6207TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006208 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006209 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006210 prepareButtons();
6211 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006212 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006213 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006214
6215 NotifyMotionArgs motionArgs;
6216
6217 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6218 processKey(mapper, BTN_TOOL_FINGER, 1);
6219 processMove(mapper, 100, 200);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6224 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6225
6226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6227 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6229 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6230
6231 // move a little
6232 processMove(mapper, 150, 250);
6233 processSync(mapper);
6234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6237 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6238
6239 // down when BTN_TOUCH is pressed, pressure defaults to 1
6240 processKey(mapper, BTN_TOUCH, 1);
6241 processSync(mapper);
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6244 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6245 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6246
6247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6248 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6250 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6251
6252 // up when BTN_TOUCH is released, hover restored
6253 processKey(mapper, BTN_TOUCH, 0);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6258 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6259
6260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6263 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6264
6265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6267 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6268 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6269
6270 // exit hover when pointer goes away
6271 processKey(mapper, BTN_TOOL_FINGER, 0);
6272 processSync(mapper);
6273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6274 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6275 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6276 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6277}
6278
6279TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006280 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006281 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282 prepareButtons();
6283 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006284 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285
6286 NotifyMotionArgs motionArgs;
6287
6288 // initially hovering because pressure is 0
6289 processDown(mapper, 100, 200);
6290 processPressure(mapper, 0);
6291 processSync(mapper);
6292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6293 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6295 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6296
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6300 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6301
6302 // move a little
6303 processMove(mapper, 150, 250);
6304 processSync(mapper);
6305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6306 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6308 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6309
6310 // down when pressure is non-zero
6311 processPressure(mapper, RAW_PRESSURE_MAX);
6312 processSync(mapper);
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6316 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6317
6318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6319 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6321 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6322
6323 // up when pressure becomes 0, hover restored
6324 processPressure(mapper, 0);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6329 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6330
6331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6334 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6335
6336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6337 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6340
6341 // exit hover when pointer goes away
6342 processUp(mapper);
6343 processSync(mapper);
6344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6346 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6347 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6348}
6349
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006350TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6351 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006352 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006353 prepareButtons();
6354 prepareAxes(POSITION | PRESSURE);
6355 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6356
6357 // Touch down.
6358 processDown(mapper, 100, 200);
6359 processPressure(mapper, 1);
6360 processSync(mapper);
6361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6362 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6363
6364 // Reset the mapper. This should cancel the ongoing gesture.
6365 resetMapper(mapper, ARBITRARY_TIME);
6366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6367 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6370}
6371
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006372TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6373 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006374 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006375 prepareButtons();
6376 prepareAxes(POSITION | PRESSURE);
6377 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6378
6379 // Set the initial state for the touch pointer.
6380 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6381 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6382 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6383 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6384
6385 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006386 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6387 // does not generate any events.
6388 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006389
6390 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6391 // the recreated touch state to generate a down event.
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6395
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6397}
6398
lilinnan687e58f2022-07-19 16:00:50 +08006399TEST_F(SingleTouchInputMapperTest,
6400 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6401 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006402 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006403 prepareButtons();
6404 prepareAxes(POSITION);
6405 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6406 NotifyMotionArgs motionArgs;
6407
6408 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006409 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006410 processSync(mapper);
6411
6412 // We should receive a down event
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6414 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6415
6416 // Change display id
6417 clearViewports();
6418 prepareSecondaryDisplay(ViewportType::INTERNAL);
6419
6420 // We should receive a cancel event
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6422 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6423 // Then receive reset called
6424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6425}
6426
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006427TEST_F(SingleTouchInputMapperTest,
6428 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6429 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006430 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006431 prepareButtons();
6432 prepareAxes(POSITION);
6433 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6435 NotifyMotionArgs motionArgs;
6436
6437 // Start a new gesture.
6438 processDown(mapper, 100, 200);
6439 processSync(mapper);
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6442
6443 // Make the viewport inactive. This will put the device in disabled mode.
6444 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6445 viewport->isActive = false;
6446 mFakePolicy->updateViewport(*viewport);
6447 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6448
6449 // We should receive a cancel event for the ongoing gesture.
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6451 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6452 // Then we should be notified that the device was reset.
6453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6454
6455 // No events are generated while the viewport is inactive.
6456 processMove(mapper, 101, 201);
6457 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006458 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006459 processSync(mapper);
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6461
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006462 // Start a new gesture while the viewport is still inactive.
6463 processDown(mapper, 300, 400);
6464 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6465 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6466 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6467 processSync(mapper);
6468
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006469 // Make the viewport active again. The device should resume processing events.
6470 viewport->isActive = true;
6471 mFakePolicy->updateViewport(*viewport);
6472 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6473
6474 // The device is reset because it changes back to direct mode, without generating any events.
6475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6477
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006478 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006479 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6481 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006482
6483 // No more events.
6484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6486}
6487
Prabir Pradhan211ba622022-10-31 21:09:21 +00006488TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6489 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006490 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006491 prepareButtons();
6492 prepareAxes(POSITION);
6493 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6495
6496 // Press a stylus button.
6497 processKey(mapper, BTN_STYLUS, 1);
6498 processSync(mapper);
6499
6500 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6501 processDown(mapper, 100, 200);
6502 processSync(mapper);
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6505 WithCoords(toDisplayX(100), toDisplayY(200)),
6506 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6508 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6509 WithCoords(toDisplayX(100), toDisplayY(200)),
6510 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6511
6512 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6513 // the button has not actually been released, since there will be no pointers through which the
6514 // button state can be reported. The event is generated at the location of the pointer before
6515 // it went up.
6516 processUp(mapper);
6517 processSync(mapper);
6518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6519 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6520 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6522 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6523 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6524}
6525
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006526TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6527 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6528 prepareDisplay(ui::ROTATION_0);
6529 prepareButtons();
6530 prepareAxes(POSITION);
6531 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6533
6534 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6535}
6536
Prabir Pradhan5632d622021-09-06 07:57:20 -07006537// --- TouchDisplayProjectionTest ---
6538
6539class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6540public:
6541 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6542 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6543 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006544 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006545 uint32_t inverseRotationFlags;
6546 auto width = DISPLAY_WIDTH;
6547 auto height = DISPLAY_HEIGHT;
6548 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006549 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006550 inverseRotationFlags = ui::Transform::ROT_270;
6551 std::swap(width, height);
6552 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006553 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006554 inverseRotationFlags = ui::Transform::ROT_180;
6555 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006556 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006557 inverseRotationFlags = ui::Transform::ROT_90;
6558 std::swap(width, height);
6559 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006560 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006561 inverseRotationFlags = ui::Transform::ROT_0;
6562 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006563 }
6564
6565 const ui::Transform rotation(inverseRotationFlags, width, height);
6566 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6567
6568 std::optional<DisplayViewport> internalViewport =
6569 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6570 DisplayViewport& v = *internalViewport;
6571 v.displayId = DISPLAY_ID;
6572 v.orientation = orientation;
6573
6574 v.logicalLeft = 0;
6575 v.logicalTop = 0;
6576 v.logicalRight = 100;
6577 v.logicalBottom = 100;
6578
6579 v.physicalLeft = rotatedPhysicalDisplay.left;
6580 v.physicalTop = rotatedPhysicalDisplay.top;
6581 v.physicalRight = rotatedPhysicalDisplay.right;
6582 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6583
6584 v.deviceWidth = width;
6585 v.deviceHeight = height;
6586
6587 v.isActive = true;
6588 v.uniqueId = UNIQUE_ID;
6589 v.type = ViewportType::INTERNAL;
6590 mFakePolicy->updateViewport(v);
6591 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6592 }
6593
6594 void assertReceivedMove(const Point& point) {
6595 NotifyMotionArgs motionArgs;
6596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6598 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6600 1, 0, 0, 0, 0, 0, 0, 0));
6601 }
6602};
6603
6604TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6605 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006606 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006607
6608 prepareButtons();
6609 prepareAxes(POSITION);
6610 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6611
6612 NotifyMotionArgs motionArgs;
6613
6614 // Configure the DisplayViewport such that the logical display maps to a subsection of
6615 // the display panel called the physical display. Here, the physical display is bounded by the
6616 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6617 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6618 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6619 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6620
Michael Wrighta9cf4192022-12-01 23:46:39 +00006621 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006622 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6623
6624 // Touches outside the physical display should be ignored, and should not generate any
6625 // events. Ensure touches at the following points that lie outside of the physical display
6626 // area do not generate any events.
6627 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6628 processDown(mapper, toRawX(point.x), toRawY(point.y));
6629 processSync(mapper);
6630 processUp(mapper);
6631 processSync(mapper);
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6633 << "Unexpected event generated for touch outside physical display at point: "
6634 << point.x << ", " << point.y;
6635 }
6636 }
6637}
6638
6639TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6640 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006641 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006642
6643 prepareButtons();
6644 prepareAxes(POSITION);
6645 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6646
6647 NotifyMotionArgs motionArgs;
6648
6649 // Configure the DisplayViewport such that the logical display maps to a subsection of
6650 // the display panel called the physical display. Here, the physical display is bounded by the
6651 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6652 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6653
Michael Wrighta9cf4192022-12-01 23:46:39 +00006654 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006655 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6656
6657 // Touches that start outside the physical display should be ignored until it enters the
6658 // physical display bounds, at which point it should generate a down event. Start a touch at
6659 // the point (5, 100), which is outside the physical display bounds.
6660 static const Point kOutsidePoint{5, 100};
6661 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6662 processSync(mapper);
6663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6664
6665 // Move the touch into the physical display area. This should generate a pointer down.
6666 processMove(mapper, toRawX(11), toRawY(21));
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6670 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6671 ASSERT_NO_FATAL_FAILURE(
6672 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6673
6674 // Move the touch inside the physical display area. This should generate a pointer move.
6675 processMove(mapper, toRawX(69), toRawY(159));
6676 processSync(mapper);
6677 assertReceivedMove({69, 159});
6678
6679 // Move outside the physical display area. Since the pointer is already down, this should
6680 // now continue generating events.
6681 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6682 processSync(mapper);
6683 assertReceivedMove(kOutsidePoint);
6684
6685 // Release. This should generate a pointer up.
6686 processUp(mapper);
6687 processSync(mapper);
6688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6689 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6691 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6692
6693 // Ensure no more events were generated.
6694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6696 }
6697}
6698
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006699// --- ExternalStylusFusionTest ---
6700
6701class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6702public:
6703 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6704 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006705 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006706 prepareButtons();
6707 prepareAxes(POSITION);
6708 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6709
6710 mStylusState.when = ARBITRARY_TIME;
6711 mStylusState.pressure = 0.f;
6712 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6713 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6714 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6715 processExternalStylusState(mapper);
6716 return mapper;
6717 }
6718
6719 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6720 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6721 for (const NotifyArgs& args : generatedArgs) {
6722 mFakeListener->notify(args);
6723 }
6724 // Loop the reader to flush the input listener queue.
6725 mReader->loopOnce();
6726 return generatedArgs;
6727 }
6728
6729protected:
6730 StylusState mStylusState{};
6731 static constexpr uint32_t EXPECTED_SOURCE =
6732 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6733
6734 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6735 auto toolTypeSource =
6736 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6737
6738 // The first pointer is withheld.
6739 processDown(mapper, 100, 200);
6740 processSync(mapper);
6741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6742 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6743 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6744
6745 // The external stylus reports pressure. The withheld finger pointer is released as a
6746 // stylus.
6747 mStylusState.pressure = 1.f;
6748 processExternalStylusState(mapper);
6749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6750 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6751 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6752
6753 // Subsequent pointer events are not withheld.
6754 processMove(mapper, 101, 201);
6755 processSync(mapper);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6757 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6758
6759 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6761 }
6762
6763 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6764 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6765
6766 // Releasing the touch pointer ends the gesture.
6767 processUp(mapper);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6770 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6771 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6772
6773 mStylusState.pressure = 0.f;
6774 processExternalStylusState(mapper);
6775 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6777 }
6778
6779 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6780 auto toolTypeSource =
6781 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6782
6783 // The first pointer is withheld when an external stylus is connected,
6784 // and a timeout is requested.
6785 processDown(mapper, 100, 200);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6788 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6789 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6790
6791 // If the timeout expires early, it is requested again.
6792 handleTimeout(mapper, ARBITRARY_TIME + 1);
6793 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6794 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6795
6796 // When the timeout expires, the withheld touch is released as a finger pointer.
6797 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6799 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6800
6801 // Subsequent pointer events are not withheld.
6802 processMove(mapper, 101, 201);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6805 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6806 processUp(mapper);
6807 processSync(mapper);
6808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6809 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6810
6811 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6813 }
6814
6815private:
6816 InputDeviceInfo mExternalStylusDeviceInfo{};
6817};
6818
6819TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6820 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6821 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6822}
6823
6824TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6825 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6826 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6827}
6828
6829TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6830 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6831 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6832}
6833
6834// Test a successful stylus fusion gesture where the pressure is reported by the external
6835// before the touch is reported by the touchscreen.
6836TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6837 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6838 auto toolTypeSource =
6839 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6840
6841 // The external stylus reports pressure first. It is ignored for now.
6842 mStylusState.pressure = 1.f;
6843 processExternalStylusState(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6845 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6846
6847 // When the touch goes down afterwards, it is reported as a stylus pointer.
6848 processDown(mapper, 100, 200);
6849 processSync(mapper);
6850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6851 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6852 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6853
6854 processMove(mapper, 101, 201);
6855 processSync(mapper);
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6857 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6858 processUp(mapper);
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6861 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6862
6863 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6865}
6866
6867TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6868 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6869
6870 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6871 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6872
6873 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6874 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6875 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6876 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6877}
6878
6879TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6880 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6881 auto toolTypeSource =
6882 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6883
6884 mStylusState.pressure = 0.8f;
6885 processExternalStylusState(mapper);
6886 processDown(mapper, 100, 200);
6887 processSync(mapper);
6888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6889 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6890 WithPressure(0.8f))));
6891 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6892
6893 // The external stylus reports a pressure change. We wait for some time for a touch event.
6894 mStylusState.pressure = 0.6f;
6895 processExternalStylusState(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6897 ASSERT_NO_FATAL_FAILURE(
6898 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6899
6900 // If a touch is reported within the timeout, it reports the updated pressure.
6901 processMove(mapper, 101, 201);
6902 processSync(mapper);
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6904 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6905 WithPressure(0.6f))));
6906 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6907
6908 // There is another pressure change.
6909 mStylusState.pressure = 0.5f;
6910 processExternalStylusState(mapper);
6911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6912 ASSERT_NO_FATAL_FAILURE(
6913 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6914
6915 // If a touch is not reported within the timeout, a move event is generated to report
6916 // the new pressure.
6917 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6919 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6920 WithPressure(0.5f))));
6921
6922 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6923 // repeated indefinitely.
6924 mStylusState.pressure = 0.0f;
6925 processExternalStylusState(mapper);
6926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6927 ASSERT_NO_FATAL_FAILURE(
6928 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6929 processMove(mapper, 102, 202);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6932 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6933 WithPressure(0.5f))));
6934 processMove(mapper, 103, 203);
6935 processSync(mapper);
6936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6937 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6938 WithPressure(0.5f))));
6939
6940 processUp(mapper);
6941 processSync(mapper);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6943 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6944 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6945
6946 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6948}
6949
6950TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6951 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6952 auto source = WithSource(EXPECTED_SOURCE);
6953
6954 mStylusState.pressure = 1.f;
6955 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
6956 processExternalStylusState(mapper);
6957 processDown(mapper, 100, 200);
6958 processSync(mapper);
6959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6960 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6961 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
6962 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6963
6964 // The external stylus reports a tool change. We wait for some time for a touch event.
6965 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6966 processExternalStylusState(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6968 ASSERT_NO_FATAL_FAILURE(
6969 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6970
6971 // If a touch is reported within the timeout, it reports the updated pressure.
6972 processMove(mapper, 101, 201);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6975 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6976 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6977 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6978
6979 // There is another tool type change.
6980 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6981 processExternalStylusState(mapper);
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6983 ASSERT_NO_FATAL_FAILURE(
6984 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6985
6986 // If a touch is not reported within the timeout, a move event is generated to report
6987 // the new tool type.
6988 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6990 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6991 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6992
6993 processUp(mapper);
6994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6996 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
6997 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6998
6999 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7001}
7002
7003TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7004 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7005 auto toolTypeSource =
7006 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7007
7008 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7009
7010 // The external stylus reports a button change. We wait for some time for a touch event.
7011 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7012 processExternalStylusState(mapper);
7013 ASSERT_NO_FATAL_FAILURE(
7014 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7015
7016 // If a touch is reported within the timeout, it reports the updated button state.
7017 processMove(mapper, 101, 201);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7020 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7021 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7023 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7024 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7025 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7026
7027 // The button is now released.
7028 mStylusState.buttons = 0;
7029 processExternalStylusState(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7031 ASSERT_NO_FATAL_FAILURE(
7032 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7033
7034 // If a touch is not reported within the timeout, a move event is generated to report
7035 // the new button state.
7036 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7038 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7039 WithButtonState(0))));
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007041 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7042 WithButtonState(0))));
7043
7044 processUp(mapper);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007047 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7048
7049 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7051}
7052
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053// --- MultiTouchInputMapperTest ---
7054
7055class MultiTouchInputMapperTest : public TouchInputMapperTest {
7056protected:
7057 void prepareAxes(int axes);
7058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7060 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7061 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7062 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7063 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7064 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7065 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7066 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7067 void processId(MultiTouchInputMapper& mapper, int32_t id);
7068 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7069 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7070 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007071 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007072 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007073 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7074 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075};
7076
7077void MultiTouchInputMapperTest::prepareAxes(int axes) {
7078 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 }
7082 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7084 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7087 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 }
7089 }
7090 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7092 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007095 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 }
7097 }
7098 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7100 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101 }
7102 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7104 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 }
7106 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7108 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109 }
7110 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7112 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 }
7114 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7116 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 }
7118 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121}
7122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007123void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7124 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7130 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7135 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137}
7138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141}
7142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007147void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7148 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154}
7155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007156void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158}
7159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007160void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162}
7163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007164void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166}
7167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007168void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170}
7171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007172void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7173 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007177void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7178 int32_t value) {
7179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185}
7186
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007187void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7188 nsecs_t readTime) {
7189 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190}
7191
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007194 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 prepareAxes(POSITION);
7196 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007197 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198
arthurhungdcef2dc2020-08-11 14:47:50 +08007199 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200
7201 NotifyMotionArgs motionArgs;
7202
7203 // Two fingers down at once.
7204 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7205 processPosition(mapper, x1, y1);
7206 processMTSync(mapper);
7207 processPosition(mapper, x2, y2);
7208 processMTSync(mapper);
7209 processSync(mapper);
7210
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7217 ASSERT_EQ(0, motionArgs.flags);
7218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7219 ASSERT_EQ(0, motionArgs.buttonState);
7220 ASSERT_EQ(0, motionArgs.edgeFlags);
7221 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7225 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007235 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236 ASSERT_EQ(0, motionArgs.flags);
7237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7238 ASSERT_EQ(0, motionArgs.buttonState);
7239 ASSERT_EQ(0, motionArgs.edgeFlags);
7240 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7242 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7243 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7244 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7246 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7248 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7249 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7250 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7252
7253 // Move.
7254 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7255 processPosition(mapper, x1, y1);
7256 processMTSync(mapper);
7257 processPosition(mapper, x2, y2);
7258 processMTSync(mapper);
7259 processSync(mapper);
7260
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7263 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7265 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(0, motionArgs.flags);
7268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7269 ASSERT_EQ(0, motionArgs.buttonState);
7270 ASSERT_EQ(0, motionArgs.edgeFlags);
7271 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7274 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7275 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7277 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7279 toDisplayX(x2), toDisplayY(y2), 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 // First finger up.
7285 x2 += 15; y2 -= 20;
7286 processPosition(mapper, x2, y2);
7287 processMTSync(mapper);
7288 processSync(mapper);
7289
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7292 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7293 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7294 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007295 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296 ASSERT_EQ(0, motionArgs.flags);
7297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7298 ASSERT_EQ(0, motionArgs.buttonState);
7299 ASSERT_EQ(0, motionArgs.edgeFlags);
7300 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7302 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7303 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7304 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7306 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7309 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7310 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7312
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7319 ASSERT_EQ(0, motionArgs.flags);
7320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7321 ASSERT_EQ(0, motionArgs.buttonState);
7322 ASSERT_EQ(0, motionArgs.edgeFlags);
7323 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7324 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7325 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7327 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7328 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7329 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7331
7332 // Move.
7333 x2 += 20; y2 -= 25;
7334 processPosition(mapper, x2, y2);
7335 processMTSync(mapper);
7336 processSync(mapper);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7340 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7344 ASSERT_EQ(0, motionArgs.flags);
7345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7346 ASSERT_EQ(0, motionArgs.buttonState);
7347 ASSERT_EQ(0, motionArgs.edgeFlags);
7348 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7349 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7350 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7353 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7354 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7356
7357 // New finger down.
7358 int32_t x3 = 700, y3 = 300;
7359 processPosition(mapper, x2, y2);
7360 processMTSync(mapper);
7361 processPosition(mapper, x3, y3);
7362 processMTSync(mapper);
7363 processSync(mapper);
7364
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7367 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7368 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7369 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007370 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 ASSERT_EQ(0, motionArgs.flags);
7372 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7373 ASSERT_EQ(0, motionArgs.buttonState);
7374 ASSERT_EQ(0, motionArgs.edgeFlags);
7375 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7378 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7379 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7381 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7383 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7385 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7387
7388 // Second finger up.
7389 x3 += 30; y3 -= 20;
7390 processPosition(mapper, x3, y3);
7391 processMTSync(mapper);
7392 processSync(mapper);
7393
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7396 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7397 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7398 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007399 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400 ASSERT_EQ(0, motionArgs.flags);
7401 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7402 ASSERT_EQ(0, motionArgs.buttonState);
7403 ASSERT_EQ(0, motionArgs.edgeFlags);
7404 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7407 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7408 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7416
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7423 ASSERT_EQ(0, motionArgs.flags);
7424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7425 ASSERT_EQ(0, motionArgs.buttonState);
7426 ASSERT_EQ(0, motionArgs.edgeFlags);
7427 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7435
7436 // Last finger up.
7437 processMTSync(mapper);
7438 processSync(mapper);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7446 ASSERT_EQ(0, motionArgs.flags);
7447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7448 ASSERT_EQ(0, motionArgs.buttonState);
7449 ASSERT_EQ(0, motionArgs.edgeFlags);
7450 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7452 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7458
7459 // Should not have sent any more keys or motions.
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7462}
7463
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007464TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007466 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007467
7468 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7469 /*fuzz*/ 0, /*resolution*/ 10);
7470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7471 /*fuzz*/ 0, /*resolution*/ 11);
7472 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7473 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7475 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7477 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7479 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7480
7481 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7482
7483 // X and Y axes
7484 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7485 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7486 // Touch major and minor
7487 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7488 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7489 // Tool major and minor
7490 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7491 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7492}
7493
7494TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7495 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007496 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007497
7498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7499 /*fuzz*/ 0, /*resolution*/ 10);
7500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7501 /*fuzz*/ 0, /*resolution*/ 11);
7502
7503 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7504
7505 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7506
7507 // Touch major and minor
7508 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7509 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7510 // Tool major and minor
7511 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7512 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7513}
7514
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 prepareAxes(POSITION | ID);
7519 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007520 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521
arthurhungdcef2dc2020-08-11 14:47:50 +08007522 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523
7524 NotifyMotionArgs motionArgs;
7525
7526 // Two fingers down at once.
7527 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7528 processPosition(mapper, x1, y1);
7529 processId(mapper, 1);
7530 processMTSync(mapper);
7531 processPosition(mapper, x2, y2);
7532 processId(mapper, 2);
7533 processMTSync(mapper);
7534 processSync(mapper);
7535
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7538 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7542 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7543
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007545 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7549 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 // Move.
7557 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7558 processPosition(mapper, x1, y1);
7559 processId(mapper, 1);
7560 processMTSync(mapper);
7561 processPosition(mapper, x2, y2);
7562 processId(mapper, 2);
7563 processMTSync(mapper);
7564 processSync(mapper);
7565
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7568 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7569 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7571 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7572 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 // First finger up.
7579 x2 += 15; y2 -= 20;
7580 processPosition(mapper, x2, y2);
7581 processId(mapper, 2);
7582 processMTSync(mapper);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7589 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7591 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7599 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7600 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // Move.
7606 x2 += 20; y2 -= 25;
7607 processPosition(mapper, x2, y2);
7608 processId(mapper, 2);
7609 processMTSync(mapper);
7610 processSync(mapper);
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7614 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7615 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7619
7620 // New finger down.
7621 int32_t x3 = 700, y3 = 300;
7622 processPosition(mapper, x2, y2);
7623 processId(mapper, 2);
7624 processMTSync(mapper);
7625 processPosition(mapper, x3, y3);
7626 processId(mapper, 3);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007631 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7634 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7638 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 // Second finger up.
7643 x3 += 30; y3 -= 20;
7644 processPosition(mapper, x3, y3);
7645 processId(mapper, 3);
7646 processMTSync(mapper);
7647 processSync(mapper);
7648
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007650 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007651 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7653 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7654 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7655 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7657 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7659 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 // Last finger up.
7670 processMTSync(mapper);
7671 processSync(mapper);
7672
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7675 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7677 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7679 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // Should not have sent any more keys or motions.
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7684}
7685
7686TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007688 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 prepareAxes(POSITION | ID | SLOT);
7690 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007691 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692
arthurhungdcef2dc2020-08-11 14:47:50 +08007693 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694
7695 NotifyMotionArgs motionArgs;
7696
7697 // Two fingers down at once.
7698 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7699 processPosition(mapper, x1, y1);
7700 processId(mapper, 1);
7701 processSlot(mapper, 1);
7702 processPosition(mapper, x2, y2);
7703 processId(mapper, 2);
7704 processSync(mapper);
7705
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7708 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7712 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7713
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007715 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7718 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7719 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7724 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7725
7726 // Move.
7727 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7728 processSlot(mapper, 0);
7729 processPosition(mapper, x1, y1);
7730 processSlot(mapper, 1);
7731 processPosition(mapper, x2, y2);
7732 processSync(mapper);
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7736 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7738 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7739 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7742 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7745
7746 // First finger up.
7747 x2 += 15; y2 -= 20;
7748 processSlot(mapper, 0);
7749 processId(mapper, -1);
7750 processSlot(mapper, 1);
7751 processPosition(mapper, x2, y2);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007755 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7758 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7759 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7765
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7768 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7769 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7770 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7772 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7773
7774 // Move.
7775 x2 += 20; y2 -= 25;
7776 processPosition(mapper, x2, y2);
7777 processSync(mapper);
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7781 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7782 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7785 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7786
7787 // New finger down.
7788 int32_t x3 = 700, y3 = 300;
7789 processPosition(mapper, x2, y2);
7790 processSlot(mapper, 0);
7791 processId(mapper, 3);
7792 processPosition(mapper, x3, y3);
7793 processSync(mapper);
7794
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007796 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 // Second finger up.
7808 x3 += 30; y3 -= 20;
7809 processSlot(mapper, 1);
7810 processId(mapper, -1);
7811 processSlot(mapper, 0);
7812 processPosition(mapper, x3, y3);
7813 processSync(mapper);
7814
7815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007816 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007817 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7820 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7825 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7829 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7834
7835 // Last finger up.
7836 processId(mapper, -1);
7837 processSync(mapper);
7838
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7841 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7842 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7846
7847 // Should not have sent any more keys or motions.
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7850}
7851
7852TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007854 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857
7858 // These calculations are based on the input device calibration documentation.
7859 int32_t rawX = 100;
7860 int32_t rawY = 200;
7861 int32_t rawTouchMajor = 7;
7862 int32_t rawTouchMinor = 6;
7863 int32_t rawToolMajor = 9;
7864 int32_t rawToolMinor = 8;
7865 int32_t rawPressure = 11;
7866 int32_t rawDistance = 0;
7867 int32_t rawOrientation = 3;
7868 int32_t id = 5;
7869
7870 float x = toDisplayX(rawX);
7871 float y = toDisplayY(rawY);
7872 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7873 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7874 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7875 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7876 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7877 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7878 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7879 float distance = float(rawDistance);
7880
7881 processPosition(mapper, rawX, rawY);
7882 processTouchMajor(mapper, rawTouchMajor);
7883 processTouchMinor(mapper, rawTouchMinor);
7884 processToolMajor(mapper, rawToolMajor);
7885 processToolMinor(mapper, rawToolMinor);
7886 processPressure(mapper, rawPressure);
7887 processOrientation(mapper, rawOrientation);
7888 processDistance(mapper, rawDistance);
7889 processId(mapper, id);
7890 processMTSync(mapper);
7891 processSync(mapper);
7892
7893 NotifyMotionArgs args;
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7895 ASSERT_EQ(0, args.pointerProperties[0].id);
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7897 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7898 orientation, distance));
7899}
7900
7901TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007903 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7905 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007906 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907
7908 // These calculations are based on the input device calibration documentation.
7909 int32_t rawX = 100;
7910 int32_t rawY = 200;
7911 int32_t rawTouchMajor = 140;
7912 int32_t rawTouchMinor = 120;
7913 int32_t rawToolMajor = 180;
7914 int32_t rawToolMinor = 160;
7915
7916 float x = toDisplayX(rawX);
7917 float y = toDisplayY(rawY);
7918 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7919 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7920 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7921 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7922 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7923
7924 processPosition(mapper, rawX, rawY);
7925 processTouchMajor(mapper, rawTouchMajor);
7926 processTouchMinor(mapper, rawTouchMinor);
7927 processToolMajor(mapper, rawToolMajor);
7928 processToolMinor(mapper, rawToolMinor);
7929 processMTSync(mapper);
7930 processSync(mapper);
7931
7932 NotifyMotionArgs args;
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7935 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7936}
7937
7938TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007940 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 prepareAxes(POSITION | TOUCH | TOOL);
7942 addConfigurationProperty("touch.size.calibration", "diameter");
7943 addConfigurationProperty("touch.size.scale", "10");
7944 addConfigurationProperty("touch.size.bias", "160");
7945 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007946 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947
7948 // These calculations are based on the input device calibration documentation.
7949 // Note: We only provide a single common touch/tool value because the device is assumed
7950 // not to emit separate values for each pointer (isSummed = 1).
7951 int32_t rawX = 100;
7952 int32_t rawY = 200;
7953 int32_t rawX2 = 150;
7954 int32_t rawY2 = 250;
7955 int32_t rawTouchMajor = 5;
7956 int32_t rawToolMajor = 8;
7957
7958 float x = toDisplayX(rawX);
7959 float y = toDisplayY(rawY);
7960 float x2 = toDisplayX(rawX2);
7961 float y2 = toDisplayY(rawY2);
7962 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7963 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7964 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7965
7966 processPosition(mapper, rawX, rawY);
7967 processTouchMajor(mapper, rawTouchMajor);
7968 processToolMajor(mapper, rawToolMajor);
7969 processMTSync(mapper);
7970 processPosition(mapper, rawX2, rawY2);
7971 processTouchMajor(mapper, rawTouchMajor);
7972 processToolMajor(mapper, rawToolMajor);
7973 processMTSync(mapper);
7974 processSync(mapper);
7975
7976 NotifyMotionArgs args;
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007981 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007982 ASSERT_EQ(size_t(2), args.pointerCount);
7983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7984 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7986 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7987}
7988
7989TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007991 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992 prepareAxes(POSITION | TOUCH | TOOL);
7993 addConfigurationProperty("touch.size.calibration", "area");
7994 addConfigurationProperty("touch.size.scale", "43");
7995 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007996 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997
7998 // These calculations are based on the input device calibration documentation.
7999 int32_t rawX = 100;
8000 int32_t rawY = 200;
8001 int32_t rawTouchMajor = 5;
8002 int32_t rawToolMajor = 8;
8003
8004 float x = toDisplayX(rawX);
8005 float y = toDisplayY(rawY);
8006 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8007 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8008 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8009
8010 processPosition(mapper, rawX, rawY);
8011 processTouchMajor(mapper, rawTouchMajor);
8012 processToolMajor(mapper, rawToolMajor);
8013 processMTSync(mapper);
8014 processSync(mapper);
8015
8016 NotifyMotionArgs args;
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8019 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8020}
8021
8022TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008024 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 prepareAxes(POSITION | PRESSURE);
8026 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8027 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029
Michael Wrightaa449c92017-12-13 21:21:43 +00008030 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008031 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008032 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8033 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8034 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8035
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 // These calculations are based on the input device calibration documentation.
8037 int32_t rawX = 100;
8038 int32_t rawY = 200;
8039 int32_t rawPressure = 60;
8040
8041 float x = toDisplayX(rawX);
8042 float y = toDisplayY(rawY);
8043 float pressure = float(rawPressure) * 0.01f;
8044
8045 processPosition(mapper, rawX, rawY);
8046 processPressure(mapper, rawPressure);
8047 processMTSync(mapper);
8048 processSync(mapper);
8049
8050 NotifyMotionArgs args;
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8053 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008058 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061
8062 NotifyMotionArgs motionArgs;
8063 NotifyKeyArgs keyArgs;
8064
8065 processId(mapper, 1);
8066 processPosition(mapper, 100, 200);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8070 ASSERT_EQ(0, motionArgs.buttonState);
8071
8072 // press BTN_LEFT, release BTN_LEFT
8073 processKey(mapper, BTN_LEFT, 1);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8077 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8078
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8081 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8082
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 processKey(mapper, BTN_LEFT, 0);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8094 processKey(mapper, BTN_RIGHT, 1);
8095 processKey(mapper, BTN_MIDDLE, 1);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8100 motionArgs.buttonState);
8101
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8104 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8108 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8109 motionArgs.buttonState);
8110
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 processKey(mapper, BTN_RIGHT, 0);
8112 processSync(mapper);
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120
8121 processKey(mapper, BTN_MIDDLE, 0);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130
8131 // press BTN_BACK, release BTN_BACK
8132 processKey(mapper, BTN_BACK, 1);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8135 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8136 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8141
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145
8146 processKey(mapper, BTN_BACK, 0);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(0, motionArgs.buttonState);
8155
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8158 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8159
8160 // press BTN_SIDE, release BTN_SIDE
8161 processKey(mapper, BTN_SIDE, 1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8164 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8165 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174
8175 processKey(mapper, BTN_SIDE, 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);
8184
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8186 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8187 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8188
8189 // press BTN_FORWARD, release BTN_FORWARD
8190 processKey(mapper, BTN_FORWARD, 1);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8193 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8202 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203
8204 processKey(mapper, BTN_FORWARD, 0);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008212 ASSERT_EQ(0, motionArgs.buttonState);
8213
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8215 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8216 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8217
8218 // press BTN_EXTRA, release BTN_EXTRA
8219 processKey(mapper, BTN_EXTRA, 1);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8223 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008227 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8228
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232
8233 processKey(mapper, BTN_EXTRA, 0);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241 ASSERT_EQ(0, motionArgs.buttonState);
8242
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8244 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8245 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8246
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8248
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 // press BTN_STYLUS, release BTN_STYLUS
8250 processKey(mapper, BTN_STYLUS, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008254 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
8260 processKey(mapper, BTN_STYLUS, 0);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 // press BTN_STYLUS2, release BTN_STYLUS2
8271 processKey(mapper, BTN_STYLUS2, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8279 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
8281 processKey(mapper, BTN_STYLUS2, 0);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008289 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 // release touch
8292 processId(mapper, -1);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8296 ASSERT_EQ(0, motionArgs.buttonState);
8297}
8298
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008299TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8300 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008301 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008302 prepareAxes(POSITION | ID | SLOT);
8303 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8304
8305 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8306 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8307
8308 // Touch down.
8309 processId(mapper, 1);
8310 processPosition(mapper, 100, 200);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8314
8315 // Press and release button mapped to the primary stylus button.
8316 processKey(mapper, BTN_A, 1);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8320 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8323 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8324
8325 processKey(mapper, BTN_A, 0);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8328 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8331
8332 // Press and release the HID usage mapped to the secondary stylus button.
8333 processHidUsage(mapper, 0xabcd, 1);
8334 processSync(mapper);
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8337 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8339 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8340 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8341
8342 processHidUsage(mapper, 0xabcd, 0);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8345 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8347 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8348
8349 // Release touch.
8350 processId(mapper, -1);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8354}
8355
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008358 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008360 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361
8362 NotifyMotionArgs motionArgs;
8363
8364 // default tool type is finger
8365 processId(mapper, 1);
8366 processPosition(mapper, 100, 200);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8370 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8371
8372 // eraser
8373 processKey(mapper, BTN_TOOL_RUBBER, 1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8377 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8378
8379 // stylus
8380 processKey(mapper, BTN_TOOL_RUBBER, 0);
8381 processKey(mapper, BTN_TOOL_PEN, 1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8385 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8386
8387 // brush
8388 processKey(mapper, BTN_TOOL_PEN, 0);
8389 processKey(mapper, BTN_TOOL_BRUSH, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8393 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8394
8395 // pencil
8396 processKey(mapper, BTN_TOOL_BRUSH, 0);
8397 processKey(mapper, BTN_TOOL_PENCIL, 1);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8401 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8402
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008403 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 processKey(mapper, BTN_TOOL_PENCIL, 0);
8405 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8406 processSync(mapper);
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8409 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8410
8411 // mouse
8412 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8413 processKey(mapper, BTN_TOOL_MOUSE, 1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8418
8419 // lens
8420 processKey(mapper, BTN_TOOL_MOUSE, 0);
8421 processKey(mapper, BTN_TOOL_LENS, 1);
8422 processSync(mapper);
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8425 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8426
8427 // double-tap
8428 processKey(mapper, BTN_TOOL_LENS, 0);
8429 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8433 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8434
8435 // triple-tap
8436 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8437 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8438 processSync(mapper);
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8441 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8442
8443 // quad-tap
8444 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8445 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8449 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8450
8451 // finger
8452 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8453 processKey(mapper, BTN_TOOL_FINGER, 1);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8457 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8458
8459 // stylus trumps finger
8460 processKey(mapper, BTN_TOOL_PEN, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8465
8466 // eraser trumps stylus
8467 processKey(mapper, BTN_TOOL_RUBBER, 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_ERASER, motionArgs.pointerProperties[0].toolType);
8472
8473 // mouse trumps eraser
8474 processKey(mapper, BTN_TOOL_MOUSE, 1);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8478 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8479
8480 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8481 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8486
8487 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8488 processToolType(mapper, MT_TOOL_PEN);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8492 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8493
8494 // back to default tool type
8495 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8496 processKey(mapper, BTN_TOOL_MOUSE, 0);
8497 processKey(mapper, BTN_TOOL_RUBBER, 0);
8498 processKey(mapper, BTN_TOOL_PEN, 0);
8499 processKey(mapper, BTN_TOOL_FINGER, 0);
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
8506TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008508 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008510 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008511 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512
8513 NotifyMotionArgs motionArgs;
8514
8515 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8516 processId(mapper, 1);
8517 processPosition(mapper, 100, 200);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8522 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8523
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8528
8529 // move a little
8530 processPosition(mapper, 150, 250);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // down when BTN_TOUCH is pressed, pressure defaults to 1
8538 processKey(mapper, BTN_TOUCH, 1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8544
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // up when BTN_TOUCH is released, hover restored
8551 processKey(mapper, BTN_TOUCH, 0);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8556 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8557
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8561 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8562
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // exit hover when pointer goes away
8569 processId(mapper, -1);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8575}
8576
8577TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008579 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008581 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 NotifyMotionArgs motionArgs;
8584
8585 // initially hovering because pressure is 0
8586 processId(mapper, 1);
8587 processPosition(mapper, 100, 200);
8588 processPressure(mapper, 0);
8589 processSync(mapper);
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8593 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8598 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8599
8600 // move a little
8601 processPosition(mapper, 150, 250);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8607
8608 // down when pressure becomes non-zero
8609 processPressure(mapper, RAW_PRESSURE_MAX);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8615
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8620
8621 // up when pressure becomes 0, hover restored
8622 processPressure(mapper, 0);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8627 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8628
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8633
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // exit hover when pointer goes away
8640 processId(mapper, -1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8646}
8647
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008648/**
8649 * Set the input device port <--> display port associations, and check that the
8650 * events are routed to the display that matches the display port.
8651 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8652 */
8653TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008654 const std::string usb2 = "USB2";
8655 const uint8_t hdmi1 = 0;
8656 const uint8_t hdmi2 = 1;
8657 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008658 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008659
8660 addConfigurationProperty("touch.deviceType", "touchScreen");
8661 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008662 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008663
8664 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8665 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8666
8667 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8668 // for this input device is specified, and the matching viewport is not present,
8669 // the input device should be disabled (at the mapper level).
8670
8671 // Add viewport for display 2 on hdmi2
8672 prepareSecondaryDisplay(type, hdmi2);
8673 // Send a touch event
8674 processPosition(mapper, 100, 100);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8677
8678 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008679 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008680 // Send a touch event again
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683
8684 NotifyMotionArgs args;
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8686 ASSERT_EQ(DISPLAY_ID, args.displayId);
8687}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008689TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8690 addConfigurationProperty("touch.deviceType", "touchScreen");
8691 prepareAxes(POSITION);
8692 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8693
8694 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8695
Michael Wrighta9cf4192022-12-01 23:46:39 +00008696 prepareDisplay(ui::ROTATION_0);
8697 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008698
8699 // Send a touch event
8700 processPosition(mapper, 100, 100);
8701 processSync(mapper);
8702
8703 NotifyMotionArgs args;
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8705 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8706}
8707
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008708TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008709 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008710 std::shared_ptr<FakePointerController> fakePointerController =
8711 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008712 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008713 fakePointerController->setPosition(100, 200);
8714 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008715 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008716
Garfield Tan888a6a42020-01-09 11:39:16 -08008717 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008718 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008719
Michael Wrighta9cf4192022-12-01 23:46:39 +00008720 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008721 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008722 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008723
Harry Cutts16a24cc2022-10-26 15:22:19 +00008724 // Check source is a touchpad that would obtain the PointerController.
8725 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008726
8727 NotifyMotionArgs motionArgs;
8728 processPosition(mapper, 100, 100);
8729 processSync(mapper);
8730
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8733 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8734}
8735
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008736/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008737 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8738 */
8739TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8740 addConfigurationProperty("touch.deviceType", "touchScreen");
8741 prepareAxes(POSITION);
8742 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8743
Michael Wrighta9cf4192022-12-01 23:46:39 +00008744 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008745 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8746 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8747 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8748 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8749
8750 NotifyMotionArgs args;
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8752 ASSERT_EQ(26, args.readTime);
8753
8754 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8755 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8756 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8757
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8759 ASSERT_EQ(33, args.readTime);
8760}
8761
8762/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008763 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8764 * events should not be delivered to the listener.
8765 */
8766TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8767 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008768 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008769 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8770 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008771 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8772 prepareAxes(POSITION);
8773 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8774
8775 NotifyMotionArgs motionArgs;
8776 processPosition(mapper, 100, 100);
8777 processSync(mapper);
8778
8779 mFakeListener->assertNotifyMotionWasNotCalled();
8780}
8781
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008782/**
8783 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8784 * the touch mapper can process the events and the events can be delivered to the listener.
8785 */
8786TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8787 addConfigurationProperty("touch.deviceType", "touchScreen");
8788 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008789 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8790 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008791 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8792 prepareAxes(POSITION);
8793 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8794
8795 NotifyMotionArgs motionArgs;
8796 processPosition(mapper, 100, 100);
8797 processSync(mapper);
8798
8799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8801}
8802
Garfield Tanc734e4f2021-01-15 20:01:39 -08008803TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8804 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008805 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008806 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8807 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008808 std::optional<DisplayViewport> optionalDisplayViewport =
8809 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8810 ASSERT_TRUE(optionalDisplayViewport.has_value());
8811 DisplayViewport displayViewport = *optionalDisplayViewport;
8812
8813 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8814 prepareAxes(POSITION);
8815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8816
8817 // Finger down
8818 int32_t x = 100, y = 100;
8819 processPosition(mapper, x, y);
8820 processSync(mapper);
8821
8822 NotifyMotionArgs motionArgs;
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8824 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8825
8826 // Deactivate display viewport
8827 displayViewport.isActive = false;
8828 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8829 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8830
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008831 // The ongoing touch should be canceled immediately
8832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8834
8835 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008836 x += 10, y += 10;
8837 processPosition(mapper, x, y);
8838 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008840
8841 // Reactivate display viewport
8842 displayViewport.isActive = true;
8843 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8844 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8845
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008846 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008847 x += 10, y += 10;
8848 processPosition(mapper, x, y);
8849 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8851 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008852}
8853
Arthur Hung7c645402019-01-25 17:45:42 +08008854TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8855 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008856 prepareAxes(POSITION | ID | SLOT);
8857 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008858 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008859
8860 // Create the second touch screen device, and enable multi fingers.
8861 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008862 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008863 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008864 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008865 std::shared_ptr<InputDevice> device2 =
8866 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008867 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008869 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8870 0 /*flat*/, 0 /*fuzz*/);
8871 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8872 0 /*flat*/, 0 /*fuzz*/);
8873 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8874 0 /*flat*/, 0 /*fuzz*/);
8875 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8876 0 /*flat*/, 0 /*fuzz*/);
8877 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8878 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8879 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008880
8881 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008882 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008883 std::list<NotifyArgs> unused =
8884 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8885 0 /*changes*/);
8886 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008887
8888 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008889 std::shared_ptr<FakePointerController> fakePointerController =
8890 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008891 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008892
8893 // Setup policy for associated displays and show touches.
8894 const uint8_t hdmi1 = 0;
8895 const uint8_t hdmi2 = 1;
8896 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8897 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8898 mFakePolicy->setShowTouches(true);
8899
8900 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008901 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008902 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008903
8904 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008905 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8906 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8907 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008908
8909 // Two fingers down at default display.
8910 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8911 processPosition(mapper, x1, y1);
8912 processId(mapper, 1);
8913 processSlot(mapper, 1);
8914 processPosition(mapper, x2, y2);
8915 processId(mapper, 2);
8916 processSync(mapper);
8917
8918 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8919 fakePointerController->getSpots().find(DISPLAY_ID);
8920 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8921 ASSERT_EQ(size_t(2), iter->second.size());
8922
8923 // Two fingers down at second display.
8924 processPosition(mapper2, x1, y1);
8925 processId(mapper2, 1);
8926 processSlot(mapper2, 1);
8927 processPosition(mapper2, x2, y2);
8928 processId(mapper2, 2);
8929 processSync(mapper2);
8930
8931 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8932 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8933 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008934
8935 // Disable the show touches configuration and ensure the spots are cleared.
8936 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008937 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8938 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008939
8940 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008941}
8942
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008943TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008944 prepareAxes(POSITION);
8945 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008946 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008947 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008948
8949 NotifyMotionArgs motionArgs;
8950 // Unrotated video frame
8951 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8952 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008953 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008954 processPosition(mapper, 100, 200);
8955 processSync(mapper);
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8957 ASSERT_EQ(frames, motionArgs.videoFrames);
8958
8959 // Subsequent touch events should not have any videoframes
8960 // This is implemented separately in FakeEventHub,
8961 // but that should match the behaviour of TouchVideoDevice.
8962 processPosition(mapper, 200, 200);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8966}
8967
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008968TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008969 prepareAxes(POSITION);
8970 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008971 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008972 // Unrotated video frame
8973 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8974 NotifyMotionArgs motionArgs;
8975
8976 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008977 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008978 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8979 clearViewports();
8980 prepareDisplay(orientation);
8981 std::vector<TouchVideoFrame> frames{frame};
8982 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8983 processPosition(mapper, 100, 200);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(frames, motionArgs.videoFrames);
8987 }
8988}
8989
8990TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8991 prepareAxes(POSITION);
8992 addConfigurationProperty("touch.deviceType", "touchScreen");
8993 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8994 // orientation-aware are affected by display rotation.
8995 addConfigurationProperty("touch.orientationAware", "0");
8996 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8997 // Unrotated video frame
8998 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8999 NotifyMotionArgs motionArgs;
9000
9001 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009002 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9004 clearViewports();
9005 prepareDisplay(orientation);
9006 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009007 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009008 processPosition(mapper, 100, 200);
9009 processSync(mapper);
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009011 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9012 // compared to the display. This is so that when the window transform (which contains the
9013 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9014 // window's coordinate space.
9015 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009016 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009017
9018 // Release finger.
9019 processSync(mapper);
9020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009021 }
9022}
9023
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009024TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009025 prepareAxes(POSITION);
9026 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009027 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009028 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9029 // so mix these.
9030 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9031 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9032 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9033 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9034 NotifyMotionArgs motionArgs;
9035
Michael Wrighta9cf4192022-12-01 23:46:39 +00009036 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009037 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009038 processPosition(mapper, 100, 200);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009041 ASSERT_EQ(frames, motionArgs.videoFrames);
9042}
9043
9044TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
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 frames. There's no rule that they must all have the same dimensions,
9052 // so mix these.
9053 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9054 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9055 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9056 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9057 NotifyMotionArgs motionArgs;
9058
Michael Wrighta9cf4192022-12-01 23:46:39 +00009059 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009060 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9061 processPosition(mapper, 100, 200);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9065 // 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.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009069 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009070 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009071 ASSERT_EQ(frames, motionArgs.videoFrames);
9072}
9073
Arthur Hung9da14732019-09-02 16:16:58 +08009074/**
9075 * If we had defined port associations, but the viewport is not ready, the touch device would be
9076 * expected to be disabled, and it should be enabled after the viewport has found.
9077 */
9078TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009079 constexpr uint8_t hdmi2 = 1;
9080 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009081 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009082
9083 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9084
9085 addConfigurationProperty("touch.deviceType", "touchScreen");
9086 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009087 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009088
9089 ASSERT_EQ(mDevice->isEnabled(), false);
9090
9091 // Add display on hdmi2, the device should be enabled and can receive touch event.
9092 prepareSecondaryDisplay(type, hdmi2);
9093 ASSERT_EQ(mDevice->isEnabled(), true);
9094
9095 // Send a touch event.
9096 processPosition(mapper, 100, 100);
9097 processSync(mapper);
9098
9099 NotifyMotionArgs args;
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9101 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9102}
9103
Arthur Hung421eb1c2020-01-16 00:09:42 +08009104TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009105 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009106 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009107 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009108 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009109
9110 NotifyMotionArgs motionArgs;
9111
9112 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9113 // finger down
9114 processId(mapper, 1);
9115 processPosition(mapper, x1, y1);
9116 processSync(mapper);
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9120
9121 // finger move
9122 processId(mapper, 1);
9123 processPosition(mapper, x2, y2);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9127 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9128
9129 // finger up.
9130 processId(mapper, -1);
9131 processSync(mapper);
9132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9133 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9134 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9135
9136 // new finger down
9137 processId(mapper, 1);
9138 processPosition(mapper, x3, y3);
9139 processSync(mapper);
9140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9141 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9142 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9143}
9144
9145/**
arthurhungcc7f9802020-04-30 17:55:40 +08009146 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9147 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009148 */
arthurhungcc7f9802020-04-30 17:55:40 +08009149TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009150 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009151 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009152 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009153 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009154
9155 NotifyMotionArgs motionArgs;
9156
9157 // default tool type is finger
9158 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009159 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009160 processPosition(mapper, x1, y1);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9165
9166 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9167 processToolType(mapper, MT_TOOL_PALM);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9171
9172 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009173 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009174 processPosition(mapper, x2, y2);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9177
9178 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009179 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009180 processSync(mapper);
9181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9182
9183 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009184 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009185 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009186 processPosition(mapper, x3, y3);
9187 processSync(mapper);
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9191}
9192
arthurhungbf89a482020-04-17 17:37:55 +08009193/**
arthurhungcc7f9802020-04-30 17:55:40 +08009194 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9195 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009196 */
arthurhungcc7f9802020-04-30 17:55:40 +08009197TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009198 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009199 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009200 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9201 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9202
9203 NotifyMotionArgs motionArgs;
9204
9205 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009206 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9207 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009208 processPosition(mapper, x1, y1);
9209 processSync(mapper);
9210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9211 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9212 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9213
9214 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009215 processSlot(mapper, SECOND_SLOT);
9216 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009217 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009220 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9222
9223 // If the tool type of the first finger changes to MT_TOOL_PALM,
9224 // we expect to receive ACTION_POINTER_UP with cancel flag.
9225 processSlot(mapper, FIRST_SLOT);
9226 processId(mapper, FIRST_TRACKING_ID);
9227 processToolType(mapper, MT_TOOL_PALM);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009230 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009231 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9232
9233 // The following MOVE events of second finger should be processed.
9234 processSlot(mapper, SECOND_SLOT);
9235 processId(mapper, SECOND_TRACKING_ID);
9236 processPosition(mapper, x2 + 1, y2 + 1);
9237 processSync(mapper);
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9240 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9241
9242 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9243 // it. Second finger receive move.
9244 processSlot(mapper, FIRST_SLOT);
9245 processId(mapper, INVALID_TRACKING_ID);
9246 processSync(mapper);
9247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9249 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9250
9251 // Second finger keeps moving.
9252 processSlot(mapper, SECOND_SLOT);
9253 processId(mapper, SECOND_TRACKING_ID);
9254 processPosition(mapper, x2 + 2, y2 + 2);
9255 processSync(mapper);
9256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9258 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9259
9260 // Second finger up.
9261 processId(mapper, INVALID_TRACKING_ID);
9262 processSync(mapper);
9263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9265 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9266}
9267
9268/**
9269 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9270 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9271 */
9272TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9273 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009274 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009275 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9276 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9277
9278 NotifyMotionArgs motionArgs;
9279
9280 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9281 // First finger down.
9282 processId(mapper, FIRST_TRACKING_ID);
9283 processPosition(mapper, x1, y1);
9284 processSync(mapper);
9285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9286 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9287 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9288
9289 // Second finger down.
9290 processSlot(mapper, SECOND_SLOT);
9291 processId(mapper, SECOND_TRACKING_ID);
9292 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009293 processSync(mapper);
9294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009295 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009296 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9297
arthurhungcc7f9802020-04-30 17:55:40 +08009298 // If the tool type of the first finger changes to MT_TOOL_PALM,
9299 // we expect to receive ACTION_POINTER_UP with cancel flag.
9300 processSlot(mapper, FIRST_SLOT);
9301 processId(mapper, FIRST_TRACKING_ID);
9302 processToolType(mapper, MT_TOOL_PALM);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009305 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009306 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9307
9308 // Second finger keeps moving.
9309 processSlot(mapper, SECOND_SLOT);
9310 processId(mapper, SECOND_TRACKING_ID);
9311 processPosition(mapper, x2 + 1, y2 + 1);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9315
9316 // second finger becomes palm, receive cancel due to only 1 finger is active.
9317 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009318 processToolType(mapper, MT_TOOL_PALM);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9322
arthurhungcc7f9802020-04-30 17:55:40 +08009323 // third finger down.
9324 processSlot(mapper, THIRD_SLOT);
9325 processId(mapper, THIRD_TRACKING_ID);
9326 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009327 processPosition(mapper, x3, y3);
9328 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9330 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009332 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9333
9334 // third finger move
9335 processId(mapper, THIRD_TRACKING_ID);
9336 processPosition(mapper, x3 + 1, y3 + 1);
9337 processSync(mapper);
9338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9340
9341 // first finger up, third finger receive move.
9342 processSlot(mapper, FIRST_SLOT);
9343 processId(mapper, INVALID_TRACKING_ID);
9344 processSync(mapper);
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9347 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9348
9349 // second finger up, third finger receive move.
9350 processSlot(mapper, SECOND_SLOT);
9351 processId(mapper, INVALID_TRACKING_ID);
9352 processSync(mapper);
9353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9355 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9356
9357 // third finger up.
9358 processSlot(mapper, THIRD_SLOT);
9359 processId(mapper, INVALID_TRACKING_ID);
9360 processSync(mapper);
9361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9362 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9363 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9364}
9365
9366/**
9367 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9368 * and the active finger could still be allowed to receive the events
9369 */
9370TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9371 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009372 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009373 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9374 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9375
9376 NotifyMotionArgs motionArgs;
9377
9378 // default tool type is finger
9379 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9380 processId(mapper, FIRST_TRACKING_ID);
9381 processPosition(mapper, x1, y1);
9382 processSync(mapper);
9383 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);
9386
9387 // Second finger down.
9388 processSlot(mapper, SECOND_SLOT);
9389 processId(mapper, SECOND_TRACKING_ID);
9390 processPosition(mapper, x2, y2);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009393 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009394 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9395
9396 // If the tool type of the second finger changes to MT_TOOL_PALM,
9397 // we expect to receive ACTION_POINTER_UP with cancel flag.
9398 processId(mapper, SECOND_TRACKING_ID);
9399 processToolType(mapper, MT_TOOL_PALM);
9400 processSync(mapper);
9401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009402 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009403 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9404
9405 // The following MOVE event should be processed.
9406 processSlot(mapper, FIRST_SLOT);
9407 processId(mapper, FIRST_TRACKING_ID);
9408 processPosition(mapper, x1 + 1, y1 + 1);
9409 processSync(mapper);
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9412 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9413
9414 // second finger up.
9415 processSlot(mapper, SECOND_SLOT);
9416 processId(mapper, INVALID_TRACKING_ID);
9417 processSync(mapper);
9418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9420
9421 // first finger keep moving
9422 processSlot(mapper, FIRST_SLOT);
9423 processId(mapper, FIRST_TRACKING_ID);
9424 processPosition(mapper, x1 + 2, y1 + 2);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9428
9429 // first finger up.
9430 processId(mapper, INVALID_TRACKING_ID);
9431 processSync(mapper);
9432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9433 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9434 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009435}
9436
Arthur Hung9ad18942021-06-19 02:04:46 +00009437/**
9438 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9439 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9440 * cause slot be valid again.
9441 */
9442TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9443 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009444 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009445 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9446 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9447
9448 NotifyMotionArgs motionArgs;
9449
9450 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9451 // First finger down.
9452 processId(mapper, FIRST_TRACKING_ID);
9453 processPosition(mapper, x1, y1);
9454 processPressure(mapper, RAW_PRESSURE_MAX);
9455 processSync(mapper);
9456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9457 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9458 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9459
9460 // First finger move.
9461 processId(mapper, FIRST_TRACKING_ID);
9462 processPosition(mapper, x1 + 1, y1 + 1);
9463 processPressure(mapper, RAW_PRESSURE_MAX);
9464 processSync(mapper);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9467 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9468
9469 // Second finger down.
9470 processSlot(mapper, SECOND_SLOT);
9471 processId(mapper, SECOND_TRACKING_ID);
9472 processPosition(mapper, x2, y2);
9473 processPressure(mapper, RAW_PRESSURE_MAX);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009476 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009477 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9478
9479 // second finger up with some unexpected data.
9480 processSlot(mapper, SECOND_SLOT);
9481 processId(mapper, INVALID_TRACKING_ID);
9482 processPosition(mapper, x2, y2);
9483 processSync(mapper);
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009485 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009486 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9487
9488 // first finger up with some unexpected data.
9489 processSlot(mapper, FIRST_SLOT);
9490 processId(mapper, INVALID_TRACKING_ID);
9491 processPosition(mapper, x2, y2);
9492 processPressure(mapper, RAW_PRESSURE_MAX);
9493 processSync(mapper);
9494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9495 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9496 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9497}
9498
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009499TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9500 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009501 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009502 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9503 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9504
9505 // First finger down.
9506 processId(mapper, FIRST_TRACKING_ID);
9507 processPosition(mapper, 100, 200);
9508 processPressure(mapper, RAW_PRESSURE_MAX);
9509 processSync(mapper);
9510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9511 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9512
9513 // Second finger down.
9514 processSlot(mapper, SECOND_SLOT);
9515 processId(mapper, SECOND_TRACKING_ID);
9516 processPosition(mapper, 300, 400);
9517 processPressure(mapper, RAW_PRESSURE_MAX);
9518 processSync(mapper);
9519 ASSERT_NO_FATAL_FAILURE(
9520 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9521
9522 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009523 // preserved. Resetting should cancel the ongoing gesture.
9524 resetMapper(mapper, ARBITRARY_TIME);
9525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9526 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009527
9528 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9529 // the existing touch state to generate a down event.
9530 processPosition(mapper, 301, 302);
9531 processSync(mapper);
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9533 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9535 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9536
9537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9538}
9539
9540TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9541 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009542 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009543 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9544 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9545
9546 // First finger touches down and releases.
9547 processId(mapper, FIRST_TRACKING_ID);
9548 processPosition(mapper, 100, 200);
9549 processPressure(mapper, RAW_PRESSURE_MAX);
9550 processSync(mapper);
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9552 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9553 processId(mapper, INVALID_TRACKING_ID);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(
9556 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9557
9558 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9559 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009560 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9562
9563 // Send an empty sync frame. Since there are no pointers, no events are generated.
9564 processSync(mapper);
9565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9566}
9567
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009568TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009569 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009570 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009571 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9572 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009574
9575 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9576 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9577 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9578 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9579 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9580
9581 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009582 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009583 processId(mapper, FIRST_TRACKING_ID);
9584 processToolType(mapper, MT_TOOL_PEN);
9585 processPosition(mapper, 100, 200);
9586 processPressure(mapper, RAW_PRESSURE_MAX);
9587 processSync(mapper);
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9589 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9590 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9591 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9592
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009593 // Now that we know the device supports styluses, ensure that the device is re-configured with
9594 // the stylus source.
9595 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9596 {
9597 const auto& devices = mReader->getInputDevices();
9598 auto deviceInfo =
9599 std::find_if(devices.begin(), devices.end(),
9600 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9601 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9603 }
9604
9605 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9607
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009608 processId(mapper, INVALID_TRACKING_ID);
9609 processSync(mapper);
9610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9611 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9612 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9613 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009614}
9615
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009616// --- MultiTouchInputMapperTest_ExternalDevice ---
9617
9618class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9619protected:
Chris Yea52ade12020-08-27 16:49:20 -07009620 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009621};
9622
9623/**
9624 * Expect fallback to internal viewport if device is external and external viewport is not present.
9625 */
9626TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9627 prepareAxes(POSITION);
9628 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009629 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009630 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9631
9632 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9633
9634 NotifyMotionArgs motionArgs;
9635
9636 // Expect the event to be sent to the internal viewport,
9637 // because an external viewport is not present.
9638 processPosition(mapper, 100, 100);
9639 processSync(mapper);
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9641 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9642
9643 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009644 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009645 processPosition(mapper, 100, 100);
9646 processSync(mapper);
9647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9648 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9649}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009650
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009651TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9652 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9653 std::shared_ptr<FakePointerController> fakePointerController =
9654 std::make_shared<FakePointerController>();
9655 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9656 fakePointerController->setPosition(0, 0);
9657 fakePointerController->setButtonState(0);
9658
9659 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009660 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009661 prepareAxes(POSITION | ID | SLOT);
9662 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9663 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9664 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009665 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9667
9668 // captured touchpad should be a touchpad source
9669 NotifyDeviceResetArgs resetArgs;
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9671 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9672
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009673 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009674
9675 const InputDeviceInfo::MotionRange* relRangeX =
9676 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9677 ASSERT_NE(relRangeX, nullptr);
9678 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9679 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9680 const InputDeviceInfo::MotionRange* relRangeY =
9681 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9682 ASSERT_NE(relRangeY, nullptr);
9683 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9684 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9685
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009686 // run captured pointer tests - note that this is unscaled, so input listener events should be
9687 // identical to what the hardware sends (accounting for any
9688 // calibration).
9689 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009690 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009691 processId(mapper, 1);
9692 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9693 processKey(mapper, BTN_TOUCH, 1);
9694 processSync(mapper);
9695
9696 // expect coord[0] to contain initial location of touch 0
9697 NotifyMotionArgs args;
9698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9699 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9700 ASSERT_EQ(1U, args.pointerCount);
9701 ASSERT_EQ(0, args.pointerProperties[0].id);
9702 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9703 ASSERT_NO_FATAL_FAILURE(
9704 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9705
9706 // FINGER 1 DOWN
9707 processSlot(mapper, 1);
9708 processId(mapper, 2);
9709 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9710 processSync(mapper);
9711
9712 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009714 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009715 ASSERT_EQ(2U, args.pointerCount);
9716 ASSERT_EQ(0, args.pointerProperties[0].id);
9717 ASSERT_EQ(1, args.pointerProperties[1].id);
9718 ASSERT_NO_FATAL_FAILURE(
9719 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9720 ASSERT_NO_FATAL_FAILURE(
9721 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9722
9723 // FINGER 1 MOVE
9724 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9725 processSync(mapper);
9726
9727 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9728 // from move
9729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9730 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9731 ASSERT_NO_FATAL_FAILURE(
9732 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9733 ASSERT_NO_FATAL_FAILURE(
9734 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9735
9736 // FINGER 0 MOVE
9737 processSlot(mapper, 0);
9738 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9739 processSync(mapper);
9740
9741 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9744 ASSERT_NO_FATAL_FAILURE(
9745 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9746 ASSERT_NO_FATAL_FAILURE(
9747 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9748
9749 // BUTTON DOWN
9750 processKey(mapper, BTN_LEFT, 1);
9751 processSync(mapper);
9752
9753 // touchinputmapper design sends a move before button press
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9757 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9758
9759 // BUTTON UP
9760 processKey(mapper, BTN_LEFT, 0);
9761 processSync(mapper);
9762
9763 // touchinputmapper design sends a move after button release
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9765 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9768
9769 // FINGER 0 UP
9770 processId(mapper, -1);
9771 processSync(mapper);
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9774
9775 // FINGER 1 MOVE
9776 processSlot(mapper, 1);
9777 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9778 processSync(mapper);
9779
9780 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9783 ASSERT_EQ(1U, args.pointerCount);
9784 ASSERT_EQ(1, args.pointerProperties[0].id);
9785 ASSERT_NO_FATAL_FAILURE(
9786 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9787
9788 // FINGER 1 UP
9789 processId(mapper, -1);
9790 processKey(mapper, BTN_TOUCH, 0);
9791 processSync(mapper);
9792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9793 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9794
Harry Cutts16a24cc2022-10-26 15:22:19 +00009795 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009796 mFakePolicy->setPointerCapture(false);
9797 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009799 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009800}
9801
9802TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9803 std::shared_ptr<FakePointerController> fakePointerController =
9804 std::make_shared<FakePointerController>();
9805 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9806 fakePointerController->setPosition(0, 0);
9807 fakePointerController->setButtonState(0);
9808
9809 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009810 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009811 prepareAxes(POSITION | ID | SLOT);
9812 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9813 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009814 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009815 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9816 // run uncaptured pointer tests - pushes out generic events
9817 // FINGER 0 DOWN
9818 processId(mapper, 3);
9819 processPosition(mapper, 100, 100);
9820 processKey(mapper, BTN_TOUCH, 1);
9821 processSync(mapper);
9822
9823 // start at (100,100), cursor should be at (0,0) * scale
9824 NotifyMotionArgs args;
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9827 ASSERT_NO_FATAL_FAILURE(
9828 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9829
9830 // FINGER 0 MOVE
9831 processPosition(mapper, 200, 200);
9832 processSync(mapper);
9833
9834 // compute scaling to help with touch position checking
9835 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9836 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9837 float scale =
9838 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9839
9840 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9844 0, 0, 0, 0, 0, 0, 0));
9845}
9846
9847TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9848 std::shared_ptr<FakePointerController> fakePointerController =
9849 std::make_shared<FakePointerController>();
9850
Michael Wrighta9cf4192022-12-01 23:46:39 +00009851 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009852 prepareAxes(POSITION | ID | SLOT);
9853 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009854 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009855 mFakePolicy->setPointerCapture(false);
9856 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9857
Harry Cutts16a24cc2022-10-26 15:22:19 +00009858 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9859 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009860
Harry Cutts16a24cc2022-10-26 15:22:19 +00009861 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009862 mFakePolicy->setPointerCapture(true);
9863 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9864 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9865}
9866
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009867// --- BluetoothMultiTouchInputMapperTest ---
9868
9869class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9870protected:
9871 void SetUp() override {
9872 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9873 }
9874};
9875
9876TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9877 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009878 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009879 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9880 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9881
9882 nsecs_t kernelEventTime = ARBITRARY_TIME;
9883 nsecs_t expectedEventTime = ARBITRARY_TIME;
9884 // Touch down.
9885 processId(mapper, FIRST_TRACKING_ID);
9886 processPosition(mapper, 100, 200);
9887 processPressure(mapper, RAW_PRESSURE_MAX);
9888 processSync(mapper, ARBITRARY_TIME);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9890 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9891
9892 // Process several events that come in quick succession, according to their timestamps.
9893 for (int i = 0; i < 3; i++) {
9894 constexpr static nsecs_t delta = ms2ns(1);
9895 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9896 kernelEventTime += delta;
9897 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9898
9899 processPosition(mapper, 101 + i, 201 + i);
9900 processSync(mapper, kernelEventTime);
9901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9902 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9903 WithEventTime(expectedEventTime))));
9904 }
9905
9906 // Release the touch.
9907 processId(mapper, INVALID_TRACKING_ID);
9908 processPressure(mapper, RAW_PRESSURE_MIN);
9909 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9911 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9912 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9913}
9914
9915// --- MultiTouchPointerModeTest ---
9916
HQ Liue6983c72022-04-19 22:14:56 +00009917class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9918protected:
9919 float mPointerMovementScale;
9920 float mPointerXZoomScale;
9921 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9922 addConfigurationProperty("touch.deviceType", "pointer");
9923 std::shared_ptr<FakePointerController> fakePointerController =
9924 std::make_shared<FakePointerController>();
9925 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9926 fakePointerController->setPosition(0, 0);
9927 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +00009928 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009929
9930 prepareAxes(POSITION);
9931 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9932 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9933 // needs to be disabled, and the pointer gesture needs to be enabled.
9934 mFakePolicy->setPointerCapture(false);
9935 mFakePolicy->setPointerGestureEnabled(true);
9936 mFakePolicy->setPointerController(fakePointerController);
9937
9938 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9939 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9940 mPointerMovementScale =
9941 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9942 mPointerXZoomScale =
9943 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9944 }
9945
9946 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9947 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9948 /*flat*/ 0,
9949 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9950 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9951 /*flat*/ 0,
9952 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9953 }
9954};
9955
9956/**
9957 * Two fingers down on a pointer mode touch pad. The width
9958 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9959 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9960 * be greater than the both value to be freeform gesture, so that after two
9961 * fingers start to move downwards, the gesture should be swipe.
9962 */
9963TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9964 // The min freeform gesture width is 25units/mm x 30mm = 750
9965 // which is greater than fraction of the diagnal length of the touchpad (349).
9966 // Thus, MaxSwipWidth is 750.
9967 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9968 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9969 NotifyMotionArgs motionArgs;
9970
9971 // Two fingers down at once.
9972 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9973 // Pointer's initial position is used the [0,0] coordinate.
9974 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9975
9976 processId(mapper, FIRST_TRACKING_ID);
9977 processPosition(mapper, x1, y1);
9978 processMTSync(mapper);
9979 processId(mapper, SECOND_TRACKING_ID);
9980 processPosition(mapper, x2, y2);
9981 processMTSync(mapper);
9982 processSync(mapper);
9983
9984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9985 ASSERT_EQ(1U, motionArgs.pointerCount);
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009988 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009989 ASSERT_NO_FATAL_FAILURE(
9990 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9991
9992 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9993 // that there should be 1 pointer.
9994 int32_t movingDistance = 200;
9995 y1 += movingDistance;
9996 y2 += movingDistance;
9997
9998 processId(mapper, FIRST_TRACKING_ID);
9999 processPosition(mapper, x1, y1);
10000 processMTSync(mapper);
10001 processId(mapper, SECOND_TRACKING_ID);
10002 processPosition(mapper, x2, y2);
10003 processMTSync(mapper);
10004 processSync(mapper);
10005
10006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10007 ASSERT_EQ(1U, motionArgs.pointerCount);
10008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10009 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010010 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10012 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10013 0, 0, 0, 0));
10014}
10015
10016/**
10017 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10018 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10019 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10020 * value to be freeform gesture, so that after two fingers start to move downwards,
10021 * the gesture should be swipe.
10022 */
10023TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10024 // The min freeform gesture width is 5units/mm x 30mm = 150
10025 // which is greater than fraction of the diagnal length of the touchpad (349).
10026 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10027 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10028 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10029 NotifyMotionArgs motionArgs;
10030
10031 // Two fingers down at once.
10032 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10033 // Pointer's initial position is used the [0,0] coordinate.
10034 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10035
10036 processId(mapper, FIRST_TRACKING_ID);
10037 processPosition(mapper, x1, y1);
10038 processMTSync(mapper);
10039 processId(mapper, SECOND_TRACKING_ID);
10040 processPosition(mapper, x2, y2);
10041 processMTSync(mapper);
10042 processSync(mapper);
10043
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10045 ASSERT_EQ(1U, motionArgs.pointerCount);
10046 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10047 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010048 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010049 ASSERT_NO_FATAL_FAILURE(
10050 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10051
10052 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10053 // and there should be 1 pointer.
10054 int32_t movingDistance = 200;
10055 y1 += movingDistance;
10056 y2 += movingDistance;
10057
10058 processId(mapper, FIRST_TRACKING_ID);
10059 processPosition(mapper, x1, y1);
10060 processMTSync(mapper);
10061 processId(mapper, SECOND_TRACKING_ID);
10062 processPosition(mapper, x2, y2);
10063 processMTSync(mapper);
10064 processSync(mapper);
10065
10066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10067 ASSERT_EQ(1U, motionArgs.pointerCount);
10068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10069 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010070 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010071 // New coordinate is the scaled relative coordinate from the initial coordinate.
10072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10073 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10074 0, 0, 0, 0));
10075}
10076
10077/**
10078 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10079 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10080 * freeform gestures after two fingers start to move downwards.
10081 */
10082TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10083 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10084 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10085
10086 NotifyMotionArgs motionArgs;
10087
10088 // Two fingers down at once. Wider than the max swipe width.
10089 // The gesture is expected to be PRESS, then transformed to FREEFORM
10090 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10091
10092 processId(mapper, FIRST_TRACKING_ID);
10093 processPosition(mapper, x1, y1);
10094 processMTSync(mapper);
10095 processId(mapper, SECOND_TRACKING_ID);
10096 processPosition(mapper, x2, y2);
10097 processMTSync(mapper);
10098 processSync(mapper);
10099
10100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10101 ASSERT_EQ(1U, motionArgs.pointerCount);
10102 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010104 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010105 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10106 ASSERT_NO_FATAL_FAILURE(
10107 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10108
10109 int32_t movingDistance = 200;
10110
10111 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10112 // then two down events for two pointers.
10113 y1 += movingDistance;
10114 y2 += movingDistance;
10115
10116 processId(mapper, FIRST_TRACKING_ID);
10117 processPosition(mapper, x1, y1);
10118 processMTSync(mapper);
10119 processId(mapper, SECOND_TRACKING_ID);
10120 processPosition(mapper, x2, y2);
10121 processMTSync(mapper);
10122 processSync(mapper);
10123
10124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10125 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10126 ASSERT_EQ(1U, motionArgs.pointerCount);
10127 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10130 ASSERT_EQ(1U, motionArgs.pointerCount);
10131 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10133 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010134 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010135 ASSERT_EQ(2U, motionArgs.pointerCount);
10136 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010138 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010139 // Two pointers' scaled relative coordinates from their initial centroid.
10140 // Initial y coordinates are 0 as y1 and y2 have the same value.
10141 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10142 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10143 // When pointers move, the new coordinates equal to the initial coordinates plus
10144 // scaled moving distance.
10145 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10146 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10147 0, 0, 0, 0));
10148 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10149 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10150 0, 0, 0, 0));
10151
10152 // Move two fingers down again, expect one MOVE motion event.
10153 y1 += movingDistance;
10154 y2 += movingDistance;
10155
10156 processId(mapper, FIRST_TRACKING_ID);
10157 processPosition(mapper, x1, y1);
10158 processMTSync(mapper);
10159 processId(mapper, SECOND_TRACKING_ID);
10160 processPosition(mapper, x2, y2);
10161 processMTSync(mapper);
10162 processSync(mapper);
10163
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(2U, motionArgs.pointerCount);
10166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10167 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010168 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10170 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10171 0, 0, 0, 0, 0));
10172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10173 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10174 0, 0, 0, 0, 0));
10175}
10176
Harry Cutts39b7ca22022-10-05 15:55:48 +000010177TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10178 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10180 NotifyMotionArgs motionArgs;
10181
10182 // Place two fingers down.
10183 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10184
10185 processId(mapper, FIRST_TRACKING_ID);
10186 processPosition(mapper, x1, y1);
10187 processMTSync(mapper);
10188 processId(mapper, SECOND_TRACKING_ID);
10189 processPosition(mapper, x2, y2);
10190 processMTSync(mapper);
10191 processSync(mapper);
10192
10193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10194 ASSERT_EQ(1U, motionArgs.pointerCount);
10195 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10196 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10197 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10198 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10199
10200 // Move the two fingers down and to the left.
10201 int32_t movingDistance = 200;
10202 x1 -= movingDistance;
10203 y1 += movingDistance;
10204 x2 -= movingDistance;
10205 y2 += movingDistance;
10206
10207 processId(mapper, FIRST_TRACKING_ID);
10208 processPosition(mapper, x1, y1);
10209 processMTSync(mapper);
10210 processId(mapper, SECOND_TRACKING_ID);
10211 processPosition(mapper, x2, y2);
10212 processMTSync(mapper);
10213 processSync(mapper);
10214
10215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10216 ASSERT_EQ(1U, motionArgs.pointerCount);
10217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10218 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10219 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10220 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10221}
10222
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010223TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10224 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10225 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10226 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10228
10229 // Start a stylus gesture.
10230 processKey(mapper, BTN_TOOL_PEN, 1);
10231 processId(mapper, FIRST_TRACKING_ID);
10232 processPosition(mapper, 100, 200);
10233 processSync(mapper);
10234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10235 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10236 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10237 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10238 // TODO(b/257078296): Pointer mode generates extra event.
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10240 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10241 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10242 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10244
10245 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10246 // gesture should be disabled.
10247 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10248 viewport->isActive = false;
10249 mFakePolicy->updateViewport(*viewport);
10250 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10252 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10253 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10254 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10255 // TODO(b/257078296): Pointer mode generates extra event.
10256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10257 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10258 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10259 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10261}
10262
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010263// --- JoystickInputMapperTest ---
10264
10265class JoystickInputMapperTest : public InputMapperTest {
10266protected:
10267 static const int32_t RAW_X_MIN;
10268 static const int32_t RAW_X_MAX;
10269 static const int32_t RAW_Y_MIN;
10270 static const int32_t RAW_Y_MAX;
10271
10272 void SetUp() override {
10273 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10274 }
10275 void prepareAxes() {
10276 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10278 }
10279
10280 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10282 }
10283
10284 void processSync(JoystickInputMapper& mapper) {
10285 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10286 }
10287
Michael Wrighta9cf4192022-12-01 23:46:39 +000010288 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010289 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10290 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10291 NO_PORT, ViewportType::VIRTUAL);
10292 }
10293};
10294
10295const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10296const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10297const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10298const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10299
10300TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10301 prepareAxes();
10302 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10303
10304 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10305
Michael Wrighta9cf4192022-12-01 23:46:39 +000010306 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010307
10308 // Send an axis event
10309 processAxis(mapper, ABS_X, 100);
10310 processSync(mapper);
10311
10312 NotifyMotionArgs args;
10313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10314 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10315
10316 // Send another axis event
10317 processAxis(mapper, ABS_Y, 100);
10318 processSync(mapper);
10319
10320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10321 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10322}
10323
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010324// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010325
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010326class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010327protected:
10328 static const char* DEVICE_NAME;
10329 static const char* DEVICE_LOCATION;
10330 static const int32_t DEVICE_ID;
10331 static const int32_t DEVICE_GENERATION;
10332 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010333 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010334 static const int32_t EVENTHUB_ID;
10335
10336 std::shared_ptr<FakeEventHub> mFakeEventHub;
10337 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010338 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010339 std::unique_ptr<InstrumentedInputReader> mReader;
10340 std::shared_ptr<InputDevice> mDevice;
10341
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010342 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010343 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010344 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010345 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010346 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010347 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010348 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10349 }
10350
10351 void SetUp() override { SetUp(DEVICE_CLASSES); }
10352
10353 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010354 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010355 mFakePolicy.clear();
10356 }
10357
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010358 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010359 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10360 mReader->requestRefreshConfiguration(changes);
10361 mReader->loopOnce();
10362 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010363 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010364 }
10365
10366 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10367 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010368 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010369 InputDeviceIdentifier identifier;
10370 identifier.name = name;
10371 identifier.location = location;
10372 std::shared_ptr<InputDevice> device =
10373 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10374 identifier);
10375 mReader->pushNextDevice(device);
10376 mFakeEventHub->addDevice(eventHubId, name, classes);
10377 mReader->loopOnce();
10378 return device;
10379 }
10380
10381 template <class T, typename... Args>
10382 T& addControllerAndConfigure(Args... args) {
10383 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10384
10385 return controller;
10386 }
10387};
10388
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010389const char* PeripheralControllerTest::DEVICE_NAME = "device";
10390const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10391const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10392const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10393const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010394const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10395 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010396const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010397
10398// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010399class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010400protected:
10401 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010402 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010403 }
10404};
10405
10406TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010407 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010408
Harry Cuttsa5b71292022-11-28 12:56:17 +000010409 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10410 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10411 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010412}
10413
10414TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010415 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010416
Harry Cuttsa5b71292022-11-28 12:56:17 +000010417 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10418 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10419 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010420}
10421
10422// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010423class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010424protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010425 void SetUp() override {
10426 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10427 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010428};
10429
Chris Ye85758332021-05-16 23:05:17 -070010430TEST_F(LightControllerTest, MonoLight) {
10431 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010432 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010433 .maxBrightness = 255,
10434 .flags = InputLightClass::BRIGHTNESS,
10435 .path = ""};
10436 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010437
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010438 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010439 InputDeviceInfo info;
10440 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010441 std::vector<InputDeviceLightInfo> lights = info.getLights();
10442 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010443 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10444 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10445
10446 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10447 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10448}
10449
10450TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10451 RawLightInfo infoMono = {.id = 1,
10452 .name = "mono_keyboard_backlight",
10453 .maxBrightness = 255,
10454 .flags = InputLightClass::BRIGHTNESS |
10455 InputLightClass::KEYBOARD_BACKLIGHT,
10456 .path = ""};
10457 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10458
10459 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10460 InputDeviceInfo info;
10461 controller.populateDeviceInfo(&info);
10462 std::vector<InputDeviceLightInfo> lights = info.getLights();
10463 ASSERT_EQ(1U, lights.size());
10464 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10465 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010466
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010467 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10468 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469}
10470
10471TEST_F(LightControllerTest, RGBLight) {
10472 RawLightInfo infoRed = {.id = 1,
10473 .name = "red",
10474 .maxBrightness = 255,
10475 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10476 .path = ""};
10477 RawLightInfo infoGreen = {.id = 2,
10478 .name = "green",
10479 .maxBrightness = 255,
10480 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10481 .path = ""};
10482 RawLightInfo infoBlue = {.id = 3,
10483 .name = "blue",
10484 .maxBrightness = 255,
10485 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10486 .path = ""};
10487 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10488 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10489 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10490
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010491 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010492 InputDeviceInfo info;
10493 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010494 std::vector<InputDeviceLightInfo> lights = info.getLights();
10495 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010496 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10497 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10498 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10499
10500 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10501 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10502}
10503
10504TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10505 RawLightInfo infoRed = {.id = 1,
10506 .name = "red_keyboard_backlight",
10507 .maxBrightness = 255,
10508 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10509 InputLightClass::KEYBOARD_BACKLIGHT,
10510 .path = ""};
10511 RawLightInfo infoGreen = {.id = 2,
10512 .name = "green_keyboard_backlight",
10513 .maxBrightness = 255,
10514 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10515 InputLightClass::KEYBOARD_BACKLIGHT,
10516 .path = ""};
10517 RawLightInfo infoBlue = {.id = 3,
10518 .name = "blue_keyboard_backlight",
10519 .maxBrightness = 255,
10520 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10521 InputLightClass::KEYBOARD_BACKLIGHT,
10522 .path = ""};
10523 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10524 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10525 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10526
10527 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10528 InputDeviceInfo info;
10529 controller.populateDeviceInfo(&info);
10530 std::vector<InputDeviceLightInfo> lights = info.getLights();
10531 ASSERT_EQ(1U, lights.size());
10532 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10533 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10534 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10535
10536 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10537 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10538}
10539
10540TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10541 RawLightInfo infoRed = {.id = 1,
10542 .name = "red",
10543 .maxBrightness = 255,
10544 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10545 .path = ""};
10546 RawLightInfo infoGreen = {.id = 2,
10547 .name = "green",
10548 .maxBrightness = 255,
10549 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10550 .path = ""};
10551 RawLightInfo infoBlue = {.id = 3,
10552 .name = "blue",
10553 .maxBrightness = 255,
10554 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10555 .path = ""};
10556 RawLightInfo infoGlobal = {.id = 3,
10557 .name = "global_keyboard_backlight",
10558 .maxBrightness = 255,
10559 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10560 InputLightClass::KEYBOARD_BACKLIGHT,
10561 .path = ""};
10562 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10563 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10564 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10565 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10566
10567 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10568 InputDeviceInfo info;
10569 controller.populateDeviceInfo(&info);
10570 std::vector<InputDeviceLightInfo> lights = info.getLights();
10571 ASSERT_EQ(1U, lights.size());
10572 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10573 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10574 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010575
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010576 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10577 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010578}
10579
10580TEST_F(LightControllerTest, MultiColorRGBLight) {
10581 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010582 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010583 .maxBrightness = 255,
10584 .flags = InputLightClass::BRIGHTNESS |
10585 InputLightClass::MULTI_INTENSITY |
10586 InputLightClass::MULTI_INDEX,
10587 .path = ""};
10588
10589 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10590
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010591 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010592 InputDeviceInfo info;
10593 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010594 std::vector<InputDeviceLightInfo> lights = info.getLights();
10595 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010596 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10597 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10598 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10599
10600 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10601 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10602}
10603
10604TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10605 RawLightInfo infoColor = {.id = 1,
10606 .name = "multi_color_keyboard_backlight",
10607 .maxBrightness = 255,
10608 .flags = InputLightClass::BRIGHTNESS |
10609 InputLightClass::MULTI_INTENSITY |
10610 InputLightClass::MULTI_INDEX |
10611 InputLightClass::KEYBOARD_BACKLIGHT,
10612 .path = ""};
10613
10614 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10615
10616 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10617 InputDeviceInfo info;
10618 controller.populateDeviceInfo(&info);
10619 std::vector<InputDeviceLightInfo> lights = info.getLights();
10620 ASSERT_EQ(1U, lights.size());
10621 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10622 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10623 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010624
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010625 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10626 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010627}
10628
10629TEST_F(LightControllerTest, PlayerIdLight) {
10630 RawLightInfo info1 = {.id = 1,
10631 .name = "player1",
10632 .maxBrightness = 255,
10633 .flags = InputLightClass::BRIGHTNESS,
10634 .path = ""};
10635 RawLightInfo info2 = {.id = 2,
10636 .name = "player2",
10637 .maxBrightness = 255,
10638 .flags = InputLightClass::BRIGHTNESS,
10639 .path = ""};
10640 RawLightInfo info3 = {.id = 3,
10641 .name = "player3",
10642 .maxBrightness = 255,
10643 .flags = InputLightClass::BRIGHTNESS,
10644 .path = ""};
10645 RawLightInfo info4 = {.id = 4,
10646 .name = "player4",
10647 .maxBrightness = 255,
10648 .flags = InputLightClass::BRIGHTNESS,
10649 .path = ""};
10650 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10651 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10652 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10653 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10654
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010655 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010656 InputDeviceInfo info;
10657 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010658 std::vector<InputDeviceLightInfo> lights = info.getLights();
10659 ASSERT_EQ(1U, lights.size());
10660 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010661 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10662 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010663
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010664 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10665 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10666 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010667}
10668
Michael Wrightd02c5b62014-02-10 15:10:22 -080010669} // namespace android