blob: ab4f63e4c7f78a45e91773227042ea7d12c266ea [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"
Michael Wrightdde67b82020-10-27 16:09:22 +000051#include "input/DisplayViewport.h"
52#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010053
Michael Wrightd02c5b62014-02-10 15:10:22 -080054namespace android {
55
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000057using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070058using std::chrono_literals::operator""ms;
59
Michael Wrightd02c5b62014-02-10 15:10:22 -080060// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000062static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t DISPLAY_WIDTH = 480;
66static constexpr int32_t DISPLAY_HEIGHT = 800;
67static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
68static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
69static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070070static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070071static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
arthurhungcc7f9802020-04-30 17:55:40 +080073static constexpr int32_t FIRST_SLOT = 0;
74static constexpr int32_t SECOND_SLOT = 1;
75static constexpr int32_t THIRD_SLOT = 2;
76static constexpr int32_t INVALID_TRACKING_ID = -1;
77static constexpr int32_t FIRST_TRACKING_ID = 0;
78static constexpr int32_t SECOND_TRACKING_ID = 1;
79static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000093// Minimum timestamp separation between subsequent input events from a Bluetooth device.
94static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
95// Maximum smoothing time delta so that we don't generate events too far into the future.
96constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098template<typename T>
99static inline T min(T a, T b) {
100 return a < b ? a : b;
101}
102
103static inline float avg(float x, float y) {
104 return (x + y) / 2;
105}
106
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107// Mapping for light color name and the light color
108const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
109 {"green", LightColor::GREEN},
110 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Michael Wrighta9cf4192022-12-01 23:46:39 +0000112static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700113 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000114 case ui::ROTATION_90:
115 return ui::ROTATION_270;
116 case ui::ROTATION_270:
117 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 default:
119 return orientation;
120 }
121}
122
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800123static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
124 InputDeviceInfo info;
125 mapper.populateDeviceInfo(&info);
126
127 const InputDeviceInfo::MotionRange* motionRange =
128 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
129 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
130}
131
132static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
133 InputDeviceInfo info;
134 mapper.populateDeviceInfo(&info);
135
136 const InputDeviceInfo::MotionRange* motionRange =
137 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
138 ASSERT_EQ(nullptr, motionRange);
139}
140
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700141[[maybe_unused]] static void dumpReader(InputReader& reader) {
142 std::string dump;
143 reader.dump(dump);
144 std::istringstream iss(dump);
145 for (std::string line; std::getline(iss, line);) {
146 ALOGE("%s", line.c_str());
147 std::this_thread::sleep_for(std::chrono::milliseconds(1));
148 }
149}
150
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151// --- FakeInputMapper ---
152
153class FakeInputMapper : public InputMapper {
154 uint32_t mSources;
155 int32_t mKeyboardType;
156 int32_t mMetaState;
157 KeyedVector<int32_t, int32_t> mKeyCodeStates;
158 KeyedVector<int32_t, int32_t> mScanCodeStates;
159 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100160 // fake mapping which would normally come from keyCharacterMap
161 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800162 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700164 std::mutex mLock;
165 std::condition_variable mStateChangedCondition;
166 bool mConfigureWasCalled GUARDED_BY(mLock);
167 bool mResetWasCalled GUARDED_BY(mLock);
168 bool mProcessWasCalled GUARDED_BY(mLock);
169 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Arthur Hungc23540e2018-11-29 20:42:11 +0800171 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800173 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
174 : InputMapper(deviceContext),
175 mSources(sources),
176 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800178 mConfigureWasCalled(false),
179 mResetWasCalled(false),
180 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Chris Yea52ade12020-08-27 16:49:20 -0700182 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
184 void setKeyboardType(int32_t keyboardType) {
185 mKeyboardType = keyboardType;
186 }
187
188 void setMetaState(int32_t metaState) {
189 mMetaState = metaState;
190 }
191
192 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700193 std::unique_lock<std::mutex> lock(mLock);
194 base::ScopedLockAssertion assumeLocked(mLock);
195 const bool configureCalled =
196 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
197 return mConfigureWasCalled;
198 });
199 if (!configureCalled) {
200 FAIL() << "Expected configure() to have been called.";
201 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 mConfigureWasCalled = false;
203 }
204
205 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 std::unique_lock<std::mutex> lock(mLock);
207 base::ScopedLockAssertion assumeLocked(mLock);
208 const bool resetCalled =
209 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
210 return mResetWasCalled;
211 });
212 if (!resetCalled) {
213 FAIL() << "Expected reset() to have been called.";
214 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 mResetWasCalled = false;
216 }
217
Yi Kong9b14ac62018-07-17 13:48:38 -0700218 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700219 std::unique_lock<std::mutex> lock(mLock);
220 base::ScopedLockAssertion assumeLocked(mLock);
221 const bool processCalled =
222 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
223 return mProcessWasCalled;
224 });
225 if (!processCalled) {
226 FAIL() << "Expected process() to have been called.";
227 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 if (outLastEvent) {
229 *outLastEvent = mLastEvent;
230 }
231 mProcessWasCalled = false;
232 }
233
234 void setKeyCodeState(int32_t keyCode, int32_t state) {
235 mKeyCodeStates.replaceValueFor(keyCode, state);
236 }
237
238 void setScanCodeState(int32_t scanCode, int32_t state) {
239 mScanCodeStates.replaceValueFor(scanCode, state);
240 }
241
242 void setSwitchState(int32_t switchCode, int32_t state) {
243 mSwitchStates.replaceValueFor(switchCode, state);
244 }
245
246 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800247 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 }
249
Philip Junker4af3b3d2021-12-14 10:36:55 +0100250 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
251 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
252 }
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100255 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256
Chris Yea52ade12020-08-27 16:49:20 -0700257 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 InputMapper::populateDeviceInfo(deviceInfo);
259
260 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
261 deviceInfo->setKeyboardType(mKeyboardType);
262 }
263 }
264
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700265 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
266 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800269
270 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800271 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800272 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
273 mViewport = config->getDisplayViewportByPort(*displayPort);
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
276 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700277 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 }
279
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700280 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700284 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 }
286
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700287 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700288 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 mLastEvent = *rawEvent;
290 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700292 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 }
294
Chris Yea52ade12020-08-27 16:49:20 -0700295 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
297 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
298 }
299
Philip Junker4af3b3d2021-12-14 10:36:55 +0100300 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
301 auto it = mKeyCodeMapping.find(locationKeyCode);
302 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
303 }
304
Chris Yea52ade12020-08-27 16:49:20 -0700305 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
307 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
308 }
309
Chris Yea52ade12020-08-27 16:49:20 -0700310 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 ssize_t index = mSwitchStates.indexOfKey(switchCode);
312 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
313 }
314
Chris Yea52ade12020-08-27 16:49:20 -0700315 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700316 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700317 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
320 if (keyCodes[i] == mSupportedKeyCodes[j]) {
321 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 }
323 }
324 }
Chris Yea52ade12020-08-27 16:49:20 -0700325 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 return result;
327 }
328
329 virtual int32_t getMetaState() {
330 return mMetaState;
331 }
332
333 virtual void fadePointer() {
334 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800335
336 virtual std::optional<int32_t> getAssociatedDisplay() {
337 if (mViewport) {
338 return std::make_optional(mViewport->displayId);
339 }
340 return std::nullopt;
341 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342};
343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700344// --- InputReaderPolicyTest ---
345class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700346protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700347 sp<FakeInputReaderPolicy> mFakePolicy;
348
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700349 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700350 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700351};
352
353/**
354 * Check that empty set of viewports is an acceptable configuration.
355 * Also try to get internal viewport two different ways - by type and by uniqueId.
356 *
357 * There will be confusion if two viewports with empty uniqueId and identical type are present.
358 * Such configuration is not currently allowed.
359 */
360TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700361 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700362
363 // We didn't add any viewports yet, so there shouldn't be any.
364 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100365 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700366 ASSERT_FALSE(internalViewport);
367
368 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000369 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000370 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700371
372 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700373 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100375 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376
377 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100378 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700380 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381
382 mFakePolicy->clearViewports();
383 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100386 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
388}
389
390TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
391 const std::string internalUniqueId = "local:0";
392 const std::string externalUniqueId = "local:1";
393 const std::string virtualUniqueId1 = "virtual:2";
394 const std::string virtualUniqueId2 = "virtual:3";
395 constexpr int32_t virtualDisplayId1 = 2;
396 constexpr int32_t virtualDisplayId2 = 3;
397
398 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000399 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000400 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000404 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406 // Add an virtual viewport
407 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000408 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000409 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 // Add another virtual viewport
411 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000412 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000413 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700414
415 // Check matching by type for internal
416 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100417 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700418 ASSERT_TRUE(internalViewport);
419 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
420
421 // Check matching by type for external
422 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100423 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700424 ASSERT_TRUE(externalViewport);
425 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
426
427 // Check matching by uniqueId for virtual viewport #1
428 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700429 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700430 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100431 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
433 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
434
435 // Check matching by uniqueId for virtual viewport #2
436 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700437 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700438 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100439 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
441 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
442}
443
444
445/**
446 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
447 * that lookup works by checking display id.
448 * Check that 2 viewports of each kind is possible, for all existing viewport types.
449 */
450TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
451 const std::string uniqueId1 = "uniqueId1";
452 const std::string uniqueId2 = "uniqueId2";
453 constexpr int32_t displayId1 = 2;
454 constexpr int32_t displayId2 = 3;
455
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
457 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700458 for (const ViewportType& type : types) {
459 mFakePolicy->clearViewports();
460 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000461 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000462 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000464 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000465 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466
467 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700468 std::optional<DisplayViewport> viewport1 =
469 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700470 ASSERT_TRUE(viewport1);
471 ASSERT_EQ(displayId1, viewport1->displayId);
472 ASSERT_EQ(type, viewport1->type);
473
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700474 std::optional<DisplayViewport> viewport2 =
475 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700476 ASSERT_TRUE(viewport2);
477 ASSERT_EQ(displayId2, viewport2->displayId);
478 ASSERT_EQ(type, viewport2->type);
479
480 // When there are multiple viewports of the same kind, and uniqueId is not specified
481 // in the call to getDisplayViewport, then that situation is not supported.
482 // The viewports can be stored in any order, so we cannot rely on the order, since that
483 // is just implementation detail.
484 // However, we can check that it still returns *a* viewport, we just cannot assert
485 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700486 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 ASSERT_TRUE(someViewport);
488 }
489}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700491/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000492 * When we have multiple internal displays make sure we always return the default display when
493 * querying by type.
494 */
495TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
496 const std::string uniqueId1 = "uniqueId1";
497 const std::string uniqueId2 = "uniqueId2";
498 constexpr int32_t nonDefaultDisplayId = 2;
499 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
500 "Test display ID should not be ADISPLAY_ID_DEFAULT");
501
502 // Add the default display first and ensure it gets returned.
503 mFakePolicy->clearViewports();
504 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000505 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000506 ViewportType::INTERNAL);
507 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000508 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 ViewportType::INTERNAL);
510
511 std::optional<DisplayViewport> viewport =
512 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
513 ASSERT_TRUE(viewport);
514 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
515 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
516
517 // Add the default display second to make sure order doesn't matter.
518 mFakePolicy->clearViewports();
519 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000520 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000521 ViewportType::INTERNAL);
522 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000523 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000524 ViewportType::INTERNAL);
525
526 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
527 ASSERT_TRUE(viewport);
528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
530}
531
532/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700533 * Check getDisplayViewportByPort
534 */
535TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100536 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700537 const std::string uniqueId1 = "uniqueId1";
538 const std::string uniqueId2 = "uniqueId2";
539 constexpr int32_t displayId1 = 1;
540 constexpr int32_t displayId2 = 2;
541 const uint8_t hdmi1 = 0;
542 const uint8_t hdmi2 = 1;
543 const uint8_t hdmi3 = 2;
544
545 mFakePolicy->clearViewports();
546 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000547 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000548 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700549 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000550 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000551 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552
553 // Check that correct display viewport was returned by comparing the display ports.
554 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
555 ASSERT_TRUE(hdmi1Viewport);
556 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
557 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
558
559 // Check that we can still get the same viewport using the uniqueId
560 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
561 ASSERT_TRUE(hdmi1Viewport);
562 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
563 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
564 ASSERT_EQ(type, hdmi1Viewport->type);
565
566 // Check that we cannot find a port with "HDMI2", because we never added one
567 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
568 ASSERT_FALSE(hdmi2Viewport);
569}
570
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571// --- InputReaderTest ---
572
573class InputReaderTest : public testing::Test {
574protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700575 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700577 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000578 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
Chris Yea52ade12020-08-27 16:49:20 -0700580 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700581 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700582 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700583 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584
Prabir Pradhan28efc192019-11-05 01:10:04 +0000585 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588
Chris Yea52ade12020-08-27 16:49:20 -0700589 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700590 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700594 void addDevice(int32_t eventHubId, const std::string& name,
595 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800596 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597
598 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 }
601 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000602 mReader->loopOnce();
603 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700604 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
605 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800608 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700609 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700611 }
612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800613 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000615 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700616 }
617
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800618 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700619 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700620 ftl::Flags<InputDeviceClass> classes,
621 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800622 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800623 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
624 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800625 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 return mapper;
628 }
629};
630
Chris Ye98d3f532020-10-01 21:48:59 -0700631TEST_F(InputReaderTest, PolicyGetInputDevices) {
632 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700633 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700634 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
636 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700637 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800639 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100640 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
642 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000643 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644}
645
Chris Yee7310032020-09-22 15:36:28 -0700646TEST_F(InputReaderTest, GetMergedInputDevices) {
647 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
648 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
649 // Add two subdevices to device
650 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
651 // Must add at least one mapper or the device will be ignored!
652 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
653 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
654
655 // Push same device instance for next device to be added, so they'll have same identifier.
656 mReader->pushNextDevice(device);
657 mReader->pushNextDevice(device);
658 ASSERT_NO_FATAL_FAILURE(
659 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
660 ASSERT_NO_FATAL_FAILURE(
661 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
662
663 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000664 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700665}
666
Chris Yee14523a2020-12-19 13:46:00 -0800667TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
668 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
669 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
670 // Add two subdevices to device
671 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
672 // Must add at least one mapper or the device will be ignored!
673 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
674 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
675
676 // Push same device instance for next device to be added, so they'll have same identifier.
677 mReader->pushNextDevice(device);
678 mReader->pushNextDevice(device);
679 // Sensor device is initially disabled
680 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
681 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
682 nullptr));
683 // Device is disabled because the only sub device is a sensor device and disabled initially.
684 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
685 ASSERT_FALSE(device->isEnabled());
686 ASSERT_NO_FATAL_FAILURE(
687 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
688 // The merged device is enabled if any sub device is enabled
689 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
690 ASSERT_TRUE(device->isEnabled());
691}
692
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700693TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700695 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800696 constexpr int32_t eventHubId = 1;
697 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700698 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800699 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800700 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800701 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700702
Yi Kong9b14ac62018-07-17 13:48:38 -0700703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700704
705 NotifyDeviceResetArgs resetArgs;
706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700707 ASSERT_EQ(deviceId, resetArgs.deviceId);
708
709 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800710 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000711 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700712
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700714 ASSERT_EQ(deviceId, resetArgs.deviceId);
715 ASSERT_EQ(device->isEnabled(), false);
716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000718 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700721 ASSERT_EQ(device->isEnabled(), false);
722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800723 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000724 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726 ASSERT_EQ(deviceId, resetArgs.deviceId);
727 ASSERT_EQ(device->isEnabled(), true);
728}
729
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800731 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700732 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800733 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800734 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800736 AINPUT_SOURCE_KEYBOARD, nullptr);
737 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738
739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
740 AINPUT_SOURCE_ANY, AKEYCODE_A))
741 << "Should return unknown when the device id is >= 0 but unknown.";
742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800743 ASSERT_EQ(AKEY_STATE_UNKNOWN,
744 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
745 << "Should return unknown when the device id is valid but the sources are not "
746 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800748 ASSERT_EQ(AKEY_STATE_DOWN,
749 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
750 AKEYCODE_A))
751 << "Should return value provided by mapper when device id is valid and the device "
752 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753
754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
755 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
756 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
757
758 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
759 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
760 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
761}
762
Philip Junker4af3b3d2021-12-14 10:36:55 +0100763TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
764 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
765 constexpr int32_t eventHubId = 1;
766 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
767 InputDeviceClass::KEYBOARD,
768 AINPUT_SOURCE_KEYBOARD, nullptr);
769 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
770
771 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
772 << "Should return unknown when the device with the specified id is not found.";
773
774 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
775 << "Should return correct mapping when device id is valid and mapping exists.";
776
777 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
778 << "Should return the location key code when device id is valid and there's no "
779 "mapping.";
780}
781
782TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
784 constexpr int32_t eventHubId = 1;
785 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
786 InputDeviceClass::JOYSTICK,
787 AINPUT_SOURCE_GAMEPAD, nullptr);
788 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
789
790 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
791 << "Should return unknown when the device id is valid but there is no keyboard mapper";
792}
793
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800795 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700796 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800797 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800798 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800799 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800800 AINPUT_SOURCE_KEYBOARD, nullptr);
801 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802
803 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
804 AINPUT_SOURCE_ANY, KEY_A))
805 << "Should return unknown when the device id is >= 0 but unknown.";
806
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800807 ASSERT_EQ(AKEY_STATE_UNKNOWN,
808 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
809 << "Should return unknown when the device id is valid but the sources are not "
810 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800812 ASSERT_EQ(AKEY_STATE_DOWN,
813 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
814 KEY_A))
815 << "Should return value provided by mapper when device id is valid and the device "
816 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817
818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
819 AINPUT_SOURCE_TRACKBALL, KEY_A))
820 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
821
822 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
823 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
824 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
825}
826
827TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800828 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700829 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800830 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800831 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800832 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800833 AINPUT_SOURCE_KEYBOARD, nullptr);
834 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
836 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
837 AINPUT_SOURCE_ANY, SW_LID))
838 << "Should return unknown when the device id is >= 0 but unknown.";
839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800840 ASSERT_EQ(AKEY_STATE_UNKNOWN,
841 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
842 << "Should return unknown when the device id is valid but the sources are not "
843 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800845 ASSERT_EQ(AKEY_STATE_DOWN,
846 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
847 SW_LID))
848 << "Should return value provided by mapper when device id is valid and the device "
849 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
852 AINPUT_SOURCE_TRACKBALL, SW_LID))
853 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
854
855 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
856 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
857 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
858}
859
860TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700862 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800863 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800864 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800866 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800868 mapper.addSupportedKeyCode(AKEYCODE_A);
869 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700871 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 uint8_t flags[4] = { 0, 0, 0, 1 };
873
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700874 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 << "Should return false when device id is >= 0 but unknown.";
876 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
877
878 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700879 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800880 << "Should return false when device id is valid but the sources are not supported by "
881 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
883
884 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700885 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 keyCodes, flags))
887 << "Should return value provided by mapper when device id is valid and the device "
888 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
890
891 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700892 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
893 << "Should return false when the device id is < 0 but the sources are not supported by "
894 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
896
897 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700898 ASSERT_TRUE(
899 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
900 << "Should return value provided by mapper when device id is < 0 and one of the "
901 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
903}
904
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000905TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700907 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
909 NotifyConfigurationChangedArgs args;
910
911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
912 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
913}
914
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000915TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800916 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700917 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000918 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000920 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000925 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000926 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
928
929 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800930 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000931 ASSERT_EQ(when, event.when);
932 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800933 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 ASSERT_EQ(EV_KEY, event.type);
935 ASSERT_EQ(KEY_A, event.code);
936 ASSERT_EQ(1, event.value);
937}
938
Garfield Tan1c7bc862020-01-28 13:24:04 -0800939TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700941 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800942 constexpr int32_t eventHubId = 1;
943 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800944 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800946 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800947 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800948
949 NotifyDeviceResetArgs resetArgs;
950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800951 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800953 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000954 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800956 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800957 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800959 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800962 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800963 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800965 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000966 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800968 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800969 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800970}
971
Garfield Tan1c7bc862020-01-28 13:24:04 -0800972TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
973 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800975 constexpr int32_t eventHubId = 1;
976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
977 // Must add at least one mapper or the device will be ignored!
978 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800979 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800980 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
981
982 NotifyDeviceResetArgs resetArgs;
983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
984 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
985}
986
Arthur Hungc23540e2018-11-29 20:42:11 +0800987TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800988 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700989 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800991 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800992 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
993 FakeInputMapper& mapper =
994 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800995 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800996
997 const uint8_t hdmi1 = 1;
998
999 // Associated touch screen with second display.
1000 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1001
1002 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001003 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001004 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001005 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001006 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001007 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001008 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001009 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001010 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001011
1012 // Add the device, and make sure all of the callbacks are triggered.
1013 // The device is added after the input port associations are processed since
1014 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001015 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001018 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001019
Arthur Hung2c9a3342019-07-23 14:18:59 +08001020 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001021 ASSERT_EQ(deviceId, device->getId());
1022 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1023 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001024
1025 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001026 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001027 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001028 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001029}
1030
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001031TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1032 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001033 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001034 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1035 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1036 // Must add at least one mapper or the device will be ignored!
1037 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1038 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1039 mReader->pushNextDevice(device);
1040 mReader->pushNextDevice(device);
1041 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1042 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1043
1044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1045
1046 NotifyDeviceResetArgs resetArgs;
1047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1048 ASSERT_EQ(deviceId, resetArgs.deviceId);
1049 ASSERT_TRUE(device->isEnabled());
1050 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1052
1053 disableDevice(deviceId);
1054 mReader->loopOnce();
1055
1056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1057 ASSERT_EQ(deviceId, resetArgs.deviceId);
1058 ASSERT_FALSE(device->isEnabled());
1059 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1060 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1061
1062 enableDevice(deviceId);
1063 mReader->loopOnce();
1064
1065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1066 ASSERT_EQ(deviceId, resetArgs.deviceId);
1067 ASSERT_TRUE(device->isEnabled());
1068 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1070}
1071
1072TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1073 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001074 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001075 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1076 // Add two subdevices to device
1077 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1078 FakeInputMapper& mapperDevice1 =
1079 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1080 FakeInputMapper& mapperDevice2 =
1081 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1082 mReader->pushNextDevice(device);
1083 mReader->pushNextDevice(device);
1084 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1086
1087 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1088 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1089
1090 ASSERT_EQ(AKEY_STATE_DOWN,
1091 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1092 ASSERT_EQ(AKEY_STATE_DOWN,
1093 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1094 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1095 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1096}
1097
Prabir Pradhan7e186182020-11-10 13:56:45 -08001098TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1099 NotifyPointerCaptureChangedArgs args;
1100
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001101 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001102 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1103 mReader->loopOnce();
1104 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001105 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1106 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001107
1108 mFakePolicy->setPointerCapture(false);
1109 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1110 mReader->loopOnce();
1111 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001112 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001113
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001114 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001115 // does not change.
1116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1117 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001118 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001119}
1120
Chris Ye87143712020-11-10 05:05:58 +00001121class FakeVibratorInputMapper : public FakeInputMapper {
1122public:
1123 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1124 : FakeInputMapper(deviceContext, sources) {}
1125
1126 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1127};
1128
1129TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1130 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001131 ftl::Flags<InputDeviceClass> deviceClass =
1132 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001133 constexpr int32_t eventHubId = 1;
1134 const char* DEVICE_LOCATION = "BLUETOOTH";
1135 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1136 FakeVibratorInputMapper& mapper =
1137 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1138 mReader->pushNextDevice(device);
1139
1140 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1141 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1142
1143 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1144 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1145}
1146
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001147// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001148
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001149class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001150public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001151 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001152
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001153 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001154
Andy Chenf9f1a022022-08-29 20:07:10 -04001155 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1156
Chris Yee2b1e5c2021-03-10 22:45:12 -08001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1158
1159 void dump(std::string& dump) override {}
1160
1161 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1162 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001163 }
1164
Chris Yee2b1e5c2021-03-10 22:45:12 -08001165 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1166 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001167 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001168
1169 bool setLightColor(int32_t lightId, int32_t color) override {
1170 getDeviceContext().setLightBrightness(lightId, color >> 24);
1171 return true;
1172 }
1173
1174 std::optional<int32_t> getLightColor(int32_t lightId) override {
1175 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1176 if (!result.has_value()) {
1177 return std::nullopt;
1178 }
1179 return result.value() << 24;
1180 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001181
1182 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1183
1184 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1185
1186private:
1187 InputDeviceContext& mDeviceContext;
1188 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1189 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001190 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001191};
1192
Chris Yee2b1e5c2021-03-10 22:45:12 -08001193TEST_F(InputReaderTest, BatteryGetCapacity) {
1194 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001195 ftl::Flags<InputDeviceClass> deviceClass =
1196 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001197 constexpr int32_t eventHubId = 1;
1198 const char* DEVICE_LOCATION = "BLUETOOTH";
1199 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001200 FakePeripheralController& controller =
1201 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001202 mReader->pushNextDevice(device);
1203
1204 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1205
Harry Cuttsa5b71292022-11-28 12:56:17 +00001206 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1207 FakeEventHub::BATTERY_CAPACITY);
1208 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001209}
1210
1211TEST_F(InputReaderTest, BatteryGetStatus) {
1212 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001213 ftl::Flags<InputDeviceClass> deviceClass =
1214 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001215 constexpr int32_t eventHubId = 1;
1216 const char* DEVICE_LOCATION = "BLUETOOTH";
1217 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218 FakePeripheralController& controller =
1219 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220 mReader->pushNextDevice(device);
1221
1222 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1223
Harry Cuttsa5b71292022-11-28 12:56:17 +00001224 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1225 FakeEventHub::BATTERY_STATUS);
1226 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227}
1228
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001229TEST_F(InputReaderTest, BatteryGetDevicePath) {
1230 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1231 ftl::Flags<InputDeviceClass> deviceClass =
1232 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1233 constexpr int32_t eventHubId = 1;
1234 const char* DEVICE_LOCATION = "BLUETOOTH";
1235 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1236 device->addController<FakePeripheralController>(eventHubId);
1237 mReader->pushNextDevice(device);
1238
1239 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1240
Harry Cuttsa5b71292022-11-28 12:56:17 +00001241 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001242}
1243
Chris Ye3fdbfef2021-01-06 18:45:18 -08001244TEST_F(InputReaderTest, LightGetColor) {
1245 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001246 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001247 constexpr int32_t eventHubId = 1;
1248 const char* DEVICE_LOCATION = "BLUETOOTH";
1249 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001250 FakePeripheralController& controller =
1251 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001252 mReader->pushNextDevice(device);
1253 RawLightInfo info = {.id = 1,
1254 .name = "Mono",
1255 .maxBrightness = 255,
1256 .flags = InputLightClass::BRIGHTNESS,
1257 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001258 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1259 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260
1261 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001262
Harry Cutts33476232023-01-30 19:57:29 +00001263 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1264 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1265 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1266 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001267}
1268
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001269// --- InputReaderIntegrationTest ---
1270
1271// These tests create and interact with the InputReader only through its interface.
1272// The InputReader is started during SetUp(), which starts its processing in its own
1273// thread. The tests use linux uinput to emulate input devices.
1274// NOTE: Interacting with the physical device while these tests are running may cause
1275// the tests to fail.
1276class InputReaderIntegrationTest : public testing::Test {
1277protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001278 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001279 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001280 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001281
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001282 std::shared_ptr<FakePointerController> mFakePointerController;
1283
Chris Yea52ade12020-08-27 16:49:20 -07001284 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001285#if !defined(__ANDROID__)
1286 GTEST_SKIP();
1287#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001288 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001289 mFakePointerController = std::make_shared<FakePointerController>();
1290 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001291 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1292 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001293
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001294 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1295 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001296 ASSERT_EQ(mReader->start(), OK);
1297
1298 // Since this test is run on a real device, all the input devices connected
1299 // to the test device will show up in mReader. We wait for those input devices to
1300 // show up before beginning the tests.
1301 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1303 }
1304
Chris Yea52ade12020-08-27 16:49:20 -07001305 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001306#if !defined(__ANDROID__)
1307 return;
1308#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001309 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 mReader.reset();
1311 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001312 mFakePolicy.clear();
1313 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001314
1315 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1317 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1318 [&name](const InputDeviceInfo& info) {
1319 return info.getIdentifier().name == name;
1320 });
1321 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1322 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001323};
1324
1325TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1326 // An invalid input device that is only used for this test.
1327 class InvalidUinputDevice : public UinputDevice {
1328 public:
Harry Cutts33476232023-01-30 19:57:29 +00001329 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001330
1331 private:
1332 void configureDevice(int fd, uinput_user_dev* device) override {}
1333 };
1334
1335 const size_t numDevices = mFakePolicy->getInputDevices().size();
1336
1337 // UinputDevice does not set any event or key bits, so InputReader should not
1338 // consider it as a valid device.
1339 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1340 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1342 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1343
1344 invalidDevice.reset();
1345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1347 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1348}
1349
1350TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1351 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1352
1353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1356 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1357
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001358 const auto device = findDeviceByName(keyboard->getName());
1359 ASSERT_TRUE(device.has_value());
1360 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1361 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1362 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001363
1364 keyboard.reset();
1365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1367 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1368}
1369
1370TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1371 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1372 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1373
1374 NotifyConfigurationChangedArgs configChangedArgs;
1375 ASSERT_NO_FATAL_FAILURE(
1376 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001377 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001378 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1379
1380 NotifyKeyArgs keyArgs;
1381 keyboard->pressAndReleaseHomeKey();
1382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001384 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001385 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001386 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001387 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001388 prevTimestamp = keyArgs.eventTime;
1389
1390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1391 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001392 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001393 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001394 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001395}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001396
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001397TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1398 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400
1401 const auto device = findDeviceByName(stylus->getName());
1402 ASSERT_TRUE(device.has_value());
1403
Prabir Pradhana3621852022-10-14 18:57:23 +00001404 // An external stylus with buttons should also be recognized as a keyboard.
1405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001406 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408
1409 const auto DOWN =
1410 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1411 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1412
1413 stylus->pressAndReleaseKey(BTN_STYLUS);
1414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1415 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1417 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1418
1419 stylus->pressAndReleaseKey(BTN_STYLUS2);
1420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1421 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1423 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1424
1425 stylus->pressAndReleaseKey(BTN_STYLUS3);
1426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1427 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1429 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1430}
1431
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001432/**
1433 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1434 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1435 * are passed to the listener.
1436 */
1437static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1438TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1439 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1440 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1441 NotifyKeyArgs keyArgs;
1442
1443 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1446 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1447
1448 controller->pressAndReleaseKey(BTN_GEAR_UP);
1449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1451 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1452}
1453
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001454// --- TouchIntegrationTest ---
1455
Arthur Hungaab25622020-01-16 11:22:11 +08001456class TouchIntegrationTest : public InputReaderIntegrationTest {
1457protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001458 const std::string UNIQUE_ID = "local:0";
1459
Chris Yea52ade12020-08-27 16:49:20 -07001460 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001461#if !defined(__ANDROID__)
1462 GTEST_SKIP();
1463#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001464 InputReaderIntegrationTest::SetUp();
1465 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001466 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1467 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001468
1469 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1470 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001472 const auto info = findDeviceByName(mDevice->getName());
1473 ASSERT_TRUE(info);
1474 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001475 }
1476
1477 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001478 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001479 std::optional<uint8_t> physicalPort,
1480 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001481 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001482 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001483 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1484 }
1485
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001486 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1487 NotifyMotionArgs args;
1488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1489 EXPECT_EQ(action, args.action);
1490 ASSERT_EQ(points.size(), args.pointerCount);
1491 for (size_t i = 0; i < args.pointerCount; i++) {
1492 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1493 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1494 }
1495 }
1496
Arthur Hungaab25622020-01-16 11:22:11 +08001497 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001498 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001499};
1500
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001501TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1502 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1503 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1504 // presses).
1505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1506 mDeviceInfo.getSources());
1507}
1508
Arthur Hungaab25622020-01-16 11:22:11 +08001509TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1510 NotifyMotionArgs args;
1511 const Point centerPoint = mDevice->getCenterPoint();
1512
1513 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001514 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001515 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1519
1520 // ACTION_MOVE
1521 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001522 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1525
1526 // ACTION_UP
1527 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001528 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1531}
1532
1533TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1534 NotifyMotionArgs args;
1535 const Point centerPoint = mDevice->getCenterPoint();
1536
1537 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001538 mDevice->sendSlot(FIRST_SLOT);
1539 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001540 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1544
1545 // ACTION_POINTER_DOWN (Second slot)
1546 const Point secondPoint = centerPoint + Point(100, 100);
1547 mDevice->sendSlot(SECOND_SLOT);
1548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001549 mDevice->sendDown(secondPoint);
1550 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001552 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001553
1554 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001555 mDevice->sendMove(secondPoint + Point(1, 1));
1556 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1559
1560 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001561 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001562 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001564 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001565
1566 // ACTION_UP
1567 mDevice->sendSlot(FIRST_SLOT);
1568 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1572}
1573
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001574/**
1575 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1576 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1577 * data?
1578 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1579 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1580 * for Pointer 0 only is generated after.
1581 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1582 * events, we will not miss any information.
1583 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1584 * event generated afterwards that contains the newest movement of pointer 0.
1585 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1586 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1587 * losing information about non-palm pointers.
1588 */
1589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1590 NotifyMotionArgs args;
1591 const Point centerPoint = mDevice->getCenterPoint();
1592
1593 // ACTION_DOWN
1594 mDevice->sendSlot(FIRST_SLOT);
1595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1596 mDevice->sendDown(centerPoint);
1597 mDevice->sendSync();
1598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1599
1600 // ACTION_POINTER_DOWN (Second slot)
1601 const Point secondPoint = centerPoint + Point(100, 100);
1602 mDevice->sendSlot(SECOND_SLOT);
1603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1604 mDevice->sendDown(secondPoint);
1605 mDevice->sendSync();
1606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1607
1608 // ACTION_MOVE (First slot)
1609 mDevice->sendSlot(FIRST_SLOT);
1610 mDevice->sendMove(centerPoint + Point(5, 5));
1611 // ACTION_POINTER_UP (Second slot)
1612 mDevice->sendSlot(SECOND_SLOT);
1613 mDevice->sendPointerUp();
1614 // Send a single sync for the above 2 pointer updates
1615 mDevice->sendSync();
1616
1617 // First, we should get POINTER_UP for the second pointer
1618 assertReceivedMotion(ACTION_POINTER_1_UP,
1619 {/*first pointer */ centerPoint + Point(5, 5),
1620 /*second pointer*/ secondPoint});
1621
1622 // Next, the MOVE event for the first pointer
1623 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1624}
1625
1626/**
1627 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1628 * move, and then it will go up, all in the same frame.
1629 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1630 * gets sent to the listener.
1631 */
1632TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1633 NotifyMotionArgs args;
1634 const Point centerPoint = mDevice->getCenterPoint();
1635
1636 // ACTION_DOWN
1637 mDevice->sendSlot(FIRST_SLOT);
1638 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1639 mDevice->sendDown(centerPoint);
1640 mDevice->sendSync();
1641 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1642
1643 // ACTION_POINTER_DOWN (Second slot)
1644 const Point secondPoint = centerPoint + Point(100, 100);
1645 mDevice->sendSlot(SECOND_SLOT);
1646 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1647 mDevice->sendDown(secondPoint);
1648 mDevice->sendSync();
1649 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1650
1651 // ACTION_MOVE (First slot)
1652 mDevice->sendSlot(FIRST_SLOT);
1653 mDevice->sendMove(centerPoint + Point(5, 5));
1654 // ACTION_POINTER_UP (Second slot)
1655 mDevice->sendSlot(SECOND_SLOT);
1656 mDevice->sendMove(secondPoint + Point(6, 6));
1657 mDevice->sendPointerUp();
1658 // Send a single sync for the above 2 pointer updates
1659 mDevice->sendSync();
1660
1661 // First, we should get POINTER_UP for the second pointer
1662 // The movement of the second pointer during the liftoff frame is ignored.
1663 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1664 assertReceivedMotion(ACTION_POINTER_1_UP,
1665 {/*first pointer */ centerPoint + Point(5, 5),
1666 /*second pointer*/ secondPoint});
1667
1668 // Next, the MOVE event for the first pointer
1669 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1670}
1671
Arthur Hungaab25622020-01-16 11:22:11 +08001672TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1673 NotifyMotionArgs args;
1674 const Point centerPoint = mDevice->getCenterPoint();
1675
1676 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001677 mDevice->sendSlot(FIRST_SLOT);
1678 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001679 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001680 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001681 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1683
arthurhungcc7f9802020-04-30 17:55:40 +08001684 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001685 const Point secondPoint = centerPoint + Point(100, 100);
1686 mDevice->sendSlot(SECOND_SLOT);
1687 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1688 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001691 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001692
arthurhungcc7f9802020-04-30 17:55:40 +08001693 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001694 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001695 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001696 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1698
arthurhungcc7f9802020-04-30 17:55:40 +08001699 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1700 // a palm event.
1701 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001702 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001703 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001704 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001705 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001706 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001707
arthurhungcc7f9802020-04-30 17:55:40 +08001708 // Send up to second slot, expect first slot send moving.
1709 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001710 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001711 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001713
arthurhungcc7f9802020-04-30 17:55:40 +08001714 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001715 mDevice->sendSlot(FIRST_SLOT);
1716 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001718
arthurhungcc7f9802020-04-30 17:55:40 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001721}
1722
Prabir Pradhanda20b172022-09-26 17:01:18 +00001723TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1724 const Point centerPoint = mDevice->getCenterPoint();
1725
1726 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1727 mDevice->sendSlot(FIRST_SLOT);
1728 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1729 mDevice->sendToolType(MT_TOOL_PEN);
1730 mDevice->sendDown(centerPoint);
1731 mDevice->sendSync();
1732 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1734 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1735
1736 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1737
1738 // Release the stylus touch.
1739 mDevice->sendUp();
1740 mDevice->sendSync();
1741 ASSERT_NO_FATAL_FAILURE(
1742 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1743
1744 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1745
1746 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1748 mDevice->sendToolType(MT_TOOL_FINGER);
1749 mDevice->sendDown(centerPoint);
1750 mDevice->sendSync();
1751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1753 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1754
1755 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1756
1757 mDevice->sendUp();
1758 mDevice->sendSync();
1759 ASSERT_NO_FATAL_FAILURE(
1760 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1761
1762 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1763 // The policy should be notified of the stylus presence.
1764 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1765 mDevice->sendToolType(MT_TOOL_PEN);
1766 mDevice->sendMove(centerPoint);
1767 mDevice->sendSync();
1768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1770 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1771
1772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1773}
1774
Prabir Pradhan124ea442022-10-28 20:27:44 +00001775// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001776
Prabir Pradhan124ea442022-10-28 20:27:44 +00001777// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1778// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1779// stylus.
1780template <typename UinputStylusDevice>
1781class StylusButtonIntegrationTest : public TouchIntegrationTest {
1782protected:
1783 void SetUp() override {
1784#if !defined(__ANDROID__)
1785 GTEST_SKIP();
1786#endif
1787 TouchIntegrationTest::SetUp();
1788 mTouchscreen = mDevice.get();
1789 mTouchscreenInfo = mDeviceInfo;
1790
1791 setUpStylusDevice();
1792 }
1793
1794 UinputStylusDevice* mStylus{nullptr};
1795 InputDeviceInfo mStylusInfo{};
1796
1797 UinputTouchScreen* mTouchscreen{nullptr};
1798 InputDeviceInfo mTouchscreenInfo{};
1799
1800private:
1801 // When we are attempting to test stylus button events that are sent from the touchscreen,
1802 // use the same Uinput device for the touchscreen and the stylus.
1803 template <typename T = UinputStylusDevice>
1804 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1805 mStylus = mDevice.get();
1806 mStylusInfo = mDeviceInfo;
1807 }
1808
1809 // When we are attempting to stylus buttons from an external stylus being merged with touches
1810 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1811 template <typename T = UinputStylusDevice>
1812 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1813 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1814 mStylus = mStylusDeviceLifecycleTracker.get();
1815 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1817 const auto info = findDeviceByName(mStylus->getName());
1818 ASSERT_TRUE(info);
1819 mStylusInfo = *info;
1820 }
1821
1822 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1823
1824 // Hide the base class's device to expose it with a different name for readability.
1825 using TouchIntegrationTest::mDevice;
1826 using TouchIntegrationTest::mDeviceInfo;
1827};
1828
1829using StylusButtonIntegrationTestTypes =
1830 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1831TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1832
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001833TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001834 const auto stylusId = TestFixture::mStylusInfo.getId();
1835
1836 TestFixture::mStylus->pressKey(BTN_STYLUS);
1837 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1838 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1839 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1840
1841 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1842 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001843 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001844 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001845}
1846
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001847TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001848 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1849 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1850 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001851
1852 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001853 TestFixture::mStylus->pressKey(BTN_STYLUS);
1854 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001855 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001856 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001857
1858 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1860 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1861 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1862 TestFixture::mTouchscreen->sendDown(centerPoint);
1863 TestFixture::mTouchscreen->sendSync();
1864 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1866 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001867 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1868 WithDeviceId(touchscreenId))));
1869 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1871 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001872 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1873 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001874
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1876 TestFixture::mTouchscreen->sendSync();
1877 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001879 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1880 WithDeviceId(touchscreenId))));
1881 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001882 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001883 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1884 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001885
1886 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001887 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1888 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001889 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001891}
1892
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001893TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001894 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1895 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1896 const auto stylusId = TestFixture::mStylusInfo.getId();
1897 auto toolTypeDevice =
1898 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1899
1900 // Press the stylus button.
1901 TestFixture::mStylus->pressKey(BTN_STYLUS);
1902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1905
1906 // Start hovering with the stylus.
1907 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1908 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1909 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1910 TestFixture::mTouchscreen->sendMove(centerPoint);
1911 TestFixture::mTouchscreen->sendSync();
1912 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1913 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1916 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1918 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1919 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1920 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1921
1922 // Touch down with the stylus.
1923 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1924 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1925 TestFixture::mTouchscreen->sendDown(centerPoint);
1926 TestFixture::mTouchscreen->sendSync();
1927 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1928 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1929 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1930
1931 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1932 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1933 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1934
1935 // Stop touching with the stylus, and start hovering.
1936 TestFixture::mTouchscreen->sendUp();
1937 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1938 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1939 TestFixture::mTouchscreen->sendMove(centerPoint);
1940 TestFixture::mTouchscreen->sendSync();
1941 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1942 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1943 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1945 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1948 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1949 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1950
1951 // Stop hovering.
1952 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1953 TestFixture::mTouchscreen->sendSync();
1954 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1955 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1956 WithButtonState(0))));
1957 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1958 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1959 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1960 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1961
1962 // Release the stylus button.
1963 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1964 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1965 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1966 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1967}
1968
Prabir Pradhan5abecc32023-03-13 14:59:59 +00001969TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00001970 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1971 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1972 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001973
1974 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001975 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1976 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1977 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1978 TestFixture::mTouchscreen->sendDown(centerPoint);
1979 TestFixture::mTouchscreen->sendSync();
1980 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001982 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1983 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001984
1985 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001986 TestFixture::mStylus->pressKey(BTN_STYLUS);
1987 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001988 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001991 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1992 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1994 WithDeviceId(touchscreenId))));
1995 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001996 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1997 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001998 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1999 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002000
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2002 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2008 WithDeviceId(touchscreenId))));
2009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002011 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2012 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013
2014 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002015 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2016 TestFixture::mTouchscreen->sendSync();
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002019 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2020 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021}
2022
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002023TYPED_TEST(StylusButtonIntegrationTest, DISABLED_StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002024 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2025 TestFixture::mReader->requestRefreshConfiguration(
2026 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2027
2028 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2029 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2030 const auto stylusId = TestFixture::mStylusInfo.getId();
2031
2032 // Start a stylus gesture. By the time this event is processed, the configuration change that
2033 // was requested is guaranteed to be completed.
2034 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2035 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2036 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2037 TestFixture::mTouchscreen->sendDown(centerPoint);
2038 TestFixture::mTouchscreen->sendSync();
2039 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2040 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2041 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2042 WithDeviceId(touchscreenId))));
2043
2044 // Press and release a stylus button. Each change only generates a MOVE motion event.
2045 // Key events are unaffected.
2046 TestFixture::mStylus->pressKey(BTN_STYLUS);
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2048 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2049 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2050 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2052 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2053 WithDeviceId(touchscreenId))));
2054
2055 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2057 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2058 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2061 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2062 WithDeviceId(touchscreenId))));
2063
2064 // Finish the stylus gesture.
2065 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2066 TestFixture::mTouchscreen->sendSync();
2067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2069 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2070 WithDeviceId(touchscreenId))));
2071}
2072
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002073// --- ExternalStylusIntegrationTest ---
2074
2075// Verify the behavior of an external stylus. An external stylus can report pressure or button
2076// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2077// ongoing stylus gesture that is being emitted by the touchscreen.
2078using ExternalStylusIntegrationTest = TouchIntegrationTest;
2079
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002080TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002081 const Point centerPoint = mDevice->getCenterPoint();
2082
2083 // Create an external stylus capable of reporting pressure data that
2084 // should be fused with a touch pointer.
2085 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2086 createUinputDevice<UinputExternalStylusWithPressure>();
2087 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2088 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2089 const auto stylusInfo = findDeviceByName(stylus->getName());
2090 ASSERT_TRUE(stylusInfo);
2091
2092 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2093
2094 const auto touchscreenId = mDeviceInfo.getId();
2095
2096 // Set a pressure value on the stylus. It doesn't generate any events.
2097 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2098 stylus->setPressure(100);
2099 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2100
2101 // Start a finger gesture, and ensure it shows up as stylus gesture
2102 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002103 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002104 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002105 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002106 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 mDevice->sendSync();
2108 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2109 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002110 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2111 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002112
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2114 // event with the updated pressure.
2115 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002116 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002118 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2119 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002120
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002121 // The external stylus did not generate any events.
2122 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2124}
2125
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002126TEST_F(ExternalStylusIntegrationTest, DISABLED_FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002127 const Point centerPoint = mDevice->getCenterPoint();
2128
2129 // Create an external stylus capable of reporting pressure data that
2130 // should be fused with a touch pointer.
2131 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2132 createUinputDevice<UinputExternalStylusWithPressure>();
2133 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2134 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2135 const auto stylusInfo = findDeviceByName(stylus->getName());
2136 ASSERT_TRUE(stylusInfo);
2137
2138 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2139
2140 const auto touchscreenId = mDeviceInfo.getId();
2141
2142 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2143 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002144 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2145 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002146 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002148
2149 // Start a finger gesture. The touch device will withhold generating any touches for
2150 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2151 mDevice->sendSlot(FIRST_SLOT);
2152 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2153 mDevice->sendToolType(MT_TOOL_FINGER);
2154 mDevice->sendDown(centerPoint);
2155 auto waitUntil = std::chrono::system_clock::now() +
2156 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002157 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Since the external stylus did not report a pressure value within the timeout,
2161 // it shows up as a finger pointer.
2162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2164 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2165 WithPressure(1.f))));
2166
2167 // Change the pressure on the external stylus. Since the pressure was not present at the start
2168 // of the gesture, it is ignored for now.
2169 stylus->setPressure(200);
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2171
2172 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002173 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2174 mDevice->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002177 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2178
2179 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2180 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2181 mDevice->sendToolType(MT_TOOL_FINGER);
2182 mDevice->sendDown(centerPoint);
2183 mDevice->sendSync();
2184 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2186 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2187 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2188
2189 // The external stylus did not generate any events.
2190 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002193
Prabir Pradhan5abecc32023-03-13 14:59:59 +00002194TEST_F(ExternalStylusIntegrationTest, DISABLED_UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002195 const Point centerPoint = mDevice->getCenterPoint();
2196
2197 // Create an external stylus device that does not support pressure. It should not affect any
2198 // touch pointers.
2199 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2200 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2202 const auto stylusInfo = findDeviceByName(stylus->getName());
2203 ASSERT_TRUE(stylusInfo);
2204
2205 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2206
2207 const auto touchscreenId = mDeviceInfo.getId();
2208
2209 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2210 // pressure data from the external stylus.
2211 mDevice->sendSlot(FIRST_SLOT);
2212 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2213 mDevice->sendToolType(MT_TOOL_FINGER);
2214 mDevice->sendDown(centerPoint);
2215 auto waitUntil = std::chrono::system_clock::now() +
2216 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2217 mDevice->sendSync();
2218 ASSERT_NO_FATAL_FAILURE(
2219 mTestListener
2220 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2221 WithToolType(
2222 AMOTION_EVENT_TOOL_TYPE_FINGER),
2223 WithButtonState(0),
2224 WithDeviceId(touchscreenId),
2225 WithPressure(1.f)),
2226 waitUntil));
2227
2228 // The external stylus did not generate any events.
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2231}
2232
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233// --- InputDeviceTest ---
2234class InputDeviceTest : public testing::Test {
2235protected:
2236 static const char* DEVICE_NAME;
2237 static const char* DEVICE_LOCATION;
2238 static const int32_t DEVICE_ID;
2239 static const int32_t DEVICE_GENERATION;
2240 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002241 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002242 static const int32_t EVENTHUB_ID;
2243 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2244
2245 std::shared_ptr<FakeEventHub> mFakeEventHub;
2246 sp<FakeInputReaderPolicy> mFakePolicy;
2247 std::unique_ptr<TestInputListener> mFakeListener;
2248 std::unique_ptr<InstrumentedInputReader> mReader;
2249 std::shared_ptr<InputDevice> mDevice;
2250
2251 void SetUp() override {
2252 mFakeEventHub = std::make_unique<FakeEventHub>();
2253 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2254 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002255 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002257 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 identifier.name = DEVICE_NAME;
2259 identifier.location = DEVICE_LOCATION;
2260 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2261 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2262 identifier);
2263 mReader->pushNextDevice(mDevice);
2264 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002265 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267
2268 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002269 mFakeListener.reset();
2270 mFakePolicy.clear();
2271 }
2272};
2273
2274const char* InputDeviceTest::DEVICE_NAME = "device";
2275const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2276const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2277const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002279const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2280 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002281const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2283
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002284TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002286 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2287 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002289
Michael Wrightd02c5b62014-02-10 15:10:22 -08002290TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2291 ASSERT_EQ(mDevice->isEnabled(), false);
2292}
2293
2294TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2295 // Configuration.
2296 InputReaderConfiguration config;
2297 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2298
2299 // Reset.
2300 unused += mDevice->reset(ARBITRARY_TIME);
2301
2302 NotifyDeviceResetArgs resetArgs;
2303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2304 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2305 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2306
2307 // Metadata.
2308 ASSERT_TRUE(mDevice->isIgnored());
2309 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2310
2311 InputDeviceInfo info = mDevice->getDeviceInfo();
2312 ASSERT_EQ(DEVICE_ID, info.getId());
2313 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2314 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2315 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2316
2317 // State queries.
2318 ASSERT_EQ(0, mDevice->getMetaState());
2319
2320 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2321 << "Ignored device should return unknown key code state.";
2322 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2323 << "Ignored device should return unknown scan code state.";
2324 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2325 << "Ignored device should return unknown switch state.";
2326
2327 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2328 uint8_t flags[2] = { 0, 1 };
2329 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2330 << "Ignored device should never mark any key codes.";
2331 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2332 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2333}
2334
2335TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2336 // Configuration.
2337 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002338
2339 FakeInputMapper& mapper1 =
2340 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2341 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342 mapper1.setMetaState(AMETA_ALT_ON);
2343 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002344 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2346 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2347 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2348 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2349 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2350
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002351 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2353 mapper2.setMetaState(AMETA_SHIFT_ON);
2354
2355 InputReaderConfiguration config;
2356 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2357
2358 std::string propertyValue;
2359 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2360 << "Device should have read configuration during configuration phase.";
2361 ASSERT_EQ("value", propertyValue);
2362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002363 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2364 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365
2366 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002367 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002368 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2369 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370
2371 NotifyDeviceResetArgs resetArgs;
2372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2373 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2374 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2375
2376 // Metadata.
2377 ASSERT_FALSE(mDevice->isIgnored());
2378 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2379
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002380 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002382 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2384 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2385
2386 // State queries.
2387 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2388 << "Should query mappers and combine meta states.";
2389
2390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2391 << "Should return unknown key code state when source not supported.";
2392 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2393 << "Should return unknown scan code state when source not supported.";
2394 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2395 << "Should return unknown switch state when source not supported.";
2396
2397 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2398 << "Should query mapper when source is supported.";
2399 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2400 << "Should query mapper when source is supported.";
2401 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2402 << "Should query mapper when source is supported.";
2403
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002404 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002406 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 << "Should do nothing when source is unsupported.";
2408 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2409 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2410 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2411 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2412
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002413 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 << "Should query mapper when source is supported.";
2415 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2416 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2417 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2418 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2419
2420 // Event handling.
2421 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002422 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002423 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002425 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2426 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427}
2428
Arthur Hung2c9a3342019-07-23 14:18:59 +08002429// A single input device is associated with a specific display. Check that:
2430// 1. Device is disabled if the viewport corresponding to the associated display is not found
2431// 2. Device is disabled when setEnabled API is called
2432TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002433 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002434
2435 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002436 std::list<NotifyArgs> unused =
2437 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002438
2439 // Device should be enabled by default.
2440 ASSERT_TRUE(mDevice->isEnabled());
2441
2442 // Prepare associated info.
2443 constexpr uint8_t hdmi = 1;
2444 const std::string UNIQUE_ID = "local:1";
2445
2446 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002447 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2448 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002449 // Device should be disabled because it is associated with a specific display via
2450 // input port <-> display port association, but the corresponding display is not found
2451 ASSERT_FALSE(mDevice->isEnabled());
2452
2453 // Prepare displays.
2454 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002455 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002456 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002457 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2458 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002459 ASSERT_TRUE(mDevice->isEnabled());
2460
2461 // Device should be disabled after set disable.
2462 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002463 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2464 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002465 ASSERT_FALSE(mDevice->isEnabled());
2466
2467 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002468 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002470 ASSERT_FALSE(mDevice->isEnabled());
2471}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Christine Franks1ba71cc2021-04-07 14:37:42 -07002473TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2474 // Device should be enabled by default.
2475 mFakePolicy->clearViewports();
2476 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002477 std::list<NotifyArgs> unused =
2478 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002479 ASSERT_TRUE(mDevice->isEnabled());
2480
2481 // Device should be disabled because it is associated with a specific display, but the
2482 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002483 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002484 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2485 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002486 ASSERT_FALSE(mDevice->isEnabled());
2487
2488 // Device should be enabled when a display is found.
2489 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002490 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002491 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002492 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2493 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002494 ASSERT_TRUE(mDevice->isEnabled());
2495
2496 // Device should be disabled after set disable.
2497 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2499 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002500 ASSERT_FALSE(mDevice->isEnabled());
2501
2502 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002503 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2504 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002505 ASSERT_FALSE(mDevice->isEnabled());
2506}
2507
Christine Franks2a2293c2022-01-18 11:51:16 -08002508TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2509 mFakePolicy->clearViewports();
2510 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 std::list<NotifyArgs> unused =
2512 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002513
Christine Franks2a2293c2022-01-18 11:51:16 -08002514 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2515 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002516 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002517 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002518 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2519 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002520 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2521}
2522
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002523/**
2524 * This test reproduces a crash caused by a dangling reference that remains after device is added
2525 * and removed. The reference is accessed in InputDevice::dump(..);
2526 */
2527TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2528 constexpr int32_t TEST_EVENTHUB_ID = 10;
2529 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2530
Harry Cutts33476232023-01-30 19:57:29 +00002531 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2532 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002533 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2534 std::string dumpStr, eventHubDevStr;
2535 device.dump(dumpStr, eventHubDevStr);
2536}
2537
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002538TEST_F(InputDeviceTest, GetBluetoothAddress) {
2539 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2540 ASSERT_TRUE(address);
2541 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2542}
2543
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544// --- SwitchInputMapperTest ---
2545
2546class SwitchInputMapperTest : public InputMapperTest {
2547protected:
2548};
2549
2550TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002553 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554}
2555
2556TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002557 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002559 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002562 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564}
2565
2566TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002567 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002568 std::list<NotifyArgs> out;
2569 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2570 ASSERT_TRUE(out.empty());
2571 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2572 ASSERT_TRUE(out.empty());
2573 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2574 ASSERT_TRUE(out.empty());
2575 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002577 ASSERT_EQ(1u, out.size());
2578 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002580 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2581 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 args.switchMask);
2583 ASSERT_EQ(uint32_t(0), args.policyFlags);
2584}
2585
Chris Ye87143712020-11-10 05:05:58 +00002586// --- VibratorInputMapperTest ---
2587class VibratorInputMapperTest : public InputMapperTest {
2588protected:
2589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2590};
2591
2592TEST_F(VibratorInputMapperTest, GetSources) {
2593 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2594
2595 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2596}
2597
2598TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2599 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2600
2601 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2602}
2603
2604TEST_F(VibratorInputMapperTest, Vibrate) {
2605 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002606 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002607 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2608
2609 VibrationElement pattern(2);
2610 VibrationSequence sequence(2);
2611 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002612 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2613 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002614 sequence.addElement(pattern);
2615 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002616 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2617 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002618 sequence.addElement(pattern);
2619
2620 std::vector<int64_t> timings = {0, 1};
2621 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2622
2623 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002624 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002625 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002626 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002627 // Verify vibrator state listener was notified.
2628 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002629 ASSERT_EQ(1u, out.size());
2630 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2631 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2632 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002633 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002634 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002635 ASSERT_FALSE(mapper.isVibrating());
2636 // Verify vibrator state listener was notified.
2637 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002638 ASSERT_EQ(1u, out.size());
2639 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2640 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2641 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002642}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Chris Yef59a2f42020-10-16 12:55:26 -07002644// --- SensorInputMapperTest ---
2645
2646class SensorInputMapperTest : public InputMapperTest {
2647protected:
2648 static const int32_t ACCEL_RAW_MIN;
2649 static const int32_t ACCEL_RAW_MAX;
2650 static const int32_t ACCEL_RAW_FUZZ;
2651 static const int32_t ACCEL_RAW_FLAT;
2652 static const int32_t ACCEL_RAW_RESOLUTION;
2653
2654 static const int32_t GYRO_RAW_MIN;
2655 static const int32_t GYRO_RAW_MAX;
2656 static const int32_t GYRO_RAW_FUZZ;
2657 static const int32_t GYRO_RAW_FLAT;
2658 static const int32_t GYRO_RAW_RESOLUTION;
2659
2660 static const float GRAVITY_MS2_UNIT;
2661 static const float DEGREE_RADIAN_UNIT;
2662
2663 void prepareAccelAxes();
2664 void prepareGyroAxes();
2665 void setAccelProperties();
2666 void setGyroProperties();
2667 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2668};
2669
2670const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2671const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2672const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2673const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2674const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2675
2676const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2677const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2678const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2679const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2680const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2681
2682const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2683const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2684
2685void SensorInputMapperTest::prepareAccelAxes() {
2686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2687 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2688 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2689 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2690 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2691 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2692}
2693
2694void SensorInputMapperTest::prepareGyroAxes() {
2695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2696 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2697 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2698 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2699 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2700 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2701}
2702
2703void SensorInputMapperTest::setAccelProperties() {
2704 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2705 /* sensorDataIndex */ 0);
2706 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2707 /* sensorDataIndex */ 1);
2708 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2709 /* sensorDataIndex */ 2);
2710 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2711 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2712 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2713 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2714 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2715}
2716
2717void SensorInputMapperTest::setGyroProperties() {
2718 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2719 /* sensorDataIndex */ 0);
2720 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2721 /* sensorDataIndex */ 1);
2722 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2723 /* sensorDataIndex */ 2);
2724 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2725 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2726 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2727 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2728 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2729}
2730
2731TEST_F(SensorInputMapperTest, GetSources) {
2732 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2733
2734 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2735}
2736
2737TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2738 setAccelProperties();
2739 prepareAccelAxes();
2740 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2741
2742 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2743 std::chrono::microseconds(10000),
2744 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002751
2752 NotifySensorArgs args;
2753 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2754 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2755 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2756
2757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2758 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2759 ASSERT_EQ(args.deviceId, DEVICE_ID);
2760 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2761 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2762 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2763 ASSERT_EQ(args.values, values);
2764 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2765}
2766
2767TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2768 setGyroProperties();
2769 prepareGyroAxes();
2770 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2771
2772 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2773 std::chrono::microseconds(10000),
2774 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002775 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002781
2782 NotifySensorArgs args;
2783 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2784 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2785 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2786
2787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2788 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2789 ASSERT_EQ(args.deviceId, DEVICE_ID);
2790 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2791 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2792 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2793 ASSERT_EQ(args.values, values);
2794 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2795}
2796
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797// --- KeyboardInputMapperTest ---
2798
2799class KeyboardInputMapperTest : public InputMapperTest {
2800protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002801 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002802 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002803 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002806 int32_t originalKeyCode, int32_t rotatedKeyCode,
2807 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808};
2809
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002810/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2811 * orientation.
2812 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002813void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002814 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2815 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002816}
2817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002819 int32_t originalScanCode, int32_t originalKeyCode,
2820 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 NotifyKeyArgs args;
2822
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2826 ASSERT_EQ(originalScanCode, args.scanCode);
2827 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2833 ASSERT_EQ(originalScanCode, args.scanCode);
2834 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836}
2837
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 KeyboardInputMapper& mapper =
2840 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2841 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
2846TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2847 const int32_t USAGE_A = 0x070004;
2848 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2850 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002851 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2852 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2853 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 KeyboardInputMapper& mapper =
2856 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002858 // Initial metastate is AMETA_NONE.
2859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
2861 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863 NotifyKeyArgs args;
2864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2865 ASSERT_EQ(DEVICE_ID, args.deviceId);
2866 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2867 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2868 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2869 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2870 ASSERT_EQ(KEY_HOME, args.scanCode);
2871 ASSERT_EQ(AMETA_NONE, args.metaState);
2872 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2873 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2874 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2875
2876 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002877 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2879 ASSERT_EQ(DEVICE_ID, args.deviceId);
2880 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2881 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2882 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2883 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2884 ASSERT_EQ(KEY_HOME, args.scanCode);
2885 ASSERT_EQ(AMETA_NONE, args.metaState);
2886 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2887 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2888 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2889
2890 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2894 ASSERT_EQ(DEVICE_ID, args.deviceId);
2895 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2897 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2898 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2899 ASSERT_EQ(0, args.scanCode);
2900 ASSERT_EQ(AMETA_NONE, args.metaState);
2901 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2902 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2903 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2904
2905 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2907 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2909 ASSERT_EQ(DEVICE_ID, args.deviceId);
2910 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2911 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2912 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2913 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2914 ASSERT_EQ(0, args.scanCode);
2915 ASSERT_EQ(AMETA_NONE, args.metaState);
2916 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2917 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2918 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2919
2920 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2924 ASSERT_EQ(DEVICE_ID, args.deviceId);
2925 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2926 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2927 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2928 ASSERT_EQ(0, args.keyCode);
2929 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2930 ASSERT_EQ(AMETA_NONE, args.metaState);
2931 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2932 ASSERT_EQ(0U, args.policyFlags);
2933 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2934
2935 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2937 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2939 ASSERT_EQ(DEVICE_ID, args.deviceId);
2940 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2941 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2942 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2943 ASSERT_EQ(0, args.keyCode);
2944 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2945 ASSERT_EQ(AMETA_NONE, args.metaState);
2946 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2947 ASSERT_EQ(0U, args.policyFlags);
2948 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2949}
2950
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002951TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2954 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2955
2956 KeyboardInputMapper& mapper =
2957 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2958 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2959
2960 // Key down by scan code.
2961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2962 NotifyKeyArgs args;
2963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2965
2966 // Key up by scan code.
2967 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2969 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2970}
2971
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002972/**
2973 * Ensure that the readTime is set to the time when the EV_KEY is received.
2974 */
2975TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2977
2978 KeyboardInputMapper& mapper =
2979 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2981 NotifyKeyArgs args;
2982
2983 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00002984 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2986 ASSERT_EQ(12, args.readTime);
2987
2988 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00002989 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(15, args.readTime);
2992}
2993
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002997 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2998 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 KeyboardInputMapper& mapper =
3002 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3003 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004
Arthur Hung95f68612022-04-07 14:08:22 +08003005 // Initial metastate is AMETA_NONE.
3006 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007
3008 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 NotifyKeyArgs args;
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003014 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015
3016 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021
3022 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003023 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027
3028 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003029 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3031 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003032 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003033 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034}
3035
3036TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003037 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003042 KeyboardInputMapper& mapper =
3043 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3044 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045
Michael Wrighta9cf4192022-12-01 23:46:39 +00003046 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3048 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3049 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3050 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3051 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3052 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3053 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3054 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3055}
3056
3057TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003058 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 KeyboardInputMapper& mapper =
3065 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3066 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
Michael Wrighta9cf4192022-12-01 23:46:39 +00003068 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003069 ASSERT_NO_FATAL_FAILURE(
3070 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3071 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3072 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3073 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3074 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3075 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3076 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003080 ASSERT_NO_FATAL_FAILURE(
3081 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3082 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3083 AKEYCODE_DPAD_UP, DISPLAY_ID));
3084 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3085 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3086 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3087 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003089 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003090 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003091 ASSERT_NO_FATAL_FAILURE(
3092 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3093 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3094 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3095 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3096 AKEYCODE_DPAD_UP, DISPLAY_ID));
3097 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3098 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003100 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003101 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003102 ASSERT_NO_FATAL_FAILURE(
3103 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3105 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3107 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3109 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 // Special case: if orientation changes while key is down, we still emit the same keycode
3112 // in the key up as we did in the key down.
3113 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003114 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003115 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3119 ASSERT_EQ(KEY_UP, args.scanCode);
3120 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3121
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003122 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003123 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3127 ASSERT_EQ(KEY_UP, args.scanCode);
3128 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3129}
3130
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003131TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3132 // If the keyboard is not orientation aware,
3133 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003136 KeyboardInputMapper& mapper =
3137 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3138 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 NotifyKeyArgs args;
3140
3141 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3146 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3147
Michael Wrighta9cf4192022-12-01 23:46:39 +00003148 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3154}
3155
3156TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3157 // If the keyboard is orientation aware,
3158 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003159 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003160
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003161 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 KeyboardInputMapper& mapper =
3163 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3164 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003165 NotifyKeyArgs args;
3166
3167 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3168 // ^--- already checked by the previous test
3169
Michael Wrighta9cf4192022-12-01 23:46:39 +00003170 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003171 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(DISPLAY_ID, args.displayId);
3177
3178 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003179 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003180 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003181 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3186 ASSERT_EQ(newDisplayId, args.displayId);
3187}
3188
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003190 KeyboardInputMapper& mapper =
3191 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3192 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003194 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003197 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199}
3200
Philip Junker4af3b3d2021-12-14 10:36:55 +01003201TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3202 KeyboardInputMapper& mapper =
3203 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3204 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3205
3206 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3207 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3208 << "If a mapping is available, the result is equal to the mapping";
3209
3210 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3211 << "If no mapping is available, the result is the key location";
3212}
3213
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 KeyboardInputMapper& mapper =
3216 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3217 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003219 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003223 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224}
3225
3226TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 KeyboardInputMapper& mapper =
3228 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3229 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003234 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 ASSERT_TRUE(flags[0]);
3236 ASSERT_FALSE(flags[1]);
3237}
3238
3239TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003240 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3241 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3242 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3243 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 KeyboardInputMapper& mapper =
3248 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003250 // Initial metastate is AMETA_NONE.
3251 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3255 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3256 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
3258 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003261 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3262 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3263 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003264 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
3266 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003269 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3270 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3271 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3278 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3279 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
3282 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3286 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3287 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
3290 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003293 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3295 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
3298 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003301 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305}
3306
Chris Yea52ade12020-08-27 16:49:20 -07003307TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3308 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3312
3313 KeyboardInputMapper& mapper =
3314 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3315 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3316
Chris Yea52ade12020-08-27 16:49:20 -07003317 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003318 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003319 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3320 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3321 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3322 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3323
3324 NotifyKeyArgs args;
3325 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3328 ASSERT_EQ(AMETA_NONE, args.metaState);
3329 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3331 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3332
3333 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003334 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3336 ASSERT_EQ(AMETA_NONE, args.metaState);
3337 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3338 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3339 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3340}
3341
Arthur Hung2c9a3342019-07-23 14:18:59 +08003342TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3343 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003348
3349 // keyboard 2.
3350 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003351 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003354 std::shared_ptr<InputDevice> device2 =
3355 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003356 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003357
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3359 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3360 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3361 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003368 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003369 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003370 std::list<NotifyArgs> unused =
3371 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003372 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003373 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003374
3375 // Prepared displays and associated info.
3376 constexpr uint8_t hdmi1 = 0;
3377 constexpr uint8_t hdmi2 = 1;
3378 const std::string SECONDARY_UNIQUE_ID = "local:1";
3379
3380 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3381 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3382
3383 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003384 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3385 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 ASSERT_FALSE(device2->isEnabled());
3387
3388 // Prepare second display.
3389 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003390 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003391 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003392 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003393 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003394 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003395 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3396 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397
3398 // Device should be enabled after the associated display is found.
3399 ASSERT_TRUE(mDevice->isEnabled());
3400 ASSERT_TRUE(device2->isEnabled());
3401
3402 // Test pad key events
3403 ASSERT_NO_FATAL_FAILURE(
3404 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3405 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3406 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3407 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3408 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3409 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3410 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3411
3412 ASSERT_NO_FATAL_FAILURE(
3413 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3414 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3415 AKEYCODE_DPAD_RIGHT, newDisplayId));
3416 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3417 AKEYCODE_DPAD_DOWN, newDisplayId));
3418 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3419 AKEYCODE_DPAD_LEFT, newDisplayId));
3420}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
arthurhungc903df12020-08-11 15:08:42 +08003422TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3423 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3425 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3429
3430 KeyboardInputMapper& mapper =
3431 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3432 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003433 // Initial metastate is AMETA_NONE.
3434 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003435
3436 // Initialization should have turned all of the lights off.
3437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3440
3441 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003444 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3445 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3446
3447 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003450 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3451 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3452
3453 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003456 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3457 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3458
3459 mFakeEventHub->removeDevice(EVENTHUB_ID);
3460 mReader->loopOnce();
3461
3462 // keyboard 2 should default toggle keys.
3463 const std::string USB2 = "USB2";
3464 const std::string DEVICE_NAME2 = "KEYBOARD2";
3465 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3466 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3467 std::shared_ptr<InputDevice> device2 =
3468 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003469 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003470 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3471 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3472 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3473 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3475 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3476
arthurhung6fe95782020-10-05 22:41:16 +08003477 KeyboardInputMapper& mapper2 =
3478 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003480 std::list<NotifyArgs> unused =
3481 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003482 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003483 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003484
3485 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3486 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3487 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003488 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3489 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003490}
3491
Arthur Hungcb40a002021-08-03 14:31:01 +00003492TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3496
3497 // Suppose we have two mappers. (DPAD + KEYBOARD)
3498 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3499 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3500 KeyboardInputMapper& mapper =
3501 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003503 // Initial metastate is AMETA_NONE.
3504 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003505
3506 mReader->toggleCapsLockState(DEVICE_ID);
3507 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3508}
3509
Arthur Hungfb3cc112022-04-13 07:39:50 +00003510TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3511 // keyboard 1.
3512 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3513 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3514 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3518
3519 KeyboardInputMapper& mapper1 =
3520 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3521 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3522
3523 // keyboard 2.
3524 const std::string USB2 = "USB2";
3525 const std::string DEVICE_NAME2 = "KEYBOARD2";
3526 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3527 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3528 std::shared_ptr<InputDevice> device2 =
3529 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3530 ftl::Flags<InputDeviceClass>(0));
3531 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3533 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3536 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3537
3538 KeyboardInputMapper& mapper2 =
3539 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003541 std::list<NotifyArgs> unused =
3542 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003543 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003544 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003545
Arthur Hung95f68612022-04-07 14:08:22 +08003546 // Initial metastate is AMETA_NONE.
3547 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3548 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3549
3550 // Toggle num lock on and off.
3551 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3552 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003553 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3554 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3555 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3556
3557 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3558 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3559 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3560 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3561 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3562
3563 // Toggle caps lock on and off.
3564 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3565 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3566 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3567 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3568 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3569
3570 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3572 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3573 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3574 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3575
3576 // Toggle scroll lock on and off.
3577 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3579 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3580 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3581 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3582
3583 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3586 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3587 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3588}
3589
Arthur Hung2141d542022-08-23 07:45:21 +00003590TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3591 const int32_t USAGE_A = 0x070004;
3592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3593 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3594
3595 KeyboardInputMapper& mapper =
3596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3598 // Key down by scan code.
3599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3600 NotifyKeyArgs args;
3601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(DEVICE_ID, args.deviceId);
3603 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3604 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3606 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3607 ASSERT_EQ(KEY_HOME, args.scanCode);
3608 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3609
3610 // Disable device, it should synthesize cancellation events for down events.
3611 mFakePolicy->addDisabledDevice(DEVICE_ID);
3612 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3613
3614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3616 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3617 ASSERT_EQ(KEY_HOME, args.scanCode);
3618 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3619}
3620
Zixuan Qufecb6062022-11-12 04:44:31 +00003621TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3622 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3623 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3624 std::list<NotifyArgs> unused =
3625 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3626
3627 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3628
3629 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3630 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3631
3632 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3633 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3634 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3635 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3636 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3637}
3638
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003639TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3640 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3641 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3642
3643 // Configuration
3644 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3646 InputReaderConfiguration config;
3647 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3648
3649 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3650 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3651}
3652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003653// --- KeyboardInputMapperTest_ExternalDevice ---
3654
3655class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3656protected:
Chris Yea52ade12020-08-27 16:49:20 -07003657 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658};
3659
3660TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003661 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3662 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3667 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
3670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003672
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003674 NotifyKeyArgs args;
3675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(uint32_t(0), args.policyFlags);
3681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003684 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(uint32_t(0), args.policyFlags);
3689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3697}
3698
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003699TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003700 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003705
Powei Fengd041c5d2019-05-03 17:11:33 -07003706 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 KeyboardInputMapper& mapper =
3708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712 NotifyKeyArgs args;
3713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3714 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3715
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3718 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3719
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(uint32_t(0), args.policyFlags);
3723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3726 ASSERT_EQ(uint32_t(0), args.policyFlags);
3727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3730 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3735}
3736
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737// --- CursorInputMapperTest ---
3738
3739class CursorInputMapperTest : public InputMapperTest {
3740protected:
3741 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3742
Michael Wright17db18e2020-06-26 20:51:44 +01003743 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Chris Yea52ade12020-08-27 16:49:20 -07003745 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746 InputMapperTest::SetUp();
3747
Michael Wright17db18e2020-06-26 20:51:44 +01003748 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003749 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 }
3751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3753 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003754
Michael Wrighta9cf4192022-12-01 23:46:39 +00003755 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003756 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3757 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3758 }
3759
3760 void prepareSecondaryDisplay() {
3761 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003762 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003763 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003764 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003765
3766 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3767 float pressure) {
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3769 0.0f, 0.0f, 0.0f, EPSILON));
3770 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771};
3772
3773const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3776 int32_t originalY, int32_t rotatedX,
3777 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 NotifyMotionArgs args;
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003785 ASSERT_NO_FATAL_FAILURE(
3786 assertCursorPointerCoords(args.pointerCoords[0],
3787 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3788 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789}
3790
3791TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796}
3797
3798TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803}
3804
3805TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003813 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3814 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3816 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3817
3818 // When the bounds are set, then there should be a valid motion range.
3819 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3820
3821 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
3824 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3825 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3826 1, 800 - 1, 0.0f, 0.0f));
3827 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3828 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3829 2, 480 - 1, 0.0f, 0.0f));
3830 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3831 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3832 0.0f, 1.0f, 0.0f, 0.0f));
3833}
3834
3835TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841
3842 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3843 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3844 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3845 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3846 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3847 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3848 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3849 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3850 0.0f, 1.0f, 0.0f, 0.0f));
3851}
3852
3853TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
3859 NotifyMotionArgs args;
3860
3861 // Button press.
3862 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3866 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3867 ASSERT_EQ(DEVICE_ID, args.deviceId);
3868 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3869 ASSERT_EQ(uint32_t(0), args.policyFlags);
3870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(0, args.flags);
3872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3873 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3874 ASSERT_EQ(0, args.edgeFlags);
3875 ASSERT_EQ(uint32_t(1), args.pointerCount);
3876 ASSERT_EQ(0, args.pointerProperties[0].id);
3877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003878 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3880 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3881 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3882
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3884 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3885 ASSERT_EQ(DEVICE_ID, args.deviceId);
3886 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3887 ASSERT_EQ(uint32_t(0), args.policyFlags);
3888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3889 ASSERT_EQ(0, args.flags);
3890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3892 ASSERT_EQ(0, args.edgeFlags);
3893 ASSERT_EQ(uint32_t(1), args.pointerCount);
3894 ASSERT_EQ(0, args.pointerProperties[0].id);
3895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003896 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003897 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3899 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3900
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3903 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3906 ASSERT_EQ(DEVICE_ID, args.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3908 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3910 ASSERT_EQ(0, args.flags);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3912 ASSERT_EQ(0, args.buttonState);
3913 ASSERT_EQ(0, args.edgeFlags);
3914 ASSERT_EQ(uint32_t(1), args.pointerCount);
3915 ASSERT_EQ(0, args.pointerProperties[0].id);
3916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003917 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3919 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3920 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3921
3922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3923 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3924 ASSERT_EQ(DEVICE_ID, args.deviceId);
3925 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3926 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3928 ASSERT_EQ(0, args.flags);
3929 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3930 ASSERT_EQ(0, args.buttonState);
3931 ASSERT_EQ(0, args.edgeFlags);
3932 ASSERT_EQ(uint32_t(1), args.pointerCount);
3933 ASSERT_EQ(0, args.pointerProperties[0].id);
3934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003935 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3939}
3940
3941TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003943 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944
3945 NotifyMotionArgs args;
3946
3947 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003952 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3953 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3954 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003961 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3962 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963}
3964
3965TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 NotifyMotionArgs args;
3970
3971 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003980 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003981
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003987 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003988
3989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003991 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992}
3993
3994TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 NotifyMotionArgs args;
3999
4000 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004007 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4008 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4009 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4014 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4015 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004023 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4024 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4025 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037}
4038
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004039TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004040 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004042 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4043 // need to be rotated.
4044 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
Michael Wrighta9cf4192022-12-01 23:46:39 +00004047 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4056}
4057
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004058TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004059 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004061 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4062 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004065 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004066 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4075
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004076 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004077 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004087 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004088 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4097
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004099 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108}
4109
4110TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113
4114 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4115 mFakePointerController->setPosition(100, 200);
4116 mFakePointerController->setButtonState(0);
4117
4118 NotifyMotionArgs motionArgs;
4119 NotifyKeyArgs keyArgs;
4120
4121 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4127 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128 ASSERT_NO_FATAL_FAILURE(
4129 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4133 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4134 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004135 ASSERT_NO_FATAL_FAILURE(
4136 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004137
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_EQ(0, motionArgs.buttonState);
4143 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004144 ASSERT_NO_FATAL_FAILURE(
4145 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 ASSERT_EQ(0, motionArgs.buttonState);
4150 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(
4152 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(0, motionArgs.buttonState);
4157 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004158 ASSERT_NO_FATAL_FAILURE(
4159 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4167 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4168 motionArgs.buttonState);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4170 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004171 ASSERT_NO_FATAL_FAILURE(
4172 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4178 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004181
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4185 motionArgs.buttonState);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4187 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(
4189 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4211 ASSERT_EQ(0, motionArgs.buttonState);
4212 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(0, motionArgs.buttonState);
4220 ASSERT_EQ(0, mFakePointerController->getButtonState());
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4226 ASSERT_EQ(0, motionArgs.buttonState);
4227 ASSERT_EQ(0, mFakePointerController->getButtonState());
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
4232 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4237 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_EQ(0, motionArgs.buttonState);
4265 ASSERT_EQ(0, mFakePointerController->getButtonState());
4266
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4270 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4271 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4272
4273 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4277 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4278 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_EQ(0, motionArgs.buttonState);
4299 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(
4301 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4305 ASSERT_EQ(0, motionArgs.buttonState);
4306 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4313
4314 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4319 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4346 ASSERT_EQ(0, motionArgs.buttonState);
4347 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4353 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4354
4355 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(0, motionArgs.buttonState);
4381 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.buttonState);
4388 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(
4390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4395}
4396
4397TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004399 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
4401 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4402 mFakePointerController->setPosition(100, 200);
4403 mFakePointerController->setButtonState(0);
4404
4405 NotifyMotionArgs args;
4406
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004411 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4414 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004415 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 addConfigurationProperty("cursor.mode", "pointer");
4420 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004421 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 NotifyDeviceResetArgs resetArgs;
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4425 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4426 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4427
4428 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4429 mFakePointerController->setPosition(100, 200);
4430 mFakePointerController->setButtonState(0);
4431
4432 NotifyMotionArgs args;
4433
4434 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4442 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004443 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004444
4445 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4452 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4454 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4457 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4458
4459 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4461 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4463 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4466 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4471 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4472
4473 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004482 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483
4484 // Disable pointer capture and check that the device generation got bumped
4485 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004486 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 mFakePolicy->setPointerCapture(false);
4488 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004489 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4501 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004502 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004505/**
4506 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4507 * pointer acceleration or speed processing should not be applied.
4508 */
4509TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4510 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004511 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4512 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004513 mFakePolicy->setVelocityControlParams(testParams);
4514 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4515
4516 NotifyDeviceResetArgs resetArgs;
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4518 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4519 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4520
4521 NotifyMotionArgs args;
4522
4523 // Move and verify scale is applied.
4524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4528 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4530 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4531 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4532 ASSERT_GT(relX, 10);
4533 ASSERT_GT(relY, 20);
4534
4535 // Enable Pointer Capture
4536 mFakePolicy->setPointerCapture(true);
4537 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4538 NotifyPointerCaptureChangedArgs captureArgs;
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4540 ASSERT_TRUE(captureArgs.request.enable);
4541
4542 // Move and verify scale is not applied.
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4549 ASSERT_EQ(10, args.pointerCoords[0].getX());
4550 ASSERT_EQ(20, args.pointerCoords[0].getY());
4551}
4552
Prabir Pradhan208360b2022-06-24 18:37:04 +00004553TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4554 addConfigurationProperty("cursor.mode", "pointer");
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 NotifyDeviceResetArgs resetArgs;
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4559 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4560 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4561
4562 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004563 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004564
4565 NotifyMotionArgs args;
4566
4567 // Verify that the coordinates are rotated.
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4572 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4574 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4575 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4576
4577 // Enable Pointer Capture.
4578 mFakePolicy->setPointerCapture(true);
4579 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4580 NotifyPointerCaptureChangedArgs captureArgs;
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4582 ASSERT_TRUE(captureArgs.request.enable);
4583
4584 // Move and verify rotation is not applied.
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_EQ(10, args.pointerCoords[0].getX());
4592 ASSERT_EQ(20, args.pointerCoords[0].getY());
4593}
4594
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004595TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004596 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004597
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004598 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004599 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600
4601 // Set up the secondary display as the display on which the pointer should be shown.
4602 // The InputDevice is not associated with any display.
4603 prepareSecondaryDisplay();
4604 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004605 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4606
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004607 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004608 mFakePointerController->setPosition(100, 200);
4609 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004610
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004616 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4617 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4618 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004619 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004620}
4621
4622TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4623 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4624
4625 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004626 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004627
4628 // Set up the secondary display as the display on which the pointer should be shown,
4629 // and associate the InputDevice with the secondary display.
4630 prepareSecondaryDisplay();
4631 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4632 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4633 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4634
4635 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4636 mFakePointerController->setPosition(100, 200);
4637 mFakePointerController->setButtonState(0);
4638
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4644 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4645 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004646 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004647}
4648
4649TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4650 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4651
4652 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004653 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004654 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4655
4656 // Associate the InputDevice with the secondary display.
4657 prepareSecondaryDisplay();
4658 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4659 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4660
4661 // The mapper should not generate any events because it is associated with a display that is
4662 // different from the pointer display.
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004667}
4668
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004669// --- BluetoothCursorInputMapperTest ---
4670
4671class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4672protected:
4673 void SetUp() override {
4674 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4675
4676 mFakePointerController = std::make_shared<FakePointerController>();
4677 mFakePolicy->setPointerController(mFakePointerController);
4678 }
4679};
4680
4681TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4682 addConfigurationProperty("cursor.mode", "pointer");
4683 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4684
4685 nsecs_t kernelEventTime = ARBITRARY_TIME;
4686 nsecs_t expectedEventTime = ARBITRARY_TIME;
4687 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4688 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4691 WithEventTime(expectedEventTime))));
4692
4693 // Process several events that come in quick succession, according to their timestamps.
4694 for (int i = 0; i < 3; i++) {
4695 constexpr static nsecs_t delta = ms2ns(1);
4696 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4697 kernelEventTime += delta;
4698 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4699
4700 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4701 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4703 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4704 WithEventTime(expectedEventTime))));
4705 }
4706}
4707
4708TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4709 addConfigurationProperty("cursor.mode", "pointer");
4710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4711
4712 nsecs_t expectedEventTime = ARBITRARY_TIME;
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4716 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4717 WithEventTime(expectedEventTime))));
4718
4719 // Process several events with the same timestamp from the kernel.
4720 // Ensure that we do not generate events too far into the future.
4721 constexpr static int32_t numEvents =
4722 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4723 for (int i = 0; i < numEvents; i++) {
4724 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4725
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4730 WithEventTime(expectedEventTime))));
4731 }
4732
4733 // By processing more events with the same timestamp, we should not generate events with a
4734 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4735 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4736 for (int i = 0; i < 3; i++) {
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4741 WithEventTime(cappedEventTime))));
4742 }
4743}
4744
4745TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4746 addConfigurationProperty("cursor.mode", "pointer");
4747 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4748
4749 nsecs_t kernelEventTime = ARBITRARY_TIME;
4750 nsecs_t expectedEventTime = ARBITRARY_TIME;
4751 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4752 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4755 WithEventTime(expectedEventTime))));
4756
4757 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4758 // smoothening is not needed, its timestamp is not affected.
4759 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4760 expectedEventTime = kernelEventTime;
4761
4762 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4763 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4766 WithEventTime(expectedEventTime))));
4767}
4768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769// --- TouchInputMapperTest ---
4770
4771class TouchInputMapperTest : public InputMapperTest {
4772protected:
4773 static const int32_t RAW_X_MIN;
4774 static const int32_t RAW_X_MAX;
4775 static const int32_t RAW_Y_MIN;
4776 static const int32_t RAW_Y_MAX;
4777 static const int32_t RAW_TOUCH_MIN;
4778 static const int32_t RAW_TOUCH_MAX;
4779 static const int32_t RAW_TOOL_MIN;
4780 static const int32_t RAW_TOOL_MAX;
4781 static const int32_t RAW_PRESSURE_MIN;
4782 static const int32_t RAW_PRESSURE_MAX;
4783 static const int32_t RAW_ORIENTATION_MIN;
4784 static const int32_t RAW_ORIENTATION_MAX;
4785 static const int32_t RAW_DISTANCE_MIN;
4786 static const int32_t RAW_DISTANCE_MAX;
4787 static const int32_t RAW_TILT_MIN;
4788 static const int32_t RAW_TILT_MAX;
4789 static const int32_t RAW_ID_MIN;
4790 static const int32_t RAW_ID_MAX;
4791 static const int32_t RAW_SLOT_MIN;
4792 static const int32_t RAW_SLOT_MAX;
4793 static const float X_PRECISION;
4794 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004795 static const float X_PRECISION_VIRTUAL;
4796 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
4798 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004799 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
4801 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4802
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004803 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004804 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004805
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 enum Axes {
4807 POSITION = 1 << 0,
4808 TOUCH = 1 << 1,
4809 TOOL = 1 << 2,
4810 PRESSURE = 1 << 3,
4811 ORIENTATION = 1 << 4,
4812 MINOR = 1 << 5,
4813 ID = 1 << 6,
4814 DISTANCE = 1 << 7,
4815 TILT = 1 << 8,
4816 SLOT = 1 << 9,
4817 TOOL_TYPE = 1 << 10,
4818 };
4819
Michael Wrighta9cf4192022-12-01 23:46:39 +00004820 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004821 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004822 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004824 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 int32_t toRawX(float displayX);
4826 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004827 int32_t toRotatedRawX(float displayX);
4828 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004829 float toCookedX(float rawX, float rawY);
4830 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004832 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004834 float toDisplayY(int32_t rawY, int32_t displayHeight);
4835
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836};
4837
4838const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4839const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4840const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4841const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4842const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4843const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4844const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4845const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004846const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4847const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4849const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4850const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4851const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4852const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4853const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4854const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4855const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4856const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4857const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4858const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4859const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004860const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4861 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4862const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4863 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004864const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4865 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866
4867const float TouchInputMapperTest::GEOMETRIC_SCALE =
4868 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4869 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4870
4871const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4872 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4873 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4874};
4875
Michael Wrighta9cf4192022-12-01 23:46:39 +00004876void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4878 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004879}
4880
4881void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4882 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004883 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
Michael Wrighta9cf4192022-12-01 23:46:39 +00004886void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004887 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4888 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4889 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004890}
4891
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004893 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4894 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
Jason Gerecke489fda82012-09-07 17:19:40 -07004899void TouchInputMapperTest::prepareLocationCalibration() {
4900 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4901}
4902
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903int32_t TouchInputMapperTest::toRawX(float displayX) {
4904 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4905}
4906
4907int32_t TouchInputMapperTest::toRawY(float displayY) {
4908 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4909}
4910
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004911int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4912 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4913}
4914
4915int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4916 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4917}
4918
Jason Gerecke489fda82012-09-07 17:19:40 -07004919float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4920 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4921 return rawX;
4922}
4923
4924float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4925 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4926 return rawY;
4927}
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 return toDisplayX(rawX, DISPLAY_WIDTH);
4931}
4932
4933float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4934 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004938 return toDisplayY(rawY, DISPLAY_HEIGHT);
4939}
4940
4941float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4942 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943}
4944
4945
4946// --- SingleTouchInputMapperTest ---
4947
4948class SingleTouchInputMapperTest : public TouchInputMapperTest {
4949protected:
4950 void prepareButtons();
4951 void prepareAxes(int axes);
4952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4954 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4955 void processUp(SingleTouchInputMapper& mappery);
4956 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4957 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4958 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4959 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4960 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4961 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962};
4963
4964void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004965 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968void SingleTouchInputMapperTest::prepareAxes(int axes) {
4969 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 }
4973 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4975 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 }
4977 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4979 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 }
4981 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4983 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 }
4985 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 }
4989}
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000}
5001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5011 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5020 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023}
5024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005025void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5026 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028}
5029
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005030void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032}
5033
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 prepareButtons();
5036 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
Josep del Río2d8c79a2023-01-23 19:33:50 +00005039 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 prepareButtons();
5044 prepareAxes(POSITION);
5045 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005046 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
5051TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005053 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 prepareButtons();
5055 prepareAxes(POSITION);
5056 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
5062 // Virtual key is down.
5063 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5064 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5065 processDown(mapper, x, y);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005069 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
5071 // Virtual key is up.
5072 processUp(mapper);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077}
5078
5079TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005081 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 prepareButtons();
5083 prepareAxes(POSITION);
5084 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089
5090 // Virtual key is down.
5091 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5092 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5093 processDown(mapper, x, y);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005097 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099 // Virtual key is up.
5100 processUp(mapper);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005104 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105}
5106
5107TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005109 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 prepareButtons();
5111 prepareAxes(POSITION);
5112 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005116 ASSERT_TRUE(
5117 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_TRUE(flags[0]);
5119 ASSERT_FALSE(flags[1]);
5120}
5121
5122TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
arthurhungdcef2dc2020-08-11 14:47:50 +08005130 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131
5132 NotifyKeyArgs args;
5133
5134 // Press virtual key.
5135 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5136 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5137 processDown(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5141 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5142 ASSERT_EQ(DEVICE_ID, args.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5147 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5148 ASSERT_EQ(KEY_HOME, args.scanCode);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5151
5152 // Release virtual key.
5153 processUp(mapper);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5158 ASSERT_EQ(DEVICE_ID, args.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5160 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5163 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5164 ASSERT_EQ(KEY_HOME, args.scanCode);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5167
5168 // Should not have sent any motions.
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5170}
5171
5172TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005174 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 prepareButtons();
5176 prepareAxes(POSITION);
5177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179
arthurhungdcef2dc2020-08-11 14:47:50 +08005180 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181
5182 NotifyKeyArgs keyArgs;
5183
5184 // Press virtual key.
5185 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5186 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5187 processDown(mapper, x, y);
5188 processSync(mapper);
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5191 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5192 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5194 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5197 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5198 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5200 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5201
5202 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5203 // into the display area.
5204 y -= 100;
5205 processMove(mapper, x, y);
5206 processSync(mapper);
5207
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5209 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5210 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5212 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5215 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5216 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5217 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5219 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5220
5221 NotifyMotionArgs motionArgs;
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5228 ASSERT_EQ(0, motionArgs.flags);
5229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5230 ASSERT_EQ(0, motionArgs.buttonState);
5231 ASSERT_EQ(0, motionArgs.edgeFlags);
5232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5236 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5240
5241 // Keep moving out of bounds. Should generate a pointer move.
5242 y -= 50;
5243 processMove(mapper, x, y);
5244 processSync(mapper);
5245
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5252 ASSERT_EQ(0, motionArgs.flags);
5253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5254 ASSERT_EQ(0, motionArgs.buttonState);
5255 ASSERT_EQ(0, motionArgs.edgeFlags);
5256 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5260 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5264
5265 // Release out of bounds. Should generate a pointer up.
5266 processUp(mapper);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5275 ASSERT_EQ(0, motionArgs.flags);
5276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5277 ASSERT_EQ(0, motionArgs.buttonState);
5278 ASSERT_EQ(0, motionArgs.edgeFlags);
5279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Should not have sent any more keys or motions.
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5291}
5292
5293TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005295 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 prepareButtons();
5297 prepareAxes(POSITION);
5298 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
arthurhungdcef2dc2020-08-11 14:47:50 +08005301 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 NotifyMotionArgs motionArgs;
5304
5305 // Initially go down out of bounds.
5306 int32_t x = -10;
5307 int32_t y = -10;
5308 processDown(mapper, x, y);
5309 processSync(mapper);
5310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5312
5313 // Move into the display area. Should generate a pointer down.
5314 x = 50;
5315 y = 75;
5316 processMove(mapper, x, y);
5317 processSync(mapper);
5318
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5325 ASSERT_EQ(0, motionArgs.flags);
5326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5327 ASSERT_EQ(0, motionArgs.buttonState);
5328 ASSERT_EQ(0, motionArgs.edgeFlags);
5329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5337
5338 // Release. Should generate a pointer up.
5339 processUp(mapper);
5340 processSync(mapper);
5341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5344 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5345 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5346 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5347 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5348 ASSERT_EQ(0, motionArgs.flags);
5349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5350 ASSERT_EQ(0, motionArgs.buttonState);
5351 ASSERT_EQ(0, motionArgs.edgeFlags);
5352 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5353 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5358 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5360
5361 // Should not have sent any more keys or motions.
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5364}
5365
Santos Cordonfa5cf462017-04-05 10:37:00 -07005366TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005367 addConfigurationProperty("touch.deviceType", "touchScreen");
5368 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5369
Michael Wrighta9cf4192022-12-01 23:46:39 +00005370 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005371 prepareButtons();
5372 prepareAxes(POSITION);
5373 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005375
arthurhungdcef2dc2020-08-11 14:47:50 +08005376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005377
5378 NotifyMotionArgs motionArgs;
5379
5380 // Down.
5381 int32_t x = 100;
5382 int32_t y = 125;
5383 processDown(mapper, x, y);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5389 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5393 ASSERT_EQ(0, motionArgs.flags);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5395 ASSERT_EQ(0, motionArgs.buttonState);
5396 ASSERT_EQ(0, motionArgs.edgeFlags);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5402 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // Move.
5408 x += 50;
5409 y += 75;
5410 processMove(mapper, x, y);
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5416 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5429 1, 0, 0, 0, 0, 0, 0, 0));
5430 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5431 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5433
5434 // Up.
5435 processUp(mapper);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5440 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5441 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5454 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5456 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5458
5459 // Should not have sent any more keys or motions.
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5462}
5463
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005466 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 prepareButtons();
5468 prepareAxes(POSITION);
5469 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005470 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471
arthurhungdcef2dc2020-08-11 14:47:50 +08005472 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 NotifyMotionArgs motionArgs;
5475
5476 // Down.
5477 int32_t x = 100;
5478 int32_t y = 125;
5479 processDown(mapper, x, y);
5480 processSync(mapper);
5481
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5500
5501 // Move.
5502 x += 50;
5503 y += 75;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5511 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5513 ASSERT_EQ(0, motionArgs.flags);
5514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5515 ASSERT_EQ(0, motionArgs.buttonState);
5516 ASSERT_EQ(0, motionArgs.edgeFlags);
5517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5521 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5525
5526 // Up.
5527 processUp(mapper);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.flags);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5538 ASSERT_EQ(0, motionArgs.buttonState);
5539 ASSERT_EQ(0, motionArgs.edgeFlags);
5540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5545 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5546 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5548
5549 // Should not have sent any more keys or motions.
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5552}
5553
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005554TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 addConfigurationProperty("touch.deviceType", "touchScreen");
5556 prepareButtons();
5557 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005558 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5559 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005560 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 NotifyMotionArgs args;
5563
5564 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 processDown(mapper, toRawX(50), toRawY(75));
5567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5571 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5572
5573 processUp(mapper);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5576}
5577
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005582 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5583 // orientation-aware are affected by display rotation.
5584 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 NotifyMotionArgs args;
5588
5589 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005590 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005591 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 processDown(mapper, toRawX(50), toRawY(75));
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5597 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5598
5599 processUp(mapper);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5602
5603 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005604 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005605 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005606 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5610 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5611 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5612
5613 processUp(mapper);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5616
5617 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005618 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005619 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5621 processSync(mapper);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5624 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5625 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5626
5627 processUp(mapper);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5630
5631 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005634 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5639 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5640
5641 processUp(mapper);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5644}
5645
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005646TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5647 addConfigurationProperty("touch.deviceType", "touchScreen");
5648 prepareButtons();
5649 prepareAxes(POSITION);
5650 addConfigurationProperty("touch.orientationAware", "1");
5651 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5652 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005653 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005654 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5655 NotifyMotionArgs args;
5656
5657 // Orientation 0.
5658 processDown(mapper, toRawX(50), toRawY(75));
5659 processSync(mapper);
5660
5661 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5662 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5663 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5664
5665 processUp(mapper);
5666 processSync(mapper);
5667 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5668}
5669
5670TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5671 addConfigurationProperty("touch.deviceType", "touchScreen");
5672 prepareButtons();
5673 prepareAxes(POSITION);
5674 addConfigurationProperty("touch.orientationAware", "1");
5675 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5676 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005677 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005678 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5679 NotifyMotionArgs args;
5680
5681 // Orientation 90.
5682 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5683 processSync(mapper);
5684
5685 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5686 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5687 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5688
5689 processUp(mapper);
5690 processSync(mapper);
5691 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5692}
5693
5694TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5695 addConfigurationProperty("touch.deviceType", "touchScreen");
5696 prepareButtons();
5697 prepareAxes(POSITION);
5698 addConfigurationProperty("touch.orientationAware", "1");
5699 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5700 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005701 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005702 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5703 NotifyMotionArgs args;
5704
5705 // Orientation 180.
5706 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5707 processSync(mapper);
5708
5709 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5710 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5711 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5712
5713 processUp(mapper);
5714 processSync(mapper);
5715 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5716}
5717
5718TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5719 addConfigurationProperty("touch.deviceType", "touchScreen");
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 addConfigurationProperty("touch.orientationAware", "1");
5723 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5724 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005725 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005726 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5727 NotifyMotionArgs args;
5728
5729 // Orientation 270.
5730 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5731 processSync(mapper);
5732
5733 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5734 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5735 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5736
5737 processUp(mapper);
5738 processSync(mapper);
5739 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5740}
5741
5742TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5743 addConfigurationProperty("touch.deviceType", "touchScreen");
5744 prepareButtons();
5745 prepareAxes(POSITION);
5746 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5747 // orientation-aware are affected by display rotation.
5748 addConfigurationProperty("touch.orientationAware", "0");
5749 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5750 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5751
5752 NotifyMotionArgs args;
5753
5754 // Orientation 90, Rotation 0.
5755 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005756 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005757 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5758 processSync(mapper);
5759
5760 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5761 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5762 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5763
5764 processUp(mapper);
5765 processSync(mapper);
5766 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5767
5768 // Orientation 90, Rotation 90.
5769 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005770 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005771 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005772 processSync(mapper);
5773
5774 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5775 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5776 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5777
5778 processUp(mapper);
5779 processSync(mapper);
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5781
5782 // Orientation 90, Rotation 180.
5783 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005784 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5786 processSync(mapper);
5787
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5789 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5790 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5791
5792 processUp(mapper);
5793 processSync(mapper);
5794 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5795
5796 // Orientation 90, Rotation 270.
5797 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005798 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005799 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005800 processSync(mapper);
5801
5802 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5803 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5804 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5805
5806 processUp(mapper);
5807 processSync(mapper);
5808 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5809}
5810
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005811TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5812 addConfigurationProperty("touch.deviceType", "touchScreen");
5813 prepareButtons();
5814 prepareAxes(POSITION);
5815 addConfigurationProperty("touch.orientationAware", "1");
5816 prepareDisplay(ui::ROTATION_0);
5817 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5818
5819 // Set a physical frame in the display viewport.
5820 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5821 viewport->physicalLeft = 20;
5822 viewport->physicalTop = 600;
5823 viewport->physicalRight = 30;
5824 viewport->physicalBottom = 610;
5825 mFakePolicy->updateViewport(*viewport);
5826 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5827
5828 // Start the touch.
5829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5830 processSync(mapper);
5831
5832 // Expect all input starting outside the physical frame to be ignored.
5833 const std::array<Point, 6> outsidePoints = {
5834 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5835 for (const auto& p : outsidePoints) {
5836 processMove(mapper, toRawX(p.x), toRawY(p.y));
5837 processSync(mapper);
5838 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5839 }
5840
5841 // Move the touch into the physical frame.
5842 processMove(mapper, toRawX(25), toRawY(605));
5843 processSync(mapper);
5844 NotifyMotionArgs args;
5845 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5846 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5847 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5848 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5849
5850 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5851 for (const auto& p : outsidePoints) {
5852 processMove(mapper, toRawX(p.x), toRawY(p.y));
5853 processSync(mapper);
5854 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5855 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5856 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5857 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5858 }
5859
5860 processUp(mapper);
5861 processSync(mapper);
5862 EXPECT_NO_FATAL_FAILURE(
5863 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5864}
5865
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005868 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 prepareButtons();
5870 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005871 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 // These calculations are based on the input device calibration documentation.
5874 int32_t rawX = 100;
5875 int32_t rawY = 200;
5876 int32_t rawPressure = 10;
5877 int32_t rawToolMajor = 12;
5878 int32_t rawDistance = 2;
5879 int32_t rawTiltX = 30;
5880 int32_t rawTiltY = 110;
5881
5882 float x = toDisplayX(rawX);
5883 float y = toDisplayY(rawY);
5884 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5885 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5886 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5887 float distance = float(rawDistance);
5888
5889 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5890 float tiltScale = M_PI / 180;
5891 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5892 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5893 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5894 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5895
5896 processDown(mapper, rawX, rawY);
5897 processPressure(mapper, rawPressure);
5898 processToolMajor(mapper, rawToolMajor);
5899 processDistance(mapper, rawDistance);
5900 processTilt(mapper, rawTiltX, rawTiltY);
5901 processSync(mapper);
5902
5903 NotifyMotionArgs args;
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5906 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5907 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5908}
5909
Jason Gerecke489fda82012-09-07 17:19:40 -07005910TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005912 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005913 prepareLocationCalibration();
5914 prepareButtons();
5915 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005916 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005917
5918 int32_t rawX = 100;
5919 int32_t rawY = 200;
5920
5921 float x = toDisplayX(toCookedX(rawX, rawY));
5922 float y = toDisplayY(toCookedY(rawX, rawY));
5923
5924 processDown(mapper, rawX, rawY);
5925 processSync(mapper);
5926
5927 NotifyMotionArgs args;
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5930 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5931}
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005935 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 prepareButtons();
5937 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005938 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939
5940 NotifyMotionArgs motionArgs;
5941 NotifyKeyArgs keyArgs;
5942
5943 processDown(mapper, 100, 200);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5947 ASSERT_EQ(0, motionArgs.buttonState);
5948
5949 // press BTN_LEFT, release BTN_LEFT
5950 processKey(mapper, BTN_LEFT, 1);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 processKey(mapper, BTN_LEFT, 0);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5971 processKey(mapper, BTN_RIGHT, 1);
5972 processKey(mapper, BTN_MIDDLE, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5977 motionArgs.buttonState);
5978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5981 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5985 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5986 motionArgs.buttonState);
5987
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 processKey(mapper, BTN_RIGHT, 0);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_MIDDLE, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // press BTN_BACK, release BTN_BACK
6009 processKey(mapper, BTN_BACK, 1);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6013 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 processKey(mapper, BTN_BACK, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031 ASSERT_EQ(0, motionArgs.buttonState);
6032
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6034 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6036
6037 // press BTN_SIDE, release BTN_SIDE
6038 processKey(mapper, BTN_SIDE, 1);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051
6052 processKey(mapper, BTN_SIDE, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
6061
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6065
6066 // press BTN_FORWARD, release BTN_FORWARD
6067 processKey(mapper, BTN_FORWARD, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6076
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 processKey(mapper, BTN_FORWARD, 0);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(0, motionArgs.buttonState);
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6093 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6094
6095 // press BTN_EXTRA, release BTN_EXTRA
6096 processKey(mapper, BTN_EXTRA, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6100 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109
6110 processKey(mapper, BTN_EXTRA, 0);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(0, motionArgs.buttonState);
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6122 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 // press BTN_STYLUS, release BTN_STYLUS
6127 processKey(mapper, BTN_STYLUS, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 processKey(mapper, BTN_STYLUS, 0);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // press BTN_STYLUS2, release BTN_STYLUS2
6148 processKey(mapper, BTN_STYLUS2, 1);
6149 processSync(mapper);
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6156 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
6158 processKey(mapper, BTN_STYLUS2, 0);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167
6168 // release touch
6169 processUp(mapper);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6173 ASSERT_EQ(0, motionArgs.buttonState);
6174}
6175
6176TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006178 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 prepareButtons();
6180 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
6183 NotifyMotionArgs motionArgs;
6184
6185 // default tool type is finger
6186 processDown(mapper, 100, 200);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6191
6192 // eraser
6193 processKey(mapper, BTN_TOOL_RUBBER, 1);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6198
6199 // stylus
6200 processKey(mapper, BTN_TOOL_RUBBER, 0);
6201 processKey(mapper, BTN_TOOL_PEN, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6206
6207 // brush
6208 processKey(mapper, BTN_TOOL_PEN, 0);
6209 processKey(mapper, BTN_TOOL_BRUSH, 1);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6214
6215 // pencil
6216 processKey(mapper, BTN_TOOL_BRUSH, 0);
6217 processKey(mapper, BTN_TOOL_PENCIL, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6222
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006223 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 processKey(mapper, BTN_TOOL_PENCIL, 0);
6225 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6230
6231 // mouse
6232 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6233 processKey(mapper, BTN_TOOL_MOUSE, 1);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6238
6239 // lens
6240 processKey(mapper, BTN_TOOL_MOUSE, 0);
6241 processKey(mapper, BTN_TOOL_LENS, 1);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6246
6247 // double-tap
6248 processKey(mapper, BTN_TOOL_LENS, 0);
6249 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6254
6255 // triple-tap
6256 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6257 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6262
6263 // quad-tap
6264 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6265 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6270
6271 // finger
6272 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6273 processKey(mapper, BTN_TOOL_FINGER, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6278
6279 // stylus trumps finger
6280 processKey(mapper, BTN_TOOL_PEN, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6285
6286 // eraser trumps stylus
6287 processKey(mapper, BTN_TOOL_RUBBER, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6292
6293 // mouse trumps eraser
6294 processKey(mapper, BTN_TOOL_MOUSE, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6299
6300 // back to default tool type
6301 processKey(mapper, BTN_TOOL_MOUSE, 0);
6302 processKey(mapper, BTN_TOOL_RUBBER, 0);
6303 processKey(mapper, BTN_TOOL_PEN, 0);
6304 processKey(mapper, BTN_TOOL_FINGER, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309}
6310
6311TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006313 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 prepareButtons();
6315 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 NotifyMotionArgs motionArgs;
6320
6321 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6322 processKey(mapper, BTN_TOOL_FINGER, 1);
6323 processMove(mapper, 100, 200);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6328 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6333 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6334
6335 // move a little
6336 processMove(mapper, 150, 250);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6342
6343 // down when BTN_TOUCH is pressed, pressure defaults to 1
6344 processKey(mapper, BTN_TOUCH, 1);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6355
6356 // up when BTN_TOUCH is released, hover restored
6357 processKey(mapper, BTN_TOUCH, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6373
6374 // exit hover when pointer goes away
6375 processKey(mapper, BTN_TOOL_FINGER, 0);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6381}
6382
6383TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006385 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 prepareButtons();
6387 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 NotifyMotionArgs motionArgs;
6391
6392 // initially hovering because pressure is 0
6393 processDown(mapper, 100, 200);
6394 processPressure(mapper, 0);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6399 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6404 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6405
6406 // move a little
6407 processMove(mapper, 150, 250);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6413
6414 // down when pressure is non-zero
6415 processPressure(mapper, RAW_PRESSURE_MAX);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // up when pressure becomes 0, hover restored
6428 processPressure(mapper, 0);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6444
6445 // exit hover when pointer goes away
6446 processUp(mapper);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6452}
6453
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006454TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006456 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006457 prepareButtons();
6458 prepareAxes(POSITION | PRESSURE);
6459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6460
6461 // Touch down.
6462 processDown(mapper, 100, 200);
6463 processPressure(mapper, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6466 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6467
6468 // Reset the mapper. This should cancel the ongoing gesture.
6469 resetMapper(mapper, ARBITRARY_TIME);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6471 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6474}
6475
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006476TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006478 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006479 prepareButtons();
6480 prepareAxes(POSITION | PRESSURE);
6481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482
6483 // Set the initial state for the touch pointer.
6484 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6485 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6486 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6487 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6488
6489 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006490 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6491 // does not generate any events.
6492 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006493
6494 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6495 // the recreated touch state to generate a down event.
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6501}
6502
lilinnan687e58f2022-07-19 16:00:50 +08006503TEST_F(SingleTouchInputMapperTest,
6504 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6505 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006506 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006507 prepareButtons();
6508 prepareAxes(POSITION);
6509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6510 NotifyMotionArgs motionArgs;
6511
6512 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006513 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006514 processSync(mapper);
6515
6516 // We should receive a down event
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6519
6520 // Change display id
6521 clearViewports();
6522 prepareSecondaryDisplay(ViewportType::INTERNAL);
6523
6524 // We should receive a cancel event
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6527 // Then receive reset called
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6529}
6530
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006531TEST_F(SingleTouchInputMapperTest,
6532 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006534 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006535 prepareButtons();
6536 prepareAxes(POSITION);
6537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6539 NotifyMotionArgs motionArgs;
6540
6541 // Start a new gesture.
6542 processDown(mapper, 100, 200);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6546
6547 // Make the viewport inactive. This will put the device in disabled mode.
6548 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6549 viewport->isActive = false;
6550 mFakePolicy->updateViewport(*viewport);
6551 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6552
6553 // We should receive a cancel event for the ongoing gesture.
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6556 // Then we should be notified that the device was reset.
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6558
6559 // No events are generated while the viewport is inactive.
6560 processMove(mapper, 101, 201);
6561 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006562 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6565
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006566 // Start a new gesture while the viewport is still inactive.
6567 processDown(mapper, 300, 400);
6568 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6569 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6570 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6571 processSync(mapper);
6572
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006573 // Make the viewport active again. The device should resume processing events.
6574 viewport->isActive = true;
6575 mFakePolicy->updateViewport(*viewport);
6576 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6577
6578 // The device is reset because it changes back to direct mode, without generating any events.
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6581
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006582 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006583 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6585 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006586
6587 // No more events.
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6590}
6591
Prabir Pradhan211ba622022-10-31 21:09:21 +00006592TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6593 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006594 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006595 prepareButtons();
6596 prepareAxes(POSITION);
6597 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6599
6600 // Press a stylus button.
6601 processKey(mapper, BTN_STYLUS, 1);
6602 processSync(mapper);
6603
6604 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6605 processDown(mapper, 100, 200);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6609 WithCoords(toDisplayX(100), toDisplayY(200)),
6610 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6613 WithCoords(toDisplayX(100), toDisplayY(200)),
6614 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6615
6616 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6617 // the button has not actually been released, since there will be no pointers through which the
6618 // button state can be reported. The event is generated at the location of the pointer before
6619 // it went up.
6620 processUp(mapper);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6624 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6627 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6628}
6629
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006630TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6631 addConfigurationProperty("touch.deviceType", "touchScreen");
6632 prepareDisplay(ui::ROTATION_0);
6633 prepareButtons();
6634 prepareAxes(POSITION);
6635
6636 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6637
6638 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6640
6641 // Press a stylus button.
6642 processKey(mapper, BTN_STYLUS, 1);
6643 processSync(mapper);
6644
6645 // Start a touch gesture and ensure that the stylus button is not reported.
6646 processDown(mapper, 100, 200);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6650
6651 // Release and press the stylus button again.
6652 processKey(mapper, BTN_STYLUS, 0);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6656 processKey(mapper, BTN_STYLUS, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6659 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6660
6661 // Release the touch gesture.
6662 processUp(mapper);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6668}
6669
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006670TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6671 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6672 prepareDisplay(ui::ROTATION_0);
6673 prepareButtons();
6674 prepareAxes(POSITION);
6675 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6677
6678 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6679}
6680
Seunghwan Choi356026c2023-02-01 14:37:25 +09006681TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6682 std::shared_ptr<FakePointerController> fakePointerController =
6683 std::make_shared<FakePointerController>();
6684 addConfigurationProperty("touch.deviceType", "touchScreen");
6685 prepareDisplay(ui::ROTATION_0);
6686 prepareButtons();
6687 prepareAxes(POSITION);
6688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6689 mFakePolicy->setPointerController(fakePointerController);
6690 mFakePolicy->setStylusPointerIconEnabled(true);
6691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6692
6693 processKey(mapper, BTN_TOOL_PEN, 1);
6694 processMove(mapper, 100, 200);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6698 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6699 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6700 ASSERT_TRUE(fakePointerController->isPointerShown());
6701 ASSERT_NO_FATAL_FAILURE(
6702 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6703}
6704
6705TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6706 std::shared_ptr<FakePointerController> fakePointerController =
6707 std::make_shared<FakePointerController>();
6708 addConfigurationProperty("touch.deviceType", "touchScreen");
6709 prepareDisplay(ui::ROTATION_0);
6710 prepareButtons();
6711 prepareAxes(POSITION);
6712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6713 mFakePolicy->setPointerController(fakePointerController);
6714 mFakePolicy->setStylusPointerIconEnabled(false);
6715 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6716
6717 processKey(mapper, BTN_TOOL_PEN, 1);
6718 processMove(mapper, 100, 200);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6721 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6722 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6723 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6724 ASSERT_FALSE(fakePointerController->isPointerShown());
6725}
6726
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006727TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6728 // Initialize the device without setting device source to touch navigation.
6729 addConfigurationProperty("touch.deviceType", "touchScreen");
6730 prepareDisplay(ui::ROTATION_0);
6731 prepareButtons();
6732 prepareAxes(POSITION);
6733 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6734
6735 // Ensure that the device is created as a touchscreen, not touch navigation.
6736 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6737
6738 // Add device type association after the device was created.
6739 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6740
6741 // Send update to the mapper.
6742 std::list<NotifyArgs> unused2 =
6743 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
6744 InputReaderConfiguration::CHANGE_DEVICE_TYPE /*changes*/);
6745
6746 // Check whether device type update was successful.
6747 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6748}
6749
Prabir Pradhan5632d622021-09-06 07:57:20 -07006750// --- TouchDisplayProjectionTest ---
6751
6752class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6753public:
6754 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6755 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6756 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006757 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6758 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6759 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006760 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006761 auto rotatedWidth = naturalDisplayWidth;
6762 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006763 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006764 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006765 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006766 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006767 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006768 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006769 inverseRotationFlags = ui::Transform::ROT_180;
6770 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006771 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006772 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006773 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006774 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006775 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006776 inverseRotationFlags = ui::Transform::ROT_0;
6777 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006778 }
6779
Prabir Pradhana9df3162022-12-05 23:57:27 +00006780 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006781 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6782
6783 std::optional<DisplayViewport> internalViewport =
6784 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6785 DisplayViewport& v = *internalViewport;
6786 v.displayId = DISPLAY_ID;
6787 v.orientation = orientation;
6788
6789 v.logicalLeft = 0;
6790 v.logicalTop = 0;
6791 v.logicalRight = 100;
6792 v.logicalBottom = 100;
6793
6794 v.physicalLeft = rotatedPhysicalDisplay.left;
6795 v.physicalTop = rotatedPhysicalDisplay.top;
6796 v.physicalRight = rotatedPhysicalDisplay.right;
6797 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6798
Prabir Pradhana9df3162022-12-05 23:57:27 +00006799 v.deviceWidth = rotatedWidth;
6800 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006801
6802 v.isActive = true;
6803 v.uniqueId = UNIQUE_ID;
6804 v.type = ViewportType::INTERNAL;
6805 mFakePolicy->updateViewport(v);
6806 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6807 }
6808
6809 void assertReceivedMove(const Point& point) {
6810 NotifyMotionArgs motionArgs;
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6813 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6814 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6815 1, 0, 0, 0, 0, 0, 0, 0));
6816 }
6817};
6818
6819TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6820 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006821 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006822
6823 prepareButtons();
6824 prepareAxes(POSITION);
6825 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6826
6827 NotifyMotionArgs motionArgs;
6828
6829 // Configure the DisplayViewport such that the logical display maps to a subsection of
6830 // the display panel called the physical display. Here, the physical display is bounded by the
6831 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6832 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6833 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6834 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6835
Michael Wrighta9cf4192022-12-01 23:46:39 +00006836 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006837 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6838
6839 // Touches outside the physical display should be ignored, and should not generate any
6840 // events. Ensure touches at the following points that lie outside of the physical display
6841 // area do not generate any events.
6842 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6843 processDown(mapper, toRawX(point.x), toRawY(point.y));
6844 processSync(mapper);
6845 processUp(mapper);
6846 processSync(mapper);
6847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6848 << "Unexpected event generated for touch outside physical display at point: "
6849 << point.x << ", " << point.y;
6850 }
6851 }
6852}
6853
6854TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6855 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006856 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006857
6858 prepareButtons();
6859 prepareAxes(POSITION);
6860 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6861
6862 NotifyMotionArgs motionArgs;
6863
6864 // Configure the DisplayViewport such that the logical display maps to a subsection of
6865 // the display panel called the physical display. Here, the physical display is bounded by the
6866 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6867 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6868
Michael Wrighta9cf4192022-12-01 23:46:39 +00006869 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006870 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6871
6872 // Touches that start outside the physical display should be ignored until it enters the
6873 // physical display bounds, at which point it should generate a down event. Start a touch at
6874 // the point (5, 100), which is outside the physical display bounds.
6875 static const Point kOutsidePoint{5, 100};
6876 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6879
6880 // Move the touch into the physical display area. This should generate a pointer down.
6881 processMove(mapper, toRawX(11), toRawY(21));
6882 processSync(mapper);
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6884 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6885 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6886 ASSERT_NO_FATAL_FAILURE(
6887 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6888
6889 // Move the touch inside the physical display area. This should generate a pointer move.
6890 processMove(mapper, toRawX(69), toRawY(159));
6891 processSync(mapper);
6892 assertReceivedMove({69, 159});
6893
6894 // Move outside the physical display area. Since the pointer is already down, this should
6895 // now continue generating events.
6896 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6897 processSync(mapper);
6898 assertReceivedMove(kOutsidePoint);
6899
6900 // Release. This should generate a pointer up.
6901 processUp(mapper);
6902 processSync(mapper);
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6904 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6906 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6907
6908 // Ensure no more events were generated.
6909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6911 }
6912}
6913
Prabir Pradhana9df3162022-12-05 23:57:27 +00006914// --- TouchscreenPrecisionTests ---
6915
6916// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6917// in various orientations and with different display rotations. We configure the touchscreen to
6918// have a higher resolution than that of the display by an integer scale factor in each axis so that
6919// we can enforce that coordinates match precisely as expected.
6920class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6921 public ::testing::WithParamInterface<ui::Rotation> {
6922public:
6923 void SetUp() override {
6924 SingleTouchInputMapperTest::SetUp();
6925
6926 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6927 // four times the resolution of the display in the Y axis.
6928 prepareButtons();
6929 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
6930 0, 0);
6931 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
6932 0, 0);
6933 }
6934
6935 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6936 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6937 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6938 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6939
6940 static const std::array<Point, 4> kRawCorners;
6941};
6942
6943const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6944 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6945 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6946 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6947 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6948}};
6949
6950// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6951// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6952// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6953TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6954 enum class Orientation {
6955 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6956 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6957 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6958 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6959 ftl_last = ORIENTATION_270,
6960 };
6961 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6962 Orientation::ORIENTATION_270;
6963 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6964 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6965 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6966 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6967 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6968 };
6969
6970 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6971
6972 // Configure the touchscreen as being installed in the one of the four different orientations
6973 // relative to the display.
6974 addConfigurationProperty("touch.deviceType", "touchScreen");
6975 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6976 prepareDisplay(ui::ROTATION_0);
6977
6978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6979
6980 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6981 // orientations of either 90 or 270) this means the display's natural resolution will be
6982 // flipped.
6983 const bool displayRotated =
6984 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6985 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6986 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6987 const Rect physicalFrame{0, 0, width, height};
6988 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6989
6990 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6991 const float expectedPrecisionX = displayRotated ? 4 : 2;
6992 const float expectedPrecisionY = displayRotated ? 2 : 4;
6993
6994 // Test all four corners.
6995 for (int i = 0; i < 4; i++) {
6996 const auto& raw = kRawCorners[i];
6997 processDown(mapper, raw.x, raw.y);
6998 processSync(mapper);
6999 const auto& expected = expectedPoints[i];
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7001 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7002 WithCoords(expected.x, expected.y),
7003 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7004 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7005 << "with touchscreen orientation "
7006 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7007 << expected.x << ", " << expected.y << ").";
7008 processUp(mapper);
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7012 WithCoords(expected.x, expected.y))));
7013 }
7014}
7015
Prabir Pradhan82687402022-12-06 01:32:53 +00007016TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7017 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7018 kMappedCorners = {
7019 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7020 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7021 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7022 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7023 };
7024
7025 const ui::Rotation displayRotation = GetParam();
7026
7027 addConfigurationProperty("touch.deviceType", "touchScreen");
7028 prepareDisplay(displayRotation);
7029
7030 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7031
7032 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7033
7034 // Test all four corners.
7035 for (int i = 0; i < 4; i++) {
7036 const auto& expected = expectedPoints[i];
7037 const auto& raw = kRawCorners[i];
7038 processDown(mapper, raw.x, raw.y);
7039 processSync(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7042 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7043 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7044 << "with display rotation " << ui::toCString(displayRotation)
7045 << ", expected point (" << expected.x << ", " << expected.y << ").";
7046 processUp(mapper);
7047 processSync(mapper);
7048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7049 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7050 WithCoords(expected.x, expected.y))));
7051 }
7052}
7053
Prabir Pradhan3e798762022-12-02 21:02:11 +00007054TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7055 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7056 kMappedCorners = {
7057 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7058 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7059 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7060 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7061 };
7062
7063 const ui::Rotation displayRotation = GetParam();
7064
7065 addConfigurationProperty("touch.deviceType", "touchScreen");
7066 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7067
7068 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7069
7070 // Ori 270, so width and height swapped
7071 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7072 prepareDisplay(displayRotation);
7073 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7074
7075 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7076
7077 // Test all four corners.
7078 for (int i = 0; i < 4; i++) {
7079 const auto& expected = expectedPoints[i];
7080 const auto& raw = kRawCorners[i];
7081 processDown(mapper, raw.x, raw.y);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7085 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7086 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7087 << "with display rotation " << ui::toCString(displayRotation)
7088 << ", expected point (" << expected.x << ", " << expected.y << ").";
7089 processUp(mapper);
7090 processSync(mapper);
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7092 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7093 WithCoords(expected.x, expected.y))));
7094 }
7095}
7096
Prabir Pradhana9df3162022-12-05 23:57:27 +00007097// Run the precision tests for all rotations.
7098INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7099 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7100 ui::ROTATION_270),
7101 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7102 return ftl::enum_string(testParamInfo.param);
7103 });
7104
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007105// --- ExternalStylusFusionTest ---
7106
7107class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7108public:
7109 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7110 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007111 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007112 prepareButtons();
7113 prepareAxes(POSITION);
7114 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7115
7116 mStylusState.when = ARBITRARY_TIME;
7117 mStylusState.pressure = 0.f;
7118 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7119 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7120 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7121 processExternalStylusState(mapper);
7122 return mapper;
7123 }
7124
7125 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7126 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7127 for (const NotifyArgs& args : generatedArgs) {
7128 mFakeListener->notify(args);
7129 }
7130 // Loop the reader to flush the input listener queue.
7131 mReader->loopOnce();
7132 return generatedArgs;
7133 }
7134
7135protected:
7136 StylusState mStylusState{};
7137 static constexpr uint32_t EXPECTED_SOURCE =
7138 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7139
7140 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7141 auto toolTypeSource =
7142 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7143
7144 // The first pointer is withheld.
7145 processDown(mapper, 100, 200);
7146 processSync(mapper);
7147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7148 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7149 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7150
7151 // The external stylus reports pressure. The withheld finger pointer is released as a
7152 // stylus.
7153 mStylusState.pressure = 1.f;
7154 processExternalStylusState(mapper);
7155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7156 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7157 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7158
7159 // Subsequent pointer events are not withheld.
7160 processMove(mapper, 101, 201);
7161 processSync(mapper);
7162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7163 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7164
7165 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7167 }
7168
7169 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7170 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7171
7172 // Releasing the touch pointer ends the gesture.
7173 processUp(mapper);
7174 processSync(mapper);
7175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7177 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7178
7179 mStylusState.pressure = 0.f;
7180 processExternalStylusState(mapper);
7181 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7183 }
7184
7185 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7186 auto toolTypeSource =
7187 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7188
7189 // The first pointer is withheld when an external stylus is connected,
7190 // and a timeout is requested.
7191 processDown(mapper, 100, 200);
7192 processSync(mapper);
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7194 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7195 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7196
7197 // If the timeout expires early, it is requested again.
7198 handleTimeout(mapper, ARBITRARY_TIME + 1);
7199 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7200 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7201
7202 // When the timeout expires, the withheld touch is released as a finger pointer.
7203 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7205 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7206
7207 // Subsequent pointer events are not withheld.
7208 processMove(mapper, 101, 201);
7209 processSync(mapper);
7210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7211 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7212 processUp(mapper);
7213 processSync(mapper);
7214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7215 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7216
7217 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7219 }
7220
7221private:
7222 InputDeviceInfo mExternalStylusDeviceInfo{};
7223};
7224
7225TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7226 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7227 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7228}
7229
7230TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7231 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7232 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7233}
7234
7235TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7236 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7237 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7238}
7239
7240// Test a successful stylus fusion gesture where the pressure is reported by the external
7241// before the touch is reported by the touchscreen.
7242TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7243 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7244 auto toolTypeSource =
7245 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7246
7247 // The external stylus reports pressure first. It is ignored for now.
7248 mStylusState.pressure = 1.f;
7249 processExternalStylusState(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7251 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7252
7253 // When the touch goes down afterwards, it is reported as a stylus pointer.
7254 processDown(mapper, 100, 200);
7255 processSync(mapper);
7256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7257 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7258 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7259
7260 processMove(mapper, 101, 201);
7261 processSync(mapper);
7262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7263 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7264 processUp(mapper);
7265 processSync(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7267 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7268
7269 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7271}
7272
7273TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7274 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7275
7276 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7277 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7278
7279 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7280 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7281 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7282 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7283}
7284
7285TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7286 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7287 auto toolTypeSource =
7288 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7289
7290 mStylusState.pressure = 0.8f;
7291 processExternalStylusState(mapper);
7292 processDown(mapper, 100, 200);
7293 processSync(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7295 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7296 WithPressure(0.8f))));
7297 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7298
7299 // The external stylus reports a pressure change. We wait for some time for a touch event.
7300 mStylusState.pressure = 0.6f;
7301 processExternalStylusState(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7303 ASSERT_NO_FATAL_FAILURE(
7304 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7305
7306 // If a touch is reported within the timeout, it reports the updated pressure.
7307 processMove(mapper, 101, 201);
7308 processSync(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7310 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7311 WithPressure(0.6f))));
7312 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7313
7314 // There is another pressure change.
7315 mStylusState.pressure = 0.5f;
7316 processExternalStylusState(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7318 ASSERT_NO_FATAL_FAILURE(
7319 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7320
7321 // If a touch is not reported within the timeout, a move event is generated to report
7322 // the new pressure.
7323 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7325 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7326 WithPressure(0.5f))));
7327
7328 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7329 // repeated indefinitely.
7330 mStylusState.pressure = 0.0f;
7331 processExternalStylusState(mapper);
7332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7333 ASSERT_NO_FATAL_FAILURE(
7334 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7335 processMove(mapper, 102, 202);
7336 processSync(mapper);
7337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7338 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7339 WithPressure(0.5f))));
7340 processMove(mapper, 103, 203);
7341 processSync(mapper);
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7343 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7344 WithPressure(0.5f))));
7345
7346 processUp(mapper);
7347 processSync(mapper);
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7349 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7350 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7351
7352 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7354}
7355
7356TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7357 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7358 auto source = WithSource(EXPECTED_SOURCE);
7359
7360 mStylusState.pressure = 1.f;
7361 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7362 processExternalStylusState(mapper);
7363 processDown(mapper, 100, 200);
7364 processSync(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7366 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7367 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7368 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7369
7370 // The external stylus reports a tool change. We wait for some time for a touch event.
7371 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7372 processExternalStylusState(mapper);
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7374 ASSERT_NO_FATAL_FAILURE(
7375 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7376
7377 // If a touch is reported within the timeout, it reports the updated pressure.
7378 processMove(mapper, 101, 201);
7379 processSync(mapper);
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7381 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7382 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7383 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7384
7385 // There is another tool type change.
7386 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7387 processExternalStylusState(mapper);
7388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7389 ASSERT_NO_FATAL_FAILURE(
7390 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7391
7392 // If a touch is not reported within the timeout, a move event is generated to report
7393 // the new tool type.
7394 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7396 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7397 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7398
7399 processUp(mapper);
7400 processSync(mapper);
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7402 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7403 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7404
7405 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7407}
7408
7409TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7410 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7411 auto toolTypeSource =
7412 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7413
7414 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7415
7416 // The external stylus reports a button change. We wait for some time for a touch event.
7417 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7418 processExternalStylusState(mapper);
7419 ASSERT_NO_FATAL_FAILURE(
7420 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7421
7422 // If a touch is reported within the timeout, it reports the updated button state.
7423 processMove(mapper, 101, 201);
7424 processSync(mapper);
7425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7426 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7427 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7429 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7430 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7431 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7432
7433 // The button is now released.
7434 mStylusState.buttons = 0;
7435 processExternalStylusState(mapper);
7436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7437 ASSERT_NO_FATAL_FAILURE(
7438 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7439
7440 // If a touch is not reported within the timeout, a move event is generated to report
7441 // the new button state.
7442 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7444 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7445 WithButtonState(0))));
7446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007447 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7448 WithButtonState(0))));
7449
7450 processUp(mapper);
7451 processSync(mapper);
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007453 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7454
7455 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7457}
7458
Michael Wrightd02c5b62014-02-10 15:10:22 -08007459// --- MultiTouchInputMapperTest ---
7460
7461class MultiTouchInputMapperTest : public TouchInputMapperTest {
7462protected:
7463 void prepareAxes(int axes);
7464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007465 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7466 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7467 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7468 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7469 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7470 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7471 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7472 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7473 void processId(MultiTouchInputMapper& mapper, int32_t id);
7474 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7475 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7476 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007477 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007478 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007479 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7480 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481};
7482
7483void MultiTouchInputMapperTest::prepareAxes(int axes) {
7484 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007485 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007487 }
7488 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007489 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7490 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007491 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007492 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7493 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007494 }
7495 }
7496 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007497 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7498 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007499 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007501 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502 }
7503 }
7504 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007505 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7506 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007507 }
7508 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7510 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007511 }
7512 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007513 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7514 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 }
7516 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007517 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7518 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 }
7520 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7522 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523 }
7524 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007525 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526 }
7527}
7528
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007529void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7530 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533}
7534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007535void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7536 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007537 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538}
7539
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007540void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7541 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543}
7544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007545void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547}
7548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007549void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551}
7552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007553void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7554 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556}
7557
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007558void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007560}
7561
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007562void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564}
7565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007566void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007568}
7569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007570void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572}
7573
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007574void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007575 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576}
7577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007578void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7579 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581}
7582
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007583void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7584 int32_t value) {
7585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7587}
7588
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007589void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591}
7592
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007593void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7594 nsecs_t readTime) {
7595 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596}
7597
Michael Wrightd02c5b62014-02-10 15:10:22 -08007598TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007599 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007600 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007601 prepareAxes(POSITION);
7602 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007603 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007604
arthurhungdcef2dc2020-08-11 14:47:50 +08007605 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606
7607 NotifyMotionArgs motionArgs;
7608
7609 // Two fingers down at once.
7610 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7611 processPosition(mapper, x1, y1);
7612 processMTSync(mapper);
7613 processPosition(mapper, x2, y2);
7614 processMTSync(mapper);
7615 processSync(mapper);
7616
7617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7618 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7619 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7620 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7621 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7623 ASSERT_EQ(0, motionArgs.flags);
7624 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7625 ASSERT_EQ(0, motionArgs.buttonState);
7626 ASSERT_EQ(0, motionArgs.edgeFlags);
7627 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7628 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7631 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7632 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7635
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7637 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7638 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7639 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7640 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007641 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 ASSERT_EQ(0, motionArgs.flags);
7643 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7644 ASSERT_EQ(0, motionArgs.buttonState);
7645 ASSERT_EQ(0, motionArgs.edgeFlags);
7646 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7647 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7648 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7649 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7650 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7651 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7652 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7654 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7655 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7656 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7657 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7658
7659 // Move.
7660 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7661 processPosition(mapper, x1, y1);
7662 processMTSync(mapper);
7663 processPosition(mapper, x2, y2);
7664 processMTSync(mapper);
7665 processSync(mapper);
7666
7667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7668 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7669 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7670 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7671 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7673 ASSERT_EQ(0, motionArgs.flags);
7674 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7675 ASSERT_EQ(0, motionArgs.buttonState);
7676 ASSERT_EQ(0, motionArgs.edgeFlags);
7677 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7678 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7680 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7681 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7682 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7683 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7684 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7685 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7686 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7687 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7689
7690 // First finger up.
7691 x2 += 15; y2 -= 20;
7692 processPosition(mapper, x2, y2);
7693 processMTSync(mapper);
7694 processSync(mapper);
7695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7698 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7699 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7700 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007701 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007702 ASSERT_EQ(0, motionArgs.flags);
7703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7704 ASSERT_EQ(0, motionArgs.buttonState);
7705 ASSERT_EQ(0, motionArgs.edgeFlags);
7706 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7712 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7714 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7715 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7716 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7718
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7721 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7725 ASSERT_EQ(0, motionArgs.flags);
7726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7727 ASSERT_EQ(0, motionArgs.buttonState);
7728 ASSERT_EQ(0, motionArgs.edgeFlags);
7729 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7730 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7731 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7733 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7734 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7735 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7737
7738 // Move.
7739 x2 += 20; y2 -= 25;
7740 processPosition(mapper, x2, y2);
7741 processMTSync(mapper);
7742 processSync(mapper);
7743
7744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7745 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7746 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7747 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7748 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7750 ASSERT_EQ(0, motionArgs.flags);
7751 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7752 ASSERT_EQ(0, motionArgs.buttonState);
7753 ASSERT_EQ(0, motionArgs.edgeFlags);
7754 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7755 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7758 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7759 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7760 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7761 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7762
7763 // New finger down.
7764 int32_t x3 = 700, y3 = 300;
7765 processPosition(mapper, x2, y2);
7766 processMTSync(mapper);
7767 processPosition(mapper, x3, y3);
7768 processMTSync(mapper);
7769 processSync(mapper);
7770
7771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7772 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7773 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7774 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7775 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007776 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 ASSERT_EQ(0, motionArgs.flags);
7778 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7779 ASSERT_EQ(0, motionArgs.buttonState);
7780 ASSERT_EQ(0, motionArgs.edgeFlags);
7781 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7782 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7784 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7785 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7787 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7789 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7790 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7791 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7793
7794 // Second finger up.
7795 x3 += 30; y3 -= 20;
7796 processPosition(mapper, x3, y3);
7797 processMTSync(mapper);
7798 processSync(mapper);
7799
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7802 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7803 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7804 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007805 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007806 ASSERT_EQ(0, motionArgs.flags);
7807 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7808 ASSERT_EQ(0, motionArgs.buttonState);
7809 ASSERT_EQ(0, motionArgs.edgeFlags);
7810 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7813 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7814 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7815 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7816 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7817 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7818 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7819 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7820 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7821 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7829 ASSERT_EQ(0, motionArgs.flags);
7830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7831 ASSERT_EQ(0, motionArgs.buttonState);
7832 ASSERT_EQ(0, motionArgs.edgeFlags);
7833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7837 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7841
7842 // Last finger up.
7843 processMTSync(mapper);
7844 processSync(mapper);
7845
7846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7847 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7848 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7849 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7850 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7852 ASSERT_EQ(0, motionArgs.flags);
7853 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7854 ASSERT_EQ(0, motionArgs.buttonState);
7855 ASSERT_EQ(0, motionArgs.edgeFlags);
7856 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7857 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7858 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7859 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7860 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7861 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7862 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7863 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7864
7865 // Should not have sent any more keys or motions.
7866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7868}
7869
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007870TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7871 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007872 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007873
7874 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7875 /*fuzz*/ 0, /*resolution*/ 10);
7876 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7877 /*fuzz*/ 0, /*resolution*/ 11);
7878 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7879 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7880 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7881 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7882 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7883 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7884 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7885 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7886
7887 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7888
7889 // X and Y axes
7890 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7891 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7892 // Touch major and minor
7893 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7894 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7895 // Tool major and minor
7896 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7897 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7898}
7899
7900TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7901 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007902 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007903
7904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7905 /*fuzz*/ 0, /*resolution*/ 10);
7906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7907 /*fuzz*/ 0, /*resolution*/ 11);
7908
7909 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7910
7911 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7912
7913 // Touch major and minor
7914 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7915 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7916 // Tool major and minor
7917 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7918 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7919}
7920
Michael Wrightd02c5b62014-02-10 15:10:22 -08007921TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007922 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007923 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007924 prepareAxes(POSITION | ID);
7925 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007926 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927
arthurhungdcef2dc2020-08-11 14:47:50 +08007928 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929
7930 NotifyMotionArgs motionArgs;
7931
7932 // Two fingers down at once.
7933 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7934 processPosition(mapper, x1, y1);
7935 processId(mapper, 1);
7936 processMTSync(mapper);
7937 processPosition(mapper, x2, y2);
7938 processId(mapper, 2);
7939 processMTSync(mapper);
7940 processSync(mapper);
7941
7942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7943 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7944 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7945 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7946 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7947 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7948 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7949
7950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007951 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7953 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7954 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7955 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7958 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7959 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7960 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7961
7962 // Move.
7963 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7964 processPosition(mapper, x1, y1);
7965 processId(mapper, 1);
7966 processMTSync(mapper);
7967 processPosition(mapper, x2, y2);
7968 processId(mapper, 2);
7969 processMTSync(mapper);
7970 processSync(mapper);
7971
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7973 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7974 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7975 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7977 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7978 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7980 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7982 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7983
7984 // First finger up.
7985 x2 += 15; y2 -= 20;
7986 processPosition(mapper, x2, y2);
7987 processId(mapper, 2);
7988 processMTSync(mapper);
7989 processSync(mapper);
7990
7991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007992 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007993 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7994 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7996 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7997 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7999 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8001 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8002
8003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8004 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8005 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8006 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8007 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8008 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8009 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8010
8011 // Move.
8012 x2 += 20; y2 -= 25;
8013 processPosition(mapper, x2, y2);
8014 processId(mapper, 2);
8015 processMTSync(mapper);
8016 processSync(mapper);
8017
8018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8020 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8021 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8022 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8023 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8024 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8025
8026 // New finger down.
8027 int32_t x3 = 700, y3 = 300;
8028 processPosition(mapper, x2, y2);
8029 processId(mapper, 2);
8030 processMTSync(mapper);
8031 processPosition(mapper, x3, y3);
8032 processId(mapper, 3);
8033 processMTSync(mapper);
8034 processSync(mapper);
8035
8036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008037 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8039 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8040 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8041 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8042 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8044 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8045 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8046 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8047
8048 // Second finger up.
8049 x3 += 30; y3 -= 20;
8050 processPosition(mapper, x3, y3);
8051 processId(mapper, 3);
8052 processMTSync(mapper);
8053 processSync(mapper);
8054
8055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008056 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8059 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8060 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8061 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8063 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8064 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8065 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8066
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8068 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8069 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8070 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8071 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8073 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8074
8075 // Last finger up.
8076 processMTSync(mapper);
8077 processSync(mapper);
8078
8079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8081 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8084 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8085 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8086
8087 // Should not have sent any more keys or motions.
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8090}
8091
8092TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008094 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 prepareAxes(POSITION | ID | SLOT);
8096 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008097 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008098
arthurhungdcef2dc2020-08-11 14:47:50 +08008099 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 NotifyMotionArgs motionArgs;
8102
8103 // Two fingers down at once.
8104 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8105 processPosition(mapper, x1, y1);
8106 processId(mapper, 1);
8107 processSlot(mapper, 1);
8108 processPosition(mapper, x2, y2);
8109 processId(mapper, 2);
8110 processSync(mapper);
8111
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8114 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8115 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8116 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8117 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8118 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8119
8120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008121 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8123 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8124 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8125 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8126 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8128 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8130 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8131
8132 // Move.
8133 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8134 processSlot(mapper, 0);
8135 processPosition(mapper, x1, y1);
8136 processSlot(mapper, 1);
8137 processPosition(mapper, x2, y2);
8138 processSync(mapper);
8139
8140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8141 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8142 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8143 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8144 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8145 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8146 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8147 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8148 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8149 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8150 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8151
8152 // First finger up.
8153 x2 += 15; y2 -= 20;
8154 processSlot(mapper, 0);
8155 processId(mapper, -1);
8156 processSlot(mapper, 1);
8157 processPosition(mapper, x2, y2);
8158 processSync(mapper);
8159
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008161 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8163 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8164 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8165 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8168 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8169 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8170 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8171
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8174 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8175 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8176 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8177 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8178 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8179
8180 // Move.
8181 x2 += 20; y2 -= 25;
8182 processPosition(mapper, x2, y2);
8183 processSync(mapper);
8184
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8187 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8188 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8189 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8190 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8191 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8192
8193 // New finger down.
8194 int32_t x3 = 700, y3 = 300;
8195 processPosition(mapper, x2, y2);
8196 processSlot(mapper, 0);
8197 processId(mapper, 3);
8198 processPosition(mapper, x3, y3);
8199 processSync(mapper);
8200
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008202 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8204 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8206 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8207 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8209 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8210 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8211 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8212
8213 // Second finger up.
8214 x3 += 30; y3 -= 20;
8215 processSlot(mapper, 1);
8216 processId(mapper, -1);
8217 processSlot(mapper, 0);
8218 processPosition(mapper, x3, y3);
8219 processSync(mapper);
8220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008222 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8224 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8225 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8226 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8227 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8228 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8229 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8231 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8232
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8235 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8236 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8238 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8239 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8240
8241 // Last finger up.
8242 processId(mapper, -1);
8243 processSync(mapper);
8244
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8246 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8247 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8248 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8249 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8251 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8252
8253 // Should not have sent any more keys or motions.
8254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8256}
8257
8258TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008260 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008263
8264 // These calculations are based on the input device calibration documentation.
8265 int32_t rawX = 100;
8266 int32_t rawY = 200;
8267 int32_t rawTouchMajor = 7;
8268 int32_t rawTouchMinor = 6;
8269 int32_t rawToolMajor = 9;
8270 int32_t rawToolMinor = 8;
8271 int32_t rawPressure = 11;
8272 int32_t rawDistance = 0;
8273 int32_t rawOrientation = 3;
8274 int32_t id = 5;
8275
8276 float x = toDisplayX(rawX);
8277 float y = toDisplayY(rawY);
8278 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8279 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8280 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8281 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8282 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8283 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8284 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8285 float distance = float(rawDistance);
8286
8287 processPosition(mapper, rawX, rawY);
8288 processTouchMajor(mapper, rawTouchMajor);
8289 processTouchMinor(mapper, rawTouchMinor);
8290 processToolMajor(mapper, rawToolMajor);
8291 processToolMinor(mapper, rawToolMinor);
8292 processPressure(mapper, rawPressure);
8293 processOrientation(mapper, rawOrientation);
8294 processDistance(mapper, rawDistance);
8295 processId(mapper, id);
8296 processMTSync(mapper);
8297 processSync(mapper);
8298
8299 NotifyMotionArgs args;
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8301 ASSERT_EQ(0, args.pointerProperties[0].id);
8302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8303 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8304 orientation, distance));
8305}
8306
8307TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008308 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008309 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8311 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313
8314 // These calculations are based on the input device calibration documentation.
8315 int32_t rawX = 100;
8316 int32_t rawY = 200;
8317 int32_t rawTouchMajor = 140;
8318 int32_t rawTouchMinor = 120;
8319 int32_t rawToolMajor = 180;
8320 int32_t rawToolMinor = 160;
8321
8322 float x = toDisplayX(rawX);
8323 float y = toDisplayY(rawY);
8324 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8325 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8326 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8327 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8328 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8329
8330 processPosition(mapper, rawX, rawY);
8331 processTouchMajor(mapper, rawTouchMajor);
8332 processTouchMinor(mapper, rawTouchMinor);
8333 processToolMajor(mapper, rawToolMajor);
8334 processToolMinor(mapper, rawToolMinor);
8335 processMTSync(mapper);
8336 processSync(mapper);
8337
8338 NotifyMotionArgs args;
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8341 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8342}
8343
8344TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008345 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008346 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347 prepareAxes(POSITION | TOUCH | TOOL);
8348 addConfigurationProperty("touch.size.calibration", "diameter");
8349 addConfigurationProperty("touch.size.scale", "10");
8350 addConfigurationProperty("touch.size.bias", "160");
8351 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008352 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008353
8354 // These calculations are based on the input device calibration documentation.
8355 // Note: We only provide a single common touch/tool value because the device is assumed
8356 // not to emit separate values for each pointer (isSummed = 1).
8357 int32_t rawX = 100;
8358 int32_t rawY = 200;
8359 int32_t rawX2 = 150;
8360 int32_t rawY2 = 250;
8361 int32_t rawTouchMajor = 5;
8362 int32_t rawToolMajor = 8;
8363
8364 float x = toDisplayX(rawX);
8365 float y = toDisplayY(rawY);
8366 float x2 = toDisplayX(rawX2);
8367 float y2 = toDisplayY(rawY2);
8368 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8369 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8370 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8371
8372 processPosition(mapper, rawX, rawY);
8373 processTouchMajor(mapper, rawTouchMajor);
8374 processToolMajor(mapper, rawToolMajor);
8375 processMTSync(mapper);
8376 processPosition(mapper, rawX2, rawY2);
8377 processTouchMajor(mapper, rawTouchMajor);
8378 processToolMajor(mapper, rawToolMajor);
8379 processMTSync(mapper);
8380 processSync(mapper);
8381
8382 NotifyMotionArgs args;
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8385
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008387 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388 ASSERT_EQ(size_t(2), args.pointerCount);
8389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8390 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8392 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8393}
8394
8395TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008397 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008398 prepareAxes(POSITION | TOUCH | TOOL);
8399 addConfigurationProperty("touch.size.calibration", "area");
8400 addConfigurationProperty("touch.size.scale", "43");
8401 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403
8404 // These calculations are based on the input device calibration documentation.
8405 int32_t rawX = 100;
8406 int32_t rawY = 200;
8407 int32_t rawTouchMajor = 5;
8408 int32_t rawToolMajor = 8;
8409
8410 float x = toDisplayX(rawX);
8411 float y = toDisplayY(rawY);
8412 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8413 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8414 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8415
8416 processPosition(mapper, rawX, rawY);
8417 processTouchMajor(mapper, rawTouchMajor);
8418 processToolMajor(mapper, rawToolMajor);
8419 processMTSync(mapper);
8420 processSync(mapper);
8421
8422 NotifyMotionArgs args;
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8425 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8426}
8427
8428TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008430 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008431 prepareAxes(POSITION | PRESSURE);
8432 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8433 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008434 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008435
Michael Wrightaa449c92017-12-13 21:21:43 +00008436 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008437 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008438 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8439 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8440 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8441
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442 // These calculations are based on the input device calibration documentation.
8443 int32_t rawX = 100;
8444 int32_t rawY = 200;
8445 int32_t rawPressure = 60;
8446
8447 float x = toDisplayX(rawX);
8448 float y = toDisplayY(rawY);
8449 float pressure = float(rawPressure) * 0.01f;
8450
8451 processPosition(mapper, rawX, rawY);
8452 processPressure(mapper, rawPressure);
8453 processMTSync(mapper);
8454 processSync(mapper);
8455
8456 NotifyMotionArgs args;
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8458 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8459 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8460}
8461
8462TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008464 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008466 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467
8468 NotifyMotionArgs motionArgs;
8469 NotifyKeyArgs keyArgs;
8470
8471 processId(mapper, 1);
8472 processPosition(mapper, 100, 200);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8476 ASSERT_EQ(0, motionArgs.buttonState);
8477
8478 // press BTN_LEFT, release BTN_LEFT
8479 processKey(mapper, BTN_LEFT, 1);
8480 processSync(mapper);
8481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8482 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8483 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8484
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8487 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8488
Michael Wrightd02c5b62014-02-10 15:10:22 -08008489 processKey(mapper, BTN_LEFT, 0);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008494
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008497 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498
8499 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8500 processKey(mapper, BTN_RIGHT, 1);
8501 processKey(mapper, BTN_MIDDLE, 1);
8502 processSync(mapper);
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8505 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8506 motionArgs.buttonState);
8507
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8510 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8511
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8515 motionArgs.buttonState);
8516
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517 processKey(mapper, BTN_RIGHT, 0);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008522
8523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008525 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526
8527 processKey(mapper, BTN_MIDDLE, 0);
8528 processSync(mapper);
8529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008532
8533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008535 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536
8537 // press BTN_BACK, release BTN_BACK
8538 processKey(mapper, BTN_BACK, 1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8541 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8542 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008543
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008546 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8547
8548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8549 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8550 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
8552 processKey(mapper, BTN_BACK, 0);
8553 processSync(mapper);
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008556 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008557
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008559 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008560 ASSERT_EQ(0, motionArgs.buttonState);
8561
Michael Wrightd02c5b62014-02-10 15:10:22 -08008562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8563 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8564 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8565
8566 // press BTN_SIDE, release BTN_SIDE
8567 processKey(mapper, BTN_SIDE, 1);
8568 processSync(mapper);
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8570 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8571 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008572
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008574 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008575 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8579 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580
8581 processKey(mapper, BTN_SIDE, 0);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008586
8587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008589 ASSERT_EQ(0, motionArgs.buttonState);
8590
Michael Wrightd02c5b62014-02-10 15:10:22 -08008591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8592 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8593 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8594
8595 // press BTN_FORWARD, release BTN_FORWARD
8596 processKey(mapper, BTN_FORWARD, 1);
8597 processSync(mapper);
8598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8599 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8600 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008601
Michael Wrightd02c5b62014-02-10 15:10:22 -08008602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008604 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8608 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609
8610 processKey(mapper, BTN_FORWARD, 0);
8611 processSync(mapper);
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008615
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008618 ASSERT_EQ(0, motionArgs.buttonState);
8619
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8621 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8622 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8623
8624 // press BTN_EXTRA, release BTN_EXTRA
8625 processKey(mapper, BTN_EXTRA, 1);
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8628 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8629 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008630
Michael Wrightd02c5b62014-02-10 15:10:22 -08008631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008633 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8634
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8637 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008638
8639 processKey(mapper, BTN_EXTRA, 0);
8640 processSync(mapper);
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008642 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008647 ASSERT_EQ(0, motionArgs.buttonState);
8648
Michael Wrightd02c5b62014-02-10 15:10:22 -08008649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8650 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8651 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8652
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8654
Michael Wrightd02c5b62014-02-10 15:10:22 -08008655 // press BTN_STYLUS, release BTN_STYLUS
8656 processKey(mapper, BTN_STYLUS, 1);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008660 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8661
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8663 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8664 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008665
8666 processKey(mapper, BTN_STYLUS, 0);
8667 processSync(mapper);
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008669 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008670 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008671
8672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008674 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675
8676 // press BTN_STYLUS2, release BTN_STYLUS2
8677 processKey(mapper, BTN_STYLUS2, 1);
8678 processSync(mapper);
8679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8680 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008681 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8682
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8685 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008686
8687 processKey(mapper, BTN_STYLUS2, 0);
8688 processSync(mapper);
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008690 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008691 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008692
8693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008695 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696
8697 // release touch
8698 processId(mapper, -1);
8699 processSync(mapper);
8700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8701 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8702 ASSERT_EQ(0, motionArgs.buttonState);
8703}
8704
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008705TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8706 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008707 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008708 prepareAxes(POSITION | ID | SLOT);
8709 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8710
8711 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8712 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8713
8714 // Touch down.
8715 processId(mapper, 1);
8716 processPosition(mapper, 100, 200);
8717 processSync(mapper);
8718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8719 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8720
8721 // Press and release button mapped to the primary stylus button.
8722 processKey(mapper, BTN_A, 1);
8723 processSync(mapper);
8724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8725 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8726 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8728 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8729 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8730
8731 processKey(mapper, BTN_A, 0);
8732 processSync(mapper);
8733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8734 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8737
8738 // Press and release the HID usage mapped to the secondary stylus button.
8739 processHidUsage(mapper, 0xabcd, 1);
8740 processSync(mapper);
8741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8742 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8743 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8745 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8746 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8747
8748 processHidUsage(mapper, 0xabcd, 0);
8749 processSync(mapper);
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8751 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8753 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8754
8755 // Release touch.
8756 processId(mapper, -1);
8757 processSync(mapper);
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8759 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8760}
8761
Michael Wrightd02c5b62014-02-10 15:10:22 -08008762TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008763 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008764 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008765 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008766 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008767
8768 NotifyMotionArgs motionArgs;
8769
8770 // default tool type is finger
8771 processId(mapper, 1);
8772 processPosition(mapper, 100, 200);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8777
8778 // eraser
8779 processKey(mapper, BTN_TOOL_RUBBER, 1);
8780 processSync(mapper);
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8783 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8784
8785 // stylus
8786 processKey(mapper, BTN_TOOL_RUBBER, 0);
8787 processKey(mapper, BTN_TOOL_PEN, 1);
8788 processSync(mapper);
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8790 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8792
8793 // brush
8794 processKey(mapper, BTN_TOOL_PEN, 0);
8795 processKey(mapper, BTN_TOOL_BRUSH, 1);
8796 processSync(mapper);
8797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8798 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8800
8801 // pencil
8802 processKey(mapper, BTN_TOOL_BRUSH, 0);
8803 processKey(mapper, BTN_TOOL_PENCIL, 1);
8804 processSync(mapper);
8805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8807 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8808
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008809 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 processKey(mapper, BTN_TOOL_PENCIL, 0);
8811 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8812 processSync(mapper);
8813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8815 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8816
8817 // mouse
8818 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8819 processKey(mapper, BTN_TOOL_MOUSE, 1);
8820 processSync(mapper);
8821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8823 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8824
8825 // lens
8826 processKey(mapper, BTN_TOOL_MOUSE, 0);
8827 processKey(mapper, BTN_TOOL_LENS, 1);
8828 processSync(mapper);
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8831 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8832
8833 // double-tap
8834 processKey(mapper, BTN_TOOL_LENS, 0);
8835 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8836 processSync(mapper);
8837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8839 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8840
8841 // triple-tap
8842 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8843 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8844 processSync(mapper);
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8848
8849 // quad-tap
8850 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8851 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8856
8857 // finger
8858 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8859 processKey(mapper, BTN_TOOL_FINGER, 1);
8860 processSync(mapper);
8861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8863 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8864
8865 // stylus trumps finger
8866 processKey(mapper, BTN_TOOL_PEN, 1);
8867 processSync(mapper);
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8870 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8871
8872 // eraser trumps stylus
8873 processKey(mapper, BTN_TOOL_RUBBER, 1);
8874 processSync(mapper);
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8878
8879 // mouse trumps eraser
8880 processKey(mapper, BTN_TOOL_MOUSE, 1);
8881 processSync(mapper);
8882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8885
8886 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8887 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8888 processSync(mapper);
8889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8890 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8891 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8892
8893 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8894 processToolType(mapper, MT_TOOL_PEN);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8898 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8899
8900 // back to default tool type
8901 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8902 processKey(mapper, BTN_TOOL_MOUSE, 0);
8903 processKey(mapper, BTN_TOOL_RUBBER, 0);
8904 processKey(mapper, BTN_TOOL_PEN, 0);
8905 processKey(mapper, BTN_TOOL_FINGER, 0);
8906 processSync(mapper);
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8909 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8910}
8911
8912TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008913 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008914 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008915 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008916 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918
8919 NotifyMotionArgs motionArgs;
8920
8921 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8922 processId(mapper, 1);
8923 processPosition(mapper, 100, 200);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8926 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8928 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8929
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8933 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8934
8935 // move a little
8936 processPosition(mapper, 150, 250);
8937 processSync(mapper);
8938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8941 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8942
8943 // down when BTN_TOUCH is pressed, pressure defaults to 1
8944 processKey(mapper, BTN_TOUCH, 1);
8945 processSync(mapper);
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8949 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8950
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8952 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8954 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8955
8956 // up when BTN_TOUCH is released, hover restored
8957 processKey(mapper, BTN_TOUCH, 0);
8958 processSync(mapper);
8959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8960 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8961 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8962 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8963
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8967 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8968
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8972 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8973
8974 // exit hover when pointer goes away
8975 processId(mapper, -1);
8976 processSync(mapper);
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8978 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8980 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8981}
8982
8983TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008985 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008986 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008987 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008988
8989 NotifyMotionArgs motionArgs;
8990
8991 // initially hovering because pressure is 0
8992 processId(mapper, 1);
8993 processPosition(mapper, 100, 200);
8994 processPressure(mapper, 0);
8995 processSync(mapper);
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8999 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9000
9001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9004 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9005
9006 // move a little
9007 processPosition(mapper, 150, 250);
9008 processSync(mapper);
9009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9010 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9011 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9012 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9013
9014 // down when pressure becomes non-zero
9015 processPressure(mapper, RAW_PRESSURE_MAX);
9016 processSync(mapper);
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9020 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9021
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9025 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9026
9027 // up when pressure becomes 0, hover restored
9028 processPressure(mapper, 0);
9029 processSync(mapper);
9030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9031 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9033 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9034
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9036 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9037 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9038 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9039
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9042 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9043 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9044
9045 // exit hover when pointer goes away
9046 processId(mapper, -1);
9047 processSync(mapper);
9048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9049 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9050 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9051 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9052}
9053
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009054/**
9055 * Set the input device port <--> display port associations, and check that the
9056 * events are routed to the display that matches the display port.
9057 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9058 */
9059TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009060 const std::string usb2 = "USB2";
9061 const uint8_t hdmi1 = 0;
9062 const uint8_t hdmi2 = 1;
9063 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009064 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009065
9066 addConfigurationProperty("touch.deviceType", "touchScreen");
9067 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009068 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009069
9070 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9071 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9072
9073 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9074 // for this input device is specified, and the matching viewport is not present,
9075 // the input device should be disabled (at the mapper level).
9076
9077 // Add viewport for display 2 on hdmi2
9078 prepareSecondaryDisplay(type, hdmi2);
9079 // Send a touch event
9080 processPosition(mapper, 100, 100);
9081 processSync(mapper);
9082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9083
9084 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009085 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009086 // Send a touch event again
9087 processPosition(mapper, 100, 100);
9088 processSync(mapper);
9089
9090 NotifyMotionArgs args;
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9092 ASSERT_EQ(DISPLAY_ID, args.displayId);
9093}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009095TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9096 addConfigurationProperty("touch.deviceType", "touchScreen");
9097 prepareAxes(POSITION);
9098 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9099
9100 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9101
Michael Wrighta9cf4192022-12-01 23:46:39 +00009102 prepareDisplay(ui::ROTATION_0);
9103 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009104
9105 // Send a touch event
9106 processPosition(mapper, 100, 100);
9107 processSync(mapper);
9108
9109 NotifyMotionArgs args;
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9111 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9112}
9113
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009114TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009115 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009116 std::shared_ptr<FakePointerController> fakePointerController =
9117 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009118 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009119 fakePointerController->setPosition(100, 200);
9120 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009121 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009122
Garfield Tan888a6a42020-01-09 11:39:16 -08009123 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009124 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009125
Michael Wrighta9cf4192022-12-01 23:46:39 +00009126 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009127 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009128 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009129
Josep del Río2d8c79a2023-01-23 19:33:50 +00009130 // Check source is mouse that would obtain the PointerController.
9131 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009132
9133 NotifyMotionArgs motionArgs;
9134 processPosition(mapper, 100, 100);
9135 processSync(mapper);
9136
9137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9138 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9139 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9140}
9141
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009142/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009143 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9144 */
9145TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9146 addConfigurationProperty("touch.deviceType", "touchScreen");
9147 prepareAxes(POSITION);
9148 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9149
Michael Wrighta9cf4192022-12-01 23:46:39 +00009150 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009151 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9152 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9153 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9154 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009155
9156 NotifyMotionArgs args;
9157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9158 ASSERT_EQ(26, args.readTime);
9159
Harry Cutts33476232023-01-30 19:57:29 +00009160 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9161 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9162 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009163
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9165 ASSERT_EQ(33, args.readTime);
9166}
9167
9168/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009169 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9170 * events should not be delivered to the listener.
9171 */
9172TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9173 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009174 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009175 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009176 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009177 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9178 prepareAxes(POSITION);
9179 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9180
9181 NotifyMotionArgs motionArgs;
9182 processPosition(mapper, 100, 100);
9183 processSync(mapper);
9184
9185 mFakeListener->assertNotifyMotionWasNotCalled();
9186}
9187
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009188/**
9189 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9190 * the touch mapper can process the events and the events can be delivered to the listener.
9191 */
9192TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9193 addConfigurationProperty("touch.deviceType", "touchScreen");
9194 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009195 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009196 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009197 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9198 prepareAxes(POSITION);
9199 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9200
9201 NotifyMotionArgs motionArgs;
9202 processPosition(mapper, 100, 100);
9203 processSync(mapper);
9204
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9207}
9208
Garfield Tanc734e4f2021-01-15 20:01:39 -08009209TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9210 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009211 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009212 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009213 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009214 std::optional<DisplayViewport> optionalDisplayViewport =
9215 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9216 ASSERT_TRUE(optionalDisplayViewport.has_value());
9217 DisplayViewport displayViewport = *optionalDisplayViewport;
9218
9219 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9220 prepareAxes(POSITION);
9221 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9222
9223 // Finger down
9224 int32_t x = 100, y = 100;
9225 processPosition(mapper, x, y);
9226 processSync(mapper);
9227
9228 NotifyMotionArgs motionArgs;
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9231
9232 // Deactivate display viewport
9233 displayViewport.isActive = false;
9234 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9235 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9236
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009237 // The ongoing touch should be canceled immediately
9238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9239 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9240
9241 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009242 x += 10, y += 10;
9243 processPosition(mapper, x, y);
9244 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009246
9247 // Reactivate display viewport
9248 displayViewport.isActive = true;
9249 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9250 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9251
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009252 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009253 x += 10, y += 10;
9254 processPosition(mapper, x, y);
9255 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9257 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009258}
9259
Arthur Hung7c645402019-01-25 17:45:42 +08009260TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9261 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009262 prepareAxes(POSITION | ID | SLOT);
9263 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009264 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009265
9266 // Create the second touch screen device, and enable multi fingers.
9267 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009268 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009269 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009270 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009271 std::shared_ptr<InputDevice> device2 =
9272 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009273 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009274
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009275 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009276 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009277 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009278 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009279 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009280 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009281 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009282 /*flat=*/0, /*fuzz=*/0);
9283 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009284 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9285 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009286
9287 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009288 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009289 std::list<NotifyArgs> unused =
9290 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009291 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009292 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009293
9294 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009295 std::shared_ptr<FakePointerController> fakePointerController =
9296 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009297 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009298
9299 // Setup policy for associated displays and show touches.
9300 const uint8_t hdmi1 = 0;
9301 const uint8_t hdmi2 = 1;
9302 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9303 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9304 mFakePolicy->setShowTouches(true);
9305
9306 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009307 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009308 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009309
9310 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009311 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9312 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9313 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009314
9315 // Two fingers down at default display.
9316 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9317 processPosition(mapper, x1, y1);
9318 processId(mapper, 1);
9319 processSlot(mapper, 1);
9320 processPosition(mapper, x2, y2);
9321 processId(mapper, 2);
9322 processSync(mapper);
9323
9324 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9325 fakePointerController->getSpots().find(DISPLAY_ID);
9326 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9327 ASSERT_EQ(size_t(2), iter->second.size());
9328
9329 // Two fingers down at second display.
9330 processPosition(mapper2, x1, y1);
9331 processId(mapper2, 1);
9332 processSlot(mapper2, 1);
9333 processPosition(mapper2, x2, y2);
9334 processId(mapper2, 2);
9335 processSync(mapper2);
9336
9337 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9338 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9339 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009340
9341 // Disable the show touches configuration and ensure the spots are cleared.
9342 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009343 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9344 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009345
9346 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009347}
9348
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009349TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009350 prepareAxes(POSITION);
9351 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009352 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009353 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009354
9355 NotifyMotionArgs motionArgs;
9356 // Unrotated video frame
9357 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9358 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009359 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009360 processPosition(mapper, 100, 200);
9361 processSync(mapper);
9362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9363 ASSERT_EQ(frames, motionArgs.videoFrames);
9364
9365 // Subsequent touch events should not have any videoframes
9366 // This is implemented separately in FakeEventHub,
9367 // but that should match the behaviour of TouchVideoDevice.
9368 processPosition(mapper, 200, 200);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9371 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9372}
9373
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009374TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009375 prepareAxes(POSITION);
9376 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009377 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009378 // Unrotated video frame
9379 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9380 NotifyMotionArgs motionArgs;
9381
9382 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009383 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009384 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9385 clearViewports();
9386 prepareDisplay(orientation);
9387 std::vector<TouchVideoFrame> frames{frame};
9388 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9389 processPosition(mapper, 100, 200);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(frames, motionArgs.videoFrames);
9393 }
9394}
9395
9396TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9397 prepareAxes(POSITION);
9398 addConfigurationProperty("touch.deviceType", "touchScreen");
9399 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9400 // orientation-aware are affected by display rotation.
9401 addConfigurationProperty("touch.orientationAware", "0");
9402 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9403 // Unrotated video frame
9404 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9405 NotifyMotionArgs motionArgs;
9406
9407 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009408 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009409 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9410 clearViewports();
9411 prepareDisplay(orientation);
9412 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009413 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009414 processPosition(mapper, 100, 200);
9415 processSync(mapper);
9416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009417 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9418 // compared to the display. This is so that when the window transform (which contains the
9419 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9420 // window's coordinate space.
9421 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009422 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009423
9424 // Release finger.
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009427 }
9428}
9429
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009430TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009431 prepareAxes(POSITION);
9432 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009433 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009434 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9435 // so mix these.
9436 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9437 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9438 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9439 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9440 NotifyMotionArgs motionArgs;
9441
Michael Wrighta9cf4192022-12-01 23:46:39 +00009442 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009443 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009444 processPosition(mapper, 100, 200);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009447 ASSERT_EQ(frames, motionArgs.videoFrames);
9448}
9449
9450TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9451 prepareAxes(POSITION);
9452 addConfigurationProperty("touch.deviceType", "touchScreen");
9453 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9454 // orientation-aware are affected by display rotation.
9455 addConfigurationProperty("touch.orientationAware", "0");
9456 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9457 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9458 // so mix these.
9459 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9460 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9461 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9462 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9463 NotifyMotionArgs motionArgs;
9464
Michael Wrighta9cf4192022-12-01 23:46:39 +00009465 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009466 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9467 processPosition(mapper, 100, 200);
9468 processSync(mapper);
9469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9470 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9471 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9472 // compared to the display. This is so that when the window transform (which contains the
9473 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9474 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009475 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009476 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009477 ASSERT_EQ(frames, motionArgs.videoFrames);
9478}
9479
Arthur Hung9da14732019-09-02 16:16:58 +08009480/**
9481 * If we had defined port associations, but the viewport is not ready, the touch device would be
9482 * expected to be disabled, and it should be enabled after the viewport has found.
9483 */
9484TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009485 constexpr uint8_t hdmi2 = 1;
9486 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009487 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009488
9489 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9490
9491 addConfigurationProperty("touch.deviceType", "touchScreen");
9492 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009494
9495 ASSERT_EQ(mDevice->isEnabled(), false);
9496
9497 // Add display on hdmi2, the device should be enabled and can receive touch event.
9498 prepareSecondaryDisplay(type, hdmi2);
9499 ASSERT_EQ(mDevice->isEnabled(), true);
9500
9501 // Send a touch event.
9502 processPosition(mapper, 100, 100);
9503 processSync(mapper);
9504
9505 NotifyMotionArgs args;
9506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9507 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9508}
9509
Arthur Hung421eb1c2020-01-16 00:09:42 +08009510TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009511 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009512 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009513 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009514 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009515
9516 NotifyMotionArgs motionArgs;
9517
9518 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9519 // finger down
9520 processId(mapper, 1);
9521 processPosition(mapper, x1, y1);
9522 processSync(mapper);
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9526
9527 // finger move
9528 processId(mapper, 1);
9529 processPosition(mapper, x2, y2);
9530 processSync(mapper);
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9532 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9533 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9534
9535 // finger up.
9536 processId(mapper, -1);
9537 processSync(mapper);
9538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9539 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9540 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9541
9542 // new finger down
9543 processId(mapper, 1);
9544 processPosition(mapper, x3, y3);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9547 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9548 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9549}
9550
9551/**
arthurhungcc7f9802020-04-30 17:55:40 +08009552 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9553 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009554 */
arthurhungcc7f9802020-04-30 17:55:40 +08009555TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009556 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009557 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009558 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009559 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009560
9561 NotifyMotionArgs motionArgs;
9562
9563 // default tool type is finger
9564 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009565 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009566 processPosition(mapper, x1, y1);
9567 processSync(mapper);
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9569 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9570 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9571
9572 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9573 processToolType(mapper, MT_TOOL_PALM);
9574 processSync(mapper);
9575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9576 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9577
9578 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009579 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009580 processPosition(mapper, x2, y2);
9581 processSync(mapper);
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9583
9584 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009585 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9588
9589 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009590 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009591 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009592 processPosition(mapper, x3, y3);
9593 processSync(mapper);
9594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9595 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9597}
9598
arthurhungbf89a482020-04-17 17:37:55 +08009599/**
arthurhungcc7f9802020-04-30 17:55:40 +08009600 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9601 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009602 */
arthurhungcc7f9802020-04-30 17:55:40 +08009603TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009604 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009605 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009606 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9607 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9608
9609 NotifyMotionArgs motionArgs;
9610
9611 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009612 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9613 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009614 processPosition(mapper, x1, y1);
9615 processSync(mapper);
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9618 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9619
9620 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009621 processSlot(mapper, SECOND_SLOT);
9622 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009623 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009626 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9628
9629 // If the tool type of the first finger changes to MT_TOOL_PALM,
9630 // we expect to receive ACTION_POINTER_UP with cancel flag.
9631 processSlot(mapper, FIRST_SLOT);
9632 processId(mapper, FIRST_TRACKING_ID);
9633 processToolType(mapper, MT_TOOL_PALM);
9634 processSync(mapper);
9635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009636 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009637 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9638
9639 // The following MOVE events of second finger should be processed.
9640 processSlot(mapper, SECOND_SLOT);
9641 processId(mapper, SECOND_TRACKING_ID);
9642 processPosition(mapper, x2 + 1, y2 + 1);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9645 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9646 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9647
9648 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9649 // it. Second finger receive move.
9650 processSlot(mapper, FIRST_SLOT);
9651 processId(mapper, INVALID_TRACKING_ID);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9655 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9656
9657 // Second finger keeps moving.
9658 processSlot(mapper, SECOND_SLOT);
9659 processId(mapper, SECOND_TRACKING_ID);
9660 processPosition(mapper, x2 + 2, y2 + 2);
9661 processSync(mapper);
9662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9663 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9664 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9665
9666 // Second finger up.
9667 processId(mapper, INVALID_TRACKING_ID);
9668 processSync(mapper);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9670 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9671 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9672}
9673
9674/**
9675 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9676 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9677 */
9678TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9679 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009680 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009681 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9682 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9683
9684 NotifyMotionArgs motionArgs;
9685
9686 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9687 // First finger down.
9688 processId(mapper, FIRST_TRACKING_ID);
9689 processPosition(mapper, x1, y1);
9690 processSync(mapper);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9693 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9694
9695 // Second finger down.
9696 processSlot(mapper, SECOND_SLOT);
9697 processId(mapper, SECOND_TRACKING_ID);
9698 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009699 processSync(mapper);
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009701 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009702 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9703
arthurhungcc7f9802020-04-30 17:55:40 +08009704 // If the tool type of the first finger changes to MT_TOOL_PALM,
9705 // we expect to receive ACTION_POINTER_UP with cancel flag.
9706 processSlot(mapper, FIRST_SLOT);
9707 processId(mapper, FIRST_TRACKING_ID);
9708 processToolType(mapper, MT_TOOL_PALM);
9709 processSync(mapper);
9710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009711 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009712 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9713
9714 // Second finger keeps moving.
9715 processSlot(mapper, SECOND_SLOT);
9716 processId(mapper, SECOND_TRACKING_ID);
9717 processPosition(mapper, x2 + 1, y2 + 1);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9721
9722 // second finger becomes palm, receive cancel due to only 1 finger is active.
9723 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009724 processToolType(mapper, MT_TOOL_PALM);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9728
arthurhungcc7f9802020-04-30 17:55:40 +08009729 // third finger down.
9730 processSlot(mapper, THIRD_SLOT);
9731 processId(mapper, THIRD_TRACKING_ID);
9732 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009733 processPosition(mapper, x3, y3);
9734 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9736 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9737 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009738 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9739
9740 // third finger move
9741 processId(mapper, THIRD_TRACKING_ID);
9742 processPosition(mapper, x3 + 1, y3 + 1);
9743 processSync(mapper);
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9746
9747 // first finger up, third finger receive move.
9748 processSlot(mapper, FIRST_SLOT);
9749 processId(mapper, INVALID_TRACKING_ID);
9750 processSync(mapper);
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9752 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9753 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9754
9755 // second finger up, third finger receive move.
9756 processSlot(mapper, SECOND_SLOT);
9757 processId(mapper, INVALID_TRACKING_ID);
9758 processSync(mapper);
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9760 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9761 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9762
9763 // third finger up.
9764 processSlot(mapper, THIRD_SLOT);
9765 processId(mapper, INVALID_TRACKING_ID);
9766 processSync(mapper);
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9768 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9769 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9770}
9771
9772/**
9773 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9774 * and the active finger could still be allowed to receive the events
9775 */
9776TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9777 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009778 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009779 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9780 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9781
9782 NotifyMotionArgs motionArgs;
9783
9784 // default tool type is finger
9785 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9786 processId(mapper, FIRST_TRACKING_ID);
9787 processPosition(mapper, x1, y1);
9788 processSync(mapper);
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9790 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9792
9793 // Second finger down.
9794 processSlot(mapper, SECOND_SLOT);
9795 processId(mapper, SECOND_TRACKING_ID);
9796 processPosition(mapper, x2, y2);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009799 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9801
9802 // If the tool type of the second finger changes to MT_TOOL_PALM,
9803 // we expect to receive ACTION_POINTER_UP with cancel flag.
9804 processId(mapper, SECOND_TRACKING_ID);
9805 processToolType(mapper, MT_TOOL_PALM);
9806 processSync(mapper);
9807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009808 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009809 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9810
9811 // The following MOVE event should be processed.
9812 processSlot(mapper, FIRST_SLOT);
9813 processId(mapper, FIRST_TRACKING_ID);
9814 processPosition(mapper, x1 + 1, y1 + 1);
9815 processSync(mapper);
9816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9818 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9819
9820 // second finger up.
9821 processSlot(mapper, SECOND_SLOT);
9822 processId(mapper, INVALID_TRACKING_ID);
9823 processSync(mapper);
9824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9825 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9826
9827 // first finger keep moving
9828 processSlot(mapper, FIRST_SLOT);
9829 processId(mapper, FIRST_TRACKING_ID);
9830 processPosition(mapper, x1 + 2, y1 + 2);
9831 processSync(mapper);
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9834
9835 // first finger up.
9836 processId(mapper, INVALID_TRACKING_ID);
9837 processSync(mapper);
9838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9839 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9840 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009841}
9842
Arthur Hung9ad18942021-06-19 02:04:46 +00009843/**
9844 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9845 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9846 * cause slot be valid again.
9847 */
9848TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9849 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009850 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009851 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9852 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9853
9854 NotifyMotionArgs motionArgs;
9855
9856 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9857 // First finger down.
9858 processId(mapper, FIRST_TRACKING_ID);
9859 processPosition(mapper, x1, y1);
9860 processPressure(mapper, RAW_PRESSURE_MAX);
9861 processSync(mapper);
9862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9863 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9864 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9865
9866 // First finger move.
9867 processId(mapper, FIRST_TRACKING_ID);
9868 processPosition(mapper, x1 + 1, y1 + 1);
9869 processPressure(mapper, RAW_PRESSURE_MAX);
9870 processSync(mapper);
9871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9872 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9873 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9874
9875 // Second finger down.
9876 processSlot(mapper, SECOND_SLOT);
9877 processId(mapper, SECOND_TRACKING_ID);
9878 processPosition(mapper, x2, y2);
9879 processPressure(mapper, RAW_PRESSURE_MAX);
9880 processSync(mapper);
9881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009882 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009883 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9884
9885 // second finger up with some unexpected data.
9886 processSlot(mapper, SECOND_SLOT);
9887 processId(mapper, INVALID_TRACKING_ID);
9888 processPosition(mapper, x2, y2);
9889 processSync(mapper);
9890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009891 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009892 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9893
9894 // first finger up with some unexpected data.
9895 processSlot(mapper, FIRST_SLOT);
9896 processId(mapper, INVALID_TRACKING_ID);
9897 processPosition(mapper, x2, y2);
9898 processPressure(mapper, RAW_PRESSURE_MAX);
9899 processSync(mapper);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9902 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9903}
9904
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009905TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9906 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009907 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009908 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9909 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9910
9911 // First finger down.
9912 processId(mapper, FIRST_TRACKING_ID);
9913 processPosition(mapper, 100, 200);
9914 processPressure(mapper, RAW_PRESSURE_MAX);
9915 processSync(mapper);
9916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9917 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9918
9919 // Second finger down.
9920 processSlot(mapper, SECOND_SLOT);
9921 processId(mapper, SECOND_TRACKING_ID);
9922 processPosition(mapper, 300, 400);
9923 processPressure(mapper, RAW_PRESSURE_MAX);
9924 processSync(mapper);
9925 ASSERT_NO_FATAL_FAILURE(
9926 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9927
9928 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009929 // preserved. Resetting should cancel the ongoing gesture.
9930 resetMapper(mapper, ARBITRARY_TIME);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9932 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009933
9934 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9935 // the existing touch state to generate a down event.
9936 processPosition(mapper, 301, 302);
9937 processSync(mapper);
9938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9941 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9942
9943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9944}
9945
9946TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9947 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009948 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009949 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9950 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9951
9952 // First finger touches down and releases.
9953 processId(mapper, FIRST_TRACKING_ID);
9954 processPosition(mapper, 100, 200);
9955 processPressure(mapper, RAW_PRESSURE_MAX);
9956 processSync(mapper);
9957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9958 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9959 processId(mapper, INVALID_TRACKING_ID);
9960 processSync(mapper);
9961 ASSERT_NO_FATAL_FAILURE(
9962 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9963
9964 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9965 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009966 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9968
9969 // Send an empty sync frame. Since there are no pointers, no events are generated.
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9972}
9973
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009974TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009975 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009976 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009977 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9978 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009980
9981 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9982 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9983 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9984 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9985 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9986
9987 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009988 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009989 processId(mapper, FIRST_TRACKING_ID);
9990 processToolType(mapper, MT_TOOL_PEN);
9991 processPosition(mapper, 100, 200);
9992 processPressure(mapper, RAW_PRESSURE_MAX);
9993 processSync(mapper);
9994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9996 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9997 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9998
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009999 // Now that we know the device supports styluses, ensure that the device is re-configured with
10000 // the stylus source.
10001 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
10002 {
10003 const auto& devices = mReader->getInputDevices();
10004 auto deviceInfo =
10005 std::find_if(devices.begin(), devices.end(),
10006 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
10007 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
10008 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
10009 }
10010
10011 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
10012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
10013
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010014 processId(mapper, INVALID_TRACKING_ID);
10015 processSync(mapper);
10016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10017 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10018 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
10019 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010020}
10021
Seunghwan Choi356026c2023-02-01 14:37:25 +090010022TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10023 addConfigurationProperty("touch.deviceType", "touchScreen");
10024 prepareDisplay(ui::ROTATION_0);
10025 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10026 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10027 // indicate stylus presence dynamically.
10028 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10029 std::shared_ptr<FakePointerController> fakePointerController =
10030 std::make_shared<FakePointerController>();
10031 mFakePolicy->setPointerController(fakePointerController);
10032 mFakePolicy->setStylusPointerIconEnabled(true);
10033 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10034
10035 processId(mapper, FIRST_TRACKING_ID);
10036 processPressure(mapper, RAW_PRESSURE_MIN);
10037 processPosition(mapper, 100, 200);
10038 processToolType(mapper, MT_TOOL_PEN);
10039 processSync(mapper);
10040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10042 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10043 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10044 ASSERT_TRUE(fakePointerController->isPointerShown());
10045 ASSERT_NO_FATAL_FAILURE(
10046 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10047}
10048
10049TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10050 addConfigurationProperty("touch.deviceType", "touchScreen");
10051 prepareDisplay(ui::ROTATION_0);
10052 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10053 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10054 // indicate stylus presence dynamically.
10055 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10056 std::shared_ptr<FakePointerController> fakePointerController =
10057 std::make_shared<FakePointerController>();
10058 mFakePolicy->setPointerController(fakePointerController);
10059 mFakePolicy->setStylusPointerIconEnabled(false);
10060 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10061
10062 processId(mapper, FIRST_TRACKING_ID);
10063 processPressure(mapper, RAW_PRESSURE_MIN);
10064 processPosition(mapper, 100, 200);
10065 processToolType(mapper, MT_TOOL_PEN);
10066 processSync(mapper);
10067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10069 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10070 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10071 ASSERT_FALSE(fakePointerController->isPointerShown());
10072}
10073
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010074// --- MultiTouchInputMapperTest_ExternalDevice ---
10075
10076class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10077protected:
Chris Yea52ade12020-08-27 16:49:20 -070010078 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010079};
10080
10081/**
10082 * Expect fallback to internal viewport if device is external and external viewport is not present.
10083 */
10084TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10085 prepareAxes(POSITION);
10086 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010087 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010088 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10089
10090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10091
10092 NotifyMotionArgs motionArgs;
10093
10094 // Expect the event to be sent to the internal viewport,
10095 // because an external viewport is not present.
10096 processPosition(mapper, 100, 100);
10097 processSync(mapper);
10098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10099 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10100
10101 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010102 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010103 processPosition(mapper, 100, 100);
10104 processSync(mapper);
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10106 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10107}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010108
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010109TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10110 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10111 std::shared_ptr<FakePointerController> fakePointerController =
10112 std::make_shared<FakePointerController>();
10113 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10114 fakePointerController->setPosition(0, 0);
10115 fakePointerController->setButtonState(0);
10116
10117 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010118 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010119 prepareAxes(POSITION | ID | SLOT);
10120 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10121 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10122 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010123 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010124 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10125
10126 // captured touchpad should be a touchpad source
10127 NotifyDeviceResetArgs resetArgs;
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10129 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10130
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010131 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010132
10133 const InputDeviceInfo::MotionRange* relRangeX =
10134 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10135 ASSERT_NE(relRangeX, nullptr);
10136 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10137 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10138 const InputDeviceInfo::MotionRange* relRangeY =
10139 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10140 ASSERT_NE(relRangeY, nullptr);
10141 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10142 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10143
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010144 // run captured pointer tests - note that this is unscaled, so input listener events should be
10145 // identical to what the hardware sends (accounting for any
10146 // calibration).
10147 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010148 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010149 processId(mapper, 1);
10150 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10151 processKey(mapper, BTN_TOUCH, 1);
10152 processSync(mapper);
10153
10154 // expect coord[0] to contain initial location of touch 0
10155 NotifyMotionArgs args;
10156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10157 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10158 ASSERT_EQ(1U, args.pointerCount);
10159 ASSERT_EQ(0, args.pointerProperties[0].id);
10160 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10161 ASSERT_NO_FATAL_FAILURE(
10162 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10163
10164 // FINGER 1 DOWN
10165 processSlot(mapper, 1);
10166 processId(mapper, 2);
10167 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10168 processSync(mapper);
10169
10170 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010172 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010173 ASSERT_EQ(2U, args.pointerCount);
10174 ASSERT_EQ(0, args.pointerProperties[0].id);
10175 ASSERT_EQ(1, args.pointerProperties[1].id);
10176 ASSERT_NO_FATAL_FAILURE(
10177 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10178 ASSERT_NO_FATAL_FAILURE(
10179 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10180
10181 // FINGER 1 MOVE
10182 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10183 processSync(mapper);
10184
10185 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10186 // from move
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10188 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10189 ASSERT_NO_FATAL_FAILURE(
10190 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10191 ASSERT_NO_FATAL_FAILURE(
10192 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10193
10194 // FINGER 0 MOVE
10195 processSlot(mapper, 0);
10196 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10197 processSync(mapper);
10198
10199 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10202 ASSERT_NO_FATAL_FAILURE(
10203 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10204 ASSERT_NO_FATAL_FAILURE(
10205 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10206
10207 // BUTTON DOWN
10208 processKey(mapper, BTN_LEFT, 1);
10209 processSync(mapper);
10210
10211 // touchinputmapper design sends a move before button press
10212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10213 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10215 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10216
10217 // BUTTON UP
10218 processKey(mapper, BTN_LEFT, 0);
10219 processSync(mapper);
10220
10221 // touchinputmapper design sends a move after button release
10222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10223 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10225 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10226
10227 // FINGER 0 UP
10228 processId(mapper, -1);
10229 processSync(mapper);
10230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10231 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10232
10233 // FINGER 1 MOVE
10234 processSlot(mapper, 1);
10235 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10236 processSync(mapper);
10237
10238 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10241 ASSERT_EQ(1U, args.pointerCount);
10242 ASSERT_EQ(1, args.pointerProperties[0].id);
10243 ASSERT_NO_FATAL_FAILURE(
10244 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10245
10246 // FINGER 1 UP
10247 processId(mapper, -1);
10248 processKey(mapper, BTN_TOUCH, 0);
10249 processSync(mapper);
10250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10251 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10252
Josep del Río2d8c79a2023-01-23 19:33:50 +000010253 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010254 mFakePolicy->setPointerCapture(false);
10255 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010257 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010258}
10259
10260TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10261 std::shared_ptr<FakePointerController> fakePointerController =
10262 std::make_shared<FakePointerController>();
10263 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10264 fakePointerController->setPosition(0, 0);
10265 fakePointerController->setButtonState(0);
10266
10267 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010268 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010269 prepareAxes(POSITION | ID | SLOT);
10270 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10271 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010272 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010273 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10274 // run uncaptured pointer tests - pushes out generic events
10275 // FINGER 0 DOWN
10276 processId(mapper, 3);
10277 processPosition(mapper, 100, 100);
10278 processKey(mapper, BTN_TOUCH, 1);
10279 processSync(mapper);
10280
10281 // start at (100,100), cursor should be at (0,0) * scale
10282 NotifyMotionArgs args;
10283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10284 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10285 ASSERT_NO_FATAL_FAILURE(
10286 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10287
10288 // FINGER 0 MOVE
10289 processPosition(mapper, 200, 200);
10290 processSync(mapper);
10291
10292 // compute scaling to help with touch position checking
10293 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10294 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10295 float scale =
10296 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10297
10298 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10300 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10301 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10302 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010303
10304 // BUTTON DOWN
10305 processKey(mapper, BTN_LEFT, 1);
10306 processSync(mapper);
10307
10308 // touchinputmapper design sends a move before button press
10309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10310 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10312 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10313
10314 // BUTTON UP
10315 processKey(mapper, BTN_LEFT, 0);
10316 processSync(mapper);
10317
10318 // touchinputmapper design sends a move after button release
10319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10322 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010323}
10324
10325TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10326 std::shared_ptr<FakePointerController> fakePointerController =
10327 std::make_shared<FakePointerController>();
10328
Michael Wrighta9cf4192022-12-01 23:46:39 +000010329 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010330 prepareAxes(POSITION | ID | SLOT);
10331 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010332 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010333 mFakePolicy->setPointerCapture(false);
10334 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10335
Josep del Río2d8c79a2023-01-23 19:33:50 +000010336 // uncaptured touchpad should be a pointer device
10337 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010338
Josep del Río2d8c79a2023-01-23 19:33:50 +000010339 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010340 mFakePolicy->setPointerCapture(true);
10341 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10342 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10343}
10344
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010345// --- BluetoothMultiTouchInputMapperTest ---
10346
10347class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10348protected:
10349 void SetUp() override {
10350 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10351 }
10352};
10353
10354TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10355 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010356 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010357 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10358 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10359
10360 nsecs_t kernelEventTime = ARBITRARY_TIME;
10361 nsecs_t expectedEventTime = ARBITRARY_TIME;
10362 // Touch down.
10363 processId(mapper, FIRST_TRACKING_ID);
10364 processPosition(mapper, 100, 200);
10365 processPressure(mapper, RAW_PRESSURE_MAX);
10366 processSync(mapper, ARBITRARY_TIME);
10367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10368 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10369
10370 // Process several events that come in quick succession, according to their timestamps.
10371 for (int i = 0; i < 3; i++) {
10372 constexpr static nsecs_t delta = ms2ns(1);
10373 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10374 kernelEventTime += delta;
10375 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10376
10377 processPosition(mapper, 101 + i, 201 + i);
10378 processSync(mapper, kernelEventTime);
10379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10380 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10381 WithEventTime(expectedEventTime))));
10382 }
10383
10384 // Release the touch.
10385 processId(mapper, INVALID_TRACKING_ID);
10386 processPressure(mapper, RAW_PRESSURE_MIN);
10387 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10389 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10390 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10391}
10392
10393// --- MultiTouchPointerModeTest ---
10394
HQ Liue6983c72022-04-19 22:14:56 +000010395class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10396protected:
10397 float mPointerMovementScale;
10398 float mPointerXZoomScale;
10399 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10400 addConfigurationProperty("touch.deviceType", "pointer");
10401 std::shared_ptr<FakePointerController> fakePointerController =
10402 std::make_shared<FakePointerController>();
10403 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10404 fakePointerController->setPosition(0, 0);
10405 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010406 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010407
10408 prepareAxes(POSITION);
10409 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10410 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10411 // needs to be disabled, and the pointer gesture needs to be enabled.
10412 mFakePolicy->setPointerCapture(false);
10413 mFakePolicy->setPointerGestureEnabled(true);
10414 mFakePolicy->setPointerController(fakePointerController);
10415
10416 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10417 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10418 mPointerMovementScale =
10419 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10420 mPointerXZoomScale =
10421 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10422 }
10423
10424 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10426 /*flat*/ 0,
10427 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10428 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10429 /*flat*/ 0,
10430 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10431 }
10432};
10433
10434/**
10435 * Two fingers down on a pointer mode touch pad. The width
10436 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10437 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10438 * be greater than the both value to be freeform gesture, so that after two
10439 * fingers start to move downwards, the gesture should be swipe.
10440 */
10441TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10442 // The min freeform gesture width is 25units/mm x 30mm = 750
10443 // which is greater than fraction of the diagnal length of the touchpad (349).
10444 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010445 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010446 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10447 NotifyMotionArgs motionArgs;
10448
10449 // Two fingers down at once.
10450 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10451 // Pointer's initial position is used the [0,0] coordinate.
10452 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10453
10454 processId(mapper, FIRST_TRACKING_ID);
10455 processPosition(mapper, x1, y1);
10456 processMTSync(mapper);
10457 processId(mapper, SECOND_TRACKING_ID);
10458 processPosition(mapper, x2, y2);
10459 processMTSync(mapper);
10460 processSync(mapper);
10461
10462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10463 ASSERT_EQ(1U, motionArgs.pointerCount);
10464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010466 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010467 ASSERT_NO_FATAL_FAILURE(
10468 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10469
10470 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10471 // that there should be 1 pointer.
10472 int32_t movingDistance = 200;
10473 y1 += movingDistance;
10474 y2 += movingDistance;
10475
10476 processId(mapper, FIRST_TRACKING_ID);
10477 processPosition(mapper, x1, y1);
10478 processMTSync(mapper);
10479 processId(mapper, SECOND_TRACKING_ID);
10480 processPosition(mapper, x2, y2);
10481 processMTSync(mapper);
10482 processSync(mapper);
10483
10484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10485 ASSERT_EQ(1U, motionArgs.pointerCount);
10486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10487 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010488 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10490 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10491 0, 0, 0, 0));
10492}
10493
10494/**
10495 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10496 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10497 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10498 * value to be freeform gesture, so that after two fingers start to move downwards,
10499 * the gesture should be swipe.
10500 */
10501TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10502 // The min freeform gesture width is 5units/mm x 30mm = 150
10503 // which is greater than fraction of the diagnal length of the touchpad (349).
10504 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010505 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010506 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10507 NotifyMotionArgs motionArgs;
10508
10509 // Two fingers down at once.
10510 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10511 // Pointer's initial position is used the [0,0] coordinate.
10512 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10513
10514 processId(mapper, FIRST_TRACKING_ID);
10515 processPosition(mapper, x1, y1);
10516 processMTSync(mapper);
10517 processId(mapper, SECOND_TRACKING_ID);
10518 processPosition(mapper, x2, y2);
10519 processMTSync(mapper);
10520 processSync(mapper);
10521
10522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10523 ASSERT_EQ(1U, motionArgs.pointerCount);
10524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010526 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010527 ASSERT_NO_FATAL_FAILURE(
10528 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10529
10530 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10531 // and there should be 1 pointer.
10532 int32_t movingDistance = 200;
10533 y1 += movingDistance;
10534 y2 += movingDistance;
10535
10536 processId(mapper, FIRST_TRACKING_ID);
10537 processPosition(mapper, x1, y1);
10538 processMTSync(mapper);
10539 processId(mapper, SECOND_TRACKING_ID);
10540 processPosition(mapper, x2, y2);
10541 processMTSync(mapper);
10542 processSync(mapper);
10543
10544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10545 ASSERT_EQ(1U, motionArgs.pointerCount);
10546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010548 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010549 // New coordinate is the scaled relative coordinate from the initial coordinate.
10550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10551 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10552 0, 0, 0, 0));
10553}
10554
10555/**
10556 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10557 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10558 * freeform gestures after two fingers start to move downwards.
10559 */
10560TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010561 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010562 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10563
10564 NotifyMotionArgs motionArgs;
10565
10566 // Two fingers down at once. Wider than the max swipe width.
10567 // The gesture is expected to be PRESS, then transformed to FREEFORM
10568 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10569
10570 processId(mapper, FIRST_TRACKING_ID);
10571 processPosition(mapper, x1, y1);
10572 processMTSync(mapper);
10573 processId(mapper, SECOND_TRACKING_ID);
10574 processPosition(mapper, x2, y2);
10575 processMTSync(mapper);
10576 processSync(mapper);
10577
10578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10579 ASSERT_EQ(1U, motionArgs.pointerCount);
10580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10581 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010582 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010583 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10584 ASSERT_NO_FATAL_FAILURE(
10585 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10586
10587 int32_t movingDistance = 200;
10588
10589 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10590 // then two down events for two pointers.
10591 y1 += movingDistance;
10592 y2 += movingDistance;
10593
10594 processId(mapper, FIRST_TRACKING_ID);
10595 processPosition(mapper, x1, y1);
10596 processMTSync(mapper);
10597 processId(mapper, SECOND_TRACKING_ID);
10598 processPosition(mapper, x2, y2);
10599 processMTSync(mapper);
10600 processSync(mapper);
10601
10602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10603 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10604 ASSERT_EQ(1U, motionArgs.pointerCount);
10605 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10607 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10608 ASSERT_EQ(1U, motionArgs.pointerCount);
10609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10611 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010612 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010613 ASSERT_EQ(2U, motionArgs.pointerCount);
10614 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010616 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010617 // Two pointers' scaled relative coordinates from their initial centroid.
10618 // Initial y coordinates are 0 as y1 and y2 have the same value.
10619 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10620 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10621 // When pointers move, the new coordinates equal to the initial coordinates plus
10622 // scaled moving distance.
10623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10624 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10625 0, 0, 0, 0));
10626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10627 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10628 0, 0, 0, 0));
10629
10630 // Move two fingers down again, expect one MOVE motion event.
10631 y1 += movingDistance;
10632 y2 += movingDistance;
10633
10634 processId(mapper, FIRST_TRACKING_ID);
10635 processPosition(mapper, x1, y1);
10636 processMTSync(mapper);
10637 processId(mapper, SECOND_TRACKING_ID);
10638 processPosition(mapper, x2, y2);
10639 processMTSync(mapper);
10640 processSync(mapper);
10641
10642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10643 ASSERT_EQ(2U, motionArgs.pointerCount);
10644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10645 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010646 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10648 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10649 0, 0, 0, 0, 0));
10650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10651 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10652 0, 0, 0, 0, 0));
10653}
10654
Harry Cutts39b7ca22022-10-05 15:55:48 +000010655TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010656 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010657 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10658 NotifyMotionArgs motionArgs;
10659
10660 // Place two fingers down.
10661 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10662
10663 processId(mapper, FIRST_TRACKING_ID);
10664 processPosition(mapper, x1, y1);
10665 processMTSync(mapper);
10666 processId(mapper, SECOND_TRACKING_ID);
10667 processPosition(mapper, x2, y2);
10668 processMTSync(mapper);
10669 processSync(mapper);
10670
10671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10672 ASSERT_EQ(1U, motionArgs.pointerCount);
10673 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10674 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10675 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10676 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10677
10678 // Move the two fingers down and to the left.
10679 int32_t movingDistance = 200;
10680 x1 -= movingDistance;
10681 y1 += movingDistance;
10682 x2 -= movingDistance;
10683 y2 += movingDistance;
10684
10685 processId(mapper, FIRST_TRACKING_ID);
10686 processPosition(mapper, x1, y1);
10687 processMTSync(mapper);
10688 processId(mapper, SECOND_TRACKING_ID);
10689 processPosition(mapper, x2, y2);
10690 processMTSync(mapper);
10691 processSync(mapper);
10692
10693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10694 ASSERT_EQ(1U, motionArgs.pointerCount);
10695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10696 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10697 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10698 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10699}
10700
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010701TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010702 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010703 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10704 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10706
10707 // Start a stylus gesture.
10708 processKey(mapper, BTN_TOOL_PEN, 1);
10709 processId(mapper, FIRST_TRACKING_ID);
10710 processPosition(mapper, 100, 200);
10711 processSync(mapper);
10712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10714 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10715 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10716 // TODO(b/257078296): Pointer mode generates extra event.
10717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10719 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10720 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10722
10723 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10724 // gesture should be disabled.
10725 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10726 viewport->isActive = false;
10727 mFakePolicy->updateViewport(*viewport);
10728 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10731 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10732 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10733 // TODO(b/257078296): Pointer mode generates extra event.
10734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10736 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10737 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10739}
10740
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010741// --- JoystickInputMapperTest ---
10742
10743class JoystickInputMapperTest : public InputMapperTest {
10744protected:
10745 static const int32_t RAW_X_MIN;
10746 static const int32_t RAW_X_MAX;
10747 static const int32_t RAW_Y_MIN;
10748 static const int32_t RAW_Y_MAX;
10749
10750 void SetUp() override {
10751 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10752 }
10753 void prepareAxes() {
10754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10756 }
10757
10758 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10760 }
10761
10762 void processSync(JoystickInputMapper& mapper) {
10763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10764 }
10765
Michael Wrighta9cf4192022-12-01 23:46:39 +000010766 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010767 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10768 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10769 NO_PORT, ViewportType::VIRTUAL);
10770 }
10771};
10772
10773const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10774const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10775const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10776const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10777
10778TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10779 prepareAxes();
10780 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10781
10782 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10783
Michael Wrighta9cf4192022-12-01 23:46:39 +000010784 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010785
10786 // Send an axis event
10787 processAxis(mapper, ABS_X, 100);
10788 processSync(mapper);
10789
10790 NotifyMotionArgs args;
10791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10792 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10793
10794 // Send another axis event
10795 processAxis(mapper, ABS_Y, 100);
10796 processSync(mapper);
10797
10798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10799 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10800}
10801
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010802// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010803
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010804class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010805protected:
10806 static const char* DEVICE_NAME;
10807 static const char* DEVICE_LOCATION;
10808 static const int32_t DEVICE_ID;
10809 static const int32_t DEVICE_GENERATION;
10810 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010811 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010812 static const int32_t EVENTHUB_ID;
10813
10814 std::shared_ptr<FakeEventHub> mFakeEventHub;
10815 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010816 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010817 std::unique_ptr<InstrumentedInputReader> mReader;
10818 std::shared_ptr<InputDevice> mDevice;
10819
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010820 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010821 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010822 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010823 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010824 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010825 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010826 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10827 }
10828
10829 void SetUp() override { SetUp(DEVICE_CLASSES); }
10830
10831 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010832 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010833 mFakePolicy.clear();
10834 }
10835
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010836 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010837 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10838 mReader->requestRefreshConfiguration(changes);
10839 mReader->loopOnce();
10840 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010841 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010842 }
10843
10844 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10845 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010846 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010847 InputDeviceIdentifier identifier;
10848 identifier.name = name;
10849 identifier.location = location;
10850 std::shared_ptr<InputDevice> device =
10851 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10852 identifier);
10853 mReader->pushNextDevice(device);
10854 mFakeEventHub->addDevice(eventHubId, name, classes);
10855 mReader->loopOnce();
10856 return device;
10857 }
10858
10859 template <class T, typename... Args>
10860 T& addControllerAndConfigure(Args... args) {
10861 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10862
10863 return controller;
10864 }
10865};
10866
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010867const char* PeripheralControllerTest::DEVICE_NAME = "device";
10868const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10869const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10870const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10871const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010872const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10873 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010874const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010875
10876// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010877class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010878protected:
10879 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010880 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010881 }
10882};
10883
10884TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010885 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010886
Harry Cuttsa5b71292022-11-28 12:56:17 +000010887 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10888 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10889 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010890}
10891
10892TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010893 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010894
Harry Cuttsa5b71292022-11-28 12:56:17 +000010895 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10896 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10897 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010898}
10899
10900// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010901class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010902protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010903 void SetUp() override {
10904 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10905 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010906};
10907
Chris Ye85758332021-05-16 23:05:17 -070010908TEST_F(LightControllerTest, MonoLight) {
10909 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010910 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010911 .maxBrightness = 255,
10912 .flags = InputLightClass::BRIGHTNESS,
10913 .path = ""};
10914 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010915
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010916 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010917 InputDeviceInfo info;
10918 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010919 std::vector<InputDeviceLightInfo> lights = info.getLights();
10920 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010921 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10922 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10923
10924 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10925 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10926}
10927
10928TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10929 RawLightInfo infoMono = {.id = 1,
10930 .name = "mono_keyboard_backlight",
10931 .maxBrightness = 255,
10932 .flags = InputLightClass::BRIGHTNESS |
10933 InputLightClass::KEYBOARD_BACKLIGHT,
10934 .path = ""};
10935 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10936
10937 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10938 InputDeviceInfo info;
10939 controller.populateDeviceInfo(&info);
10940 std::vector<InputDeviceLightInfo> lights = info.getLights();
10941 ASSERT_EQ(1U, lights.size());
10942 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10943 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010944
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010945 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10946 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010947}
10948
10949TEST_F(LightControllerTest, RGBLight) {
10950 RawLightInfo infoRed = {.id = 1,
10951 .name = "red",
10952 .maxBrightness = 255,
10953 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10954 .path = ""};
10955 RawLightInfo infoGreen = {.id = 2,
10956 .name = "green",
10957 .maxBrightness = 255,
10958 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10959 .path = ""};
10960 RawLightInfo infoBlue = {.id = 3,
10961 .name = "blue",
10962 .maxBrightness = 255,
10963 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10964 .path = ""};
10965 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10966 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10967 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10968
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010969 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010970 InputDeviceInfo info;
10971 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010972 std::vector<InputDeviceLightInfo> lights = info.getLights();
10973 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010974 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10975 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10976 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10977
10978 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10979 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10980}
10981
10982TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10983 RawLightInfo infoRed = {.id = 1,
10984 .name = "red_keyboard_backlight",
10985 .maxBrightness = 255,
10986 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10987 InputLightClass::KEYBOARD_BACKLIGHT,
10988 .path = ""};
10989 RawLightInfo infoGreen = {.id = 2,
10990 .name = "green_keyboard_backlight",
10991 .maxBrightness = 255,
10992 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10993 InputLightClass::KEYBOARD_BACKLIGHT,
10994 .path = ""};
10995 RawLightInfo infoBlue = {.id = 3,
10996 .name = "blue_keyboard_backlight",
10997 .maxBrightness = 255,
10998 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10999 InputLightClass::KEYBOARD_BACKLIGHT,
11000 .path = ""};
11001 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11002 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11003 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11004
11005 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11006 InputDeviceInfo info;
11007 controller.populateDeviceInfo(&info);
11008 std::vector<InputDeviceLightInfo> lights = info.getLights();
11009 ASSERT_EQ(1U, lights.size());
11010 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11011 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11012 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11013
11014 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11015 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11016}
11017
11018TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
11019 RawLightInfo infoRed = {.id = 1,
11020 .name = "red",
11021 .maxBrightness = 255,
11022 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11023 .path = ""};
11024 RawLightInfo infoGreen = {.id = 2,
11025 .name = "green",
11026 .maxBrightness = 255,
11027 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11028 .path = ""};
11029 RawLightInfo infoBlue = {.id = 3,
11030 .name = "blue",
11031 .maxBrightness = 255,
11032 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11033 .path = ""};
11034 RawLightInfo infoGlobal = {.id = 3,
11035 .name = "global_keyboard_backlight",
11036 .maxBrightness = 255,
11037 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11038 InputLightClass::KEYBOARD_BACKLIGHT,
11039 .path = ""};
11040 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11041 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11042 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11043 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11044
11045 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11046 InputDeviceInfo info;
11047 controller.populateDeviceInfo(&info);
11048 std::vector<InputDeviceLightInfo> lights = info.getLights();
11049 ASSERT_EQ(1U, lights.size());
11050 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11051 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11052 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011053
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011054 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11055 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011056}
11057
11058TEST_F(LightControllerTest, MultiColorRGBLight) {
11059 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011060 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011061 .maxBrightness = 255,
11062 .flags = InputLightClass::BRIGHTNESS |
11063 InputLightClass::MULTI_INTENSITY |
11064 InputLightClass::MULTI_INDEX,
11065 .path = ""};
11066
11067 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11068
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011069 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011070 InputDeviceInfo info;
11071 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011072 std::vector<InputDeviceLightInfo> lights = info.getLights();
11073 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011074 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11075 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11076 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11077
11078 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11079 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11080}
11081
11082TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11083 RawLightInfo infoColor = {.id = 1,
11084 .name = "multi_color_keyboard_backlight",
11085 .maxBrightness = 255,
11086 .flags = InputLightClass::BRIGHTNESS |
11087 InputLightClass::MULTI_INTENSITY |
11088 InputLightClass::MULTI_INDEX |
11089 InputLightClass::KEYBOARD_BACKLIGHT,
11090 .path = ""};
11091
11092 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11093
11094 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11095 InputDeviceInfo info;
11096 controller.populateDeviceInfo(&info);
11097 std::vector<InputDeviceLightInfo> lights = info.getLights();
11098 ASSERT_EQ(1U, lights.size());
11099 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11100 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11101 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011102
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011103 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11104 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011105}
11106
11107TEST_F(LightControllerTest, PlayerIdLight) {
11108 RawLightInfo info1 = {.id = 1,
11109 .name = "player1",
11110 .maxBrightness = 255,
11111 .flags = InputLightClass::BRIGHTNESS,
11112 .path = ""};
11113 RawLightInfo info2 = {.id = 2,
11114 .name = "player2",
11115 .maxBrightness = 255,
11116 .flags = InputLightClass::BRIGHTNESS,
11117 .path = ""};
11118 RawLightInfo info3 = {.id = 3,
11119 .name = "player3",
11120 .maxBrightness = 255,
11121 .flags = InputLightClass::BRIGHTNESS,
11122 .path = ""};
11123 RawLightInfo info4 = {.id = 4,
11124 .name = "player4",
11125 .maxBrightness = 255,
11126 .flags = InputLightClass::BRIGHTNESS,
11127 .path = ""};
11128 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11129 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11130 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11131 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11132
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011133 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011134 InputDeviceInfo info;
11135 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011136 std::vector<InputDeviceLightInfo> lights = info.getLights();
11137 ASSERT_EQ(1U, lights.size());
11138 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011139 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11140 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011141
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011142 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11143 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11144 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011145}
11146
Michael Wrightd02c5b62014-02-10 15:10:22 -080011147} // namespace android