blob: 3516092cef0bf5e12b3d7d7f49d2e878a03913a4 [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000039#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050041#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070044#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000046#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000047#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000048#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000049#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000050#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000051#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000055using android::hardware::input::InputDeviceCountryCode;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057namespace android {
58
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000060using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061using std::chrono_literals::operator""ms;
62
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000067static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
70static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
71static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
72static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070073static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070074static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
arthurhungcc7f9802020-04-30 17:55:40 +080076static constexpr int32_t FIRST_SLOT = 0;
77static constexpr int32_t SECOND_SLOT = 1;
78static constexpr int32_t THIRD_SLOT = 2;
79static constexpr int32_t INVALID_TRACKING_ID = -1;
80static constexpr int32_t FIRST_TRACKING_ID = 0;
81static constexpr int32_t SECOND_TRACKING_ID = 1;
82static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080083static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
84static constexpr int32_t LIGHT_COLOR = 0x7F448866;
85static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080086
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080087static constexpr int32_t ACTION_POINTER_0_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_0_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_DOWN =
92 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000096// Minimum timestamp separation between subsequent input events from a Bluetooth device.
97static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
98// Maximum smoothing time delta so that we don't generate events too far into the future.
99constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117 case ui::ROTATION_90:
118 return ui::ROTATION_270;
119 case ui::ROTATION_270:
120 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700144[[maybe_unused]] static void dumpReader(InputReader& reader) {
145 std::string dump;
146 reader.dump(dump);
147 std::istringstream iss(dump);
148 for (std::string line; std::getline(iss, line);) {
149 ALOGE("%s", line.c_str());
150 std::this_thread::sleep_for(std::chrono::milliseconds(1));
151 }
152}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154// --- FakeInputMapper ---
155
156class FakeInputMapper : public InputMapper {
157 uint32_t mSources;
158 int32_t mKeyboardType;
159 int32_t mMetaState;
160 KeyedVector<int32_t, int32_t> mKeyCodeStates;
161 KeyedVector<int32_t, int32_t> mScanCodeStates;
162 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100163 // fake mapping which would normally come from keyCharacterMap
164 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800165 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700167 std::mutex mLock;
168 std::condition_variable mStateChangedCondition;
169 bool mConfigureWasCalled GUARDED_BY(mLock);
170 bool mResetWasCalled GUARDED_BY(mLock);
171 bool mProcessWasCalled GUARDED_BY(mLock);
172 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Arthur Hungc23540e2018-11-29 20:42:11 +0800174 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800176 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
177 : InputMapper(deviceContext),
178 mSources(sources),
179 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mConfigureWasCalled(false),
182 mResetWasCalled(false),
183 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 void setKeyboardType(int32_t keyboardType) {
188 mKeyboardType = keyboardType;
189 }
190
191 void setMetaState(int32_t metaState) {
192 mMetaState = metaState;
193 }
194
195 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700196 std::unique_lock<std::mutex> lock(mLock);
197 base::ScopedLockAssertion assumeLocked(mLock);
198 const bool configureCalled =
199 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
200 return mConfigureWasCalled;
201 });
202 if (!configureCalled) {
203 FAIL() << "Expected configure() to have been called.";
204 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 mConfigureWasCalled = false;
206 }
207
208 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::unique_lock<std::mutex> lock(mLock);
210 base::ScopedLockAssertion assumeLocked(mLock);
211 const bool resetCalled =
212 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
213 return mResetWasCalled;
214 });
215 if (!resetCalled) {
216 FAIL() << "Expected reset() to have been called.";
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mResetWasCalled = false;
219 }
220
Yi Kong9b14ac62018-07-17 13:48:38 -0700221 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 std::unique_lock<std::mutex> lock(mLock);
223 base::ScopedLockAssertion assumeLocked(mLock);
224 const bool processCalled =
225 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mProcessWasCalled;
227 });
228 if (!processCalled) {
229 FAIL() << "Expected process() to have been called.";
230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 if (outLastEvent) {
232 *outLastEvent = mLastEvent;
233 }
234 mProcessWasCalled = false;
235 }
236
237 void setKeyCodeState(int32_t keyCode, int32_t state) {
238 mKeyCodeStates.replaceValueFor(keyCode, state);
239 }
240
241 void setScanCodeState(int32_t scanCode, int32_t state) {
242 mScanCodeStates.replaceValueFor(scanCode, state);
243 }
244
245 void setSwitchState(int32_t switchCode, int32_t state) {
246 mSwitchStates.replaceValueFor(switchCode, state);
247 }
248
249 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800250 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251 }
252
Philip Junker4af3b3d2021-12-14 10:36:55 +0100253 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
254 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
255 }
256
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100258 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
Chris Yea52ade12020-08-27 16:49:20 -0700260 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 InputMapper::populateDeviceInfo(deviceInfo);
262
263 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
264 deviceInfo->setKeyboardType(mKeyboardType);
265 }
266 }
267
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700268 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
269 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700270 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800272
273 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800274 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800275 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
276 mViewport = config->getDisplayViewportByPort(*displayPort);
277 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700278
279 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700280 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700283 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700287 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 }
289
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700290 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 mLastEvent = *rawEvent;
293 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
Chris Yea52ade12020-08-27 16:49:20 -0700298 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
300 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
301 }
302
Philip Junker4af3b3d2021-12-14 10:36:55 +0100303 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
304 auto it = mKeyCodeMapping.find(locationKeyCode);
305 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
306 }
307
Chris Yea52ade12020-08-27 16:49:20 -0700308 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
310 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
311 }
312
Chris Yea52ade12020-08-27 16:49:20 -0700313 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 ssize_t index = mSwitchStates.indexOfKey(switchCode);
315 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
316 }
317
Chris Yea52ade12020-08-27 16:49:20 -0700318 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700319 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700320 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700321 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
323 if (keyCodes[i] == mSupportedKeyCodes[j]) {
324 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326 }
327 }
Chris Yea52ade12020-08-27 16:49:20 -0700328 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 return result;
330 }
331
332 virtual int32_t getMetaState() {
333 return mMetaState;
334 }
335
336 virtual void fadePointer() {
337 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800338
339 virtual std::optional<int32_t> getAssociatedDisplay() {
340 if (mViewport) {
341 return std::make_optional(mViewport->displayId);
342 }
343 return std::nullopt;
344 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345};
346
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700347// --- InputReaderPolicyTest ---
348class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700349protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700350 sp<FakeInputReaderPolicy> mFakePolicy;
351
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700352 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700353 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700354};
355
356/**
357 * Check that empty set of viewports is an acceptable configuration.
358 * Also try to get internal viewport two different ways - by type and by uniqueId.
359 *
360 * There will be confusion if two viewports with empty uniqueId and identical type are present.
361 * Such configuration is not currently allowed.
362 */
363TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700364 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700365
366 // We didn't add any viewports yet, so there shouldn't be any.
367 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100368 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369 ASSERT_FALSE(internalViewport);
370
371 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000372 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
373 true /*isActive*/, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374
375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379
380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700384
385 mFakePolicy->clearViewports();
386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_FALSE(internalViewport);
391}
392
393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
394 const std::string internalUniqueId = "local:0";
395 const std::string externalUniqueId = "local:1";
396 const std::string virtualUniqueId1 = "virtual:2";
397 const std::string virtualUniqueId2 = "virtual:3";
398 constexpr int32_t virtualDisplayId1 = 2;
399 constexpr int32_t virtualDisplayId2 = 3;
400
401 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
403 true /*isActive*/, internalUniqueId, NO_PORT,
404 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
407 true /*isActive*/, externalUniqueId, NO_PORT,
408 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700409 // Add an virtual viewport
410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ui::ROTATION_0, true /*isActive*/, virtualUniqueId1, NO_PORT,
412 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 // Add another virtual viewport
414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ui::ROTATION_0, true /*isActive*/, virtualUniqueId2, NO_PORT,
416 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700417
418 // Check matching by type for internal
419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421 ASSERT_TRUE(internalViewport);
422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
423
424 // Check matching by type for external
425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 ASSERT_TRUE(externalViewport);
428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
429
430 // Check matching by uniqueId for virtual viewport #1
431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
437
438 // Check matching by uniqueId for virtual viewport #2
439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
445}
446
447
448/**
449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
450 * that lookup works by checking display id.
451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
452 */
453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
454 const std::string uniqueId1 = "uniqueId1";
455 const std::string uniqueId2 = "uniqueId2";
456 constexpr int32_t displayId1 = 2;
457 constexpr int32_t displayId2 = 3;
458
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700461 for (const ViewportType& type : types) {
462 mFakePolicy->clearViewports();
463 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
465 true /*isActive*/, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000467 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
468 true /*isActive*/, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700469
470 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700471 std::optional<DisplayViewport> viewport1 =
472 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700473 ASSERT_TRUE(viewport1);
474 ASSERT_EQ(displayId1, viewport1->displayId);
475 ASSERT_EQ(type, viewport1->type);
476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700477 std::optional<DisplayViewport> viewport2 =
478 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700479 ASSERT_TRUE(viewport2);
480 ASSERT_EQ(displayId2, viewport2->displayId);
481 ASSERT_EQ(type, viewport2->type);
482
483 // When there are multiple viewports of the same kind, and uniqueId is not specified
484 // in the call to getDisplayViewport, then that situation is not supported.
485 // The viewports can be stored in any order, so we cannot rely on the order, since that
486 // is just implementation detail.
487 // However, we can check that it still returns *a* viewport, we just cannot assert
488 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700489 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490 ASSERT_TRUE(someViewport);
491 }
492}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000495 * When we have multiple internal displays make sure we always return the default display when
496 * querying by type.
497 */
498TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
499 const std::string uniqueId1 = "uniqueId1";
500 const std::string uniqueId2 = "uniqueId2";
501 constexpr int32_t nonDefaultDisplayId = 2;
502 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
503 "Test display ID should not be ADISPLAY_ID_DEFAULT");
504
505 // Add the default display first and ensure it gets returned.
506 mFakePolicy->clearViewports();
507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000508 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 ViewportType::INTERNAL);
510 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000511 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000512 ViewportType::INTERNAL);
513
514 std::optional<DisplayViewport> viewport =
515 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
516 ASSERT_TRUE(viewport);
517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
519
520 // Add the default display second to make sure order doesn't matter.
521 mFakePolicy->clearViewports();
522 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000523 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000524 ViewportType::INTERNAL);
525 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000526 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000527 ViewportType::INTERNAL);
528
529 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
530 ASSERT_TRUE(viewport);
531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
533}
534
535/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700536 * Check getDisplayViewportByPort
537 */
538TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100539 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700540 const std::string uniqueId1 = "uniqueId1";
541 const std::string uniqueId2 = "uniqueId2";
542 constexpr int32_t displayId1 = 1;
543 constexpr int32_t displayId2 = 2;
544 const uint8_t hdmi1 = 0;
545 const uint8_t hdmi2 = 1;
546 const uint8_t hdmi3 = 2;
547
548 mFakePolicy->clearViewports();
549 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000550 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
551 true /*isActive*/, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000553 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
554 true /*isActive*/, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700555
556 // Check that correct display viewport was returned by comparing the display ports.
557 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
558 ASSERT_TRUE(hdmi1Viewport);
559 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
560 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
561
562 // Check that we can still get the same viewport using the uniqueId
563 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
564 ASSERT_TRUE(hdmi1Viewport);
565 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
566 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
567 ASSERT_EQ(type, hdmi1Viewport->type);
568
569 // Check that we cannot find a port with "HDMI2", because we never added one
570 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
571 ASSERT_FALSE(hdmi2Viewport);
572}
573
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574// --- InputReaderTest ---
575
576class InputReaderTest : public testing::Test {
577protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700578 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700580 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000581 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Chris Yea52ade12020-08-27 16:49:20 -0700583 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700584 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700585 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Prabir Pradhan28efc192019-11-05 01:10:04 +0000588 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700589 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Chris Yea52ade12020-08-27 16:49:20 -0700592 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700593 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 }
596
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700597 void addDevice(int32_t eventHubId, const std::string& name,
598 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
601 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800602 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 }
604 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000605 mReader->loopOnce();
606 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700607 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
627 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800628 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800629 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 return mapper;
631 }
632};
633
Chris Ye98d3f532020-10-01 21:48:59 -0700634TEST_F(InputReaderTest, PolicyGetInputDevices) {
635 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700636 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700637 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638
639 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700640 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800642 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100643 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
645 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000646 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647}
648
Chris Yee7310032020-09-22 15:36:28 -0700649TEST_F(InputReaderTest, GetMergedInputDevices) {
650 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
651 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
652 // Add two subdevices to device
653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
654 // Must add at least one mapper or the device will be ignored!
655 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
656 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
657
658 // Push same device instance for next device to be added, so they'll have same identifier.
659 mReader->pushNextDevice(device);
660 mReader->pushNextDevice(device);
661 ASSERT_NO_FATAL_FAILURE(
662 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
663 ASSERT_NO_FATAL_FAILURE(
664 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
665
666 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000667 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700668}
669
Chris Yee14523a2020-12-19 13:46:00 -0800670TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
671 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
672 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
673 // Add two subdevices to device
674 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
675 // Must add at least one mapper or the device will be ignored!
676 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
677 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
678
679 // Push same device instance for next device to be added, so they'll have same identifier.
680 mReader->pushNextDevice(device);
681 mReader->pushNextDevice(device);
682 // Sensor device is initially disabled
683 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
684 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
685 nullptr));
686 // Device is disabled because the only sub device is a sensor device and disabled initially.
687 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
688 ASSERT_FALSE(device->isEnabled());
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
691 // The merged device is enabled if any sub device is enabled
692 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
693 ASSERT_TRUE(device->isEnabled());
694}
695
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700696TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700698 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800699 constexpr int32_t eventHubId = 1;
700 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700701 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800702 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800703 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800704 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700705
Yi Kong9b14ac62018-07-17 13:48:38 -0700706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700707
708 NotifyDeviceResetArgs resetArgs;
709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700710 ASSERT_EQ(deviceId, resetArgs.deviceId);
711
712 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800713 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000714 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700715
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700717 ASSERT_EQ(deviceId, resetArgs.deviceId);
718 ASSERT_EQ(device->isEnabled(), false);
719
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800720 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000721 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700724 ASSERT_EQ(device->isEnabled(), false);
725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800726 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000727 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729 ASSERT_EQ(deviceId, resetArgs.deviceId);
730 ASSERT_EQ(device->isEnabled(), true);
731}
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700735 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800739 AINPUT_SOURCE_KEYBOARD, nullptr);
740 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
742 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
743 AINPUT_SOURCE_ANY, AKEYCODE_A))
744 << "Should return unknown when the device id is >= 0 but unknown.";
745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800746 ASSERT_EQ(AKEY_STATE_UNKNOWN,
747 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
748 << "Should return unknown when the device id is valid but the sources are not "
749 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800751 ASSERT_EQ(AKEY_STATE_DOWN,
752 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
753 AKEYCODE_A))
754 << "Should return value provided by mapper when device id is valid and the device "
755 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756
757 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
758 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
759 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
760
761 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
762 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
763 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
764}
765
Philip Junker4af3b3d2021-12-14 10:36:55 +0100766TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
768 constexpr int32_t eventHubId = 1;
769 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
770 InputDeviceClass::KEYBOARD,
771 AINPUT_SOURCE_KEYBOARD, nullptr);
772 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
773
774 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
775 << "Should return unknown when the device with the specified id is not found.";
776
777 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
778 << "Should return correct mapping when device id is valid and mapping exists.";
779
780 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
781 << "Should return the location key code when device id is valid and there's no "
782 "mapping.";
783}
784
785TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
787 constexpr int32_t eventHubId = 1;
788 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
789 InputDeviceClass::JOYSTICK,
790 AINPUT_SOURCE_GAMEPAD, nullptr);
791 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
792
793 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
794 << "Should return unknown when the device id is valid but there is no keyboard mapper";
795}
796
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700799 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800801 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800802 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800803 AINPUT_SOURCE_KEYBOARD, nullptr);
804 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
807 AINPUT_SOURCE_ANY, KEY_A))
808 << "Should return unknown when the device id is >= 0 but unknown.";
809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800810 ASSERT_EQ(AKEY_STATE_UNKNOWN,
811 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
812 << "Should return unknown when the device id is valid but the sources are not "
813 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800815 ASSERT_EQ(AKEY_STATE_DOWN,
816 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
817 KEY_A))
818 << "Should return value provided by mapper when device id is valid and the device "
819 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
821 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
822 AINPUT_SOURCE_TRACKBALL, KEY_A))
823 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
824
825 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
826 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
827 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
828}
829
830TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700832 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800835 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800836 AINPUT_SOURCE_KEYBOARD, nullptr);
837 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838
839 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
840 AINPUT_SOURCE_ANY, SW_LID))
841 << "Should return unknown when the device id is >= 0 but unknown.";
842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800843 ASSERT_EQ(AKEY_STATE_UNKNOWN,
844 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
845 << "Should return unknown when the device id is valid but the sources are not "
846 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 ASSERT_EQ(AKEY_STATE_DOWN,
849 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
850 SW_LID))
851 << "Should return value provided by mapper when device id is valid and the device "
852 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853
854 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
855 AINPUT_SOURCE_TRACKBALL, SW_LID))
856 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
857
858 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
859 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
860 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
861}
862
863TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700865 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800868 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800869 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800871 mapper.addSupportedKeyCode(AKEYCODE_A);
872 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700874 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 uint8_t flags[4] = { 0, 0, 0, 1 };
876
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700877 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 << "Should return false when device id is >= 0 but unknown.";
879 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
880
881 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700882 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 << "Should return false when device id is valid but the sources are not supported by "
884 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
886
887 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700888 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 keyCodes, flags))
890 << "Should return value provided by mapper when device id is valid and the device "
891 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
893
894 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700895 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
896 << "Should return false when the device id is < 0 but the sources are not supported by "
897 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
899
900 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700901 ASSERT_TRUE(
902 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
903 << "Should return value provided by mapper when device id is < 0 and one of the "
904 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
906}
907
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000908TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800909 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700910 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
912 NotifyConfigurationChangedArgs args;
913
914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
915 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
916}
917
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000918TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000921 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000923 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800925 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800926 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000928 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000929 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
931
932 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800933 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000934 ASSERT_EQ(when, event.when);
935 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800936 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 ASSERT_EQ(EV_KEY, event.type);
938 ASSERT_EQ(KEY_A, event.code);
939 ASSERT_EQ(1, event.value);
940}
941
Garfield Tan1c7bc862020-01-28 13:24:04 -0800942TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700944 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 constexpr int32_t eventHubId = 1;
946 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800947 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800949 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800951
952 NotifyDeviceResetArgs resetArgs;
953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800954 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800962 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800965 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800966 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800968 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000969 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800971 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800972 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800973}
974
Garfield Tan1c7bc862020-01-28 13:24:04 -0800975TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
976 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800978 constexpr int32_t eventHubId = 1;
979 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
980 // Must add at least one mapper or the device will be ignored!
981 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800982 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800983 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
984
985 NotifyDeviceResetArgs resetArgs;
986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
987 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
988}
989
Arthur Hungc23540e2018-11-29 20:42:11 +0800990TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700992 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800993 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800994 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800995 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
996 FakeInputMapper& mapper =
997 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800998 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800999
1000 const uint8_t hdmi1 = 1;
1001
1002 // Associated touch screen with second display.
1003 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1004
1005 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001006 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001007 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1008 true /*isActive*/, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001009 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001010 ui::ROTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001011 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001012 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001014
1015 // Add the device, and make sure all of the callbacks are triggered.
1016 // The device is added after the input port associations are processed since
1017 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001018 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001021 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001022
Arthur Hung2c9a3342019-07-23 14:18:59 +08001023 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001024 ASSERT_EQ(deviceId, device->getId());
1025 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1026 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001027
1028 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001029 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001030 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001031 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001032}
1033
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001034TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1035 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001036 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001037 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1038 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1039 // Must add at least one mapper or the device will be ignored!
1040 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1041 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1042 mReader->pushNextDevice(device);
1043 mReader->pushNextDevice(device);
1044 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1045 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1046
1047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1048
1049 NotifyDeviceResetArgs resetArgs;
1050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1051 ASSERT_EQ(deviceId, resetArgs.deviceId);
1052 ASSERT_TRUE(device->isEnabled());
1053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1055
1056 disableDevice(deviceId);
1057 mReader->loopOnce();
1058
1059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1060 ASSERT_EQ(deviceId, resetArgs.deviceId);
1061 ASSERT_FALSE(device->isEnabled());
1062 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1063 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1064
1065 enableDevice(deviceId);
1066 mReader->loopOnce();
1067
1068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1069 ASSERT_EQ(deviceId, resetArgs.deviceId);
1070 ASSERT_TRUE(device->isEnabled());
1071 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1072 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1073}
1074
1075TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1076 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001077 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1079 // Add two subdevices to device
1080 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1081 FakeInputMapper& mapperDevice1 =
1082 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1083 FakeInputMapper& mapperDevice2 =
1084 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1085 mReader->pushNextDevice(device);
1086 mReader->pushNextDevice(device);
1087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1088 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1089
1090 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1091 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1092
1093 ASSERT_EQ(AKEY_STATE_DOWN,
1094 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1095 ASSERT_EQ(AKEY_STATE_DOWN,
1096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1097 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1099}
1100
Prabir Pradhan7e186182020-11-10 13:56:45 -08001101TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1102 NotifyPointerCaptureChangedArgs args;
1103
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001104 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001105 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1106 mReader->loopOnce();
1107 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001108 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1109 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001110
1111 mFakePolicy->setPointerCapture(false);
1112 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1113 mReader->loopOnce();
1114 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001115 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001116
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001117 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001118 // does not change.
1119 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1120 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001121 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001122}
1123
Chris Ye87143712020-11-10 05:05:58 +00001124class FakeVibratorInputMapper : public FakeInputMapper {
1125public:
1126 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1127 : FakeInputMapper(deviceContext, sources) {}
1128
1129 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1130};
1131
1132TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1133 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001134 ftl::Flags<InputDeviceClass> deviceClass =
1135 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001136 constexpr int32_t eventHubId = 1;
1137 const char* DEVICE_LOCATION = "BLUETOOTH";
1138 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1139 FakeVibratorInputMapper& mapper =
1140 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1141 mReader->pushNextDevice(device);
1142
1143 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1144 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1145
1146 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1147 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1148}
1149
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001150// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001152class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001153public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001154 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001155
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001156 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001157
Andy Chenf9f1a022022-08-29 20:07:10 -04001158 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1159
Chris Yee2b1e5c2021-03-10 22:45:12 -08001160 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1161
1162 void dump(std::string& dump) override {}
1163
1164 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1165 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001166 }
1167
Chris Yee2b1e5c2021-03-10 22:45:12 -08001168 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1169 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001170 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001171
1172 bool setLightColor(int32_t lightId, int32_t color) override {
1173 getDeviceContext().setLightBrightness(lightId, color >> 24);
1174 return true;
1175 }
1176
1177 std::optional<int32_t> getLightColor(int32_t lightId) override {
1178 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1179 if (!result.has_value()) {
1180 return std::nullopt;
1181 }
1182 return result.value() << 24;
1183 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001184
1185 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1186
1187 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1188
1189private:
1190 InputDeviceContext& mDeviceContext;
1191 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1192 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001193 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001194};
1195
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196TEST_F(InputReaderTest, BatteryGetCapacity) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001203 FakePeripheralController& controller =
1204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001205 mReader->pushNextDevice(device);
1206
1207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1208
Harry Cuttsa5b71292022-11-28 12:56:17 +00001209 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1210 FakeEventHub::BATTERY_CAPACITY);
1211 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212}
1213
1214TEST_F(InputReaderTest, BatteryGetStatus) {
1215 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001216 ftl::Flags<InputDeviceClass> deviceClass =
1217 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001218 constexpr int32_t eventHubId = 1;
1219 const char* DEVICE_LOCATION = "BLUETOOTH";
1220 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001221 FakePeripheralController& controller =
1222 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223 mReader->pushNextDevice(device);
1224
1225 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1226
Harry Cuttsa5b71292022-11-28 12:56:17 +00001227 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1228 FakeEventHub::BATTERY_STATUS);
1229 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001230}
1231
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001232TEST_F(InputReaderTest, BatteryGetDevicePath) {
1233 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1234 ftl::Flags<InputDeviceClass> deviceClass =
1235 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1236 constexpr int32_t eventHubId = 1;
1237 const char* DEVICE_LOCATION = "BLUETOOTH";
1238 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1239 device->addController<FakePeripheralController>(eventHubId);
1240 mReader->pushNextDevice(device);
1241
1242 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1243
Harry Cuttsa5b71292022-11-28 12:56:17 +00001244 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001245}
1246
Chris Ye3fdbfef2021-01-06 18:45:18 -08001247TEST_F(InputReaderTest, LightGetColor) {
1248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001249 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001250 constexpr int32_t eventHubId = 1;
1251 const char* DEVICE_LOCATION = "BLUETOOTH";
1252 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001253 FakePeripheralController& controller =
1254 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001255 mReader->pushNextDevice(device);
1256 RawLightInfo info = {.id = 1,
1257 .name = "Mono",
1258 .maxBrightness = 255,
1259 .flags = InputLightClass::BRIGHTNESS,
1260 .path = ""};
1261 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1262 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1263
1264 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001265
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1267 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001268 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1269 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1270}
1271
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001272// --- InputReaderIntegrationTest ---
1273
1274// These tests create and interact with the InputReader only through its interface.
1275// The InputReader is started during SetUp(), which starts its processing in its own
1276// thread. The tests use linux uinput to emulate input devices.
1277// NOTE: Interacting with the physical device while these tests are running may cause
1278// the tests to fail.
1279class InputReaderIntegrationTest : public testing::Test {
1280protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001282 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001283 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001284
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001285 std::shared_ptr<FakePointerController> mFakePointerController;
1286
Chris Yea52ade12020-08-27 16:49:20 -07001287 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001288#if !defined(__ANDROID__)
1289 GTEST_SKIP();
1290#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001291 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001292 mFakePointerController = std::make_shared<FakePointerController>();
1293 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001294 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1295 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001296
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001297 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1298 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001299 ASSERT_EQ(mReader->start(), OK);
1300
1301 // Since this test is run on a real device, all the input devices connected
1302 // to the test device will show up in mReader. We wait for those input devices to
1303 // show up before beginning the tests.
1304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1306 }
1307
Chris Yea52ade12020-08-27 16:49:20 -07001308 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001309#if !defined(__ANDROID__)
1310 return;
1311#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001312 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001313 mReader.reset();
1314 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315 mFakePolicy.clear();
1316 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001317
1318 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1319 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1320 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1321 [&name](const InputDeviceInfo& info) {
1322 return info.getIdentifier().name == name;
1323 });
1324 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1325 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001326};
1327
1328TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1329 // An invalid input device that is only used for this test.
1330 class InvalidUinputDevice : public UinputDevice {
1331 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001332 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001333
1334 private:
1335 void configureDevice(int fd, uinput_user_dev* device) override {}
1336 };
1337
1338 const size_t numDevices = mFakePolicy->getInputDevices().size();
1339
1340 // UinputDevice does not set any event or key bits, so InputReader should not
1341 // consider it as a valid device.
1342 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1345 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1346
1347 invalidDevice.reset();
1348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1350 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1351}
1352
1353TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1354 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1355
1356 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1359 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1360
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001361 const auto device = findDeviceByName(keyboard->getName());
1362 ASSERT_TRUE(device.has_value());
1363 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1364 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1365 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366
1367 keyboard.reset();
1368 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1370 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1371}
1372
1373TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1374 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1375 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1376
1377 NotifyConfigurationChangedArgs configChangedArgs;
1378 ASSERT_NO_FATAL_FAILURE(
1379 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001380 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001381 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1382
1383 NotifyKeyArgs keyArgs;
1384 keyboard->pressAndReleaseHomeKey();
1385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001387 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001388 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001389 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001390 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001391 prevTimestamp = keyArgs.eventTime;
1392
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001395 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001397 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001400TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1401 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1402 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1403
1404 const auto device = findDeviceByName(stylus->getName());
1405 ASSERT_TRUE(device.has_value());
1406
Prabir Pradhana3621852022-10-14 18:57:23 +00001407 // An external stylus with buttons should also be recognized as a keyboard.
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001409 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1410 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1411
1412 const auto DOWN =
1413 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1414 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1415
1416 stylus->pressAndReleaseKey(BTN_STYLUS);
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1418 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1420 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1421
1422 stylus->pressAndReleaseKey(BTN_STYLUS2);
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1424 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1426 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1427
1428 stylus->pressAndReleaseKey(BTN_STYLUS3);
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1430 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1432 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1433}
1434
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001435/**
1436 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1437 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1438 * are passed to the listener.
1439 */
1440static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1441TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1442 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 NotifyKeyArgs keyArgs;
1445
1446 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1448 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1449 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1450
1451 controller->pressAndReleaseKey(BTN_GEAR_UP);
1452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1454 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1455}
1456
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001457// --- TouchIntegrationTest ---
1458
Arthur Hungaab25622020-01-16 11:22:11 +08001459class TouchIntegrationTest : public InputReaderIntegrationTest {
1460protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001461 const std::string UNIQUE_ID = "local:0";
1462
Chris Yea52ade12020-08-27 16:49:20 -07001463 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001464#if !defined(__ANDROID__)
1465 GTEST_SKIP();
1466#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001467 InputReaderIntegrationTest::SetUp();
1468 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001469 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1470 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001471
1472 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1473 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001475 const auto info = findDeviceByName(mDevice->getName());
1476 ASSERT_TRUE(info);
1477 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001478 }
1479
1480 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001481 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001482 std::optional<uint8_t> physicalPort,
1483 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001484 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1485 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001486 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1487 }
1488
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001489 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1490 NotifyMotionArgs args;
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1492 EXPECT_EQ(action, args.action);
1493 ASSERT_EQ(points.size(), args.pointerCount);
1494 for (size_t i = 0; i < args.pointerCount; i++) {
1495 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1496 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1497 }
1498 }
1499
Arthur Hungaab25622020-01-16 11:22:11 +08001500 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001501 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001502};
1503
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001504TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1505 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1506 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1507 // presses).
1508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1509 mDeviceInfo.getSources());
1510}
1511
Arthur Hungaab25622020-01-16 11:22:11 +08001512TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1513 NotifyMotionArgs args;
1514 const Point centerPoint = mDevice->getCenterPoint();
1515
1516 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001517 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001518 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001519 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001520 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1522
1523 // ACTION_MOVE
1524 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1528
1529 // ACTION_UP
1530 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001531 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1533 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1534}
1535
1536TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1537 NotifyMotionArgs args;
1538 const Point centerPoint = mDevice->getCenterPoint();
1539
1540 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001541 mDevice->sendSlot(FIRST_SLOT);
1542 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001543 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001544 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1547
1548 // ACTION_POINTER_DOWN (Second slot)
1549 const Point secondPoint = centerPoint + Point(100, 100);
1550 mDevice->sendSlot(SECOND_SLOT);
1551 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001552 mDevice->sendDown(secondPoint);
1553 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001555 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001556
1557 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001558 mDevice->sendMove(secondPoint + Point(1, 1));
1559 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1562
1563 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001564 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001565 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001566 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001567 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001568
1569 // ACTION_UP
1570 mDevice->sendSlot(FIRST_SLOT);
1571 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001572 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1575}
1576
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001577/**
1578 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1579 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1580 * data?
1581 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1582 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1583 * for Pointer 0 only is generated after.
1584 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1585 * events, we will not miss any information.
1586 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1587 * event generated afterwards that contains the newest movement of pointer 0.
1588 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1589 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1590 * losing information about non-palm pointers.
1591 */
1592TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1593 NotifyMotionArgs args;
1594 const Point centerPoint = mDevice->getCenterPoint();
1595
1596 // ACTION_DOWN
1597 mDevice->sendSlot(FIRST_SLOT);
1598 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1599 mDevice->sendDown(centerPoint);
1600 mDevice->sendSync();
1601 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1602
1603 // ACTION_POINTER_DOWN (Second slot)
1604 const Point secondPoint = centerPoint + Point(100, 100);
1605 mDevice->sendSlot(SECOND_SLOT);
1606 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1607 mDevice->sendDown(secondPoint);
1608 mDevice->sendSync();
1609 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1610
1611 // ACTION_MOVE (First slot)
1612 mDevice->sendSlot(FIRST_SLOT);
1613 mDevice->sendMove(centerPoint + Point(5, 5));
1614 // ACTION_POINTER_UP (Second slot)
1615 mDevice->sendSlot(SECOND_SLOT);
1616 mDevice->sendPointerUp();
1617 // Send a single sync for the above 2 pointer updates
1618 mDevice->sendSync();
1619
1620 // First, we should get POINTER_UP for the second pointer
1621 assertReceivedMotion(ACTION_POINTER_1_UP,
1622 {/*first pointer */ centerPoint + Point(5, 5),
1623 /*second pointer*/ secondPoint});
1624
1625 // Next, the MOVE event for the first pointer
1626 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1627}
1628
1629/**
1630 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1631 * move, and then it will go up, all in the same frame.
1632 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1633 * gets sent to the listener.
1634 */
1635TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1636 NotifyMotionArgs args;
1637 const Point centerPoint = mDevice->getCenterPoint();
1638
1639 // ACTION_DOWN
1640 mDevice->sendSlot(FIRST_SLOT);
1641 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1642 mDevice->sendDown(centerPoint);
1643 mDevice->sendSync();
1644 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1645
1646 // ACTION_POINTER_DOWN (Second slot)
1647 const Point secondPoint = centerPoint + Point(100, 100);
1648 mDevice->sendSlot(SECOND_SLOT);
1649 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1650 mDevice->sendDown(secondPoint);
1651 mDevice->sendSync();
1652 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1653
1654 // ACTION_MOVE (First slot)
1655 mDevice->sendSlot(FIRST_SLOT);
1656 mDevice->sendMove(centerPoint + Point(5, 5));
1657 // ACTION_POINTER_UP (Second slot)
1658 mDevice->sendSlot(SECOND_SLOT);
1659 mDevice->sendMove(secondPoint + Point(6, 6));
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 // The movement of the second pointer during the liftoff frame is ignored.
1666 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1667 assertReceivedMotion(ACTION_POINTER_1_UP,
1668 {/*first pointer */ centerPoint + Point(5, 5),
1669 /*second pointer*/ secondPoint});
1670
1671 // Next, the MOVE event for the first pointer
1672 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1673}
1674
Arthur Hungaab25622020-01-16 11:22:11 +08001675TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1676 NotifyMotionArgs args;
1677 const Point centerPoint = mDevice->getCenterPoint();
1678
1679 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001680 mDevice->sendSlot(FIRST_SLOT);
1681 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001682 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1686
arthurhungcc7f9802020-04-30 17:55:40 +08001687 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001688 const Point secondPoint = centerPoint + Point(100, 100);
1689 mDevice->sendSlot(SECOND_SLOT);
1690 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1691 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001693 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001694 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001695
arthurhungcc7f9802020-04-30 17:55:40 +08001696 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001697 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1701
arthurhungcc7f9802020-04-30 17:55:40 +08001702 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1703 // a palm event.
1704 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001705 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001706 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001707 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001708 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001709 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001710
arthurhungcc7f9802020-04-30 17:55:40 +08001711 // Send up to second slot, expect first slot send moving.
1712 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001713 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001714 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001716
arthurhungcc7f9802020-04-30 17:55:40 +08001717 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001718 mDevice->sendSlot(FIRST_SLOT);
1719 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721
arthurhungcc7f9802020-04-30 17:55:40 +08001722 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001724}
1725
Prabir Pradhanda20b172022-09-26 17:01:18 +00001726TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1727 const Point centerPoint = mDevice->getCenterPoint();
1728
1729 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1730 mDevice->sendSlot(FIRST_SLOT);
1731 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1732 mDevice->sendToolType(MT_TOOL_PEN);
1733 mDevice->sendDown(centerPoint);
1734 mDevice->sendSync();
1735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1737 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1738
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1740
1741 // Release the stylus touch.
1742 mDevice->sendUp();
1743 mDevice->sendSync();
1744 ASSERT_NO_FATAL_FAILURE(
1745 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1746
1747 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1748
1749 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1750 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1751 mDevice->sendToolType(MT_TOOL_FINGER);
1752 mDevice->sendDown(centerPoint);
1753 mDevice->sendSync();
1754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1756 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1757
1758 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1759
1760 mDevice->sendUp();
1761 mDevice->sendSync();
1762 ASSERT_NO_FATAL_FAILURE(
1763 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1764
1765 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1766 // The policy should be notified of the stylus presence.
1767 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1768 mDevice->sendToolType(MT_TOOL_PEN);
1769 mDevice->sendMove(centerPoint);
1770 mDevice->sendSync();
1771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1773 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1774
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1776}
1777
Prabir Pradhan124ea442022-10-28 20:27:44 +00001778// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001779
Prabir Pradhan124ea442022-10-28 20:27:44 +00001780// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1781// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1782// stylus.
1783template <typename UinputStylusDevice>
1784class StylusButtonIntegrationTest : public TouchIntegrationTest {
1785protected:
1786 void SetUp() override {
1787#if !defined(__ANDROID__)
1788 GTEST_SKIP();
1789#endif
1790 TouchIntegrationTest::SetUp();
1791 mTouchscreen = mDevice.get();
1792 mTouchscreenInfo = mDeviceInfo;
1793
1794 setUpStylusDevice();
1795 }
1796
1797 UinputStylusDevice* mStylus{nullptr};
1798 InputDeviceInfo mStylusInfo{};
1799
1800 UinputTouchScreen* mTouchscreen{nullptr};
1801 InputDeviceInfo mTouchscreenInfo{};
1802
1803private:
1804 // When we are attempting to test stylus button events that are sent from the touchscreen,
1805 // use the same Uinput device for the touchscreen and the stylus.
1806 template <typename T = UinputStylusDevice>
1807 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1808 mStylus = mDevice.get();
1809 mStylusInfo = mDeviceInfo;
1810 }
1811
1812 // When we are attempting to stylus buttons from an external stylus being merged with touches
1813 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1814 template <typename T = UinputStylusDevice>
1815 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1816 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1817 mStylus = mStylusDeviceLifecycleTracker.get();
1818 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1820 const auto info = findDeviceByName(mStylus->getName());
1821 ASSERT_TRUE(info);
1822 mStylusInfo = *info;
1823 }
1824
1825 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1826
1827 // Hide the base class's device to expose it with a different name for readability.
1828 using TouchIntegrationTest::mDevice;
1829 using TouchIntegrationTest::mDeviceInfo;
1830};
1831
1832using StylusButtonIntegrationTestTypes =
1833 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1834TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1835
1836TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1837 const auto stylusId = TestFixture::mStylusInfo.getId();
1838
1839 TestFixture::mStylus->pressKey(BTN_STYLUS);
1840 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1841 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1842 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1843
1844 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1845 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001846 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001847 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001848}
1849
Prabir Pradhan124ea442022-10-28 20:27:44 +00001850TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1851 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1852 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1853 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001854
1855 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001856 TestFixture::mStylus->pressKey(BTN_STYLUS);
1857 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001858 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001860
1861 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001862 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1863 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1864 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1865 TestFixture::mTouchscreen->sendDown(centerPoint);
1866 TestFixture::mTouchscreen->sendSync();
1867 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001868 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1869 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001870 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1871 WithDeviceId(touchscreenId))));
1872 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1874 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1876 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001877
Prabir Pradhan124ea442022-10-28 20:27:44 +00001878 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1879 TestFixture::mTouchscreen->sendSync();
1880 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001882 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1883 WithDeviceId(touchscreenId))));
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001886 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1887 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001888
1889 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1891 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001893 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001894}
1895
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001896TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
1897 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1898 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1899 const auto stylusId = TestFixture::mStylusInfo.getId();
1900 auto toolTypeDevice =
1901 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1902
1903 // Press the stylus button.
1904 TestFixture::mStylus->pressKey(BTN_STYLUS);
1905 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1906 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1907 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1908
1909 // Start hovering with the stylus.
1910 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1911 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1912 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1913 TestFixture::mTouchscreen->sendMove(centerPoint);
1914 TestFixture::mTouchscreen->sendSync();
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1916 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1918 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1919 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1920 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1921 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1922 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1923 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1924
1925 // Touch down with the stylus.
1926 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1927 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1928 TestFixture::mTouchscreen->sendDown(centerPoint);
1929 TestFixture::mTouchscreen->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1931 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1933
1934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1935 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1936 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1937
1938 // Stop touching with the stylus, and start hovering.
1939 TestFixture::mTouchscreen->sendUp();
1940 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1941 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1942 TestFixture::mTouchscreen->sendMove(centerPoint);
1943 TestFixture::mTouchscreen->sendSync();
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1945 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1948 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1949 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1950 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1951 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1952 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1953
1954 // Stop hovering.
1955 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1958 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1959 WithButtonState(0))));
1960 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1961 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1962 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1963 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1964
1965 // Release the stylus button.
1966 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1970}
1971
Prabir Pradhan124ea442022-10-28 20:27:44 +00001972TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
1973 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1974 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1975 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001976
1977 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001978 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1979 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1980 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1981 TestFixture::mTouchscreen->sendDown(centerPoint);
1982 TestFixture::mTouchscreen->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001984 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001985 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1986 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001987
1988 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 TestFixture::mStylus->pressKey(BTN_STYLUS);
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001991 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1993 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1995 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1997 WithDeviceId(touchscreenId))));
1998 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2000 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2002 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2008 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002010 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2011 WithDeviceId(touchscreenId))));
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002014 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2015 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002016
2017 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2019 TestFixture::mTouchscreen->sendSync();
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2023 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002024}
2025
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002026// --- ExternalStylusIntegrationTest ---
2027
2028// Verify the behavior of an external stylus. An external stylus can report pressure or button
2029// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2030// ongoing stylus gesture that is being emitted by the touchscreen.
2031using ExternalStylusIntegrationTest = TouchIntegrationTest;
2032
2033TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2034 const Point centerPoint = mDevice->getCenterPoint();
2035
2036 // Create an external stylus capable of reporting pressure data that
2037 // should be fused with a touch pointer.
2038 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2039 createUinputDevice<UinputExternalStylusWithPressure>();
2040 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2041 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2042 const auto stylusInfo = findDeviceByName(stylus->getName());
2043 ASSERT_TRUE(stylusInfo);
2044
2045 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2046
2047 const auto touchscreenId = mDeviceInfo.getId();
2048
2049 // Set a pressure value on the stylus. It doesn't generate any events.
2050 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2051 stylus->setPressure(100);
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2053
2054 // Start a finger gesture, and ensure it shows up as stylus gesture
2055 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002056 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002057 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002058 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002059 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002063 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2064 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002065
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002066 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2067 // event with the updated pressure.
2068 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002069 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002071 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2072 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002073
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002074 // The external stylus did not generate any events.
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2076 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2077}
2078
2079TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2080 const Point centerPoint = mDevice->getCenterPoint();
2081
2082 // Create an external stylus capable of reporting pressure data that
2083 // should be fused with a touch pointer.
2084 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2085 createUinputDevice<UinputExternalStylusWithPressure>();
2086 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2087 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2088 const auto stylusInfo = findDeviceByName(stylus->getName());
2089 ASSERT_TRUE(stylusInfo);
2090
2091 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2092
2093 const auto touchscreenId = mDeviceInfo.getId();
2094
2095 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2096 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002097 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2098 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002099 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002100 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002101
2102 // Start a finger gesture. The touch device will withhold generating any touches for
2103 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2104 mDevice->sendSlot(FIRST_SLOT);
2105 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2106 mDevice->sendToolType(MT_TOOL_FINGER);
2107 mDevice->sendDown(centerPoint);
2108 auto waitUntil = std::chrono::system_clock::now() +
2109 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002110 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 // Since the external stylus did not report a pressure value within the timeout,
2114 // it shows up as a finger pointer.
2115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2117 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2118 WithPressure(1.f))));
2119
2120 // Change the pressure on the external stylus. Since the pressure was not present at the start
2121 // of the gesture, it is ignored for now.
2122 stylus->setPressure(200);
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2124
2125 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2127 mDevice->sendSync();
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002130 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2131
2132 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2133 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2134 mDevice->sendToolType(MT_TOOL_FINGER);
2135 mDevice->sendDown(centerPoint);
2136 mDevice->sendSync();
2137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2139 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2140 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2141
2142 // The external stylus did not generate any events.
2143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002146
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2148 const Point centerPoint = mDevice->getCenterPoint();
2149
2150 // Create an external stylus device that does not support pressure. It should not affect any
2151 // touch pointers.
2152 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2153 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2155 const auto stylusInfo = findDeviceByName(stylus->getName());
2156 ASSERT_TRUE(stylusInfo);
2157
2158 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2159
2160 const auto touchscreenId = mDeviceInfo.getId();
2161
2162 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2163 // pressure data from the external stylus.
2164 mDevice->sendSlot(FIRST_SLOT);
2165 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2166 mDevice->sendToolType(MT_TOOL_FINGER);
2167 mDevice->sendDown(centerPoint);
2168 auto waitUntil = std::chrono::system_clock::now() +
2169 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2170 mDevice->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(
2172 mTestListener
2173 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2174 WithToolType(
2175 AMOTION_EVENT_TOOL_TYPE_FINGER),
2176 WithButtonState(0),
2177 WithDeviceId(touchscreenId),
2178 WithPressure(1.f)),
2179 waitUntil));
2180
2181 // The external stylus did not generate any events.
2182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2184}
2185
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186// --- InputDeviceTest ---
2187class InputDeviceTest : public testing::Test {
2188protected:
2189 static const char* DEVICE_NAME;
2190 static const char* DEVICE_LOCATION;
2191 static const int32_t DEVICE_ID;
2192 static const int32_t DEVICE_GENERATION;
2193 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002194 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195 static const int32_t EVENTHUB_ID;
2196 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2197
2198 std::shared_ptr<FakeEventHub> mFakeEventHub;
2199 sp<FakeInputReaderPolicy> mFakePolicy;
2200 std::unique_ptr<TestInputListener> mFakeListener;
2201 std::unique_ptr<InstrumentedInputReader> mReader;
2202 std::shared_ptr<InputDevice> mDevice;
2203
2204 void SetUp() override {
2205 mFakeEventHub = std::make_unique<FakeEventHub>();
2206 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2207 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002208 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002210 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 identifier.name = DEVICE_NAME;
2212 identifier.location = DEVICE_LOCATION;
2213 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2214 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2215 identifier);
2216 mReader->pushNextDevice(mDevice);
2217 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002218 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220
2221 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002222 mFakeListener.reset();
2223 mFakePolicy.clear();
2224 }
2225};
2226
2227const char* InputDeviceTest::DEVICE_NAME = "device";
2228const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2229const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2230const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002232const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2233 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002234const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002237TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002239 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2240 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002242
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002244 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2245
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246 // Configuration
2247 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002248 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002249 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2250
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2252}
2253
2254TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2255 ASSERT_EQ(mDevice->isEnabled(), false);
2256}
2257
2258TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2259 // Configuration.
2260 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002261 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002263 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 unused += mDevice->reset(ARBITRARY_TIME);
2265
2266 NotifyDeviceResetArgs resetArgs;
2267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2268 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2269 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2270
2271 // Metadata.
2272 ASSERT_TRUE(mDevice->isIgnored());
2273 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2274
2275 InputDeviceInfo info = mDevice->getDeviceInfo();
2276 ASSERT_EQ(DEVICE_ID, info.getId());
2277 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2278 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2279 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2280
2281 // State queries.
2282 ASSERT_EQ(0, mDevice->getMetaState());
2283
2284 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002285 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002287 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2289 << "Ignored device should return unknown switch state.";
2290
2291 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2292 uint8_t flags[2] = { 0, 1 };
2293 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002294 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2296 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2297}
2298
2299TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2300 // Configuration.
2301 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2302
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002303 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002304 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002306 mapper1.setMetaState(AMETA_ALT_ON);
2307 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002308 mapper1.addSupportedKeyCode(AKEYCODE_B);
2309 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002310 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2311 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2312 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2313 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
Arthur Hung2c9a3342019-07-23 14:18:59 +08002315 FakeInputMapper& mapper2 =
2316 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002317 mapper2.setMetaState(AMETA_SHIFT_ON);
2318
Arthur Hung2c9a3342019-07-23 14:18:59 +08002319 InputReaderConfiguration config;
2320 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2321
2322 std::string propertyValue;
2323 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2324 << "Device should have read configuration during configuration phase.";
2325 ASSERT_EQ("value", propertyValue);
2326
2327 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002328 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2329
Arthur Hung2c9a3342019-07-23 14:18:59 +08002330 // Reset
2331 unused += mDevice->reset(ARBITRARY_TIME);
2332 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2333 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2334
2335 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2337 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002338 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2339
Arthur Hung2c9a3342019-07-23 14:18:59 +08002340 // Metadata.
2341 ASSERT_FALSE(mDevice->isIgnored());
2342 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2343
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002344 InputDeviceInfo info = mDevice->getDeviceInfo();
2345 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002346 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2347 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2348 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002349
2350 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002351 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2352 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Christine Franks1ba71cc2021-04-07 14:37:42 -07002354 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2355 << "Should return unknown key code state when source not supported.";
2356 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2357 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002358 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2359 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002360
2361 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2362 << "Should query mapper when source is supported.";
2363 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002364 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002365 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2366 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002367
2368 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2369 uint8_t flags[4] = { 0, 0, 0, 1 };
2370 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2371 << "Should do nothing when source is unsupported.";
2372 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002373 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2374 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002375 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2376
2377 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2378 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002379 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2380 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002381 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2382 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002384 // Event handling.
2385 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002386 event.deviceId = EVENTHUB_ID;
2387 unused += mDevice->process(&event, 1);
2388
Christine Franks2a2293c2022-01-18 11:51:16 -08002389 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2390 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2391}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002392
2393// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002394// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002395// 2. Device is disabled when setEnabled API is called
2396TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2397 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2398
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002399 // First Configuration.
2400 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002401 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2402
2403 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002404 ASSERT_TRUE(mDevice->isEnabled());
2405
2406 // Prepare associated info.
2407 constexpr uint8_t hdmi = 1;
2408 const std::string UNIQUE_ID = "local:1";
2409
2410 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2411 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2412 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2413 // Device should be disabled because it is associated with a specific display via
2414 // input port <-> display port association, but the corresponding display is not found
2415 ASSERT_FALSE(mDevice->isEnabled());
2416
2417 // Prepare displays.
2418 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002419 ui::ROTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 ViewportType::INTERNAL);
2421 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2422 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2423 ASSERT_TRUE(mDevice->isEnabled());
2424
2425 // Device should be disabled after set disable.
2426 mFakePolicy->addDisabledDevice(mDevice->getId());
2427 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2428 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2429 ASSERT_FALSE(mDevice->isEnabled());
2430
2431 // Device should still be disabled even found the associated display.
2432 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2433 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2434 ASSERT_FALSE(mDevice->isEnabled());
2435}
2436
2437TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2438 // Device should be enabled by default.
2439 mFakePolicy->clearViewports();
2440 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2441 std::list<NotifyArgs> unused =
2442 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2443 ASSERT_TRUE(mDevice->isEnabled());
2444
2445 // Device should be disabled because it is associated with a specific display, but the
2446 // corresponding display is not found.
2447 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2448 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2450 ASSERT_FALSE(mDevice->isEnabled());
2451
2452 // Device should be enabled when a display is found.
2453 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002454 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 NO_PORT, ViewportType::INTERNAL);
2456 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2457 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2458 ASSERT_TRUE(mDevice->isEnabled());
2459
2460 // Device should be disabled after set disable.
2461 mFakePolicy->addDisabledDevice(mDevice->getId());
2462 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2463 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2464 ASSERT_FALSE(mDevice->isEnabled());
2465
2466 // Device should still be disabled even found the associated display.
2467 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2468 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2469 ASSERT_FALSE(mDevice->isEnabled());
2470}
2471
2472TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2473 mFakePolicy->clearViewports();
2474 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2475 std::list<NotifyArgs> unused =
2476 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002477
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2479 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002480 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Chris Ye1b0c7342020-07-28 21:57:03 -07002481 NO_PORT, ViewportType::INTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002484 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485}
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002486
arthurhungdcef2dc2020-08-11 14:47:50 +08002487/**
2488 * This test reproduces a crash caused by a dangling reference that remains after device is added
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 * and removed. The reference is accessed in InputDevice::dump(..);
Chris Ye1b0c7342020-07-28 21:57:03 -07002490 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002491TEST_F(InputDeviceTest, DumpDoesNotCrash) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 constexpr int32_t TEST_EVENTHUB_ID = 10;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002493 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
arthurhungdcef2dc2020-08-11 14:47:50 +08002494
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002495 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002496 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2498 std::string dumpStr, eventHubDevStr;
2499 device.dump(dumpStr, eventHubDevStr);
2500}
2501
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002502TEST_F(InputDeviceTest, GetBluetoothAddress) {
2503 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2504 ASSERT_TRUE(address);
2505 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2506}
2507
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508// --- SwitchInputMapperTest ---
2509
2510class SwitchInputMapperTest : public InputMapperTest {
2511protected:
2512};
2513
2514TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002517 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518}
2519
2520TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002523 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002526 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002527 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528}
2529
2530TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002531 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002532 std::list<NotifyArgs> out;
2533 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2534 ASSERT_TRUE(out.empty());
2535 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2536 ASSERT_TRUE(out.empty());
2537 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2538 ASSERT_TRUE(out.empty());
2539 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002541 ASSERT_EQ(1u, out.size());
2542 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002544 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2545 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 args.switchMask);
2547 ASSERT_EQ(uint32_t(0), args.policyFlags);
2548}
2549
Chris Ye87143712020-11-10 05:05:58 +00002550// --- VibratorInputMapperTest ---
2551class VibratorInputMapperTest : public InputMapperTest {
2552protected:
2553 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2554};
2555
2556TEST_F(VibratorInputMapperTest, GetSources) {
2557 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2558
2559 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2560}
2561
2562TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2563 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2564
2565 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2566}
2567
2568TEST_F(VibratorInputMapperTest, Vibrate) {
2569 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002570 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002571 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2572
2573 VibrationElement pattern(2);
2574 VibrationSequence sequence(2);
2575 pattern.duration = std::chrono::milliseconds(200);
2576 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2577 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2578 sequence.addElement(pattern);
2579 pattern.duration = std::chrono::milliseconds(500);
2580 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2581 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2582 sequence.addElement(pattern);
2583
2584 std::vector<int64_t> timings = {0, 1};
2585 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2586
2587 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002588 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002589 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002590 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002591 // Verify vibrator state listener was notified.
2592 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002593 ASSERT_EQ(1u, out.size());
2594 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2595 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2596 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002597 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002598 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002599 ASSERT_FALSE(mapper.isVibrating());
2600 // Verify vibrator state listener was notified.
2601 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002602 ASSERT_EQ(1u, out.size());
2603 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2604 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2605 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002606}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Chris Yef59a2f42020-10-16 12:55:26 -07002608// --- SensorInputMapperTest ---
2609
2610class SensorInputMapperTest : public InputMapperTest {
2611protected:
2612 static const int32_t ACCEL_RAW_MIN;
2613 static const int32_t ACCEL_RAW_MAX;
2614 static const int32_t ACCEL_RAW_FUZZ;
2615 static const int32_t ACCEL_RAW_FLAT;
2616 static const int32_t ACCEL_RAW_RESOLUTION;
2617
2618 static const int32_t GYRO_RAW_MIN;
2619 static const int32_t GYRO_RAW_MAX;
2620 static const int32_t GYRO_RAW_FUZZ;
2621 static const int32_t GYRO_RAW_FLAT;
2622 static const int32_t GYRO_RAW_RESOLUTION;
2623
2624 static const float GRAVITY_MS2_UNIT;
2625 static const float DEGREE_RADIAN_UNIT;
2626
2627 void prepareAccelAxes();
2628 void prepareGyroAxes();
2629 void setAccelProperties();
2630 void setGyroProperties();
2631 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2632};
2633
2634const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2635const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2636const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2637const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2638const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2639
2640const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2641const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2642const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2643const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2644const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2645
2646const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2647const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2648
2649void SensorInputMapperTest::prepareAccelAxes() {
2650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2651 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2653 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2655 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2656}
2657
2658void SensorInputMapperTest::prepareGyroAxes() {
2659 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2660 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2661 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2662 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2663 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2664 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2665}
2666
2667void SensorInputMapperTest::setAccelProperties() {
2668 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2669 /* sensorDataIndex */ 0);
2670 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2671 /* sensorDataIndex */ 1);
2672 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2673 /* sensorDataIndex */ 2);
2674 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2675 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2676 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2677 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2678 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2679}
2680
2681void SensorInputMapperTest::setGyroProperties() {
2682 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2683 /* sensorDataIndex */ 0);
2684 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2685 /* sensorDataIndex */ 1);
2686 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2687 /* sensorDataIndex */ 2);
2688 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2689 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2690 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2691 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2692 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2693}
2694
2695TEST_F(SensorInputMapperTest, GetSources) {
2696 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2697
2698 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2699}
2700
2701TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2702 setAccelProperties();
2703 prepareAccelAxes();
2704 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2705
2706 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2707 std::chrono::microseconds(10000),
2708 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002709 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002715
2716 NotifySensorArgs args;
2717 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2718 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2719 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2720
2721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2722 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2723 ASSERT_EQ(args.deviceId, DEVICE_ID);
2724 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2725 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2726 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2727 ASSERT_EQ(args.values, values);
2728 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2729}
2730
2731TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2732 setGyroProperties();
2733 prepareGyroAxes();
2734 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2735
2736 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2737 std::chrono::microseconds(10000),
2738 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002739 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002745
2746 NotifySensorArgs args;
2747 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2748 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2749 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2750
2751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2752 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2753 ASSERT_EQ(args.deviceId, DEVICE_ID);
2754 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2755 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2756 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2757 ASSERT_EQ(args.values, values);
2758 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761// --- KeyboardInputMapperTest ---
2762
2763class KeyboardInputMapperTest : public InputMapperTest {
2764protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002765 const std::string UNIQUE_ID = "local:0";
2766
Michael Wrighta9cf4192022-12-01 23:46:39 +00002767 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002770 int32_t originalKeyCode, int32_t rotatedKeyCode,
2771 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772};
2773
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2775 * orientation.
2776 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002777void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002778 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2779 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002780}
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002783 int32_t originalScanCode, int32_t originalKeyCode,
2784 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 NotifyKeyArgs args;
2786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2789 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2790 ASSERT_EQ(originalScanCode, args.scanCode);
2791 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002792 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2796 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2797 ASSERT_EQ(originalScanCode, args.scanCode);
2798 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002799 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800}
2801
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 KeyboardInputMapper& mapper =
2804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808}
2809
2810TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2811 const int32_t USAGE_A = 0x070004;
2812 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2814 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002815 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2816 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2817 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 KeyboardInputMapper& mapper =
2820 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2821 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002822 // Initial metastate is AMETA_NONE.
2823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 NotifyKeyArgs args;
2828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2829 ASSERT_EQ(DEVICE_ID, args.deviceId);
2830 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2831 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2833 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2834 ASSERT_EQ(KEY_HOME, args.scanCode);
2835 ASSERT_EQ(AMETA_NONE, args.metaState);
2836 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2837 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2838 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2839
2840 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002841 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2843 ASSERT_EQ(DEVICE_ID, args.deviceId);
2844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2845 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2847 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2848 ASSERT_EQ(KEY_HOME, args.scanCode);
2849 ASSERT_EQ(AMETA_NONE, args.metaState);
2850 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2853
2854 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2858 ASSERT_EQ(DEVICE_ID, args.deviceId);
2859 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2860 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2861 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2862 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2863 ASSERT_EQ(0, args.scanCode);
2864 ASSERT_EQ(AMETA_NONE, args.metaState);
2865 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2866 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2867 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2868
2869 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2871 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2873 ASSERT_EQ(DEVICE_ID, args.deviceId);
2874 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2875 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2877 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2878 ASSERT_EQ(0, args.scanCode);
2879 ASSERT_EQ(AMETA_NONE, args.metaState);
2880 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2881 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2882 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2883
2884 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2888 ASSERT_EQ(DEVICE_ID, args.deviceId);
2889 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2890 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2892 ASSERT_EQ(0, args.keyCode);
2893 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2894 ASSERT_EQ(AMETA_NONE, args.metaState);
2895 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2896 ASSERT_EQ(0U, args.policyFlags);
2897 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2898
2899 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2901 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2906 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2907 ASSERT_EQ(0, args.keyCode);
2908 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2909 ASSERT_EQ(AMETA_NONE, args.metaState);
2910 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2911 ASSERT_EQ(0U, args.policyFlags);
2912 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2913}
2914
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002915TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2918 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2919
2920 KeyboardInputMapper& mapper =
2921 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2922 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2923
2924 // Key down by scan code.
2925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2926 NotifyKeyArgs args;
2927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2928 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2929
2930 // Key up by scan code.
2931 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2933 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2934}
2935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936/**
2937 * Ensure that the readTime is set to the time when the EV_KEY is received.
2938 */
2939TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2941
2942 KeyboardInputMapper& mapper =
2943 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2944 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2945 NotifyKeyArgs args;
2946
2947 // Key down
2948 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
2949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(12, args.readTime);
2951
2952 // Key up
2953 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
2954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2955 ASSERT_EQ(15, args.readTime);
2956}
2957
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002961 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 KeyboardInputMapper& mapper =
2966 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968
Arthur Hung95f68612022-04-07 14:08:22 +08002969 // Initial metastate is AMETA_NONE.
2970 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
2972 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 NotifyKeyArgs args;
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979
2980 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002987 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
2992 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
3000TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
Michael Wrighta9cf4192022-12-01 23:46:39 +00003010 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3012 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3014 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3015 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3016 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3017 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3018 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3019}
3020
3021TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Michael Wrighta9cf4192022-12-01 23:46:39 +00003032 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003033 ASSERT_NO_FATAL_FAILURE(
3034 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3036 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3037 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3038 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3039 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3040 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003043 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 ASSERT_NO_FATAL_FAILURE(
3045 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3046 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3047 AKEYCODE_DPAD_UP, DISPLAY_ID));
3048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3049 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3051 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003053 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003054 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003055 ASSERT_NO_FATAL_FAILURE(
3056 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3058 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3060 AKEYCODE_DPAD_UP, DISPLAY_ID));
3061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3062 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003065 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 ASSERT_NO_FATAL_FAILURE(
3067 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3068 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3069 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3070 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3071 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3073 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Special case: if orientation changes while key is down, we still emit the same keycode
3076 // in the key up as we did in the key down.
3077 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_EQ(KEY_UP, args.scanCode);
3084 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003086 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003087 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3091 ASSERT_EQ(KEY_UP, args.scanCode);
3092 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3093}
3094
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003095TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3096 // If the keyboard is not orientation aware,
3097 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003103 NotifyKeyArgs args;
3104
3105 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3111
Michael Wrighta9cf4192022-12-01 23:46:39 +00003112 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3118}
3119
3120TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3121 // If the keyboard is orientation aware,
3122 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003124
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003125 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003129 NotifyKeyArgs args;
3130
3131 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3132 // ^--- already checked by the previous test
3133
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003135 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DISPLAY_ID, args.displayId);
3141
3142 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003143 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003144 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003145 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(newDisplayId, args.displayId);
3151}
3152
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 KeyboardInputMapper& mapper =
3155 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003158 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003161 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163}
3164
Philip Junker4af3b3d2021-12-14 10:36:55 +01003165TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3166 KeyboardInputMapper& mapper =
3167 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3168 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3169
3170 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3171 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3172 << "If a mapping is available, the result is equal to the mapping";
3173
3174 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3175 << "If no mapping is available, the result is the key location";
3176}
3177
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 KeyboardInputMapper& mapper =
3180 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3181 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003183 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003184 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188}
3189
3190TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003198 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_TRUE(flags[0]);
3200 ASSERT_FALSE(flags[1]);
3201}
3202
3203TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003204 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3205 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3206 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 KeyboardInputMapper& mapper =
3212 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3213 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003214 // Initial metastate is AMETA_NONE.
3215 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216
3217 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003218 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3219 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3220 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
3222 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003225 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3226 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3227 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229
3230 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003233 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3234 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3235 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
3238 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3242 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3243 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003244 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
3246 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3250 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3251 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3258 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3259 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
3262 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3266 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3267 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269}
3270
Chris Yea52ade12020-08-27 16:49:20 -07003271TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3272 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3274 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3276
3277 KeyboardInputMapper& mapper =
3278 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3279 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3280
Chris Yea52ade12020-08-27 16:49:20 -07003281 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003282 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003283 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3284 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3285 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3286 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3287
3288 NotifyKeyArgs args;
3289 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AMETA_NONE, args.metaState);
3293 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3294 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3295 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3296
3297 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_NONE, args.metaState);
3301 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3303 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3304}
3305
Arthur Hung2c9a3342019-07-23 14:18:59 +08003306TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3307 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003312
3313 // keyboard 2.
3314 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003315 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003316 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003318 std::shared_ptr<InputDevice> device2 =
3319 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003320 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003321
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003322 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3323 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3324 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3325 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 KeyboardInputMapper& mapper =
3328 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3329 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003334 std::list<NotifyArgs> unused =
3335 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3336 0 /*changes*/);
3337 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003338
3339 // Prepared displays and associated info.
3340 constexpr uint8_t hdmi1 = 0;
3341 constexpr uint8_t hdmi2 = 1;
3342 const std::string SECONDARY_UNIQUE_ID = "local:1";
3343
3344 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3345 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3346
3347 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003348 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_FALSE(device2->isEnabled());
3351
3352 // Prepare second display.
3353 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003354 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003355 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003356 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003357 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003358 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003359 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3360 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361
3362 // Device should be enabled after the associated display is found.
3363 ASSERT_TRUE(mDevice->isEnabled());
3364 ASSERT_TRUE(device2->isEnabled());
3365
3366 // Test pad key events
3367 ASSERT_NO_FATAL_FAILURE(
3368 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3370 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3372 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3374 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3375
3376 ASSERT_NO_FATAL_FAILURE(
3377 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3379 AKEYCODE_DPAD_RIGHT, newDisplayId));
3380 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3381 AKEYCODE_DPAD_DOWN, newDisplayId));
3382 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3383 AKEYCODE_DPAD_LEFT, newDisplayId));
3384}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
arthurhungc903df12020-08-11 15:08:42 +08003386TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3387 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3388 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3389 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3393
3394 KeyboardInputMapper& mapper =
3395 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003397 // Initial metastate is AMETA_NONE.
3398 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003399
3400 // Initialization should have turned all of the lights off.
3401 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3402 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3403 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3404
3405 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003408 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3409 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3410
3411 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003414 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3415 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3416
3417 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003420 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3421 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3422
3423 mFakeEventHub->removeDevice(EVENTHUB_ID);
3424 mReader->loopOnce();
3425
3426 // keyboard 2 should default toggle keys.
3427 const std::string USB2 = "USB2";
3428 const std::string DEVICE_NAME2 = "KEYBOARD2";
3429 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3430 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3431 std::shared_ptr<InputDevice> device2 =
3432 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003433 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003434 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3435 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3436 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3437 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3438 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3439 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3440
arthurhung6fe95782020-10-05 22:41:16 +08003441 KeyboardInputMapper& mapper2 =
3442 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003444 std::list<NotifyArgs> unused =
3445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3446 0 /*changes*/);
3447 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003448
3449 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3450 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3451 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003452 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3453 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003454}
3455
Arthur Hungcb40a002021-08-03 14:31:01 +00003456TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3460
3461 // Suppose we have two mappers. (DPAD + KEYBOARD)
3462 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3463 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3464 KeyboardInputMapper& mapper =
3465 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3466 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003467 // Initial metastate is AMETA_NONE.
3468 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003469
3470 mReader->toggleCapsLockState(DEVICE_ID);
3471 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3472}
3473
Arthur Hungfb3cc112022-04-13 07:39:50 +00003474TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3475 // keyboard 1.
3476 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3477 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3478 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3482
3483 KeyboardInputMapper& mapper1 =
3484 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3486
3487 // keyboard 2.
3488 const std::string USB2 = "USB2";
3489 const std::string DEVICE_NAME2 = "KEYBOARD2";
3490 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3491 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3492 std::shared_ptr<InputDevice> device2 =
3493 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3494 ftl::Flags<InputDeviceClass>(0));
3495 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3496 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3497 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3498 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3499 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3500 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3501
3502 KeyboardInputMapper& mapper2 =
3503 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> unused =
3506 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3507 0 /*changes*/);
3508 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003509
Arthur Hung95f68612022-04-07 14:08:22 +08003510 // Initial metastate is AMETA_NONE.
3511 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3512 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3513
3514 // Toggle num lock on and off.
3515 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3516 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003517 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3519 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3520
3521 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3522 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3524 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3525 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3526
3527 // Toggle caps lock on and off.
3528 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3529 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3531 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3532 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3533
3534 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3535 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3538 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3539
3540 // Toggle scroll lock on and off.
3541 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3542 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3544 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3545 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3546
3547 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3550 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3551 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3552}
3553
Arthur Hung2141d542022-08-23 07:45:21 +00003554TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3555 const int32_t USAGE_A = 0x070004;
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3557 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3558
3559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3562 // Key down by scan code.
3563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3564 NotifyKeyArgs args;
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(DEVICE_ID, args.deviceId);
3567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3568 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3570 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3571 ASSERT_EQ(KEY_HOME, args.scanCode);
3572 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3573
3574 // Disable device, it should synthesize cancellation events for down events.
3575 mFakePolicy->addDisabledDevice(DEVICE_ID);
3576 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3577
3578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3580 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3581 ASSERT_EQ(KEY_HOME, args.scanCode);
3582 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3583}
3584
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585// --- KeyboardInputMapperTest_ExternalDevice ---
3586
3587class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3588protected:
Chris Yea52ade12020-08-27 16:49:20 -07003589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590};
3591
3592TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003593 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3594 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003595
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3599 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 KeyboardInputMapper& mapper =
3602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003604
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003606 NotifyKeyArgs args;
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3609
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(uint32_t(0), args.policyFlags);
3613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(uint32_t(0), args.policyFlags);
3617
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(uint32_t(0), args.policyFlags);
3621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3628 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3629}
3630
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003631TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003632 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003637
Powei Fengd041c5d2019-05-03 17:11:33 -07003638 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003642
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003644 NotifyKeyArgs args;
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3647
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(uint32_t(0), args.policyFlags);
3655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(uint32_t(0), args.policyFlags);
3659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3662 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3667}
3668
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669// --- CursorInputMapperTest ---
3670
3671class CursorInputMapperTest : public InputMapperTest {
3672protected:
3673 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3674
Michael Wright17db18e2020-06-26 20:51:44 +01003675 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676
Chris Yea52ade12020-08-27 16:49:20 -07003677 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 InputMapperTest::SetUp();
3679
Michael Wright17db18e2020-06-26 20:51:44 +01003680 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003681 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 }
3683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3685 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003686
Michael Wrighta9cf4192022-12-01 23:46:39 +00003687 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003688 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3689 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3690 }
3691
3692 void prepareSecondaryDisplay() {
3693 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003694 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003695 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003696 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003697
3698 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3699 float pressure) {
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3701 0.0f, 0.0f, 0.0f, EPSILON));
3702 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703};
3704
3705const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3708 int32_t originalY, int32_t rotatedX,
3709 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 NotifyMotionArgs args;
3711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003717 ASSERT_NO_FATAL_FAILURE(
3718 assertCursorPointerCoords(args.pointerCoords[0],
3719 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3720 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721}
3722
3723TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728}
3729
3730TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735}
3736
3737TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003739 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
3741 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
3744 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003745 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3746 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3748 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3749
3750 // When the bounds are set, then there should be a valid motion range.
3751 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3752
3753 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3757 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3758 1, 800 - 1, 0.0f, 0.0f));
3759 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3760 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3761 2, 480 - 1, 0.0f, 0.0f));
3762 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3763 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3764 0.0f, 1.0f, 0.0f, 0.0f));
3765}
3766
3767TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3775 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3776 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3777 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3778 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3779 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3780 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3781 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3782 0.0f, 1.0f, 0.0f, 0.0f));
3783}
3784
3785TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
arthurhungdcef2dc2020-08-11 14:47:50 +08003789 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 NotifyMotionArgs args;
3792
3793 // Button press.
3794 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3798 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3799 ASSERT_EQ(DEVICE_ID, args.deviceId);
3800 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3803 ASSERT_EQ(0, args.flags);
3804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3805 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3806 ASSERT_EQ(0, args.edgeFlags);
3807 ASSERT_EQ(uint32_t(1), args.pointerCount);
3808 ASSERT_EQ(0, args.pointerProperties[0].id);
3809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003810 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3812 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3813 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3816 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3817 ASSERT_EQ(DEVICE_ID, args.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3819 ASSERT_EQ(uint32_t(0), args.policyFlags);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3821 ASSERT_EQ(0, args.flags);
3822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3823 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3824 ASSERT_EQ(0, args.edgeFlags);
3825 ASSERT_EQ(uint32_t(1), args.pointerCount);
3826 ASSERT_EQ(0, args.pointerProperties[0].id);
3827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003828 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003829 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3830 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3831 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3832
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3835 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3837 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3838 ASSERT_EQ(DEVICE_ID, args.deviceId);
3839 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3840 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3842 ASSERT_EQ(0, args.flags);
3843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3844 ASSERT_EQ(0, args.buttonState);
3845 ASSERT_EQ(0, args.edgeFlags);
3846 ASSERT_EQ(uint32_t(1), args.pointerCount);
3847 ASSERT_EQ(0, args.pointerProperties[0].id);
3848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003849 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003850 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3851 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3853
3854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3855 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3856 ASSERT_EQ(DEVICE_ID, args.deviceId);
3857 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3858 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3860 ASSERT_EQ(0, args.flags);
3861 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3862 ASSERT_EQ(0, args.buttonState);
3863 ASSERT_EQ(0, args.edgeFlags);
3864 ASSERT_EQ(uint32_t(1), args.pointerCount);
3865 ASSERT_EQ(0, args.pointerProperties[0].id);
3866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003867 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3869 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3870 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3871}
3872
3873TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876
3877 NotifyMotionArgs args;
3878
3879 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003884 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3885 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3886 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003893 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3894 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895}
3896
3897TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901 NotifyMotionArgs args;
3902
3903 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003908 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003912 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003919 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003920
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003923 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
3930 NotifyMotionArgs args;
3931
3932 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003939 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3940 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3941 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003945 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3946 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3947 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003948
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
3952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003955 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3956 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3957 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003964 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003965
3966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003968 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003971TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003974 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
3975 // need to be rotated.
3976 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
Michael Wrighta9cf4192022-12-01 23:46:39 +00003979 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3987 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3988}
3989
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003990TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003991 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003993 // Since InputReader works in the un-rotated coordinate space, only devices that are not
3994 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003997 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4007
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004008 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004009 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004019 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004020 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4029
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004030 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004031 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4034 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4035 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4036 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4037 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4038 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4039 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4047 mFakePointerController->setPosition(100, 200);
4048 mFakePointerController->setButtonState(0);
4049
4050 NotifyMotionArgs motionArgs;
4051 NotifyKeyArgs keyArgs;
4052
4053 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4058 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(
4061 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4065 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4066 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(
4068 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 ASSERT_EQ(0, motionArgs.buttonState);
4075 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004076 ASSERT_NO_FATAL_FAILURE(
4077 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_EQ(0, motionArgs.buttonState);
4082 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(
4084 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088 ASSERT_EQ(0, motionArgs.buttonState);
4089 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(
4091 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4100 motionArgs.buttonState);
4101 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4102 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(
4104 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4108 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4110 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(
4112 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4117 motionArgs.buttonState);
4118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4119 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004120 ASSERT_NO_FATAL_FAILURE(
4121 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4128 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004129 ASSERT_NO_FATAL_FAILURE(
4130 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131
4132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004136 ASSERT_NO_FATAL_FAILURE(
4137 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4143 ASSERT_EQ(0, motionArgs.buttonState);
4144 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004145 ASSERT_NO_FATAL_FAILURE(
4146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(0, motionArgs.buttonState);
4152 ASSERT_EQ(0, mFakePointerController->getButtonState());
4153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004154 ASSERT_NO_FATAL_FAILURE(
4155 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4158 ASSERT_EQ(0, motionArgs.buttonState);
4159 ASSERT_EQ(0, mFakePointerController->getButtonState());
4160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004161 ASSERT_NO_FATAL_FAILURE(
4162 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
4164 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004170
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4174 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004175 ASSERT_NO_FATAL_FAILURE(
4176 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(
4183 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(0, motionArgs.buttonState);
4190 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191 ASSERT_NO_FATAL_FAILURE(
4192 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193
4194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196 ASSERT_EQ(0, motionArgs.buttonState);
4197 ASSERT_EQ(0, mFakePointerController->getButtonState());
4198
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4204
4205 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4222 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4237 ASSERT_EQ(0, motionArgs.buttonState);
4238 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4244 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4245
4246 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4250 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4263 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_EQ(0, motionArgs.buttonState);
4272 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275
4276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4278 ASSERT_EQ(0, motionArgs.buttonState);
4279 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4284 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4285 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4286
4287 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4291 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4292 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4304 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(0, motionArgs.buttonState);
4313 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(
4315 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
4317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4319 ASSERT_EQ(0, motionArgs.buttonState);
4320 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4325 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4326 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4327}
4328
4329TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4334 mFakePointerController->setPosition(100, 200);
4335 mFakePointerController->setButtonState(0);
4336
4337 NotifyMotionArgs args;
4338
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004343 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4346 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004347 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004348}
4349
4350TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004351 addConfigurationProperty("cursor.mode", "pointer");
4352 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004353 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004354
4355 NotifyDeviceResetArgs resetArgs;
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4357 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4358 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4359
4360 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4361 mFakePointerController->setPosition(100, 200);
4362 mFakePointerController->setButtonState(0);
4363
4364 NotifyMotionArgs args;
4365
4366 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4371 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4374 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004375 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004376
4377 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4384 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4386 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4389 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4390
4391 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004392 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4393 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4395 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4398 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4400 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4403 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4404
4405 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4410 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4413 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004414 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004415
4416 // Disable pointer capture and check that the device generation got bumped
4417 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004418 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 mFakePolicy->setPointerCapture(false);
4420 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004421 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004424 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4425
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4430 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4433 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Michael Wright17db18e2020-06-26 20:51:44 +01004434 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004437/**
4438 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4439 * pointer acceleration or speed processing should not be applied.
4440 */
4441TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4442 addConfigurationProperty("cursor.mode", "pointer");
4443 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4444 100.f /*high threshold*/, 10.f /*acceleration*/);
4445 mFakePolicy->setVelocityControlParams(testParams);
4446 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4447
4448 NotifyDeviceResetArgs resetArgs;
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4452
4453 NotifyMotionArgs args;
4454
4455 // Move and verify scale is applied.
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4462 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4463 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4464 ASSERT_GT(relX, 10);
4465 ASSERT_GT(relY, 20);
4466
4467 // Enable Pointer Capture
4468 mFakePolicy->setPointerCapture(true);
4469 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4470 NotifyPointerCaptureChangedArgs captureArgs;
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4472 ASSERT_TRUE(captureArgs.request.enable);
4473
4474 // Move and verify scale is not applied.
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4481 ASSERT_EQ(10, args.pointerCoords[0].getX());
4482 ASSERT_EQ(20, args.pointerCoords[0].getY());
4483}
4484
Prabir Pradhan208360b2022-06-24 18:37:04 +00004485TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4486 addConfigurationProperty("cursor.mode", "pointer");
4487 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4488
4489 NotifyDeviceResetArgs resetArgs;
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4491 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
4494 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004495 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004496
4497 NotifyMotionArgs args;
4498
4499 // Verify that the coordinates are rotated.
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4506 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4507 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4508
4509 // Enable Pointer Capture.
4510 mFakePolicy->setPointerCapture(true);
4511 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4512 NotifyPointerCaptureChangedArgs captureArgs;
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4514 ASSERT_TRUE(captureArgs.request.enable);
4515
4516 // Move and verify rotation is not applied.
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4523 ASSERT_EQ(10, args.pointerCoords[0].getX());
4524 ASSERT_EQ(20, args.pointerCoords[0].getY());
4525}
4526
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004527TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004529
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004530 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004531 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004532
4533 // Set up the secondary display as the display on which the pointer should be shown.
4534 // The InputDevice is not associated with any display.
4535 prepareSecondaryDisplay();
4536 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004537 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4538
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004539 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004542
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004543 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4549 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4550 WithCoords(110.0f, 220.0f))));
Michael Wright17db18e2020-06-26 20:51:44 +01004551 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004552}
4553
4554TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004558 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004559
4560 // Set up the secondary display as the display on which the pointer should be shown,
4561 // and associate the InputDevice with the secondary display.
4562 prepareSecondaryDisplay();
4563 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4566
4567 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4568 mFakePointerController->setPosition(100, 200);
4569 mFakePointerController->setButtonState(0);
4570
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4576 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4577 WithCoords(110.0f, 220.0f))));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004578 ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f));
4579}
4580
4581TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4582 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4583
4584 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004585 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004586 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4587
4588 // Associate the InputDevice with the secondary display.
4589 prepareSecondaryDisplay();
4590 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4591 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4592
4593 // The mapper should not generate any events because it is associated with a display that is
4594 // different from the pointer display.
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004599}
4600
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004601// --- BluetoothCursorInputMapperTest ---
4602
4603class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4604protected:
4605 void SetUp() override {
4606 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4607
4608 mFakePointerController = std::make_shared<FakePointerController>();
4609 mFakePolicy->setPointerController(mFakePointerController);
4610 }
4611};
4612
4613TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4614 addConfigurationProperty("cursor.mode", "pointer");
4615 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4616
4617 nsecs_t kernelEventTime = ARBITRARY_TIME;
4618 nsecs_t expectedEventTime = ARBITRARY_TIME;
4619 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4620 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4622 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4623 WithEventTime(expectedEventTime))));
4624
4625 // Process several events that come in quick succession, according to their timestamps.
4626 for (int i = 0; i < 3; i++) {
4627 constexpr static nsecs_t delta = ms2ns(1);
4628 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4629 kernelEventTime += delta;
4630 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4631
4632 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4633 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4635 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4636 WithEventTime(expectedEventTime))));
4637 }
4638}
4639
4640TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4641 addConfigurationProperty("cursor.mode", "pointer");
4642 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4643
4644 nsecs_t expectedEventTime = ARBITRARY_TIME;
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4648 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4649 WithEventTime(expectedEventTime))));
4650
4651 // Process several events with the same timestamp from the kernel.
4652 // Ensure that we do not generate events too far into the future.
4653 constexpr static int32_t numEvents =
4654 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4655 for (int i = 0; i < numEvents; i++) {
4656 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4657
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4662 WithEventTime(expectedEventTime))));
4663 }
4664
4665 // By processing more events with the same timestamp, we should not generate events with a
4666 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4667 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4668 for (int i = 0; i < 3; i++) {
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4673 WithEventTime(cappedEventTime))));
4674 }
4675}
4676
4677TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4678 addConfigurationProperty("cursor.mode", "pointer");
4679 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4680
4681 nsecs_t kernelEventTime = ARBITRARY_TIME;
4682 nsecs_t expectedEventTime = ARBITRARY_TIME;
4683 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4684 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4687 WithEventTime(expectedEventTime))));
4688
4689 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4690 // smoothening is not needed, its timestamp is not affected.
4691 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4692 expectedEventTime = kernelEventTime;
4693
4694 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4695 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4698 WithEventTime(expectedEventTime))));
4699}
4700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701// --- TouchInputMapperTest ---
4702
4703class TouchInputMapperTest : public InputMapperTest {
4704protected:
4705 static const int32_t RAW_X_MIN;
4706 static const int32_t RAW_X_MAX;
4707 static const int32_t RAW_Y_MIN;
4708 static const int32_t RAW_Y_MAX;
4709 static const int32_t RAW_TOUCH_MIN;
4710 static const int32_t RAW_TOUCH_MAX;
4711 static const int32_t RAW_TOOL_MIN;
4712 static const int32_t RAW_TOOL_MAX;
4713 static const int32_t RAW_PRESSURE_MIN;
4714 static const int32_t RAW_PRESSURE_MAX;
4715 static const int32_t RAW_ORIENTATION_MIN;
4716 static const int32_t RAW_ORIENTATION_MAX;
4717 static const int32_t RAW_DISTANCE_MIN;
4718 static const int32_t RAW_DISTANCE_MAX;
4719 static const int32_t RAW_TILT_MIN;
4720 static const int32_t RAW_TILT_MAX;
4721 static const int32_t RAW_ID_MIN;
4722 static const int32_t RAW_ID_MAX;
4723 static const int32_t RAW_SLOT_MIN;
4724 static const int32_t RAW_SLOT_MAX;
4725 static const float X_PRECISION;
4726 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004727 static const float X_PRECISION_VIRTUAL;
4728 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
4730 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004731 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
4733 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4734
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004735 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004736 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 enum Axes {
4739 POSITION = 1 << 0,
4740 TOUCH = 1 << 1,
4741 TOOL = 1 << 2,
4742 PRESSURE = 1 << 3,
4743 ORIENTATION = 1 << 4,
4744 MINOR = 1 << 5,
4745 ID = 1 << 6,
4746 DISTANCE = 1 << 7,
4747 TILT = 1 << 8,
4748 SLOT = 1 << 9,
4749 TOOL_TYPE = 1 << 10,
4750 };
4751
Michael Wrighta9cf4192022-12-01 23:46:39 +00004752 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004753 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004754 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004756 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 int32_t toRawX(float displayX);
4758 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004759 int32_t toRotatedRawX(float displayX);
4760 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004761 float toCookedX(float rawX, float rawY);
4762 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004764 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004766 float toDisplayY(int32_t rawY, int32_t displayHeight);
4767
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768};
4769
4770const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4771const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4772const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4773const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4774const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4775const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4776const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4777const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004778const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4779const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4781const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4782const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4783const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4784const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4785const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4786const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4787const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4788const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4789const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4790const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4791const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004792const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4793 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4794const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4795 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004796const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4797 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
4799const float TouchInputMapperTest::GEOMETRIC_SCALE =
4800 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4801 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4802
4803const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4804 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4805 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4806};
4807
Michael Wrighta9cf4192022-12-01 23:46:39 +00004808void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004809 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4810 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004811}
4812
4813void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4814 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004815 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816}
4817
Michael Wrighta9cf4192022-12-01 23:46:39 +00004818void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004819 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4820 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4821 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004822}
4823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004825 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4826 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829}
4830
Jason Gerecke489fda82012-09-07 17:19:40 -07004831void TouchInputMapperTest::prepareLocationCalibration() {
4832 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4833}
4834
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835int32_t TouchInputMapperTest::toRawX(float displayX) {
4836 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4837}
4838
4839int32_t TouchInputMapperTest::toRawY(float displayY) {
4840 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4841}
4842
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004843int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4844 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4845}
4846
4847int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4848 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4849}
4850
Jason Gerecke489fda82012-09-07 17:19:40 -07004851float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4852 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4853 return rawX;
4854}
4855
4856float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4857 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4858 return rawY;
4859}
4860
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004862 return toDisplayX(rawX, DISPLAY_WIDTH);
4863}
4864
4865float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4866 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
4869float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004870 return toDisplayY(rawY, DISPLAY_HEIGHT);
4871}
4872
4873float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4874 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
4877
4878// --- SingleTouchInputMapperTest ---
4879
4880class SingleTouchInputMapperTest : public TouchInputMapperTest {
4881protected:
4882 void prepareButtons();
4883 void prepareAxes(int axes);
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4886 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4887 void processUp(SingleTouchInputMapper& mappery);
4888 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4889 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4890 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4891 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4892 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4893 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894};
4895
4896void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004897 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
4900void SingleTouchInputMapperTest::prepareAxes(int axes) {
4901 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 }
4905 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4907 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 }
4909 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4911 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 }
4913 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4915 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 }
4917 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 }
4921}
4922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932}
4933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004938void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940}
4941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4943 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004951void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4952 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4958 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960}
4961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 prepareButtons();
4968 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Harry Cutts16a24cc2022-10-26 15:22:19 +00004971 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972}
4973
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 prepareButtons();
4976 prepareAxes(POSITION);
4977 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
4983TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004985 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 prepareButtons();
4987 prepareAxes(POSITION);
4988 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004989 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990
4991 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 // Virtual key is down.
4995 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4996 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4997 processDown(mapper, x, y);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
5003 // Virtual key is up.
5004 processUp(mapper);
5005 processSync(mapper);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
5011TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005013 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 prepareButtons();
5015 prepareAxes(POSITION);
5016 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
5022 // Virtual key is down.
5023 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5024 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5025 processDown(mapper, x, y);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005029 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 // Virtual key is up.
5032 processUp(mapper);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
5039TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005041 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 prepareButtons();
5043 prepareAxes(POSITION);
5044 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005048 ASSERT_TRUE(
5049 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_TRUE(flags[0]);
5051 ASSERT_FALSE(flags[1]);
5052}
5053
5054TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005056 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 prepareButtons();
5058 prepareAxes(POSITION);
5059 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
arthurhungdcef2dc2020-08-11 14:47:50 +08005062 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063
5064 NotifyKeyArgs args;
5065
5066 // Press virtual key.
5067 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5068 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5069 processDown(mapper, x, y);
5070 processSync(mapper);
5071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5073 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5074 ASSERT_EQ(DEVICE_ID, args.deviceId);
5075 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5076 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5077 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5079 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5080 ASSERT_EQ(KEY_HOME, args.scanCode);
5081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5082 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5083
5084 // Release virtual key.
5085 processUp(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5089 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5090 ASSERT_EQ(DEVICE_ID, args.deviceId);
5091 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5092 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5093 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5094 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5095 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5096 ASSERT_EQ(KEY_HOME, args.scanCode);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5098 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5099
5100 // Should not have sent any motions.
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5102}
5103
5104TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005106 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 prepareButtons();
5108 prepareAxes(POSITION);
5109 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
arthurhungdcef2dc2020-08-11 14:47:50 +08005112 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 NotifyKeyArgs keyArgs;
5115
5116 // Press virtual key.
5117 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5118 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5119 processDown(mapper, x, y);
5120 processSync(mapper);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5125 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5126 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5128 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5129 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5130 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5132 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5133
5134 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5135 // into the display area.
5136 y -= 100;
5137 processMove(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5141 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5142 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5147 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5148 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5149 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5151 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5152
5153 NotifyMotionArgs motionArgs;
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5157 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5158 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5160 ASSERT_EQ(0, motionArgs.flags);
5161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5162 ASSERT_EQ(0, motionArgs.buttonState);
5163 ASSERT_EQ(0, motionArgs.edgeFlags);
5164 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5165 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5168 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5169 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5170 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5172
5173 // Keep moving out of bounds. Should generate a pointer move.
5174 y -= 50;
5175 processMove(mapper, x, y);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5180 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5182 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5184 ASSERT_EQ(0, motionArgs.flags);
5185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5186 ASSERT_EQ(0, motionArgs.buttonState);
5187 ASSERT_EQ(0, motionArgs.edgeFlags);
5188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5193 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5194 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5196
5197 // Release out of bounds. Should generate a pointer up.
5198 processUp(mapper);
5199 processSync(mapper);
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5203 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5204 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5205 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.flags);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5209 ASSERT_EQ(0, motionArgs.buttonState);
5210 ASSERT_EQ(0, motionArgs.edgeFlags);
5211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5212 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5215 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 // Should not have sent any more keys or motions.
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235 NotifyMotionArgs motionArgs;
5236
5237 // Initially go down out of bounds.
5238 int32_t x = -10;
5239 int32_t y = -10;
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5244
5245 // Move into the display area. Should generate a pointer down.
5246 x = 50;
5247 y = 75;
5248 processMove(mapper, x, y);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5257 ASSERT_EQ(0, motionArgs.flags);
5258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5259 ASSERT_EQ(0, motionArgs.buttonState);
5260 ASSERT_EQ(0, motionArgs.edgeFlags);
5261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5265 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5269
5270 // Release. Should generate a pointer up.
5271 processUp(mapper);
5272 processSync(mapper);
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5276 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5278 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5280 ASSERT_EQ(0, motionArgs.flags);
5281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5282 ASSERT_EQ(0, motionArgs.buttonState);
5283 ASSERT_EQ(0, motionArgs.edgeFlags);
5284 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5292
5293 // Should not have sent any more keys or motions.
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5296}
5297
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005299 addConfigurationProperty("touch.deviceType", "touchScreen");
5300 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5301
Michael Wrighta9cf4192022-12-01 23:46:39 +00005302 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005303 prepareButtons();
5304 prepareAxes(POSITION);
5305 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307
arthurhungdcef2dc2020-08-11 14:47:50 +08005308 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005309
5310 NotifyMotionArgs motionArgs;
5311
5312 // Down.
5313 int32_t x = 100;
5314 int32_t y = 125;
5315 processDown(mapper, x, y);
5316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5321 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5325 ASSERT_EQ(0, motionArgs.flags);
5326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5327 ASSERT_EQ(0, motionArgs.buttonState);
5328 ASSERT_EQ(0, motionArgs.edgeFlags);
5329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5334 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Move.
5340 x += 50;
5341 y += 75;
5342 processMove(mapper, x, y);
5343 processSync(mapper);
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5347 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5348 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5352 ASSERT_EQ(0, motionArgs.flags);
5353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, motionArgs.edgeFlags);
5356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5361 1, 0, 0, 0, 0, 0, 0, 0));
5362 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5363 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5365
5366 // Up.
5367 processUp(mapper);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5373 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5375 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5377 ASSERT_EQ(0, motionArgs.flags);
5378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, motionArgs.edgeFlags);
5381 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5382 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5385 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5386 1, 0, 0, 0, 0, 0, 0, 0));
5387 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5388 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5390
5391 // Should not have sent any more keys or motions.
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5394}
5395
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005398 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 prepareButtons();
5400 prepareAxes(POSITION);
5401 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
arthurhungdcef2dc2020-08-11 14:47:50 +08005404 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 NotifyMotionArgs motionArgs;
5407
5408 // Down.
5409 int32_t x = 100;
5410 int32_t y = 125;
5411 processDown(mapper, x, y);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5432
5433 // Move.
5434 x += 50;
5435 y += 75;
5436 processMove(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5457
5458 // Up.
5459 processUp(mapper);
5460 processSync(mapper);
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
5472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Should not have sent any more keys or motions.
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5484}
5485
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005486TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 addConfigurationProperty("touch.deviceType", "touchScreen");
5488 prepareButtons();
5489 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005490 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5491 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 NotifyMotionArgs args;
5495
5496 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005497 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 processDown(mapper, toRawX(50), toRawY(75));
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5502 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5503 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5504
5505 processUp(mapper);
5506 processSync(mapper);
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5508}
5509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005510TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareButtons();
5513 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5515 // orientation-aware are affected by display rotation.
5516 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005517 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 NotifyMotionArgs args;
5520
5521 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005522 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005523 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 processDown(mapper, toRawX(50), toRawY(75));
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5528 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5529 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5530
5531 processUp(mapper);
5532 processSync(mapper);
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5534
5535 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005537 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005538 processDown(mapper, toRawX(75), RAW_Y_MAX - toRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5542 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5543 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5544
5545 processUp(mapper);
5546 processSync(mapper);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5548
5549 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005550 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005551 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5553 processSync(mapper);
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5556 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5557 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5558
5559 processUp(mapper);
5560 processSync(mapper);
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5562
5563 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005564 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005566 processDown(mapper, RAW_X_MAX - toRawX(75) + RAW_X_MIN, toRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5571 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5572
5573 processUp(mapper);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5576}
5577
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
5582 addConfigurationProperty("touch.orientationAware", "1");
5583 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5584 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005585 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005586 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5587 NotifyMotionArgs args;
5588
5589 // Orientation 0.
5590 processDown(mapper, toRawX(50), toRawY(75));
5591 processSync(mapper);
5592
5593 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5594 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5595 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5596
5597 processUp(mapper);
5598 processSync(mapper);
5599 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5600}
5601
5602TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5603 addConfigurationProperty("touch.deviceType", "touchScreen");
5604 prepareButtons();
5605 prepareAxes(POSITION);
5606 addConfigurationProperty("touch.orientationAware", "1");
5607 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5608 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005609 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005610 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5611 NotifyMotionArgs args;
5612
5613 // Orientation 90.
5614 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5615 processSync(mapper);
5616
5617 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5618 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5619 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5620
5621 processUp(mapper);
5622 processSync(mapper);
5623 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5624}
5625
5626TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5627 addConfigurationProperty("touch.deviceType", "touchScreen");
5628 prepareButtons();
5629 prepareAxes(POSITION);
5630 addConfigurationProperty("touch.orientationAware", "1");
5631 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005634 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5635 NotifyMotionArgs args;
5636
5637 // Orientation 180.
5638 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5639 processSync(mapper);
5640
5641 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5642 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5643 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5644
5645 processUp(mapper);
5646 processSync(mapper);
5647 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5648}
5649
5650TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5651 addConfigurationProperty("touch.deviceType", "touchScreen");
5652 prepareButtons();
5653 prepareAxes(POSITION);
5654 addConfigurationProperty("touch.orientationAware", "1");
5655 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5656 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005657 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005658 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5659 NotifyMotionArgs args;
5660
5661 // Orientation 270.
5662 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5663 processSync(mapper);
5664
5665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5666 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5667 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5668
5669 processUp(mapper);
5670 processSync(mapper);
5671 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5672}
5673
5674TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5675 addConfigurationProperty("touch.deviceType", "touchScreen");
5676 prepareButtons();
5677 prepareAxes(POSITION);
5678 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5679 // orientation-aware are affected by display rotation.
5680 addConfigurationProperty("touch.orientationAware", "0");
5681 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5682 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5683
5684 NotifyMotionArgs args;
5685
5686 // Orientation 90, Rotation 0.
5687 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005688 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005689 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5690 processSync(mapper);
5691
5692 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5693 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5694 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5695
5696 processUp(mapper);
5697 processSync(mapper);
5698 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5699
5700 // Orientation 90, Rotation 90.
5701 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005702 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005703 processDown(mapper, toRotatedRawX(50), toRotatedRawY(75));
5704 processSync(mapper);
5705
5706 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5708 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5709
5710 processUp(mapper);
5711 processSync(mapper);
5712 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5713
5714 // Orientation 90, Rotation 180.
5715 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005716 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005717 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5718 processSync(mapper);
5719
5720 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5721 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5722 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5723
5724 processUp(mapper);
5725 processSync(mapper);
5726 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5727
5728 // Orientation 90, Rotation 270.
5729 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005730 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005731 processDown(mapper, RAW_X_MAX - toRotatedRawX(50) + RAW_X_MIN,
5732 RAW_Y_MAX - toRotatedRawY(75) + RAW_Y_MIN);
5733 processSync(mapper);
5734
5735 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5736 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5737 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5738
5739 processUp(mapper);
5740 processSync(mapper);
5741 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5742}
5743
Michael Wrightd02c5b62014-02-10 15:10:22 -08005744TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005745 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005746 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747 prepareButtons();
5748 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005749 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 // These calculations are based on the input device calibration documentation.
5752 int32_t rawX = 100;
5753 int32_t rawY = 200;
5754 int32_t rawPressure = 10;
5755 int32_t rawToolMajor = 12;
5756 int32_t rawDistance = 2;
5757 int32_t rawTiltX = 30;
5758 int32_t rawTiltY = 110;
5759
5760 float x = toDisplayX(rawX);
5761 float y = toDisplayY(rawY);
5762 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5763 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5764 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5765 float distance = float(rawDistance);
5766
5767 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5768 float tiltScale = M_PI / 180;
5769 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5770 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5771 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5772 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5773
5774 processDown(mapper, rawX, rawY);
5775 processPressure(mapper, rawPressure);
5776 processToolMajor(mapper, rawToolMajor);
5777 processDistance(mapper, rawDistance);
5778 processTilt(mapper, rawTiltX, rawTiltY);
5779 processSync(mapper);
5780
5781 NotifyMotionArgs args;
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5784 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5785 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5786}
5787
Jason Gerecke489fda82012-09-07 17:19:40 -07005788TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005789 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005790 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005791 prepareLocationCalibration();
5792 prepareButtons();
5793 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005794 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005795
5796 int32_t rawX = 100;
5797 int32_t rawY = 200;
5798
5799 float x = toDisplayX(toCookedX(rawX, rawY));
5800 float y = toDisplayY(toCookedY(rawX, rawY));
5801
5802 processDown(mapper, rawX, rawY);
5803 processSync(mapper);
5804
5805 NotifyMotionArgs args;
5806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5808 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5809}
5810
Michael Wrightd02c5b62014-02-10 15:10:22 -08005811TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005813 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005814 prepareButtons();
5815 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005816 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817
5818 NotifyMotionArgs motionArgs;
5819 NotifyKeyArgs keyArgs;
5820
5821 processDown(mapper, 100, 200);
5822 processSync(mapper);
5823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5824 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5825 ASSERT_EQ(0, motionArgs.buttonState);
5826
5827 // press BTN_LEFT, release BTN_LEFT
5828 processKey(mapper, BTN_LEFT, 1);
5829 processSync(mapper);
5830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5832 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5833
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5836 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5837
Michael Wrightd02c5b62014-02-10 15:10:22 -08005838 processKey(mapper, BTN_LEFT, 0);
5839 processSync(mapper);
5840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005843
5844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005845 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005846 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005847
5848 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5849 processKey(mapper, BTN_RIGHT, 1);
5850 processKey(mapper, BTN_MIDDLE, 1);
5851 processSync(mapper);
5852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5854 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5855 motionArgs.buttonState);
5856
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5858 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5859 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5860
5861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5862 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5863 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5864 motionArgs.buttonState);
5865
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 processKey(mapper, BTN_RIGHT, 0);
5867 processSync(mapper);
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005869 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005871
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005874 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875
5876 processKey(mapper, BTN_MIDDLE, 0);
5877 processSync(mapper);
5878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005879 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005880 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005881
5882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005884 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005885
5886 // press BTN_BACK, release BTN_BACK
5887 processKey(mapper, BTN_BACK, 1);
5888 processSync(mapper);
5889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5890 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5891 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005892
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005895 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5896
5897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5898 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5899 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900
5901 processKey(mapper, BTN_BACK, 0);
5902 processSync(mapper);
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005904 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005905 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005906
5907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005909 ASSERT_EQ(0, motionArgs.buttonState);
5910
Michael Wrightd02c5b62014-02-10 15:10:22 -08005911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5912 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5913 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5914
5915 // press BTN_SIDE, release BTN_SIDE
5916 processKey(mapper, BTN_SIDE, 1);
5917 processSync(mapper);
5918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5919 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5920 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005921
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005924 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5925
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5928 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 processKey(mapper, BTN_SIDE, 0);
5931 processSync(mapper);
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005938 ASSERT_EQ(0, motionArgs.buttonState);
5939
Michael Wrightd02c5b62014-02-10 15:10:22 -08005940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5941 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5942 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5943
5944 // press BTN_FORWARD, release BTN_FORWARD
5945 processKey(mapper, BTN_FORWARD, 1);
5946 processSync(mapper);
5947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5948 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5949 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005950
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005952 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005953 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5954
5955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5956 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5957 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005958
5959 processKey(mapper, BTN_FORWARD, 0);
5960 processSync(mapper);
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005962 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005963 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005964
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005966 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005967 ASSERT_EQ(0, motionArgs.buttonState);
5968
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5970 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5971 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5972
5973 // press BTN_EXTRA, release BTN_EXTRA
5974 processKey(mapper, BTN_EXTRA, 1);
5975 processSync(mapper);
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5977 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5978 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979
Michael Wrightd02c5b62014-02-10 15:10:22 -08005980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005982 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5983
5984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5985 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5986 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005987
5988 processKey(mapper, BTN_EXTRA, 0);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(0, motionArgs.buttonState);
5997
Michael Wrightd02c5b62014-02-10 15:10:22 -08005998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5999 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6000 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6001
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6003
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004 // press BTN_STYLUS, release BTN_STYLUS
6005 processKey(mapper, BTN_STYLUS, 1);
6006 processSync(mapper);
6007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6008 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006009 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6010
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6013 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014
6015 processKey(mapper, BTN_STYLUS, 0);
6016 processSync(mapper);
6017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006019 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006020
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006023 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024
6025 // press BTN_STYLUS2, release BTN_STYLUS2
6026 processKey(mapper, BTN_STYLUS2, 1);
6027 processSync(mapper);
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006030 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6031
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6033 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6034 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035
6036 processKey(mapper, BTN_STYLUS2, 0);
6037 processSync(mapper);
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006040 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006041
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006044 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045
6046 // release touch
6047 processUp(mapper);
6048 processSync(mapper);
6049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6050 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6051 ASSERT_EQ(0, motionArgs.buttonState);
6052}
6053
6054TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006056 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006057 prepareButtons();
6058 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006059 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006060
6061 NotifyMotionArgs motionArgs;
6062
6063 // default tool type is finger
6064 processDown(mapper, 100, 200);
6065 processSync(mapper);
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6068 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6069
6070 // eraser
6071 processKey(mapper, BTN_TOOL_RUBBER, 1);
6072 processSync(mapper);
6073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6075 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6076
6077 // stylus
6078 processKey(mapper, BTN_TOOL_RUBBER, 0);
6079 processKey(mapper, BTN_TOOL_PEN, 1);
6080 processSync(mapper);
6081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6084
6085 // brush
6086 processKey(mapper, BTN_TOOL_PEN, 0);
6087 processKey(mapper, BTN_TOOL_BRUSH, 1);
6088 processSync(mapper);
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6091 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6092
6093 // pencil
6094 processKey(mapper, BTN_TOOL_BRUSH, 0);
6095 processKey(mapper, BTN_TOOL_PENCIL, 1);
6096 processSync(mapper);
6097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6099 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6100
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006101 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 processKey(mapper, BTN_TOOL_PENCIL, 0);
6103 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6104 processSync(mapper);
6105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6107 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6108
6109 // mouse
6110 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6111 processKey(mapper, BTN_TOOL_MOUSE, 1);
6112 processSync(mapper);
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6116
6117 // lens
6118 processKey(mapper, BTN_TOOL_MOUSE, 0);
6119 processKey(mapper, BTN_TOOL_LENS, 1);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6124
6125 // double-tap
6126 processKey(mapper, BTN_TOOL_LENS, 0);
6127 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6132
6133 // triple-tap
6134 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6135 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6136 processSync(mapper);
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6140
6141 // quad-tap
6142 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6143 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6144 processSync(mapper);
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6147 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6148
6149 // finger
6150 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6151 processKey(mapper, BTN_TOOL_FINGER, 1);
6152 processSync(mapper);
6153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6156
6157 // stylus trumps finger
6158 processKey(mapper, BTN_TOOL_PEN, 1);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6162 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6163
6164 // eraser trumps stylus
6165 processKey(mapper, BTN_TOOL_RUBBER, 1);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6170
6171 // mouse trumps eraser
6172 processKey(mapper, BTN_TOOL_MOUSE, 1);
6173 processSync(mapper);
6174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6177
6178 // back to default tool type
6179 processKey(mapper, BTN_TOOL_MOUSE, 0);
6180 processKey(mapper, BTN_TOOL_RUBBER, 0);
6181 processKey(mapper, BTN_TOOL_PEN, 0);
6182 processKey(mapper, BTN_TOOL_FINGER, 0);
6183 processSync(mapper);
6184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6186 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6187}
6188
6189TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006190 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006191 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006192 prepareButtons();
6193 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006194 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006195 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006196
6197 NotifyMotionArgs motionArgs;
6198
6199 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6200 processKey(mapper, BTN_TOOL_FINGER, 1);
6201 processMove(mapper, 100, 200);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6206 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6207
6208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6209 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6211 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6212
6213 // move a little
6214 processMove(mapper, 150, 250);
6215 processSync(mapper);
6216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6220
6221 // down when BTN_TOUCH is pressed, pressure defaults to 1
6222 processKey(mapper, BTN_TOUCH, 1);
6223 processSync(mapper);
6224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6225 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6226 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6227 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6228
6229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6230 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6233
6234 // up when BTN_TOUCH is released, hover restored
6235 processKey(mapper, BTN_TOUCH, 0);
6236 processSync(mapper);
6237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6238 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6240 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6241
6242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6243 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
6249 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6250 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6251
6252 // exit hover when pointer goes away
6253 processKey(mapper, BTN_TOOL_FINGER, 0);
6254 processSync(mapper);
6255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6258 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6259}
6260
6261TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006262 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006263 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006264 prepareButtons();
6265 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006266 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006267
6268 NotifyMotionArgs motionArgs;
6269
6270 // initially hovering because pressure is 0
6271 processDown(mapper, 100, 200);
6272 processPressure(mapper, 0);
6273 processSync(mapper);
6274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6275 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6277 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6278
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6282 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6283
6284 // move a little
6285 processMove(mapper, 150, 250);
6286 processSync(mapper);
6287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6288 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6290 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6291
6292 // down when pressure is non-zero
6293 processPressure(mapper, RAW_PRESSURE_MAX);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6296 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6297 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6298 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6303 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6304
6305 // up when pressure becomes 0, hover restored
6306 processPressure(mapper, 0);
6307 processSync(mapper);
6308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6309 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6310 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6311 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6312
6313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6314 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
6320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6321 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6322
6323 // exit hover when pointer goes away
6324 processUp(mapper);
6325 processSync(mapper);
6326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6327 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6328 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6329 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6330}
6331
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006332TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6333 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006334 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006335 prepareButtons();
6336 prepareAxes(POSITION | PRESSURE);
6337 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6338
6339 // Touch down.
6340 processDown(mapper, 100, 200);
6341 processPressure(mapper, 1);
6342 processSync(mapper);
6343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6344 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6345
6346 // Reset the mapper. This should cancel the ongoing gesture.
6347 resetMapper(mapper, ARBITRARY_TIME);
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6349 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6352}
6353
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006354TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006356 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006357 prepareButtons();
6358 prepareAxes(POSITION | PRESSURE);
6359 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6360
6361 // Set the initial state for the touch pointer.
6362 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6363 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6364 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6365 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6366
6367 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006368 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6369 // does not generate any events.
6370 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006371
6372 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6373 // the recreated touch state to generate a down event.
6374 processSync(mapper);
6375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6376 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6377
6378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6379}
6380
lilinnan687e58f2022-07-19 16:00:50 +08006381TEST_F(SingleTouchInputMapperTest,
6382 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6383 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006384 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006385 prepareButtons();
6386 prepareAxes(POSITION);
6387 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6388 NotifyMotionArgs motionArgs;
6389
6390 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006391 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006392 processSync(mapper);
6393
6394 // We should receive a down event
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6396 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6397
6398 // Change display id
6399 clearViewports();
6400 prepareSecondaryDisplay(ViewportType::INTERNAL);
6401
6402 // We should receive a cancel event
6403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6404 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6405 // Then receive reset called
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6407}
6408
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006409TEST_F(SingleTouchInputMapperTest,
6410 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6411 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006412 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006413 prepareButtons();
6414 prepareAxes(POSITION);
6415 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6417 NotifyMotionArgs motionArgs;
6418
6419 // Start a new gesture.
6420 processDown(mapper, 100, 200);
6421 processSync(mapper);
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6424
6425 // Make the viewport inactive. This will put the device in disabled mode.
6426 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6427 viewport->isActive = false;
6428 mFakePolicy->updateViewport(*viewport);
6429 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6430
6431 // We should receive a cancel event for the ongoing gesture.
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6433 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6434 // Then we should be notified that the device was reset.
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6436
6437 // No events are generated while the viewport is inactive.
6438 processMove(mapper, 101, 201);
6439 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006440 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006441 processSync(mapper);
6442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6443
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006444 // Start a new gesture while the viewport is still inactive.
6445 processDown(mapper, 300, 400);
6446 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6447 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6448 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6449 processSync(mapper);
6450
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006451 // Make the viewport active again. The device should resume processing events.
6452 viewport->isActive = true;
6453 mFakePolicy->updateViewport(*viewport);
6454 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6455
6456 // The device is reset because it changes back to direct mode, without generating any events.
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6459
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006460 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006461 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6463 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006464
6465 // No more events.
6466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6468}
6469
Prabir Pradhan211ba622022-10-31 21:09:21 +00006470TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6471 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006472 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006473 prepareButtons();
6474 prepareAxes(POSITION);
6475 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6477
6478 // Press a stylus button.
6479 processKey(mapper, BTN_STYLUS, 1);
6480 processSync(mapper);
6481
6482 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6483 processDown(mapper, 100, 200);
6484 processSync(mapper);
6485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6486 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6487 WithCoords(toDisplayX(100), toDisplayY(200)),
6488 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6490 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6491 WithCoords(toDisplayX(100), toDisplayY(200)),
6492 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6493
6494 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6495 // the button has not actually been released, since there will be no pointers through which the
6496 // button state can be reported. The event is generated at the location of the pointer before
6497 // it went up.
6498 processUp(mapper);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6501 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6502 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6504 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6505 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6506}
6507
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006508TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6509 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6510 prepareDisplay(ui::ROTATION_0);
6511 prepareButtons();
6512 prepareAxes(POSITION);
6513 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6515
6516 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6517}
6518
Prabir Pradhan5632d622021-09-06 07:57:20 -07006519// --- TouchDisplayProjectionTest ---
6520
6521class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6522public:
6523 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6524 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6525 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006526 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006527 uint32_t inverseRotationFlags;
6528 auto width = DISPLAY_WIDTH;
6529 auto height = DISPLAY_HEIGHT;
6530 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006531 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006532 inverseRotationFlags = ui::Transform::ROT_270;
6533 std::swap(width, height);
6534 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006535 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006536 inverseRotationFlags = ui::Transform::ROT_180;
6537 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006538 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006539 inverseRotationFlags = ui::Transform::ROT_90;
6540 std::swap(width, height);
6541 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006542 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006543 inverseRotationFlags = ui::Transform::ROT_0;
6544 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006545 }
6546
6547 const ui::Transform rotation(inverseRotationFlags, width, height);
6548 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6549
6550 std::optional<DisplayViewport> internalViewport =
6551 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6552 DisplayViewport& v = *internalViewport;
6553 v.displayId = DISPLAY_ID;
6554 v.orientation = orientation;
6555
6556 v.logicalLeft = 0;
6557 v.logicalTop = 0;
6558 v.logicalRight = 100;
6559 v.logicalBottom = 100;
6560
6561 v.physicalLeft = rotatedPhysicalDisplay.left;
6562 v.physicalTop = rotatedPhysicalDisplay.top;
6563 v.physicalRight = rotatedPhysicalDisplay.right;
6564 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6565
6566 v.deviceWidth = width;
6567 v.deviceHeight = height;
6568
6569 v.isActive = true;
6570 v.uniqueId = UNIQUE_ID;
6571 v.type = ViewportType::INTERNAL;
6572 mFakePolicy->updateViewport(v);
6573 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6574 }
6575
6576 void assertReceivedMove(const Point& point) {
6577 NotifyMotionArgs motionArgs;
6578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6580 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6582 1, 0, 0, 0, 0, 0, 0, 0));
6583 }
6584};
6585
6586TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6587 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006588 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006589
6590 prepareButtons();
6591 prepareAxes(POSITION);
6592 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6593
6594 NotifyMotionArgs motionArgs;
6595
6596 // Configure the DisplayViewport such that the logical display maps to a subsection of
6597 // the display panel called the physical display. Here, the physical display is bounded by the
6598 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6599 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6600 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6601 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6602
Michael Wrighta9cf4192022-12-01 23:46:39 +00006603 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006604 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6605
6606 // Touches outside the physical display should be ignored, and should not generate any
6607 // events. Ensure touches at the following points that lie outside of the physical display
6608 // area do not generate any events.
6609 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6610 processDown(mapper, toRawX(point.x), toRawY(point.y));
6611 processSync(mapper);
6612 processUp(mapper);
6613 processSync(mapper);
6614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6615 << "Unexpected event generated for touch outside physical display at point: "
6616 << point.x << ", " << point.y;
6617 }
6618 }
6619}
6620
6621TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6622 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006623 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006624
6625 prepareButtons();
6626 prepareAxes(POSITION);
6627 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6628
6629 NotifyMotionArgs motionArgs;
6630
6631 // Configure the DisplayViewport such that the logical display maps to a subsection of
6632 // the display panel called the physical display. Here, the physical display is bounded by the
6633 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6634 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6635
Michael Wrighta9cf4192022-12-01 23:46:39 +00006636 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006637 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6638
6639 // Touches that start outside the physical display should be ignored until it enters the
6640 // physical display bounds, at which point it should generate a down event. Start a touch at
6641 // the point (5, 100), which is outside the physical display bounds.
6642 static const Point kOutsidePoint{5, 100};
6643 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6646
6647 // Move the touch into the physical display area. This should generate a pointer down.
6648 processMove(mapper, toRawX(11), toRawY(21));
6649 processSync(mapper);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6651 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6652 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6653 ASSERT_NO_FATAL_FAILURE(
6654 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6655
6656 // Move the touch inside the physical display area. This should generate a pointer move.
6657 processMove(mapper, toRawX(69), toRawY(159));
6658 processSync(mapper);
6659 assertReceivedMove({69, 159});
6660
6661 // Move outside the physical display area. Since the pointer is already down, this should
6662 // now continue generating events.
6663 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6664 processSync(mapper);
6665 assertReceivedMove(kOutsidePoint);
6666
6667 // Release. This should generate a pointer up.
6668 processUp(mapper);
6669 processSync(mapper);
6670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6671 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6673 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6674
6675 // Ensure no more events were generated.
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6678 }
6679}
6680
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006681// --- ExternalStylusFusionTest ---
6682
6683class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6684public:
6685 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6686 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006687 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006688 prepareButtons();
6689 prepareAxes(POSITION);
6690 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6691
6692 mStylusState.when = ARBITRARY_TIME;
6693 mStylusState.pressure = 0.f;
6694 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6695 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6696 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6697 processExternalStylusState(mapper);
6698 return mapper;
6699 }
6700
6701 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6702 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6703 for (const NotifyArgs& args : generatedArgs) {
6704 mFakeListener->notify(args);
6705 }
6706 // Loop the reader to flush the input listener queue.
6707 mReader->loopOnce();
6708 return generatedArgs;
6709 }
6710
6711protected:
6712 StylusState mStylusState{};
6713 static constexpr uint32_t EXPECTED_SOURCE =
6714 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6715
6716 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6717 auto toolTypeSource =
6718 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6719
6720 // The first pointer is withheld.
6721 processDown(mapper, 100, 200);
6722 processSync(mapper);
6723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6724 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6725 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6726
6727 // The external stylus reports pressure. The withheld finger pointer is released as a
6728 // stylus.
6729 mStylusState.pressure = 1.f;
6730 processExternalStylusState(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6732 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6733 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6734
6735 // Subsequent pointer events are not withheld.
6736 processMove(mapper, 101, 201);
6737 processSync(mapper);
6738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6739 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6740
6741 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6743 }
6744
6745 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6746 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6747
6748 // Releasing the touch pointer ends the gesture.
6749 processUp(mapper);
6750 processSync(mapper);
6751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6753 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6754
6755 mStylusState.pressure = 0.f;
6756 processExternalStylusState(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6759 }
6760
6761 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6762 auto toolTypeSource =
6763 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6764
6765 // The first pointer is withheld when an external stylus is connected,
6766 // and a timeout is requested.
6767 processDown(mapper, 100, 200);
6768 processSync(mapper);
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6770 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6771 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6772
6773 // If the timeout expires early, it is requested again.
6774 handleTimeout(mapper, ARBITRARY_TIME + 1);
6775 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6776 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6777
6778 // When the timeout expires, the withheld touch is released as a finger pointer.
6779 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6781 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6782
6783 // Subsequent pointer events are not withheld.
6784 processMove(mapper, 101, 201);
6785 processSync(mapper);
6786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6787 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6788 processUp(mapper);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6791 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6792
6793 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6795 }
6796
6797private:
6798 InputDeviceInfo mExternalStylusDeviceInfo{};
6799};
6800
6801TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6802 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6803 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6804}
6805
6806TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6807 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6808 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6809}
6810
6811TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6812 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6813 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6814}
6815
6816// Test a successful stylus fusion gesture where the pressure is reported by the external
6817// before the touch is reported by the touchscreen.
6818TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6819 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6820 auto toolTypeSource =
6821 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6822
6823 // The external stylus reports pressure first. It is ignored for now.
6824 mStylusState.pressure = 1.f;
6825 processExternalStylusState(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6827 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6828
6829 // When the touch goes down afterwards, it is reported as a stylus pointer.
6830 processDown(mapper, 100, 200);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6833 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6834 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6835
6836 processMove(mapper, 101, 201);
6837 processSync(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6839 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6840 processUp(mapper);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6843 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6844
6845 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6847}
6848
6849TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6850 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6851
6852 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6853 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6854
6855 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6856 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6857 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6858 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6859}
6860
6861TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6862 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6863 auto toolTypeSource =
6864 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6865
6866 mStylusState.pressure = 0.8f;
6867 processExternalStylusState(mapper);
6868 processDown(mapper, 100, 200);
6869 processSync(mapper);
6870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6871 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6872 WithPressure(0.8f))));
6873 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6874
6875 // The external stylus reports a pressure change. We wait for some time for a touch event.
6876 mStylusState.pressure = 0.6f;
6877 processExternalStylusState(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6879 ASSERT_NO_FATAL_FAILURE(
6880 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6881
6882 // If a touch is reported within the timeout, it reports the updated pressure.
6883 processMove(mapper, 101, 201);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6886 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6887 WithPressure(0.6f))));
6888 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6889
6890 // There is another pressure change.
6891 mStylusState.pressure = 0.5f;
6892 processExternalStylusState(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6894 ASSERT_NO_FATAL_FAILURE(
6895 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6896
6897 // If a touch is not reported within the timeout, a move event is generated to report
6898 // the new pressure.
6899 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6901 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6902 WithPressure(0.5f))));
6903
6904 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6905 // repeated indefinitely.
6906 mStylusState.pressure = 0.0f;
6907 processExternalStylusState(mapper);
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6909 ASSERT_NO_FATAL_FAILURE(
6910 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6911 processMove(mapper, 102, 202);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6915 WithPressure(0.5f))));
6916 processMove(mapper, 103, 203);
6917 processSync(mapper);
6918 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6919 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6920 WithPressure(0.5f))));
6921
6922 processUp(mapper);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6926 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6927
6928 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6930}
6931
6932TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6933 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6934 auto source = WithSource(EXPECTED_SOURCE);
6935
6936 mStylusState.pressure = 1.f;
6937 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
6938 processExternalStylusState(mapper);
6939 processDown(mapper, 100, 200);
6940 processSync(mapper);
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6942 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6943 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
6944 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6945
6946 // The external stylus reports a tool change. We wait for some time for a touch event.
6947 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6948 processExternalStylusState(mapper);
6949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6950 ASSERT_NO_FATAL_FAILURE(
6951 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6952
6953 // If a touch is reported within the timeout, it reports the updated pressure.
6954 processMove(mapper, 101, 201);
6955 processSync(mapper);
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6957 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6958 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6959 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6960
6961 // There is another tool type change.
6962 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
6963 processExternalStylusState(mapper);
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6965 ASSERT_NO_FATAL_FAILURE(
6966 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6967
6968 // If a touch is not reported within the timeout, a move event is generated to report
6969 // the new tool type.
6970 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6972 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6973 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6974
6975 processUp(mapper);
6976 processSync(mapper);
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6978 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
6979 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
6980
6981 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6983}
6984
6985TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6986 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6987 auto toolTypeSource =
6988 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6989
6990 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6991
6992 // The external stylus reports a button change. We wait for some time for a touch event.
6993 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6994 processExternalStylusState(mapper);
6995 ASSERT_NO_FATAL_FAILURE(
6996 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6997
6998 // If a touch is reported within the timeout, it reports the updated button state.
6999 processMove(mapper, 101, 201);
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7002 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7003 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7005 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7006 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7007 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7008
7009 // The button is now released.
7010 mStylusState.buttons = 0;
7011 processExternalStylusState(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7013 ASSERT_NO_FATAL_FAILURE(
7014 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7015
7016 // If a touch is not reported within the timeout, a move event is generated to report
7017 // the new button state.
7018 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7020 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7021 WithButtonState(0))));
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007023 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7024 WithButtonState(0))));
7025
7026 processUp(mapper);
7027 processSync(mapper);
7028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007029 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7030
7031 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7033}
7034
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035// --- MultiTouchInputMapperTest ---
7036
7037class MultiTouchInputMapperTest : public TouchInputMapperTest {
7038protected:
7039 void prepareAxes(int axes);
7040
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007041 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7042 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7043 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7044 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7045 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7046 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7047 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7048 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7049 void processId(MultiTouchInputMapper& mapper, int32_t id);
7050 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7051 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7052 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007053 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007054 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007055 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7056 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007057};
7058
7059void MultiTouchInputMapperTest::prepareAxes(int axes) {
7060 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063 }
7064 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7066 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007068 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7069 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007070 }
7071 }
7072 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7074 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007076 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007077 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 }
7079 }
7080 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007081 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7082 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007083 }
7084 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007085 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7086 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087 }
7088 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007089 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7090 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 }
7092 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007093 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7094 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 }
7096 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007097 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7098 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099 }
7100 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007101 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007102 }
7103}
7104
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007105void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7106 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109}
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7112 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114}
7115
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007116void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7117 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007119}
7120
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007121void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007123}
7124
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007125void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7130 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140}
7141
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007142void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007143 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144}
7145
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007146void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148}
7149
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007150void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152}
7153
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007154void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7155 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157}
7158
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007159void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7160 int32_t value) {
7161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007167}
7168
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007169void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7170 nsecs_t readTime) {
7171 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172}
7173
Michael Wrightd02c5b62014-02-10 15:10:22 -08007174TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007176 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177 prepareAxes(POSITION);
7178 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007180
arthurhungdcef2dc2020-08-11 14:47:50 +08007181 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007182
7183 NotifyMotionArgs motionArgs;
7184
7185 // Two fingers down at once.
7186 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7187 processPosition(mapper, x1, y1);
7188 processMTSync(mapper);
7189 processPosition(mapper, x2, y2);
7190 processMTSync(mapper);
7191 processSync(mapper);
7192
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7196 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7199 ASSERT_EQ(0, motionArgs.flags);
7200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7201 ASSERT_EQ(0, motionArgs.buttonState);
7202 ASSERT_EQ(0, motionArgs.edgeFlags);
7203 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7207 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7208 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7209 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7211
7212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7213 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7214 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7215 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7216 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007217 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218 ASSERT_EQ(0, motionArgs.flags);
7219 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7220 ASSERT_EQ(0, motionArgs.buttonState);
7221 ASSERT_EQ(0, motionArgs.edgeFlags);
7222 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7223 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7224 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7225 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7228 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7229 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7230 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7231 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7232 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7233 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7234
7235 // Move.
7236 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7237 processPosition(mapper, x1, y1);
7238 processMTSync(mapper);
7239 processPosition(mapper, x2, y2);
7240 processMTSync(mapper);
7241 processSync(mapper);
7242
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7244 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7245 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7246 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7247 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7249 ASSERT_EQ(0, motionArgs.flags);
7250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7251 ASSERT_EQ(0, motionArgs.buttonState);
7252 ASSERT_EQ(0, motionArgs.edgeFlags);
7253 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7254 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7255 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7256 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7258 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7259 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7261 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7263 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7265
7266 // First finger up.
7267 x2 += 15; y2 -= 20;
7268 processPosition(mapper, x2, y2);
7269 processMTSync(mapper);
7270 processSync(mapper);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007277 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
7282 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7285 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7288 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7290 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7291 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7292 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7301 ASSERT_EQ(0, motionArgs.flags);
7302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7303 ASSERT_EQ(0, motionArgs.buttonState);
7304 ASSERT_EQ(0, motionArgs.edgeFlags);
7305 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7306 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7307 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7309 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7313
7314 // Move.
7315 x2 += 20; y2 -= 25;
7316 processPosition(mapper, x2, y2);
7317 processMTSync(mapper);
7318 processSync(mapper);
7319
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7326 ASSERT_EQ(0, motionArgs.flags);
7327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7328 ASSERT_EQ(0, motionArgs.buttonState);
7329 ASSERT_EQ(0, motionArgs.edgeFlags);
7330 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7331 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7333 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7334 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7335 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7336 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7338
7339 // New finger down.
7340 int32_t x3 = 700, y3 = 300;
7341 processPosition(mapper, x2, y2);
7342 processMTSync(mapper);
7343 processPosition(mapper, x3, y3);
7344 processMTSync(mapper);
7345 processSync(mapper);
7346
7347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7348 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7349 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7350 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7351 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007352 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007353 ASSERT_EQ(0, motionArgs.flags);
7354 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7355 ASSERT_EQ(0, motionArgs.buttonState);
7356 ASSERT_EQ(0, motionArgs.edgeFlags);
7357 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7358 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7360 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7365 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7367 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7368 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7369
7370 // Second finger up.
7371 x3 += 30; y3 -= 20;
7372 processPosition(mapper, x3, y3);
7373 processMTSync(mapper);
7374 processSync(mapper);
7375
7376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7377 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7378 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7379 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7380 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007381 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_EQ(0, motionArgs.flags);
7383 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7384 ASSERT_EQ(0, motionArgs.buttonState);
7385 ASSERT_EQ(0, motionArgs.edgeFlags);
7386 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7387 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7389 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7390 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7392 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7394 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7395 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7396 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7397 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7400 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7401 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7402 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7403 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7404 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7405 ASSERT_EQ(0, motionArgs.flags);
7406 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7407 ASSERT_EQ(0, motionArgs.buttonState);
7408 ASSERT_EQ(0, motionArgs.edgeFlags);
7409 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7410 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7411 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7413 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7414 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7415 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7416 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7417
7418 // Last finger up.
7419 processMTSync(mapper);
7420 processSync(mapper);
7421
7422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7424 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7425 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7426 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7427 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7428 ASSERT_EQ(0, motionArgs.flags);
7429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7430 ASSERT_EQ(0, motionArgs.buttonState);
7431 ASSERT_EQ(0, motionArgs.edgeFlags);
7432 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7433 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7434 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7436 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7437 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7438 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7440
7441 // Should not have sent any more keys or motions.
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7444}
7445
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007446TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7447 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007448 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007449
7450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7451 /*fuzz*/ 0, /*resolution*/ 10);
7452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7453 /*fuzz*/ 0, /*resolution*/ 11);
7454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7455 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7456 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7457 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7459 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7460 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7461 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7462
7463 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7464
7465 // X and Y axes
7466 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7467 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7468 // Touch major and minor
7469 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7470 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7471 // Tool major and minor
7472 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7473 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7474}
7475
7476TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007478 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007479
7480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7481 /*fuzz*/ 0, /*resolution*/ 10);
7482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7483 /*fuzz*/ 0, /*resolution*/ 11);
7484
7485 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7486
7487 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7488
7489 // Touch major and minor
7490 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7491 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7492 // Tool major and minor
7493 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7494 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7495}
7496
Michael Wrightd02c5b62014-02-10 15:10:22 -08007497TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007498 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007499 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500 prepareAxes(POSITION | ID);
7501 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007502 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503
arthurhungdcef2dc2020-08-11 14:47:50 +08007504 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505
7506 NotifyMotionArgs motionArgs;
7507
7508 // Two fingers down at once.
7509 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7510 processPosition(mapper, x1, y1);
7511 processId(mapper, 1);
7512 processMTSync(mapper);
7513 processPosition(mapper, x2, y2);
7514 processId(mapper, 2);
7515 processMTSync(mapper);
7516 processSync(mapper);
7517
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7519 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7520 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7521 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7522 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7524 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7525
7526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007527 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7529 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7531 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7532 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7535 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7536 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7537
7538 // Move.
7539 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7540 processPosition(mapper, x1, y1);
7541 processId(mapper, 1);
7542 processMTSync(mapper);
7543 processPosition(mapper, x2, y2);
7544 processId(mapper, 2);
7545 processMTSync(mapper);
7546 processSync(mapper);
7547
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7550 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7551 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7553 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7554 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7556 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7558 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7559
7560 // First finger up.
7561 x2 += 15; y2 -= 20;
7562 processPosition(mapper, x2, y2);
7563 processId(mapper, 2);
7564 processMTSync(mapper);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007568 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7570 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7571 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7572 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7577 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7578
7579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7581 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7582 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7583 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7585 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7586
7587 // Move.
7588 x2 += 20; y2 -= 25;
7589 processPosition(mapper, x2, y2);
7590 processId(mapper, 2);
7591 processMTSync(mapper);
7592 processSync(mapper);
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7595 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7596 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7597 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7598 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7600 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7601
7602 // New finger down.
7603 int32_t x3 = 700, y3 = 300;
7604 processPosition(mapper, x2, y2);
7605 processId(mapper, 2);
7606 processMTSync(mapper);
7607 processPosition(mapper, x3, y3);
7608 processId(mapper, 3);
7609 processMTSync(mapper);
7610 processSync(mapper);
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007613 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7615 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7616 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7617 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7619 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7620 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7622 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // Second finger up.
7625 x3 += 30; y3 -= 20;
7626 processPosition(mapper, x3, y3);
7627 processId(mapper, 3);
7628 processMTSync(mapper);
7629 processSync(mapper);
7630
7631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007632 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7634 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7635 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7636 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7639 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7640 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7641 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7642
7643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7645 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7646 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7648 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7649 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7650
7651 // Last finger up.
7652 processMTSync(mapper);
7653 processSync(mapper);
7654
7655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7656 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7657 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7658 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7659 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7660 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7661 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7662
7663 // Should not have sent any more keys or motions.
7664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7666}
7667
7668TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007669 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007670 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671 prepareAxes(POSITION | ID | SLOT);
7672 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007673 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674
arthurhungdcef2dc2020-08-11 14:47:50 +08007675 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676
7677 NotifyMotionArgs motionArgs;
7678
7679 // Two fingers down at once.
7680 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7681 processPosition(mapper, x1, y1);
7682 processId(mapper, 1);
7683 processSlot(mapper, 1);
7684 processPosition(mapper, x2, y2);
7685 processId(mapper, 2);
7686 processSync(mapper);
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7689 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7690 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7693 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7694 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007697 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007698 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7699 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7700 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7701 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7703 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7704 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7705 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7706 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7707
7708 // Move.
7709 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7710 processSlot(mapper, 0);
7711 processPosition(mapper, x1, y1);
7712 processSlot(mapper, 1);
7713 processPosition(mapper, x2, y2);
7714 processSync(mapper);
7715
7716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7717 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7718 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7719 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7720 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7721 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7722 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7724 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7726 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7727
7728 // First finger up.
7729 x2 += 15; y2 -= 20;
7730 processSlot(mapper, 0);
7731 processId(mapper, -1);
7732 processSlot(mapper, 1);
7733 processPosition(mapper, x2, y2);
7734 processSync(mapper);
7735
7736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007737 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7739 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7740 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7741 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7742 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7744 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7745 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7746 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7747
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7750 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7751 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7752 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7754 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7755
7756 // Move.
7757 x2 += 20; y2 -= 25;
7758 processPosition(mapper, x2, y2);
7759 processSync(mapper);
7760
7761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7763 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7764 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7766 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7767 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7768
7769 // New finger down.
7770 int32_t x3 = 700, y3 = 300;
7771 processPosition(mapper, x2, y2);
7772 processSlot(mapper, 0);
7773 processId(mapper, 3);
7774 processPosition(mapper, x3, y3);
7775 processSync(mapper);
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007778 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007779 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7780 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7782 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7785 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7787 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7788
7789 // Second finger up.
7790 x3 += 30; y3 -= 20;
7791 processSlot(mapper, 1);
7792 processId(mapper, -1);
7793 processSlot(mapper, 0);
7794 processPosition(mapper, x3, y3);
7795 processSync(mapper);
7796
7797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007798 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7801 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7802 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7803 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7807 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7808
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7811 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7812 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7813 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7815 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7816
7817 // Last finger up.
7818 processId(mapper, -1);
7819 processSync(mapper);
7820
7821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7822 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7823 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7824 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7826 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7827 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7828
7829 // Should not have sent any more keys or motions.
7830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7832}
7833
7834TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007835 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007836 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007837 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839
7840 // These calculations are based on the input device calibration documentation.
7841 int32_t rawX = 100;
7842 int32_t rawY = 200;
7843 int32_t rawTouchMajor = 7;
7844 int32_t rawTouchMinor = 6;
7845 int32_t rawToolMajor = 9;
7846 int32_t rawToolMinor = 8;
7847 int32_t rawPressure = 11;
7848 int32_t rawDistance = 0;
7849 int32_t rawOrientation = 3;
7850 int32_t id = 5;
7851
7852 float x = toDisplayX(rawX);
7853 float y = toDisplayY(rawY);
7854 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7855 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7856 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7857 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7858 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7859 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7860 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7861 float distance = float(rawDistance);
7862
7863 processPosition(mapper, rawX, rawY);
7864 processTouchMajor(mapper, rawTouchMajor);
7865 processTouchMinor(mapper, rawTouchMinor);
7866 processToolMajor(mapper, rawToolMajor);
7867 processToolMinor(mapper, rawToolMinor);
7868 processPressure(mapper, rawPressure);
7869 processOrientation(mapper, rawOrientation);
7870 processDistance(mapper, rawDistance);
7871 processId(mapper, id);
7872 processMTSync(mapper);
7873 processSync(mapper);
7874
7875 NotifyMotionArgs args;
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7877 ASSERT_EQ(0, args.pointerProperties[0].id);
7878 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7879 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7880 orientation, distance));
7881}
7882
7883TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007884 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007885 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7887 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889
7890 // These calculations are based on the input device calibration documentation.
7891 int32_t rawX = 100;
7892 int32_t rawY = 200;
7893 int32_t rawTouchMajor = 140;
7894 int32_t rawTouchMinor = 120;
7895 int32_t rawToolMajor = 180;
7896 int32_t rawToolMinor = 160;
7897
7898 float x = toDisplayX(rawX);
7899 float y = toDisplayY(rawY);
7900 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7901 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7902 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7903 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7904 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7905
7906 processPosition(mapper, rawX, rawY);
7907 processTouchMajor(mapper, rawTouchMajor);
7908 processTouchMinor(mapper, rawTouchMinor);
7909 processToolMajor(mapper, rawToolMajor);
7910 processToolMinor(mapper, rawToolMinor);
7911 processMTSync(mapper);
7912 processSync(mapper);
7913
7914 NotifyMotionArgs args;
7915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7917 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7918}
7919
7920TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007922 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007923 prepareAxes(POSITION | TOUCH | TOOL);
7924 addConfigurationProperty("touch.size.calibration", "diameter");
7925 addConfigurationProperty("touch.size.scale", "10");
7926 addConfigurationProperty("touch.size.bias", "160");
7927 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007928 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929
7930 // These calculations are based on the input device calibration documentation.
7931 // Note: We only provide a single common touch/tool value because the device is assumed
7932 // not to emit separate values for each pointer (isSummed = 1).
7933 int32_t rawX = 100;
7934 int32_t rawY = 200;
7935 int32_t rawX2 = 150;
7936 int32_t rawY2 = 250;
7937 int32_t rawTouchMajor = 5;
7938 int32_t rawToolMajor = 8;
7939
7940 float x = toDisplayX(rawX);
7941 float y = toDisplayY(rawY);
7942 float x2 = toDisplayX(rawX2);
7943 float y2 = toDisplayY(rawY2);
7944 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7945 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7946 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7947
7948 processPosition(mapper, rawX, rawY);
7949 processTouchMajor(mapper, rawTouchMajor);
7950 processToolMajor(mapper, rawToolMajor);
7951 processMTSync(mapper);
7952 processPosition(mapper, rawX2, rawY2);
7953 processTouchMajor(mapper, rawTouchMajor);
7954 processToolMajor(mapper, rawToolMajor);
7955 processMTSync(mapper);
7956 processSync(mapper);
7957
7958 NotifyMotionArgs args;
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7961
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007963 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007964 ASSERT_EQ(size_t(2), args.pointerCount);
7965 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7966 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7968 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7969}
7970
7971TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007973 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974 prepareAxes(POSITION | TOUCH | TOOL);
7975 addConfigurationProperty("touch.size.calibration", "area");
7976 addConfigurationProperty("touch.size.scale", "43");
7977 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979
7980 // These calculations are based on the input device calibration documentation.
7981 int32_t rawX = 100;
7982 int32_t rawY = 200;
7983 int32_t rawTouchMajor = 5;
7984 int32_t rawToolMajor = 8;
7985
7986 float x = toDisplayX(rawX);
7987 float y = toDisplayY(rawY);
7988 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7989 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7990 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7991
7992 processPosition(mapper, rawX, rawY);
7993 processTouchMajor(mapper, rawTouchMajor);
7994 processToolMajor(mapper, rawToolMajor);
7995 processMTSync(mapper);
7996 processSync(mapper);
7997
7998 NotifyMotionArgs args;
7999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8001 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8002}
8003
8004TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008005 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008006 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008007 prepareAxes(POSITION | PRESSURE);
8008 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8009 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011
Michael Wrightaa449c92017-12-13 21:21:43 +00008012 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008013 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008014 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8015 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8016 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8017
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 // These calculations are based on the input device calibration documentation.
8019 int32_t rawX = 100;
8020 int32_t rawY = 200;
8021 int32_t rawPressure = 60;
8022
8023 float x = toDisplayX(rawX);
8024 float y = toDisplayY(rawY);
8025 float pressure = float(rawPressure) * 0.01f;
8026
8027 processPosition(mapper, rawX, rawY);
8028 processPressure(mapper, rawPressure);
8029 processMTSync(mapper);
8030 processSync(mapper);
8031
8032 NotifyMotionArgs args;
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8034 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8035 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8036}
8037
8038TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008039 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008040 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008042 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008043
8044 NotifyMotionArgs motionArgs;
8045 NotifyKeyArgs keyArgs;
8046
8047 processId(mapper, 1);
8048 processPosition(mapper, 100, 200);
8049 processSync(mapper);
8050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8051 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8052 ASSERT_EQ(0, motionArgs.buttonState);
8053
8054 // press BTN_LEFT, release BTN_LEFT
8055 processKey(mapper, BTN_LEFT, 1);
8056 processSync(mapper);
8057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8060
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8064
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 processKey(mapper, BTN_LEFT, 0);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074
8075 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8076 processKey(mapper, BTN_RIGHT, 1);
8077 processKey(mapper, BTN_MIDDLE, 1);
8078 processSync(mapper);
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8081 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8082 motionArgs.buttonState);
8083
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8085 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8086 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8087
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8089 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8090 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8091 motionArgs.buttonState);
8092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 processKey(mapper, BTN_RIGHT, 0);
8094 processSync(mapper);
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008098
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008101 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008102
8103 processKey(mapper, BTN_MIDDLE, 0);
8104 processSync(mapper);
8105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108
8109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008111 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008112
8113 // press BTN_BACK, release BTN_BACK
8114 processKey(mapper, BTN_BACK, 1);
8115 processSync(mapper);
8116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8117 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8118 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8126 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008127
8128 processKey(mapper, BTN_BACK, 0);
8129 processSync(mapper);
8130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008132 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008136 ASSERT_EQ(0, motionArgs.buttonState);
8137
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8139 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8140 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8141
8142 // press BTN_SIDE, release BTN_SIDE
8143 processKey(mapper, BTN_SIDE, 1);
8144 processSync(mapper);
8145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8146 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8147 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008148
Michael Wrightd02c5b62014-02-10 15:10:22 -08008149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8154 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8155 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
8157 processKey(mapper, BTN_SIDE, 0);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
8166
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8168 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8170
8171 // press BTN_FORWARD, release BTN_FORWARD
8172 processKey(mapper, BTN_FORWARD, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8175 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8176 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008177
Michael Wrightd02c5b62014-02-10 15:10:22 -08008178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8181
8182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8184 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185
8186 processKey(mapper, BTN_FORWARD, 0);
8187 processSync(mapper);
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008189 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008190 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008194 ASSERT_EQ(0, motionArgs.buttonState);
8195
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8197 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8198 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8199
8200 // press BTN_EXTRA, release BTN_EXTRA
8201 processKey(mapper, BTN_EXTRA, 1);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8204 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8205 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008206
Michael Wrightd02c5b62014-02-10 15:10:22 -08008207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_EXTRA, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
8224
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8226 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8227 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8228
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8230
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 // press BTN_STYLUS, release BTN_STYLUS
8232 processKey(mapper, BTN_STYLUS, 1);
8233 processSync(mapper);
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8237
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8240 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008241
8242 processKey(mapper, BTN_STYLUS, 0);
8243 processSync(mapper);
8244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008245 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008246 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008250 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008251
8252 // press BTN_STYLUS2, release BTN_STYLUS2
8253 processKey(mapper, BTN_STYLUS2, 1);
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008257 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8258
8259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8260 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8261 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262
8263 processKey(mapper, BTN_STYLUS2, 0);
8264 processSync(mapper);
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268
8269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008271 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272
8273 // release touch
8274 processId(mapper, -1);
8275 processSync(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8278 ASSERT_EQ(0, motionArgs.buttonState);
8279}
8280
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008281TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8282 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008283 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008284 prepareAxes(POSITION | ID | SLOT);
8285 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8286
8287 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8288 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8289
8290 // Touch down.
8291 processId(mapper, 1);
8292 processPosition(mapper, 100, 200);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8295 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8296
8297 // Press and release button mapped to the primary stylus button.
8298 processKey(mapper, BTN_A, 1);
8299 processSync(mapper);
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8302 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8304 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8305 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8306
8307 processKey(mapper, BTN_A, 0);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8313
8314 // Press and release the HID usage mapped to the secondary stylus button.
8315 processHidUsage(mapper, 0xabcd, 1);
8316 processSync(mapper);
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8318 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8319 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8321 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8322 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8323
8324 processHidUsage(mapper, 0xabcd, 0);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8327 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8329 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8330
8331 // Release touch.
8332 processId(mapper, -1);
8333 processSync(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8335 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8336}
8337
Michael Wrightd02c5b62014-02-10 15:10:22 -08008338TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008339 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008340 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008342 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008343
8344 NotifyMotionArgs motionArgs;
8345
8346 // default tool type is finger
8347 processId(mapper, 1);
8348 processPosition(mapper, 100, 200);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8351 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8352 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8353
8354 // eraser
8355 processKey(mapper, BTN_TOOL_RUBBER, 1);
8356 processSync(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8359 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8360
8361 // stylus
8362 processKey(mapper, BTN_TOOL_RUBBER, 0);
8363 processKey(mapper, BTN_TOOL_PEN, 1);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8368
8369 // brush
8370 processKey(mapper, BTN_TOOL_PEN, 0);
8371 processKey(mapper, BTN_TOOL_BRUSH, 1);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8375 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8376
8377 // pencil
8378 processKey(mapper, BTN_TOOL_BRUSH, 0);
8379 processKey(mapper, BTN_TOOL_PENCIL, 1);
8380 processSync(mapper);
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8384
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008385 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386 processKey(mapper, BTN_TOOL_PENCIL, 0);
8387 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8388 processSync(mapper);
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8391 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8392
8393 // mouse
8394 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8395 processKey(mapper, BTN_TOOL_MOUSE, 1);
8396 processSync(mapper);
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8398 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8400
8401 // lens
8402 processKey(mapper, BTN_TOOL_MOUSE, 0);
8403 processKey(mapper, BTN_TOOL_LENS, 1);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8407 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8408
8409 // double-tap
8410 processKey(mapper, BTN_TOOL_LENS, 0);
8411 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8412 processSync(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8416
8417 // triple-tap
8418 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8419 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8420 processSync(mapper);
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8423 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8424
8425 // quad-tap
8426 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8427 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8428 processSync(mapper);
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8432
8433 // finger
8434 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8435 processKey(mapper, BTN_TOOL_FINGER, 1);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8439 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8440
8441 // stylus trumps finger
8442 processKey(mapper, BTN_TOOL_PEN, 1);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8447
8448 // eraser trumps stylus
8449 processKey(mapper, BTN_TOOL_RUBBER, 1);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8454
8455 // mouse trumps eraser
8456 processKey(mapper, BTN_TOOL_MOUSE, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8460 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8461
8462 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8463 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8464 processSync(mapper);
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8468
8469 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8470 processToolType(mapper, MT_TOOL_PEN);
8471 processSync(mapper);
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8475
8476 // back to default tool type
8477 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8478 processKey(mapper, BTN_TOOL_MOUSE, 0);
8479 processKey(mapper, BTN_TOOL_RUBBER, 0);
8480 processKey(mapper, BTN_TOOL_PEN, 0);
8481 processKey(mapper, BTN_TOOL_FINGER, 0);
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
8488TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008490 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008491 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008492 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494
8495 NotifyMotionArgs motionArgs;
8496
8497 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8498 processId(mapper, 1);
8499 processPosition(mapper, 100, 200);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8503 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8504 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8505
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8507 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8509 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8510
8511 // move a little
8512 processPosition(mapper, 150, 250);
8513 processSync(mapper);
8514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8515 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8516 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8517 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8518
8519 // down when BTN_TOUCH is pressed, pressure defaults to 1
8520 processKey(mapper, BTN_TOUCH, 1);
8521 processSync(mapper);
8522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8523 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8525 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8526
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8530 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8531
8532 // up when BTN_TOUCH is released, hover restored
8533 processKey(mapper, BTN_TOUCH, 0);
8534 processSync(mapper);
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8537 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8538 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8539
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // exit hover when pointer goes away
8551 processId(mapper, -1);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8556 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8557}
8558
8559TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008560 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008561 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008562 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008563 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008564
8565 NotifyMotionArgs motionArgs;
8566
8567 // initially hovering because pressure is 0
8568 processId(mapper, 1);
8569 processPosition(mapper, 100, 200);
8570 processPressure(mapper, 0);
8571 processSync(mapper);
8572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8575 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8579 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8580 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8581
8582 // move a little
8583 processPosition(mapper, 150, 250);
8584 processSync(mapper);
8585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8586 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8588 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8589
8590 // down when pressure becomes non-zero
8591 processPressure(mapper, RAW_PRESSURE_MAX);
8592 processSync(mapper);
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8594 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8595 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8596 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8597
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8601 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8602
8603 // up when pressure becomes 0, hover restored
8604 processPressure(mapper, 0);
8605 processSync(mapper);
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8608 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8609 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8610
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, 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_HOVER_MOVE, motionArgs.action);
8618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8620
8621 // exit hover when pointer goes away
8622 processId(mapper, -1);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8627 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8628}
8629
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008630/**
8631 * Set the input device port <--> display port associations, and check that the
8632 * events are routed to the display that matches the display port.
8633 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8634 */
8635TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008636 const std::string usb2 = "USB2";
8637 const uint8_t hdmi1 = 0;
8638 const uint8_t hdmi2 = 1;
8639 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008640 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008641
8642 addConfigurationProperty("touch.deviceType", "touchScreen");
8643 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008644 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008645
8646 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8647 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8648
8649 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8650 // for this input device is specified, and the matching viewport is not present,
8651 // the input device should be disabled (at the mapper level).
8652
8653 // Add viewport for display 2 on hdmi2
8654 prepareSecondaryDisplay(type, hdmi2);
8655 // Send a touch event
8656 processPosition(mapper, 100, 100);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8659
8660 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008661 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008662 // Send a touch event again
8663 processPosition(mapper, 100, 100);
8664 processSync(mapper);
8665
8666 NotifyMotionArgs args;
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8668 ASSERT_EQ(DISPLAY_ID, args.displayId);
8669}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008671TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8672 addConfigurationProperty("touch.deviceType", "touchScreen");
8673 prepareAxes(POSITION);
8674 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8675
8676 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8677
Michael Wrighta9cf4192022-12-01 23:46:39 +00008678 prepareDisplay(ui::ROTATION_0);
8679 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008680
8681 // Send a touch event
8682 processPosition(mapper, 100, 100);
8683 processSync(mapper);
8684
8685 NotifyMotionArgs args;
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8687 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8688}
8689
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008690TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008691 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008692 std::shared_ptr<FakePointerController> fakePointerController =
8693 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008694 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008695 fakePointerController->setPosition(100, 200);
8696 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008697 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008698
Garfield Tan888a6a42020-01-09 11:39:16 -08008699 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008700 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008701
Michael Wrighta9cf4192022-12-01 23:46:39 +00008702 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008703 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008705
Harry Cutts16a24cc2022-10-26 15:22:19 +00008706 // Check source is a touchpad that would obtain the PointerController.
8707 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008708
8709 NotifyMotionArgs motionArgs;
8710 processPosition(mapper, 100, 100);
8711 processSync(mapper);
8712
8713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8714 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8715 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8716}
8717
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008718/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008719 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8720 */
8721TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8722 addConfigurationProperty("touch.deviceType", "touchScreen");
8723 prepareAxes(POSITION);
8724 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8725
Michael Wrighta9cf4192022-12-01 23:46:39 +00008726 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008727 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8728 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8729 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8730 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8731
8732 NotifyMotionArgs args;
8733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8734 ASSERT_EQ(26, args.readTime);
8735
8736 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8737 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8738 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8739
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8741 ASSERT_EQ(33, args.readTime);
8742}
8743
8744/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008745 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8746 * events should not be delivered to the listener.
8747 */
8748TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8749 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008750 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008751 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8752 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008753 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8754 prepareAxes(POSITION);
8755 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8756
8757 NotifyMotionArgs motionArgs;
8758 processPosition(mapper, 100, 100);
8759 processSync(mapper);
8760
8761 mFakeListener->assertNotifyMotionWasNotCalled();
8762}
8763
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008764/**
8765 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8766 * the touch mapper can process the events and the events can be delivered to the listener.
8767 */
8768TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8769 addConfigurationProperty("touch.deviceType", "touchScreen");
8770 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008771 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8772 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008773 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8774 prepareAxes(POSITION);
8775 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8776
8777 NotifyMotionArgs motionArgs;
8778 processPosition(mapper, 100, 100);
8779 processSync(mapper);
8780
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8782 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8783}
8784
Garfield Tanc734e4f2021-01-15 20:01:39 -08008785TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8786 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008787 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008788 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8789 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008790 std::optional<DisplayViewport> optionalDisplayViewport =
8791 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8792 ASSERT_TRUE(optionalDisplayViewport.has_value());
8793 DisplayViewport displayViewport = *optionalDisplayViewport;
8794
8795 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8796 prepareAxes(POSITION);
8797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8798
8799 // Finger down
8800 int32_t x = 100, y = 100;
8801 processPosition(mapper, x, y);
8802 processSync(mapper);
8803
8804 NotifyMotionArgs motionArgs;
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8806 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8807
8808 // Deactivate display viewport
8809 displayViewport.isActive = false;
8810 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8811 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8812
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008813 // The ongoing touch should be canceled immediately
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8816
8817 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008818 x += 10, y += 10;
8819 processPosition(mapper, x, y);
8820 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008822
8823 // Reactivate display viewport
8824 displayViewport.isActive = true;
8825 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8826 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8827
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008828 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008829 x += 10, y += 10;
8830 processPosition(mapper, x, y);
8831 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8833 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008834}
8835
Arthur Hung7c645402019-01-25 17:45:42 +08008836TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8837 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008838 prepareAxes(POSITION | ID | SLOT);
8839 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008840 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008841
8842 // Create the second touch screen device, and enable multi fingers.
8843 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008844 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008845 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008846 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008847 std::shared_ptr<InputDevice> device2 =
8848 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008849 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008850
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008851 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8852 0 /*flat*/, 0 /*fuzz*/);
8853 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8854 0 /*flat*/, 0 /*fuzz*/);
8855 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8856 0 /*flat*/, 0 /*fuzz*/);
8857 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8858 0 /*flat*/, 0 /*fuzz*/);
8859 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8860 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8861 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008862
8863 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008864 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008865 std::list<NotifyArgs> unused =
8866 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8867 0 /*changes*/);
8868 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008869
8870 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008871 std::shared_ptr<FakePointerController> fakePointerController =
8872 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008873 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008874
8875 // Setup policy for associated displays and show touches.
8876 const uint8_t hdmi1 = 0;
8877 const uint8_t hdmi2 = 1;
8878 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8879 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8880 mFakePolicy->setShowTouches(true);
8881
8882 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008883 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008884 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008885
8886 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008887 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8888 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8889 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008890
8891 // Two fingers down at default display.
8892 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8893 processPosition(mapper, x1, y1);
8894 processId(mapper, 1);
8895 processSlot(mapper, 1);
8896 processPosition(mapper, x2, y2);
8897 processId(mapper, 2);
8898 processSync(mapper);
8899
8900 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8901 fakePointerController->getSpots().find(DISPLAY_ID);
8902 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8903 ASSERT_EQ(size_t(2), iter->second.size());
8904
8905 // Two fingers down at second display.
8906 processPosition(mapper2, x1, y1);
8907 processId(mapper2, 1);
8908 processSlot(mapper2, 1);
8909 processPosition(mapper2, x2, y2);
8910 processId(mapper2, 2);
8911 processSync(mapper2);
8912
8913 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8914 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8915 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008916
8917 // Disable the show touches configuration and ensure the spots are cleared.
8918 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008919 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8920 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008921
8922 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008923}
8924
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008925TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008926 prepareAxes(POSITION);
8927 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008928 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008929 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008930
8931 NotifyMotionArgs motionArgs;
8932 // Unrotated video frame
8933 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8934 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008935 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008936 processPosition(mapper, 100, 200);
8937 processSync(mapper);
8938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8939 ASSERT_EQ(frames, motionArgs.videoFrames);
8940
8941 // Subsequent touch events should not have any videoframes
8942 // This is implemented separately in FakeEventHub,
8943 // but that should match the behaviour of TouchVideoDevice.
8944 processPosition(mapper, 200, 200);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8948}
8949
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008950TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008951 prepareAxes(POSITION);
8952 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008953 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008954 // Unrotated video frame
8955 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8956 NotifyMotionArgs motionArgs;
8957
8958 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008959 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008960 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8961 clearViewports();
8962 prepareDisplay(orientation);
8963 std::vector<TouchVideoFrame> frames{frame};
8964 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8965 processPosition(mapper, 100, 200);
8966 processSync(mapper);
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 ASSERT_EQ(frames, motionArgs.videoFrames);
8969 }
8970}
8971
8972TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8973 prepareAxes(POSITION);
8974 addConfigurationProperty("touch.deviceType", "touchScreen");
8975 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8976 // orientation-aware are affected by display rotation.
8977 addConfigurationProperty("touch.orientationAware", "0");
8978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8979 // Unrotated video frame
8980 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8981 NotifyMotionArgs motionArgs;
8982
8983 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008984 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008985 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
8986 clearViewports();
8987 prepareDisplay(orientation);
8988 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008989 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008990 processPosition(mapper, 100, 200);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008993 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8994 // compared to the display. This is so that when the window transform (which contains the
8995 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8996 // window's coordinate space.
8997 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008998 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008999
9000 // Release finger.
9001 processSync(mapper);
9002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009003 }
9004}
9005
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009006TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009007 prepareAxes(POSITION);
9008 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009009 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009010 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9011 // so mix these.
9012 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9013 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9014 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9015 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9016 NotifyMotionArgs motionArgs;
9017
Michael Wrighta9cf4192022-12-01 23:46:39 +00009018 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009019 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009020 processPosition(mapper, 100, 200);
9021 processSync(mapper);
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009023 ASSERT_EQ(frames, motionArgs.videoFrames);
9024}
9025
9026TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9027 prepareAxes(POSITION);
9028 addConfigurationProperty("touch.deviceType", "touchScreen");
9029 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9030 // orientation-aware are affected by display rotation.
9031 addConfigurationProperty("touch.orientationAware", "0");
9032 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9033 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9034 // so mix these.
9035 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9036 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9037 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9038 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9039 NotifyMotionArgs motionArgs;
9040
Michael Wrighta9cf4192022-12-01 23:46:39 +00009041 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009042 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9043 processPosition(mapper, 100, 200);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9047 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9048 // compared to the display. This is so that when the window transform (which contains the
9049 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9050 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009051 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009052 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009053 ASSERT_EQ(frames, motionArgs.videoFrames);
9054}
9055
Arthur Hung9da14732019-09-02 16:16:58 +08009056/**
9057 * If we had defined port associations, but the viewport is not ready, the touch device would be
9058 * expected to be disabled, and it should be enabled after the viewport has found.
9059 */
9060TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009061 constexpr uint8_t hdmi2 = 1;
9062 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009063 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009064
9065 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9066
9067 addConfigurationProperty("touch.deviceType", "touchScreen");
9068 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009069 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009070
9071 ASSERT_EQ(mDevice->isEnabled(), false);
9072
9073 // Add display on hdmi2, the device should be enabled and can receive touch event.
9074 prepareSecondaryDisplay(type, hdmi2);
9075 ASSERT_EQ(mDevice->isEnabled(), true);
9076
9077 // Send a touch event.
9078 processPosition(mapper, 100, 100);
9079 processSync(mapper);
9080
9081 NotifyMotionArgs args;
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9083 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9084}
9085
Arthur Hung421eb1c2020-01-16 00:09:42 +08009086TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009087 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009088 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009089 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009090 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009091
9092 NotifyMotionArgs motionArgs;
9093
9094 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9095 // finger down
9096 processId(mapper, 1);
9097 processPosition(mapper, x1, y1);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9102
9103 // finger move
9104 processId(mapper, 1);
9105 processPosition(mapper, x2, y2);
9106 processSync(mapper);
9107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9109 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9110
9111 // finger up.
9112 processId(mapper, -1);
9113 processSync(mapper);
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9117
9118 // new finger down
9119 processId(mapper, 1);
9120 processPosition(mapper, x3, y3);
9121 processSync(mapper);
9122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9123 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9125}
9126
9127/**
arthurhungcc7f9802020-04-30 17:55:40 +08009128 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9129 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009130 */
arthurhungcc7f9802020-04-30 17:55:40 +08009131TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009132 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009133 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009134 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009135 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009136
9137 NotifyMotionArgs motionArgs;
9138
9139 // default tool type is finger
9140 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009141 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009142 processPosition(mapper, x1, y1);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9145 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9147
9148 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9149 processToolType(mapper, MT_TOOL_PALM);
9150 processSync(mapper);
9151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9152 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9153
9154 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009155 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009156 processPosition(mapper, x2, y2);
9157 processSync(mapper);
9158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9159
9160 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009161 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009162 processSync(mapper);
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9164
9165 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009166 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009167 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009168 processPosition(mapper, x3, y3);
9169 processSync(mapper);
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9172 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9173}
9174
arthurhungbf89a482020-04-17 17:37:55 +08009175/**
arthurhungcc7f9802020-04-30 17:55:40 +08009176 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9177 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009178 */
arthurhungcc7f9802020-04-30 17:55:40 +08009179TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009180 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009181 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009182 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9183 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9184
9185 NotifyMotionArgs motionArgs;
9186
9187 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009188 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9189 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009190 processPosition(mapper, x1, y1);
9191 processSync(mapper);
9192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9193 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9194 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9195
9196 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009197 processSlot(mapper, SECOND_SLOT);
9198 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009199 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009200 processSync(mapper);
9201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009202 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9204
9205 // If the tool type of the first finger changes to MT_TOOL_PALM,
9206 // we expect to receive ACTION_POINTER_UP with cancel flag.
9207 processSlot(mapper, FIRST_SLOT);
9208 processId(mapper, FIRST_TRACKING_ID);
9209 processToolType(mapper, MT_TOOL_PALM);
9210 processSync(mapper);
9211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009212 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009213 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9214
9215 // The following MOVE events of second finger should be processed.
9216 processSlot(mapper, SECOND_SLOT);
9217 processId(mapper, SECOND_TRACKING_ID);
9218 processPosition(mapper, x2 + 1, y2 + 1);
9219 processSync(mapper);
9220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9222 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9223
9224 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9225 // it. Second finger receive move.
9226 processSlot(mapper, FIRST_SLOT);
9227 processId(mapper, INVALID_TRACKING_ID);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9231 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9232
9233 // Second finger keeps moving.
9234 processSlot(mapper, SECOND_SLOT);
9235 processId(mapper, SECOND_TRACKING_ID);
9236 processPosition(mapper, x2 + 2, y2 + 2);
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 // Second finger up.
9243 processId(mapper, INVALID_TRACKING_ID);
9244 processSync(mapper);
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9246 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9247 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9248}
9249
9250/**
9251 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9252 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9253 */
9254TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9255 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009256 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009257 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9258 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9259
9260 NotifyMotionArgs motionArgs;
9261
9262 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9263 // First finger down.
9264 processId(mapper, FIRST_TRACKING_ID);
9265 processPosition(mapper, x1, y1);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9270
9271 // Second finger down.
9272 processSlot(mapper, SECOND_SLOT);
9273 processId(mapper, SECOND_TRACKING_ID);
9274 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009277 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9279
arthurhungcc7f9802020-04-30 17:55:40 +08009280 // If the tool type of the first finger changes to MT_TOOL_PALM,
9281 // we expect to receive ACTION_POINTER_UP with cancel flag.
9282 processSlot(mapper, FIRST_SLOT);
9283 processId(mapper, FIRST_TRACKING_ID);
9284 processToolType(mapper, MT_TOOL_PALM);
9285 processSync(mapper);
9286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009287 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009288 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9289
9290 // Second finger keeps moving.
9291 processSlot(mapper, SECOND_SLOT);
9292 processId(mapper, SECOND_TRACKING_ID);
9293 processPosition(mapper, x2 + 1, y2 + 1);
9294 processSync(mapper);
9295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9297
9298 // second finger becomes palm, receive cancel due to only 1 finger is active.
9299 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009300 processToolType(mapper, MT_TOOL_PALM);
9301 processSync(mapper);
9302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9303 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9304
arthurhungcc7f9802020-04-30 17:55:40 +08009305 // third finger down.
9306 processSlot(mapper, THIRD_SLOT);
9307 processId(mapper, THIRD_TRACKING_ID);
9308 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009309 processPosition(mapper, x3, y3);
9310 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9312 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9313 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009314 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9315
9316 // third finger move
9317 processId(mapper, THIRD_TRACKING_ID);
9318 processPosition(mapper, x3 + 1, y3 + 1);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9321 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9322
9323 // first finger up, third finger receive move.
9324 processSlot(mapper, FIRST_SLOT);
9325 processId(mapper, INVALID_TRACKING_ID);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9329 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9330
9331 // second finger up, third finger receive move.
9332 processSlot(mapper, SECOND_SLOT);
9333 processId(mapper, INVALID_TRACKING_ID);
9334 processSync(mapper);
9335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9336 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9337 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9338
9339 // third finger up.
9340 processSlot(mapper, THIRD_SLOT);
9341 processId(mapper, INVALID_TRACKING_ID);
9342 processSync(mapper);
9343 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9344 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9345 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9346}
9347
9348/**
9349 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9350 * and the active finger could still be allowed to receive the events
9351 */
9352TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9353 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009354 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009355 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9356 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9357
9358 NotifyMotionArgs motionArgs;
9359
9360 // default tool type is finger
9361 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9362 processId(mapper, FIRST_TRACKING_ID);
9363 processPosition(mapper, x1, y1);
9364 processSync(mapper);
9365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9368
9369 // Second finger down.
9370 processSlot(mapper, SECOND_SLOT);
9371 processId(mapper, SECOND_TRACKING_ID);
9372 processPosition(mapper, x2, y2);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009375 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009376 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9377
9378 // If the tool type of the second finger changes to MT_TOOL_PALM,
9379 // we expect to receive ACTION_POINTER_UP with cancel flag.
9380 processId(mapper, SECOND_TRACKING_ID);
9381 processToolType(mapper, MT_TOOL_PALM);
9382 processSync(mapper);
9383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009384 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009385 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9386
9387 // The following MOVE event should be processed.
9388 processSlot(mapper, FIRST_SLOT);
9389 processId(mapper, FIRST_TRACKING_ID);
9390 processPosition(mapper, x1 + 1, y1 + 1);
9391 processSync(mapper);
9392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9393 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9394 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9395
9396 // second finger up.
9397 processSlot(mapper, SECOND_SLOT);
9398 processId(mapper, INVALID_TRACKING_ID);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402
9403 // first finger keep moving
9404 processSlot(mapper, FIRST_SLOT);
9405 processId(mapper, FIRST_TRACKING_ID);
9406 processPosition(mapper, x1 + 2, y1 + 2);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9410
9411 // first finger up.
9412 processId(mapper, INVALID_TRACKING_ID);
9413 processSync(mapper);
9414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9415 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9416 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009417}
9418
Arthur Hung9ad18942021-06-19 02:04:46 +00009419/**
9420 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9421 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9422 * cause slot be valid again.
9423 */
9424TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9425 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009426 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009427 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9428 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9429
9430 NotifyMotionArgs motionArgs;
9431
9432 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9433 // First finger down.
9434 processId(mapper, FIRST_TRACKING_ID);
9435 processPosition(mapper, x1, y1);
9436 processPressure(mapper, RAW_PRESSURE_MAX);
9437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9439 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9440 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9441
9442 // First finger move.
9443 processId(mapper, FIRST_TRACKING_ID);
9444 processPosition(mapper, x1 + 1, y1 + 1);
9445 processPressure(mapper, RAW_PRESSURE_MAX);
9446 processSync(mapper);
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9449 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9450
9451 // Second finger down.
9452 processSlot(mapper, SECOND_SLOT);
9453 processId(mapper, SECOND_TRACKING_ID);
9454 processPosition(mapper, x2, y2);
9455 processPressure(mapper, RAW_PRESSURE_MAX);
9456 processSync(mapper);
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009458 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009459 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9460
9461 // second finger up with some unexpected data.
9462 processSlot(mapper, SECOND_SLOT);
9463 processId(mapper, INVALID_TRACKING_ID);
9464 processPosition(mapper, x2, y2);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009467 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009468 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9469
9470 // first finger up with some unexpected data.
9471 processSlot(mapper, FIRST_SLOT);
9472 processId(mapper, INVALID_TRACKING_ID);
9473 processPosition(mapper, x2, y2);
9474 processPressure(mapper, RAW_PRESSURE_MAX);
9475 processSync(mapper);
9476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9477 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9478 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9479}
9480
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009481TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9482 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009483 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009484 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9485 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9486
9487 // First finger down.
9488 processId(mapper, FIRST_TRACKING_ID);
9489 processPosition(mapper, 100, 200);
9490 processPressure(mapper, RAW_PRESSURE_MAX);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9493 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9494
9495 // Second finger down.
9496 processSlot(mapper, SECOND_SLOT);
9497 processId(mapper, SECOND_TRACKING_ID);
9498 processPosition(mapper, 300, 400);
9499 processPressure(mapper, RAW_PRESSURE_MAX);
9500 processSync(mapper);
9501 ASSERT_NO_FATAL_FAILURE(
9502 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9503
9504 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009505 // preserved. Resetting should cancel the ongoing gesture.
9506 resetMapper(mapper, ARBITRARY_TIME);
9507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9508 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009509
9510 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9511 // the existing touch state to generate a down event.
9512 processPosition(mapper, 301, 302);
9513 processSync(mapper);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9515 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9517 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9518
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9520}
9521
9522TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9523 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009524 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009525 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9526 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9527
9528 // First finger touches down and releases.
9529 processId(mapper, FIRST_TRACKING_ID);
9530 processPosition(mapper, 100, 200);
9531 processPressure(mapper, RAW_PRESSURE_MAX);
9532 processSync(mapper);
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9534 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9535 processId(mapper, INVALID_TRACKING_ID);
9536 processSync(mapper);
9537 ASSERT_NO_FATAL_FAILURE(
9538 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9539
9540 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9541 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009542 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9544
9545 // Send an empty sync frame. Since there are no pointers, no events are generated.
9546 processSync(mapper);
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9548}
9549
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009550TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009551 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009552 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009553 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9554 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009556
9557 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9558 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9559 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9560 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9561 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9562
9563 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009564 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009565 processId(mapper, FIRST_TRACKING_ID);
9566 processToolType(mapper, MT_TOOL_PEN);
9567 processPosition(mapper, 100, 200);
9568 processPressure(mapper, RAW_PRESSURE_MAX);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9571 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9572 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9573 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9574
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009575 // Now that we know the device supports styluses, ensure that the device is re-configured with
9576 // the stylus source.
9577 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9578 {
9579 const auto& devices = mReader->getInputDevices();
9580 auto deviceInfo =
9581 std::find_if(devices.begin(), devices.end(),
9582 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9583 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9584 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9585 }
9586
9587 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9589
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009590 processId(mapper, INVALID_TRACKING_ID);
9591 processSync(mapper);
9592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9594 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9595 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009596}
9597
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009598// --- MultiTouchInputMapperTest_ExternalDevice ---
9599
9600class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9601protected:
Chris Yea52ade12020-08-27 16:49:20 -07009602 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009603};
9604
9605/**
9606 * Expect fallback to internal viewport if device is external and external viewport is not present.
9607 */
9608TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9609 prepareAxes(POSITION);
9610 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009611 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009612 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9613
9614 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9615
9616 NotifyMotionArgs motionArgs;
9617
9618 // Expect the event to be sent to the internal viewport,
9619 // because an external viewport is not present.
9620 processPosition(mapper, 100, 100);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9623 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9624
9625 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009626 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009627 processPosition(mapper, 100, 100);
9628 processSync(mapper);
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9630 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9631}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009632
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009633TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9634 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9635 std::shared_ptr<FakePointerController> fakePointerController =
9636 std::make_shared<FakePointerController>();
9637 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9638 fakePointerController->setPosition(0, 0);
9639 fakePointerController->setButtonState(0);
9640
9641 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009642 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009643 prepareAxes(POSITION | ID | SLOT);
9644 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9646 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009647 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009648 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9649
9650 // captured touchpad should be a touchpad source
9651 NotifyDeviceResetArgs resetArgs;
9652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9653 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9654
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009655 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009656
9657 const InputDeviceInfo::MotionRange* relRangeX =
9658 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9659 ASSERT_NE(relRangeX, nullptr);
9660 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9661 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9662 const InputDeviceInfo::MotionRange* relRangeY =
9663 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9664 ASSERT_NE(relRangeY, nullptr);
9665 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9666 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9667
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009668 // run captured pointer tests - note that this is unscaled, so input listener events should be
9669 // identical to what the hardware sends (accounting for any
9670 // calibration).
9671 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009672 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009673 processId(mapper, 1);
9674 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9675 processKey(mapper, BTN_TOUCH, 1);
9676 processSync(mapper);
9677
9678 // expect coord[0] to contain initial location of touch 0
9679 NotifyMotionArgs args;
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9682 ASSERT_EQ(1U, args.pointerCount);
9683 ASSERT_EQ(0, args.pointerProperties[0].id);
9684 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9685 ASSERT_NO_FATAL_FAILURE(
9686 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9687
9688 // FINGER 1 DOWN
9689 processSlot(mapper, 1);
9690 processId(mapper, 2);
9691 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9692 processSync(mapper);
9693
9694 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009696 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009697 ASSERT_EQ(2U, args.pointerCount);
9698 ASSERT_EQ(0, args.pointerProperties[0].id);
9699 ASSERT_EQ(1, args.pointerProperties[1].id);
9700 ASSERT_NO_FATAL_FAILURE(
9701 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9702 ASSERT_NO_FATAL_FAILURE(
9703 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9704
9705 // FINGER 1 MOVE
9706 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9707 processSync(mapper);
9708
9709 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9710 // from move
9711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9713 ASSERT_NO_FATAL_FAILURE(
9714 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9715 ASSERT_NO_FATAL_FAILURE(
9716 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9717
9718 // FINGER 0 MOVE
9719 processSlot(mapper, 0);
9720 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9721 processSync(mapper);
9722
9723 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9726 ASSERT_NO_FATAL_FAILURE(
9727 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9728 ASSERT_NO_FATAL_FAILURE(
9729 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9730
9731 // BUTTON DOWN
9732 processKey(mapper, BTN_LEFT, 1);
9733 processSync(mapper);
9734
9735 // touchinputmapper design sends a move before button press
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9739 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9740
9741 // BUTTON UP
9742 processKey(mapper, BTN_LEFT, 0);
9743 processSync(mapper);
9744
9745 // touchinputmapper design sends a move after button release
9746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9747 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9750
9751 // FINGER 0 UP
9752 processId(mapper, -1);
9753 processSync(mapper);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9755 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9756
9757 // FINGER 1 MOVE
9758 processSlot(mapper, 1);
9759 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9760 processSync(mapper);
9761
9762 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9765 ASSERT_EQ(1U, args.pointerCount);
9766 ASSERT_EQ(1, args.pointerProperties[0].id);
9767 ASSERT_NO_FATAL_FAILURE(
9768 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9769
9770 // FINGER 1 UP
9771 processId(mapper, -1);
9772 processKey(mapper, BTN_TOUCH, 0);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9776
Harry Cutts16a24cc2022-10-26 15:22:19 +00009777 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009778 mFakePolicy->setPointerCapture(false);
9779 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009781 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009782}
9783
9784TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9785 std::shared_ptr<FakePointerController> fakePointerController =
9786 std::make_shared<FakePointerController>();
9787 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9788 fakePointerController->setPosition(0, 0);
9789 fakePointerController->setButtonState(0);
9790
9791 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009792 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009793 prepareAxes(POSITION | ID | SLOT);
9794 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9795 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009796 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009797 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9798 // run uncaptured pointer tests - pushes out generic events
9799 // FINGER 0 DOWN
9800 processId(mapper, 3);
9801 processPosition(mapper, 100, 100);
9802 processKey(mapper, BTN_TOUCH, 1);
9803 processSync(mapper);
9804
9805 // start at (100,100), cursor should be at (0,0) * scale
9806 NotifyMotionArgs args;
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9808 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9809 ASSERT_NO_FATAL_FAILURE(
9810 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9811
9812 // FINGER 0 MOVE
9813 processPosition(mapper, 200, 200);
9814 processSync(mapper);
9815
9816 // compute scaling to help with touch position checking
9817 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9818 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9819 float scale =
9820 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9821
9822 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9824 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9825 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9826 0, 0, 0, 0, 0, 0, 0));
9827}
9828
9829TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9830 std::shared_ptr<FakePointerController> fakePointerController =
9831 std::make_shared<FakePointerController>();
9832
Michael Wrighta9cf4192022-12-01 23:46:39 +00009833 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009834 prepareAxes(POSITION | ID | SLOT);
9835 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009836 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009837 mFakePolicy->setPointerCapture(false);
9838 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9839
Harry Cutts16a24cc2022-10-26 15:22:19 +00009840 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9841 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009842
Harry Cutts16a24cc2022-10-26 15:22:19 +00009843 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009844 mFakePolicy->setPointerCapture(true);
9845 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9847}
9848
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009849// --- BluetoothMultiTouchInputMapperTest ---
9850
9851class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9852protected:
9853 void SetUp() override {
9854 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9855 }
9856};
9857
9858TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9859 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009860 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009861 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9862 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9863
9864 nsecs_t kernelEventTime = ARBITRARY_TIME;
9865 nsecs_t expectedEventTime = ARBITRARY_TIME;
9866 // Touch down.
9867 processId(mapper, FIRST_TRACKING_ID);
9868 processPosition(mapper, 100, 200);
9869 processPressure(mapper, RAW_PRESSURE_MAX);
9870 processSync(mapper, ARBITRARY_TIME);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9872 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9873
9874 // Process several events that come in quick succession, according to their timestamps.
9875 for (int i = 0; i < 3; i++) {
9876 constexpr static nsecs_t delta = ms2ns(1);
9877 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9878 kernelEventTime += delta;
9879 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9880
9881 processPosition(mapper, 101 + i, 201 + i);
9882 processSync(mapper, kernelEventTime);
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9885 WithEventTime(expectedEventTime))));
9886 }
9887
9888 // Release the touch.
9889 processId(mapper, INVALID_TRACKING_ID);
9890 processPressure(mapper, RAW_PRESSURE_MIN);
9891 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9893 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9894 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9895}
9896
9897// --- MultiTouchPointerModeTest ---
9898
HQ Liue6983c72022-04-19 22:14:56 +00009899class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9900protected:
9901 float mPointerMovementScale;
9902 float mPointerXZoomScale;
9903 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9904 addConfigurationProperty("touch.deviceType", "pointer");
9905 std::shared_ptr<FakePointerController> fakePointerController =
9906 std::make_shared<FakePointerController>();
9907 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9908 fakePointerController->setPosition(0, 0);
9909 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +00009910 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009911
9912 prepareAxes(POSITION);
9913 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9914 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9915 // needs to be disabled, and the pointer gesture needs to be enabled.
9916 mFakePolicy->setPointerCapture(false);
9917 mFakePolicy->setPointerGestureEnabled(true);
9918 mFakePolicy->setPointerController(fakePointerController);
9919
9920 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9921 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9922 mPointerMovementScale =
9923 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9924 mPointerXZoomScale =
9925 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9926 }
9927
9928 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9930 /*flat*/ 0,
9931 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9932 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9933 /*flat*/ 0,
9934 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9935 }
9936};
9937
9938/**
9939 * Two fingers down on a pointer mode touch pad. The width
9940 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9941 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9942 * be greater than the both value to be freeform gesture, so that after two
9943 * fingers start to move downwards, the gesture should be swipe.
9944 */
9945TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9946 // The min freeform gesture width is 25units/mm x 30mm = 750
9947 // which is greater than fraction of the diagnal length of the touchpad (349).
9948 // Thus, MaxSwipWidth is 750.
9949 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
9950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9951 NotifyMotionArgs motionArgs;
9952
9953 // Two fingers down at once.
9954 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9955 // Pointer's initial position is used the [0,0] coordinate.
9956 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9957
9958 processId(mapper, FIRST_TRACKING_ID);
9959 processPosition(mapper, x1, y1);
9960 processMTSync(mapper);
9961 processId(mapper, SECOND_TRACKING_ID);
9962 processPosition(mapper, x2, y2);
9963 processMTSync(mapper);
9964 processSync(mapper);
9965
9966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9967 ASSERT_EQ(1U, motionArgs.pointerCount);
9968 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9969 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009970 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009971 ASSERT_NO_FATAL_FAILURE(
9972 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9973
9974 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9975 // that there should be 1 pointer.
9976 int32_t movingDistance = 200;
9977 y1 += movingDistance;
9978 y2 += movingDistance;
9979
9980 processId(mapper, FIRST_TRACKING_ID);
9981 processPosition(mapper, x1, y1);
9982 processMTSync(mapper);
9983 processId(mapper, SECOND_TRACKING_ID);
9984 processPosition(mapper, x2, y2);
9985 processMTSync(mapper);
9986 processSync(mapper);
9987
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 ASSERT_EQ(1U, motionArgs.pointerCount);
9990 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9991 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009992 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9994 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9995 0, 0, 0, 0));
9996}
9997
9998/**
9999 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10000 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10001 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10002 * value to be freeform gesture, so that after two fingers start to move downwards,
10003 * the gesture should be swipe.
10004 */
10005TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10006 // The min freeform gesture width is 5units/mm x 30mm = 150
10007 // which is greater than fraction of the diagnal length of the touchpad (349).
10008 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10009 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10011 NotifyMotionArgs motionArgs;
10012
10013 // Two fingers down at once.
10014 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10015 // Pointer's initial position is used the [0,0] coordinate.
10016 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10017
10018 processId(mapper, FIRST_TRACKING_ID);
10019 processPosition(mapper, x1, y1);
10020 processMTSync(mapper);
10021 processId(mapper, SECOND_TRACKING_ID);
10022 processPosition(mapper, x2, y2);
10023 processMTSync(mapper);
10024 processSync(mapper);
10025
10026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10027 ASSERT_EQ(1U, motionArgs.pointerCount);
10028 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10029 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010030 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010031 ASSERT_NO_FATAL_FAILURE(
10032 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10033
10034 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10035 // and there should be 1 pointer.
10036 int32_t movingDistance = 200;
10037 y1 += movingDistance;
10038 y2 += movingDistance;
10039
10040 processId(mapper, FIRST_TRACKING_ID);
10041 processPosition(mapper, x1, y1);
10042 processMTSync(mapper);
10043 processId(mapper, SECOND_TRACKING_ID);
10044 processPosition(mapper, x2, y2);
10045 processMTSync(mapper);
10046 processSync(mapper);
10047
10048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10049 ASSERT_EQ(1U, motionArgs.pointerCount);
10050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10051 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010052 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010053 // New coordinate is the scaled relative coordinate from the initial coordinate.
10054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10055 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10056 0, 0, 0, 0));
10057}
10058
10059/**
10060 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10061 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10062 * freeform gestures after two fingers start to move downwards.
10063 */
10064TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10065 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10066 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10067
10068 NotifyMotionArgs motionArgs;
10069
10070 // Two fingers down at once. Wider than the max swipe width.
10071 // The gesture is expected to be PRESS, then transformed to FREEFORM
10072 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10073
10074 processId(mapper, FIRST_TRACKING_ID);
10075 processPosition(mapper, x1, y1);
10076 processMTSync(mapper);
10077 processId(mapper, SECOND_TRACKING_ID);
10078 processPosition(mapper, x2, y2);
10079 processMTSync(mapper);
10080 processSync(mapper);
10081
10082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10083 ASSERT_EQ(1U, motionArgs.pointerCount);
10084 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10085 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010086 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010087 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10088 ASSERT_NO_FATAL_FAILURE(
10089 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10090
10091 int32_t movingDistance = 200;
10092
10093 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10094 // then two down events for two pointers.
10095 y1 += movingDistance;
10096 y2 += movingDistance;
10097
10098 processId(mapper, FIRST_TRACKING_ID);
10099 processPosition(mapper, x1, y1);
10100 processMTSync(mapper);
10101 processId(mapper, SECOND_TRACKING_ID);
10102 processPosition(mapper, x2, y2);
10103 processMTSync(mapper);
10104 processSync(mapper);
10105
10106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10107 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10108 ASSERT_EQ(1U, motionArgs.pointerCount);
10109 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10111 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10112 ASSERT_EQ(1U, motionArgs.pointerCount);
10113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10115 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010116 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010117 ASSERT_EQ(2U, motionArgs.pointerCount);
10118 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10119 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010120 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010121 // Two pointers' scaled relative coordinates from their initial centroid.
10122 // Initial y coordinates are 0 as y1 and y2 have the same value.
10123 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10124 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10125 // When pointers move, the new coordinates equal to the initial coordinates plus
10126 // scaled moving distance.
10127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10128 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10129 0, 0, 0, 0));
10130 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10131 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10132 0, 0, 0, 0));
10133
10134 // Move two fingers down again, expect one MOVE motion event.
10135 y1 += movingDistance;
10136 y2 += movingDistance;
10137
10138 processId(mapper, FIRST_TRACKING_ID);
10139 processPosition(mapper, x1, y1);
10140 processMTSync(mapper);
10141 processId(mapper, SECOND_TRACKING_ID);
10142 processPosition(mapper, x2, y2);
10143 processMTSync(mapper);
10144 processSync(mapper);
10145
10146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10147 ASSERT_EQ(2U, motionArgs.pointerCount);
10148 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10149 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010150 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10152 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10153 0, 0, 0, 0, 0));
10154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10155 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10156 0, 0, 0, 0, 0));
10157}
10158
Harry Cutts39b7ca22022-10-05 15:55:48 +000010159TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10160 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10161 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10162 NotifyMotionArgs motionArgs;
10163
10164 // Place two fingers down.
10165 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10166
10167 processId(mapper, FIRST_TRACKING_ID);
10168 processPosition(mapper, x1, y1);
10169 processMTSync(mapper);
10170 processId(mapper, SECOND_TRACKING_ID);
10171 processPosition(mapper, x2, y2);
10172 processMTSync(mapper);
10173 processSync(mapper);
10174
10175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10176 ASSERT_EQ(1U, motionArgs.pointerCount);
10177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10178 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10179 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10180 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10181
10182 // Move the two fingers down and to the left.
10183 int32_t movingDistance = 200;
10184 x1 -= movingDistance;
10185 y1 += movingDistance;
10186 x2 -= movingDistance;
10187 y2 += movingDistance;
10188
10189 processId(mapper, FIRST_TRACKING_ID);
10190 processPosition(mapper, x1, y1);
10191 processMTSync(mapper);
10192 processId(mapper, SECOND_TRACKING_ID);
10193 processPosition(mapper, x2, y2);
10194 processMTSync(mapper);
10195 processSync(mapper);
10196
10197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10198 ASSERT_EQ(1U, motionArgs.pointerCount);
10199 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10200 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10201 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10202 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10203}
10204
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010205TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10206 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10207 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10208 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10210
10211 // Start a stylus gesture.
10212 processKey(mapper, BTN_TOOL_PEN, 1);
10213 processId(mapper, FIRST_TRACKING_ID);
10214 processPosition(mapper, 100, 200);
10215 processSync(mapper);
10216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10217 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10218 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10219 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10220 // TODO(b/257078296): Pointer mode generates extra event.
10221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10222 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10223 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10224 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10226
10227 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10228 // gesture should be disabled.
10229 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10230 viewport->isActive = false;
10231 mFakePolicy->updateViewport(*viewport);
10232 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10234 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10235 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10236 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10237 // TODO(b/257078296): Pointer mode generates extra event.
10238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10240 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10241 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10243}
10244
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010245// --- JoystickInputMapperTest ---
10246
10247class JoystickInputMapperTest : public InputMapperTest {
10248protected:
10249 static const int32_t RAW_X_MIN;
10250 static const int32_t RAW_X_MAX;
10251 static const int32_t RAW_Y_MIN;
10252 static const int32_t RAW_Y_MAX;
10253
10254 void SetUp() override {
10255 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10256 }
10257 void prepareAxes() {
10258 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10260 }
10261
10262 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10264 }
10265
10266 void processSync(JoystickInputMapper& mapper) {
10267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10268 }
10269
Michael Wrighta9cf4192022-12-01 23:46:39 +000010270 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010271 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10272 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10273 NO_PORT, ViewportType::VIRTUAL);
10274 }
10275};
10276
10277const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10278const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10279const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10280const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10281
10282TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10283 prepareAxes();
10284 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10285
10286 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10287
Michael Wrighta9cf4192022-12-01 23:46:39 +000010288 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010289
10290 // Send an axis event
10291 processAxis(mapper, ABS_X, 100);
10292 processSync(mapper);
10293
10294 NotifyMotionArgs args;
10295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10296 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10297
10298 // Send another axis event
10299 processAxis(mapper, ABS_Y, 100);
10300 processSync(mapper);
10301
10302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10303 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10304}
10305
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010306// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010307
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010308class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010309protected:
10310 static const char* DEVICE_NAME;
10311 static const char* DEVICE_LOCATION;
10312 static const int32_t DEVICE_ID;
10313 static const int32_t DEVICE_GENERATION;
10314 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010315 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010316 static const int32_t EVENTHUB_ID;
10317
10318 std::shared_ptr<FakeEventHub> mFakeEventHub;
10319 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010320 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010321 std::unique_ptr<InstrumentedInputReader> mReader;
10322 std::shared_ptr<InputDevice> mDevice;
10323
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010324 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010325 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010326 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010327 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010328 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010329 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010330 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10331 }
10332
10333 void SetUp() override { SetUp(DEVICE_CLASSES); }
10334
10335 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010336 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010337 mFakePolicy.clear();
10338 }
10339
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010340 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010341 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10342 mReader->requestRefreshConfiguration(changes);
10343 mReader->loopOnce();
10344 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010345 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010346 }
10347
10348 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10349 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010350 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010351 InputDeviceIdentifier identifier;
10352 identifier.name = name;
10353 identifier.location = location;
10354 std::shared_ptr<InputDevice> device =
10355 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10356 identifier);
10357 mReader->pushNextDevice(device);
10358 mFakeEventHub->addDevice(eventHubId, name, classes);
10359 mReader->loopOnce();
10360 return device;
10361 }
10362
10363 template <class T, typename... Args>
10364 T& addControllerAndConfigure(Args... args) {
10365 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10366
10367 return controller;
10368 }
10369};
10370
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010371const char* PeripheralControllerTest::DEVICE_NAME = "device";
10372const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10373const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10374const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10375const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010376const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10377 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010378const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010379
10380// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010381class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010382protected:
10383 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010384 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010385 }
10386};
10387
10388TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010389 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010390
Harry Cuttsa5b71292022-11-28 12:56:17 +000010391 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10392 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10393 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010394}
10395
10396TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010397 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010398
Harry Cuttsa5b71292022-11-28 12:56:17 +000010399 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10400 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10401 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010402}
10403
10404// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010405class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010406protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010407 void SetUp() override {
10408 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10409 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010410};
10411
Chris Ye85758332021-05-16 23:05:17 -070010412TEST_F(LightControllerTest, MonoLight) {
10413 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010414 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010415 .maxBrightness = 255,
10416 .flags = InputLightClass::BRIGHTNESS,
10417 .path = ""};
10418 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010419
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010420 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010421 InputDeviceInfo info;
10422 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010423 std::vector<InputDeviceLightInfo> lights = info.getLights();
10424 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010425 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10426 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10427
10428 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10429 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10430}
10431
10432TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10433 RawLightInfo infoMono = {.id = 1,
10434 .name = "mono_keyboard_backlight",
10435 .maxBrightness = 255,
10436 .flags = InputLightClass::BRIGHTNESS |
10437 InputLightClass::KEYBOARD_BACKLIGHT,
10438 .path = ""};
10439 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10440
10441 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10442 InputDeviceInfo info;
10443 controller.populateDeviceInfo(&info);
10444 std::vector<InputDeviceLightInfo> lights = info.getLights();
10445 ASSERT_EQ(1U, lights.size());
10446 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10447 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010449 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10450 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010451}
10452
10453TEST_F(LightControllerTest, RGBLight) {
10454 RawLightInfo infoRed = {.id = 1,
10455 .name = "red",
10456 .maxBrightness = 255,
10457 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10458 .path = ""};
10459 RawLightInfo infoGreen = {.id = 2,
10460 .name = "green",
10461 .maxBrightness = 255,
10462 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10463 .path = ""};
10464 RawLightInfo infoBlue = {.id = 3,
10465 .name = "blue",
10466 .maxBrightness = 255,
10467 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10468 .path = ""};
10469 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10470 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10471 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10472
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010473 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010474 InputDeviceInfo info;
10475 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010476 std::vector<InputDeviceLightInfo> lights = info.getLights();
10477 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010478 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10479 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10480 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10481
10482 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10483 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10484}
10485
10486TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10487 RawLightInfo infoRed = {.id = 1,
10488 .name = "red_keyboard_backlight",
10489 .maxBrightness = 255,
10490 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10491 InputLightClass::KEYBOARD_BACKLIGHT,
10492 .path = ""};
10493 RawLightInfo infoGreen = {.id = 2,
10494 .name = "green_keyboard_backlight",
10495 .maxBrightness = 255,
10496 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10497 InputLightClass::KEYBOARD_BACKLIGHT,
10498 .path = ""};
10499 RawLightInfo infoBlue = {.id = 3,
10500 .name = "blue_keyboard_backlight",
10501 .maxBrightness = 255,
10502 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10503 InputLightClass::KEYBOARD_BACKLIGHT,
10504 .path = ""};
10505 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10506 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10507 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10508
10509 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10510 InputDeviceInfo info;
10511 controller.populateDeviceInfo(&info);
10512 std::vector<InputDeviceLightInfo> lights = info.getLights();
10513 ASSERT_EQ(1U, lights.size());
10514 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10515 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10516 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10517
10518 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10519 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10520}
10521
10522TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10523 RawLightInfo infoRed = {.id = 1,
10524 .name = "red",
10525 .maxBrightness = 255,
10526 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10527 .path = ""};
10528 RawLightInfo infoGreen = {.id = 2,
10529 .name = "green",
10530 .maxBrightness = 255,
10531 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10532 .path = ""};
10533 RawLightInfo infoBlue = {.id = 3,
10534 .name = "blue",
10535 .maxBrightness = 255,
10536 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10537 .path = ""};
10538 RawLightInfo infoGlobal = {.id = 3,
10539 .name = "global_keyboard_backlight",
10540 .maxBrightness = 255,
10541 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10542 InputLightClass::KEYBOARD_BACKLIGHT,
10543 .path = ""};
10544 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10545 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10546 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10547 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10548
10549 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10550 InputDeviceInfo info;
10551 controller.populateDeviceInfo(&info);
10552 std::vector<InputDeviceLightInfo> lights = info.getLights();
10553 ASSERT_EQ(1U, lights.size());
10554 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10555 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10556 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010557
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010558 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10559 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010560}
10561
10562TEST_F(LightControllerTest, MultiColorRGBLight) {
10563 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010564 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010565 .maxBrightness = 255,
10566 .flags = InputLightClass::BRIGHTNESS |
10567 InputLightClass::MULTI_INTENSITY |
10568 InputLightClass::MULTI_INDEX,
10569 .path = ""};
10570
10571 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10572
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010573 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010574 InputDeviceInfo info;
10575 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010576 std::vector<InputDeviceLightInfo> lights = info.getLights();
10577 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010578 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10579 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10580 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10581
10582 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10583 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10584}
10585
10586TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10587 RawLightInfo infoColor = {.id = 1,
10588 .name = "multi_color_keyboard_backlight",
10589 .maxBrightness = 255,
10590 .flags = InputLightClass::BRIGHTNESS |
10591 InputLightClass::MULTI_INTENSITY |
10592 InputLightClass::MULTI_INDEX |
10593 InputLightClass::KEYBOARD_BACKLIGHT,
10594 .path = ""};
10595
10596 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10597
10598 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10599 InputDeviceInfo info;
10600 controller.populateDeviceInfo(&info);
10601 std::vector<InputDeviceLightInfo> lights = info.getLights();
10602 ASSERT_EQ(1U, lights.size());
10603 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10604 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10605 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010606
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010607 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10608 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010609}
10610
10611TEST_F(LightControllerTest, PlayerIdLight) {
10612 RawLightInfo info1 = {.id = 1,
10613 .name = "player1",
10614 .maxBrightness = 255,
10615 .flags = InputLightClass::BRIGHTNESS,
10616 .path = ""};
10617 RawLightInfo info2 = {.id = 2,
10618 .name = "player2",
10619 .maxBrightness = 255,
10620 .flags = InputLightClass::BRIGHTNESS,
10621 .path = ""};
10622 RawLightInfo info3 = {.id = 3,
10623 .name = "player3",
10624 .maxBrightness = 255,
10625 .flags = InputLightClass::BRIGHTNESS,
10626 .path = ""};
10627 RawLightInfo info4 = {.id = 4,
10628 .name = "player4",
10629 .maxBrightness = 255,
10630 .flags = InputLightClass::BRIGHTNESS,
10631 .path = ""};
10632 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10633 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10634 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10635 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10636
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010637 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010638 InputDeviceInfo info;
10639 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010640 std::vector<InputDeviceLightInfo> lights = info.getLights();
10641 ASSERT_EQ(1U, lights.size());
10642 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010643 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10644 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010645
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010646 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10647 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10648 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010649}
10650
Michael Wrightd02c5b62014-02-10 15:10:22 -080010651} // namespace android