blob: e1c54e991445a56288062c008289a9264a4ae1d1 [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
1833TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1834 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 Pradhan124ea442022-10-28 20:27:44 +00001847TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1848 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 Pradhan9a561c22022-11-07 16:11:23 +00001893TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
1894 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 Pradhan124ea442022-10-28 20:27:44 +00001969TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
1970 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 Pradhan7aa7ff02022-12-21 21:05:38 +00002023TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
2024 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
2080TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2081 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
2126TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2127 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 Pradhan3f7545f2022-10-19 16:56:39 +00002194TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2195 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) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3662 // marked as WAKE in the keylayout file to trigger wake.
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));
3684 ASSERT_EQ(uint32_t(0), args.policyFlags);
3685
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
Prabir Pradhan5632d622021-09-06 07:57:20 -07006681// --- TouchDisplayProjectionTest ---
6682
6683class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6684public:
6685 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6686 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6687 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006688 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006689 uint32_t inverseRotationFlags;
6690 auto width = DISPLAY_WIDTH;
6691 auto height = DISPLAY_HEIGHT;
6692 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006693 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006694 inverseRotationFlags = ui::Transform::ROT_270;
6695 std::swap(width, height);
6696 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006697 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006698 inverseRotationFlags = ui::Transform::ROT_180;
6699 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006700 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006701 inverseRotationFlags = ui::Transform::ROT_90;
6702 std::swap(width, height);
6703 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006704 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006705 inverseRotationFlags = ui::Transform::ROT_0;
6706 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006707 }
6708
6709 const ui::Transform rotation(inverseRotationFlags, width, height);
6710 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6711
6712 std::optional<DisplayViewport> internalViewport =
6713 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6714 DisplayViewport& v = *internalViewport;
6715 v.displayId = DISPLAY_ID;
6716 v.orientation = orientation;
6717
6718 v.logicalLeft = 0;
6719 v.logicalTop = 0;
6720 v.logicalRight = 100;
6721 v.logicalBottom = 100;
6722
6723 v.physicalLeft = rotatedPhysicalDisplay.left;
6724 v.physicalTop = rotatedPhysicalDisplay.top;
6725 v.physicalRight = rotatedPhysicalDisplay.right;
6726 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6727
6728 v.deviceWidth = width;
6729 v.deviceHeight = height;
6730
6731 v.isActive = true;
6732 v.uniqueId = UNIQUE_ID;
6733 v.type = ViewportType::INTERNAL;
6734 mFakePolicy->updateViewport(v);
6735 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6736 }
6737
6738 void assertReceivedMove(const Point& point) {
6739 NotifyMotionArgs motionArgs;
6740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6742 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6744 1, 0, 0, 0, 0, 0, 0, 0));
6745 }
6746};
6747
6748TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6749 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006750 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751
6752 prepareButtons();
6753 prepareAxes(POSITION);
6754 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6755
6756 NotifyMotionArgs motionArgs;
6757
6758 // Configure the DisplayViewport such that the logical display maps to a subsection of
6759 // the display panel called the physical display. Here, the physical display is bounded by the
6760 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6761 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6762 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6763 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6764
Michael Wrighta9cf4192022-12-01 23:46:39 +00006765 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006766 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6767
6768 // Touches outside the physical display should be ignored, and should not generate any
6769 // events. Ensure touches at the following points that lie outside of the physical display
6770 // area do not generate any events.
6771 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6772 processDown(mapper, toRawX(point.x), toRawY(point.y));
6773 processSync(mapper);
6774 processUp(mapper);
6775 processSync(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6777 << "Unexpected event generated for touch outside physical display at point: "
6778 << point.x << ", " << point.y;
6779 }
6780 }
6781}
6782
6783TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6784 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006785 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006786
6787 prepareButtons();
6788 prepareAxes(POSITION);
6789 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6790
6791 NotifyMotionArgs motionArgs;
6792
6793 // Configure the DisplayViewport such that the logical display maps to a subsection of
6794 // the display panel called the physical display. Here, the physical display is bounded by the
6795 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6796 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6797
Michael Wrighta9cf4192022-12-01 23:46:39 +00006798 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006799 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6800
6801 // Touches that start outside the physical display should be ignored until it enters the
6802 // physical display bounds, at which point it should generate a down event. Start a touch at
6803 // the point (5, 100), which is outside the physical display bounds.
6804 static const Point kOutsidePoint{5, 100};
6805 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6808
6809 // Move the touch into the physical display area. This should generate a pointer down.
6810 processMove(mapper, toRawX(11), toRawY(21));
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6813 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6814 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6815 ASSERT_NO_FATAL_FAILURE(
6816 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6817
6818 // Move the touch inside the physical display area. This should generate a pointer move.
6819 processMove(mapper, toRawX(69), toRawY(159));
6820 processSync(mapper);
6821 assertReceivedMove({69, 159});
6822
6823 // Move outside the physical display area. Since the pointer is already down, this should
6824 // now continue generating events.
6825 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6826 processSync(mapper);
6827 assertReceivedMove(kOutsidePoint);
6828
6829 // Release. This should generate a pointer up.
6830 processUp(mapper);
6831 processSync(mapper);
6832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6833 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6834 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6835 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6836
6837 // Ensure no more events were generated.
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6840 }
6841}
6842
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006843// --- ExternalStylusFusionTest ---
6844
6845class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6846public:
6847 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6848 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006849 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006850 prepareButtons();
6851 prepareAxes(POSITION);
6852 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6853
6854 mStylusState.when = ARBITRARY_TIME;
6855 mStylusState.pressure = 0.f;
6856 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6857 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6858 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6859 processExternalStylusState(mapper);
6860 return mapper;
6861 }
6862
6863 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6864 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6865 for (const NotifyArgs& args : generatedArgs) {
6866 mFakeListener->notify(args);
6867 }
6868 // Loop the reader to flush the input listener queue.
6869 mReader->loopOnce();
6870 return generatedArgs;
6871 }
6872
6873protected:
6874 StylusState mStylusState{};
6875 static constexpr uint32_t EXPECTED_SOURCE =
6876 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6877
6878 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6879 auto toolTypeSource =
6880 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6881
6882 // The first pointer is withheld.
6883 processDown(mapper, 100, 200);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6886 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6887 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6888
6889 // The external stylus reports pressure. The withheld finger pointer is released as a
6890 // stylus.
6891 mStylusState.pressure = 1.f;
6892 processExternalStylusState(mapper);
6893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6894 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6895 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6896
6897 // Subsequent pointer events are not withheld.
6898 processMove(mapper, 101, 201);
6899 processSync(mapper);
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6901 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6902
6903 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6905 }
6906
6907 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6908 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6909
6910 // Releasing the touch pointer ends the gesture.
6911 processUp(mapper);
6912 processSync(mapper);
6913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6915 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6916
6917 mStylusState.pressure = 0.f;
6918 processExternalStylusState(mapper);
6919 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6921 }
6922
6923 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6924 auto toolTypeSource =
6925 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6926
6927 // The first pointer is withheld when an external stylus is connected,
6928 // and a timeout is requested.
6929 processDown(mapper, 100, 200);
6930 processSync(mapper);
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6932 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6933 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6934
6935 // If the timeout expires early, it is requested again.
6936 handleTimeout(mapper, ARBITRARY_TIME + 1);
6937 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6938 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6939
6940 // When the timeout expires, the withheld touch is released as a finger pointer.
6941 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6943 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6944
6945 // Subsequent pointer events are not withheld.
6946 processMove(mapper, 101, 201);
6947 processSync(mapper);
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6949 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6950 processUp(mapper);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6953 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6954
6955 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6957 }
6958
6959private:
6960 InputDeviceInfo mExternalStylusDeviceInfo{};
6961};
6962
6963TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6964 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6965 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6966}
6967
6968TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6969 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6970 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6971}
6972
6973TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6974 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6975 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6976}
6977
6978// Test a successful stylus fusion gesture where the pressure is reported by the external
6979// before the touch is reported by the touchscreen.
6980TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6981 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6982 auto toolTypeSource =
6983 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6984
6985 // The external stylus reports pressure first. It is ignored for now.
6986 mStylusState.pressure = 1.f;
6987 processExternalStylusState(mapper);
6988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6989 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6990
6991 // When the touch goes down afterwards, it is reported as a stylus pointer.
6992 processDown(mapper, 100, 200);
6993 processSync(mapper);
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6995 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6996 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6997
6998 processMove(mapper, 101, 201);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7001 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7002 processUp(mapper);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7005 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7006
7007 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7009}
7010
7011TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7012 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7013
7014 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7015 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7016
7017 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7018 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7019 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7020 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7021}
7022
7023TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7024 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7025 auto toolTypeSource =
7026 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7027
7028 mStylusState.pressure = 0.8f;
7029 processExternalStylusState(mapper);
7030 processDown(mapper, 100, 200);
7031 processSync(mapper);
7032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7033 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7034 WithPressure(0.8f))));
7035 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7036
7037 // The external stylus reports a pressure change. We wait for some time for a touch event.
7038 mStylusState.pressure = 0.6f;
7039 processExternalStylusState(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7041 ASSERT_NO_FATAL_FAILURE(
7042 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7043
7044 // If a touch is reported within the timeout, it reports the updated pressure.
7045 processMove(mapper, 101, 201);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7048 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7049 WithPressure(0.6f))));
7050 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7051
7052 // There is another pressure change.
7053 mStylusState.pressure = 0.5f;
7054 processExternalStylusState(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7056 ASSERT_NO_FATAL_FAILURE(
7057 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7058
7059 // If a touch is not reported within the timeout, a move event is generated to report
7060 // the new pressure.
7061 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7063 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7064 WithPressure(0.5f))));
7065
7066 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7067 // repeated indefinitely.
7068 mStylusState.pressure = 0.0f;
7069 processExternalStylusState(mapper);
7070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7071 ASSERT_NO_FATAL_FAILURE(
7072 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7073 processMove(mapper, 102, 202);
7074 processSync(mapper);
7075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7076 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7077 WithPressure(0.5f))));
7078 processMove(mapper, 103, 203);
7079 processSync(mapper);
7080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7081 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7082 WithPressure(0.5f))));
7083
7084 processUp(mapper);
7085 processSync(mapper);
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7087 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7088 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7089
7090 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7092}
7093
7094TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7095 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7096 auto source = WithSource(EXPECTED_SOURCE);
7097
7098 mStylusState.pressure = 1.f;
7099 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7100 processExternalStylusState(mapper);
7101 processDown(mapper, 100, 200);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7104 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7105 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7106 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7107
7108 // The external stylus reports a tool change. We wait for some time for a touch event.
7109 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7110 processExternalStylusState(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7112 ASSERT_NO_FATAL_FAILURE(
7113 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7114
7115 // If a touch is reported within the timeout, it reports the updated pressure.
7116 processMove(mapper, 101, 201);
7117 processSync(mapper);
7118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7119 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7120 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7121 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7122
7123 // There is another tool type change.
7124 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7125 processExternalStylusState(mapper);
7126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7127 ASSERT_NO_FATAL_FAILURE(
7128 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7129
7130 // If a touch is not reported within the timeout, a move event is generated to report
7131 // the new tool type.
7132 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7134 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7135 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7136
7137 processUp(mapper);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7140 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7141 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7142
7143 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7145}
7146
7147TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7148 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7149 auto toolTypeSource =
7150 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7151
7152 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7153
7154 // The external stylus reports a button change. We wait for some time for a touch event.
7155 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7156 processExternalStylusState(mapper);
7157 ASSERT_NO_FATAL_FAILURE(
7158 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7159
7160 // If a touch is reported within the timeout, it reports the updated button state.
7161 processMove(mapper, 101, 201);
7162 processSync(mapper);
7163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7164 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7165 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7167 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7168 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7169 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7170
7171 // The button is now released.
7172 mStylusState.buttons = 0;
7173 processExternalStylusState(mapper);
7174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7175 ASSERT_NO_FATAL_FAILURE(
7176 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7177
7178 // If a touch is not reported within the timeout, a move event is generated to report
7179 // the new button state.
7180 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7182 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7183 WithButtonState(0))));
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007185 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7186 WithButtonState(0))));
7187
7188 processUp(mapper);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007191 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7192
7193 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7195}
7196
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197// --- MultiTouchInputMapperTest ---
7198
7199class MultiTouchInputMapperTest : public TouchInputMapperTest {
7200protected:
7201 void prepareAxes(int axes);
7202
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007203 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7204 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7205 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7206 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7207 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7208 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7209 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7210 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7211 void processId(MultiTouchInputMapper& mapper, int32_t id);
7212 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7213 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7214 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007215 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007216 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007217 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7218 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007219};
7220
7221void MultiTouchInputMapperTest::prepareAxes(int axes) {
7222 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007223 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7224 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225 }
7226 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007227 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7228 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007230 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7231 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232 }
7233 }
7234 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007235 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7236 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007237 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007238 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007239 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 }
7241 }
7242 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007243 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7244 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 }
7246 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007247 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7248 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007249 }
7250 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007251 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7252 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007253 }
7254 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007255 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7256 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 }
7258 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007259 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7260 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007261 }
7262 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007263 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264 }
7265}
7266
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007267void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7268 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007269 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007271}
7272
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007273void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7274 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007276}
7277
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007278void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7279 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281}
7282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007283void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285}
7286
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007287void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007289}
7290
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007291void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7292 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007293 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007294}
7295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007296void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007297 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007298}
7299
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007300void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007302}
7303
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007304void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007305 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306}
7307
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007308void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007310}
7311
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007312void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007314}
7315
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007316void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7317 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007318 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319}
7320
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007321void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7322 int32_t value) {
7323 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7324 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7325}
7326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007327void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007329}
7330
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007331void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7332 nsecs_t readTime) {
7333 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334}
7335
Michael Wrightd02c5b62014-02-10 15:10:22 -08007336TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007337 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007338 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007339 prepareAxes(POSITION);
7340 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007341 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007342
arthurhungdcef2dc2020-08-11 14:47:50 +08007343 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007344
7345 NotifyMotionArgs motionArgs;
7346
7347 // Two fingers down at once.
7348 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7349 processPosition(mapper, x1, y1);
7350 processMTSync(mapper);
7351 processPosition(mapper, x2, y2);
7352 processMTSync(mapper);
7353 processSync(mapper);
7354
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7357 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7358 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7359 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7360 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7361 ASSERT_EQ(0, motionArgs.flags);
7362 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7363 ASSERT_EQ(0, motionArgs.buttonState);
7364 ASSERT_EQ(0, motionArgs.edgeFlags);
7365 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7366 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7369 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7370 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7371 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007379 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
7384 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7385 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7388 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7389 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7390 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7391 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7392 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7393 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7394 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7396
7397 // Move.
7398 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7399 processPosition(mapper, x1, y1);
7400 processMTSync(mapper);
7401 processPosition(mapper, x2, y2);
7402 processMTSync(mapper);
7403 processSync(mapper);
7404
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7406 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7407 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7408 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7409 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7411 ASSERT_EQ(0, motionArgs.flags);
7412 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7413 ASSERT_EQ(0, motionArgs.buttonState);
7414 ASSERT_EQ(0, motionArgs.edgeFlags);
7415 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7416 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7417 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7418 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7419 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7420 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7421 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7422 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7423 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7424 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7425 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7426 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7427
7428 // First finger up.
7429 x2 += 15; y2 -= 20;
7430 processPosition(mapper, x2, y2);
7431 processMTSync(mapper);
7432 processSync(mapper);
7433
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7435 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7436 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7438 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007439 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007440 ASSERT_EQ(0, motionArgs.flags);
7441 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7442 ASSERT_EQ(0, motionArgs.buttonState);
7443 ASSERT_EQ(0, motionArgs.edgeFlags);
7444 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7445 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7446 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7447 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7448 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7450 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7452 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7453 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7454 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7455 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7456
7457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7458 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7459 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7460 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7461 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7462 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7463 ASSERT_EQ(0, motionArgs.flags);
7464 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7465 ASSERT_EQ(0, motionArgs.buttonState);
7466 ASSERT_EQ(0, motionArgs.edgeFlags);
7467 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7468 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7471 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7472 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7473 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7474 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7475
7476 // Move.
7477 x2 += 20; y2 -= 25;
7478 processPosition(mapper, x2, y2);
7479 processMTSync(mapper);
7480 processSync(mapper);
7481
7482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7488 ASSERT_EQ(0, motionArgs.flags);
7489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7490 ASSERT_EQ(0, motionArgs.buttonState);
7491 ASSERT_EQ(0, motionArgs.edgeFlags);
7492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7493 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7496 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7500
7501 // New finger down.
7502 int32_t x3 = 700, y3 = 300;
7503 processPosition(mapper, x2, y2);
7504 processMTSync(mapper);
7505 processPosition(mapper, x3, y3);
7506 processMTSync(mapper);
7507 processSync(mapper);
7508
7509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7510 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7511 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7513 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007514 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515 ASSERT_EQ(0, motionArgs.flags);
7516 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7517 ASSERT_EQ(0, motionArgs.buttonState);
7518 ASSERT_EQ(0, motionArgs.edgeFlags);
7519 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7520 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7522 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7523 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7524 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7525 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7527 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7528 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7529 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7530 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7531
7532 // Second finger up.
7533 x3 += 30; y3 -= 20;
7534 processPosition(mapper, x3, y3);
7535 processMTSync(mapper);
7536 processSync(mapper);
7537
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7539 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7540 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7541 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7542 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007543 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(0, motionArgs.flags);
7545 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7546 ASSERT_EQ(0, motionArgs.buttonState);
7547 ASSERT_EQ(0, motionArgs.edgeFlags);
7548 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7549 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7550 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7551 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7554 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7556 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7557 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7558 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7559 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7563 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7564 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7565 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7567 ASSERT_EQ(0, motionArgs.flags);
7568 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7569 ASSERT_EQ(0, motionArgs.buttonState);
7570 ASSERT_EQ(0, motionArgs.edgeFlags);
7571 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7572 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7574 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7575 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7576 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7577 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7578 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7579
7580 // Last finger up.
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7585 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7586 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7587 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7588 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7589 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7590 ASSERT_EQ(0, motionArgs.flags);
7591 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7592 ASSERT_EQ(0, motionArgs.buttonState);
7593 ASSERT_EQ(0, motionArgs.edgeFlags);
7594 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7595 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7596 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7598 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7599 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7600 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7601 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7602
7603 // Should not have sent any more keys or motions.
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7606}
7607
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007608TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007610 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007611
7612 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7613 /*fuzz*/ 0, /*resolution*/ 10);
7614 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7615 /*fuzz*/ 0, /*resolution*/ 11);
7616 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7617 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7618 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7619 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7620 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7621 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7622 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7623 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7624
7625 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7626
7627 // X and Y axes
7628 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7629 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7630 // Touch major and minor
7631 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7632 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7633 // Tool major and minor
7634 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7635 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7636}
7637
7638TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7639 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007640 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007641
7642 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7643 /*fuzz*/ 0, /*resolution*/ 10);
7644 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7645 /*fuzz*/ 0, /*resolution*/ 11);
7646
7647 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7648
7649 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7650
7651 // Touch major and minor
7652 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7653 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7654 // Tool major and minor
7655 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7656 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7657}
7658
Michael Wrightd02c5b62014-02-10 15:10:22 -08007659TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007660 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007661 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007662 prepareAxes(POSITION | ID);
7663 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007664 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665
arthurhungdcef2dc2020-08-11 14:47:50 +08007666 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007667
7668 NotifyMotionArgs motionArgs;
7669
7670 // Two fingers down at once.
7671 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7672 processPosition(mapper, x1, y1);
7673 processId(mapper, 1);
7674 processMTSync(mapper);
7675 processPosition(mapper, x2, y2);
7676 processId(mapper, 2);
7677 processMTSync(mapper);
7678 processSync(mapper);
7679
7680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7681 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7682 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7684 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7686 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007689 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007690 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7692 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7694 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7699
7700 // Move.
7701 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7702 processPosition(mapper, x1, y1);
7703 processId(mapper, 1);
7704 processMTSync(mapper);
7705 processPosition(mapper, x2, y2);
7706 processId(mapper, 2);
7707 processMTSync(mapper);
7708 processSync(mapper);
7709
7710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7711 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7712 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7713 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7715 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7716 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7718 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7719 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7720 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7721
7722 // First finger up.
7723 x2 += 15; y2 -= 20;
7724 processPosition(mapper, x2, y2);
7725 processId(mapper, 2);
7726 processMTSync(mapper);
7727 processSync(mapper);
7728
7729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007730 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007731 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7732 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7735 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7737 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7740
7741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7742 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7743 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7744 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7745 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7746 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7747 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7748
7749 // Move.
7750 x2 += 20; y2 -= 25;
7751 processPosition(mapper, x2, y2);
7752 processId(mapper, 2);
7753 processMTSync(mapper);
7754 processSync(mapper);
7755
7756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7758 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7759 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7763
7764 // New finger down.
7765 int32_t x3 = 700, y3 = 300;
7766 processPosition(mapper, x2, y2);
7767 processId(mapper, 2);
7768 processMTSync(mapper);
7769 processPosition(mapper, x3, y3);
7770 processId(mapper, 3);
7771 processMTSync(mapper);
7772 processSync(mapper);
7773
7774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007775 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7778 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7779 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7780 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7782 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7784 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7785
7786 // Second finger up.
7787 x3 += 30; y3 -= 20;
7788 processPosition(mapper, x3, y3);
7789 processId(mapper, 3);
7790 processMTSync(mapper);
7791 processSync(mapper);
7792
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007794 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7798 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7799 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7800 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7801 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7803 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7804
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7807 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7808 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7812
7813 // Last finger up.
7814 processMTSync(mapper);
7815 processSync(mapper);
7816
7817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7818 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7819 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7820 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7821 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7824
7825 // Should not have sent any more keys or motions.
7826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7828}
7829
7830TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007831 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007832 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 prepareAxes(POSITION | ID | SLOT);
7834 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007835 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007836
arthurhungdcef2dc2020-08-11 14:47:50 +08007837 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007838
7839 NotifyMotionArgs motionArgs;
7840
7841 // Two fingers down at once.
7842 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7843 processPosition(mapper, x1, y1);
7844 processId(mapper, 1);
7845 processSlot(mapper, 1);
7846 processPosition(mapper, x2, y2);
7847 processId(mapper, 2);
7848 processSync(mapper);
7849
7850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7851 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7852 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7853 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7856 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7857
7858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007859 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007860 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7861 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7862 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7863 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7864 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7866 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7867 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7868 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7869
7870 // Move.
7871 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7872 processSlot(mapper, 0);
7873 processPosition(mapper, x1, y1);
7874 processSlot(mapper, 1);
7875 processPosition(mapper, x2, y2);
7876 processSync(mapper);
7877
7878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7879 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7880 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7882 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7883 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7884 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7886 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7888 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7889
7890 // First finger up.
7891 x2 += 15; y2 -= 20;
7892 processSlot(mapper, 0);
7893 processId(mapper, -1);
7894 processSlot(mapper, 1);
7895 processPosition(mapper, x2, y2);
7896 processSync(mapper);
7897
7898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007899 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007900 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7901 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7902 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7903 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7904 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7906 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7908 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7909
7910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7912 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7913 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7914 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7916 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7917
7918 // Move.
7919 x2 += 20; y2 -= 25;
7920 processPosition(mapper, x2, y2);
7921 processSync(mapper);
7922
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7925 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7926 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7927 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7929 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7930
7931 // New finger down.
7932 int32_t x3 = 700, y3 = 300;
7933 processPosition(mapper, x2, y2);
7934 processSlot(mapper, 0);
7935 processId(mapper, 3);
7936 processPosition(mapper, x3, y3);
7937 processSync(mapper);
7938
7939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007940 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7942 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7943 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7944 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7945 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7947 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7949 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7950
7951 // Second finger up.
7952 x3 += 30; y3 -= 20;
7953 processSlot(mapper, 1);
7954 processId(mapper, -1);
7955 processSlot(mapper, 0);
7956 processPosition(mapper, x3, y3);
7957 processSync(mapper);
7958
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007960 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007961 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7963 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7964 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7965 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7967 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7970
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7972 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7973 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7974 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7975 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7977 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7978
7979 // Last finger up.
7980 processId(mapper, -1);
7981 processSync(mapper);
7982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7985 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7986 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7987 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7990
7991 // Should not have sent any more keys or motions.
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7994}
7995
7996TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007999 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001
8002 // These calculations are based on the input device calibration documentation.
8003 int32_t rawX = 100;
8004 int32_t rawY = 200;
8005 int32_t rawTouchMajor = 7;
8006 int32_t rawTouchMinor = 6;
8007 int32_t rawToolMajor = 9;
8008 int32_t rawToolMinor = 8;
8009 int32_t rawPressure = 11;
8010 int32_t rawDistance = 0;
8011 int32_t rawOrientation = 3;
8012 int32_t id = 5;
8013
8014 float x = toDisplayX(rawX);
8015 float y = toDisplayY(rawY);
8016 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8017 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8018 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8019 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8020 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8021 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8022 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8023 float distance = float(rawDistance);
8024
8025 processPosition(mapper, rawX, rawY);
8026 processTouchMajor(mapper, rawTouchMajor);
8027 processTouchMinor(mapper, rawTouchMinor);
8028 processToolMajor(mapper, rawToolMajor);
8029 processToolMinor(mapper, rawToolMinor);
8030 processPressure(mapper, rawPressure);
8031 processOrientation(mapper, rawOrientation);
8032 processDistance(mapper, rawDistance);
8033 processId(mapper, id);
8034 processMTSync(mapper);
8035 processSync(mapper);
8036
8037 NotifyMotionArgs args;
8038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8039 ASSERT_EQ(0, args.pointerProperties[0].id);
8040 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8041 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8042 orientation, distance));
8043}
8044
8045TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008047 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008048 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8049 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008050 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008051
8052 // These calculations are based on the input device calibration documentation.
8053 int32_t rawX = 100;
8054 int32_t rawY = 200;
8055 int32_t rawTouchMajor = 140;
8056 int32_t rawTouchMinor = 120;
8057 int32_t rawToolMajor = 180;
8058 int32_t rawToolMinor = 160;
8059
8060 float x = toDisplayX(rawX);
8061 float y = toDisplayY(rawY);
8062 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8063 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8064 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8065 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8066 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8067
8068 processPosition(mapper, rawX, rawY);
8069 processTouchMajor(mapper, rawTouchMajor);
8070 processTouchMinor(mapper, rawTouchMinor);
8071 processToolMajor(mapper, rawToolMajor);
8072 processToolMinor(mapper, rawToolMinor);
8073 processMTSync(mapper);
8074 processSync(mapper);
8075
8076 NotifyMotionArgs args;
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8078 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8079 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8080}
8081
8082TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008084 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085 prepareAxes(POSITION | TOUCH | TOOL);
8086 addConfigurationProperty("touch.size.calibration", "diameter");
8087 addConfigurationProperty("touch.size.scale", "10");
8088 addConfigurationProperty("touch.size.bias", "160");
8089 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008090 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008091
8092 // These calculations are based on the input device calibration documentation.
8093 // Note: We only provide a single common touch/tool value because the device is assumed
8094 // not to emit separate values for each pointer (isSummed = 1).
8095 int32_t rawX = 100;
8096 int32_t rawY = 200;
8097 int32_t rawX2 = 150;
8098 int32_t rawY2 = 250;
8099 int32_t rawTouchMajor = 5;
8100 int32_t rawToolMajor = 8;
8101
8102 float x = toDisplayX(rawX);
8103 float y = toDisplayY(rawY);
8104 float x2 = toDisplayX(rawX2);
8105 float y2 = toDisplayY(rawY2);
8106 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8107 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8108 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8109
8110 processPosition(mapper, rawX, rawY);
8111 processTouchMajor(mapper, rawTouchMajor);
8112 processToolMajor(mapper, rawToolMajor);
8113 processMTSync(mapper);
8114 processPosition(mapper, rawX2, rawY2);
8115 processTouchMajor(mapper, rawTouchMajor);
8116 processToolMajor(mapper, rawToolMajor);
8117 processMTSync(mapper);
8118 processSync(mapper);
8119
8120 NotifyMotionArgs args;
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8123
8124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008125 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(size_t(2), args.pointerCount);
8127 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8128 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8129 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8130 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8131}
8132
8133TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008135 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 prepareAxes(POSITION | TOUCH | TOOL);
8137 addConfigurationProperty("touch.size.calibration", "area");
8138 addConfigurationProperty("touch.size.scale", "43");
8139 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141
8142 // These calculations are based on the input device calibration documentation.
8143 int32_t rawX = 100;
8144 int32_t rawY = 200;
8145 int32_t rawTouchMajor = 5;
8146 int32_t rawToolMajor = 8;
8147
8148 float x = toDisplayX(rawX);
8149 float y = toDisplayY(rawY);
8150 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8151 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8152 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8153
8154 processPosition(mapper, rawX, rawY);
8155 processTouchMajor(mapper, rawTouchMajor);
8156 processToolMajor(mapper, rawToolMajor);
8157 processMTSync(mapper);
8158 processSync(mapper);
8159
8160 NotifyMotionArgs args;
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8163 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8164}
8165
8166TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008168 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 prepareAxes(POSITION | PRESSURE);
8170 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8171 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008173
Michael Wrightaa449c92017-12-13 21:21:43 +00008174 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008175 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008176 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8177 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8178 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8179
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 // These calculations are based on the input device calibration documentation.
8181 int32_t rawX = 100;
8182 int32_t rawY = 200;
8183 int32_t rawPressure = 60;
8184
8185 float x = toDisplayX(rawX);
8186 float y = toDisplayY(rawY);
8187 float pressure = float(rawPressure) * 0.01f;
8188
8189 processPosition(mapper, rawX, rawY);
8190 processPressure(mapper, rawPressure);
8191 processMTSync(mapper);
8192 processSync(mapper);
8193
8194 NotifyMotionArgs args;
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8196 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8197 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8198}
8199
8200TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008201 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008202 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008204 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008205
8206 NotifyMotionArgs motionArgs;
8207 NotifyKeyArgs keyArgs;
8208
8209 processId(mapper, 1);
8210 processPosition(mapper, 100, 200);
8211 processSync(mapper);
8212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8213 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8214 ASSERT_EQ(0, motionArgs.buttonState);
8215
8216 // press BTN_LEFT, release BTN_LEFT
8217 processKey(mapper, BTN_LEFT, 1);
8218 processSync(mapper);
8219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8221 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8222
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8224 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8225 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8226
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227 processKey(mapper, BTN_LEFT, 0);
8228 processSync(mapper);
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008231 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008232
8233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008235 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236
8237 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8238 processKey(mapper, BTN_RIGHT, 1);
8239 processKey(mapper, BTN_MIDDLE, 1);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8243 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8244 motionArgs.buttonState);
8245
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8248 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8249
8250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8251 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8252 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8253 motionArgs.buttonState);
8254
Michael Wrightd02c5b62014-02-10 15:10:22 -08008255 processKey(mapper, BTN_RIGHT, 0);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008258 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008260
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264
8265 processKey(mapper, BTN_MIDDLE, 0);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008270
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008273 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274
8275 // press BTN_BACK, release BTN_BACK
8276 processKey(mapper, BTN_BACK, 1);
8277 processSync(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8279 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8280 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008281
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8285
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8287 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8288 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008289
8290 processKey(mapper, BTN_BACK, 0);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008294 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008295
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008298 ASSERT_EQ(0, motionArgs.buttonState);
8299
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8301 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8302 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8303
8304 // press BTN_SIDE, release BTN_SIDE
8305 processKey(mapper, BTN_SIDE, 1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8309 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008310
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008313 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8314
8315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8316 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8317 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008318
8319 processKey(mapper, BTN_SIDE, 0);
8320 processSync(mapper);
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008323 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008324
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008327 ASSERT_EQ(0, motionArgs.buttonState);
8328
Michael Wrightd02c5b62014-02-10 15:10:22 -08008329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8330 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8331 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8332
8333 // press BTN_FORWARD, release BTN_FORWARD
8334 processKey(mapper, BTN_FORWARD, 1);
8335 processSync(mapper);
8336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8338 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008339
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008342 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8343
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8345 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8346 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008347
8348 processKey(mapper, BTN_FORWARD, 0);
8349 processSync(mapper);
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008351 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008352 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008353
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008356 ASSERT_EQ(0, motionArgs.buttonState);
8357
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8359 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8361
8362 // press BTN_EXTRA, release BTN_EXTRA
8363 processKey(mapper, BTN_EXTRA, 1);
8364 processSync(mapper);
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8366 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8367 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008368
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8372
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8374 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8375 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376
8377 processKey(mapper, BTN_EXTRA, 0);
8378 processSync(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008380 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008382
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008385 ASSERT_EQ(0, motionArgs.buttonState);
8386
Michael Wrightd02c5b62014-02-10 15:10:22 -08008387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8388 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8389 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8390
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8392
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 // press BTN_STYLUS, release BTN_STYLUS
8394 processKey(mapper, BTN_STYLUS, 1);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008398 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8399
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8401 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8402 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008403
8404 processKey(mapper, BTN_STYLUS, 0);
8405 processSync(mapper);
8406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008407 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008409
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008412 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 // press BTN_STYLUS2, release BTN_STYLUS2
8415 processKey(mapper, BTN_STYLUS2, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008419 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8420
8421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8422 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8423 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424
8425 processKey(mapper, BTN_STYLUS2, 0);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008428 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008429 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008430
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008433 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434
8435 // release touch
8436 processId(mapper, -1);
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8440 ASSERT_EQ(0, motionArgs.buttonState);
8441}
8442
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008443TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8444 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008445 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008446 prepareAxes(POSITION | ID | SLOT);
8447 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8448
8449 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8450 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8451
8452 // Touch down.
8453 processId(mapper, 1);
8454 processPosition(mapper, 100, 200);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8457 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8458
8459 // Press and release button mapped to the primary stylus button.
8460 processKey(mapper, BTN_A, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8463 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8464 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8467 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8468
8469 processKey(mapper, BTN_A, 0);
8470 processSync(mapper);
8471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8472 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8474 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8475
8476 // Press and release the HID usage mapped to the secondary stylus button.
8477 processHidUsage(mapper, 0xabcd, 1);
8478 processSync(mapper);
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8480 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8481 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8483 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8484 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8485
8486 processHidUsage(mapper, 0xabcd, 0);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8489 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8492
8493 // Release touch.
8494 processId(mapper, -1);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8497 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8498}
8499
Michael Wrightd02c5b62014-02-10 15:10:22 -08008500TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008502 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008504 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505
8506 NotifyMotionArgs motionArgs;
8507
8508 // default tool type is finger
8509 processId(mapper, 1);
8510 processPosition(mapper, 100, 200);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8515
8516 // eraser
8517 processKey(mapper, BTN_TOOL_RUBBER, 1);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8522
8523 // stylus
8524 processKey(mapper, BTN_TOOL_RUBBER, 0);
8525 processKey(mapper, BTN_TOOL_PEN, 1);
8526 processSync(mapper);
8527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8530
8531 // brush
8532 processKey(mapper, BTN_TOOL_PEN, 0);
8533 processKey(mapper, BTN_TOOL_BRUSH, 1);
8534 processSync(mapper);
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8537 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8538
8539 // pencil
8540 processKey(mapper, BTN_TOOL_BRUSH, 0);
8541 processKey(mapper, BTN_TOOL_PENCIL, 1);
8542 processSync(mapper);
8543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8544 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8546
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008547 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008548 processKey(mapper, BTN_TOOL_PENCIL, 0);
8549 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8550 processSync(mapper);
8551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8553 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8554
8555 // mouse
8556 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8557 processKey(mapper, BTN_TOOL_MOUSE, 1);
8558 processSync(mapper);
8559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8562
8563 // lens
8564 processKey(mapper, BTN_TOOL_MOUSE, 0);
8565 processKey(mapper, BTN_TOOL_LENS, 1);
8566 processSync(mapper);
8567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8569 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8570
8571 // double-tap
8572 processKey(mapper, BTN_TOOL_LENS, 0);
8573 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8576 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8577 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8578
8579 // triple-tap
8580 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8581 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8582 processSync(mapper);
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8585 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8586
8587 // quad-tap
8588 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8589 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8590 processSync(mapper);
8591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8593 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8594
8595 // finger
8596 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8597 processKey(mapper, BTN_TOOL_FINGER, 1);
8598 processSync(mapper);
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8601 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8602
8603 // stylus trumps finger
8604 processKey(mapper, BTN_TOOL_PEN, 1);
8605 processSync(mapper);
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8609
8610 // eraser trumps stylus
8611 processKey(mapper, BTN_TOOL_RUBBER, 1);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8616
8617 // mouse trumps eraser
8618 processKey(mapper, BTN_TOOL_MOUSE, 1);
8619 processSync(mapper);
8620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8623
8624 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8625 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8626 processSync(mapper);
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8629 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8630
8631 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8632 processToolType(mapper, MT_TOOL_PEN);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8637
8638 // back to default tool type
8639 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8640 processKey(mapper, BTN_TOOL_MOUSE, 0);
8641 processKey(mapper, BTN_TOOL_RUBBER, 0);
8642 processKey(mapper, BTN_TOOL_PEN, 0);
8643 processKey(mapper, BTN_TOOL_FINGER, 0);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8647 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8648}
8649
8650TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008651 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008652 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008653 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008654 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008655 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656
8657 NotifyMotionArgs motionArgs;
8658
8659 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8660 processId(mapper, 1);
8661 processPosition(mapper, 100, 200);
8662 processSync(mapper);
8663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8664 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8666 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8667
8668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8670 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8671 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8672
8673 // move a little
8674 processPosition(mapper, 150, 250);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8679 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8680
8681 // down when BTN_TOUCH is pressed, pressure defaults to 1
8682 processKey(mapper, BTN_TOUCH, 1);
8683 processSync(mapper);
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8685 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8686 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8687 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8688
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8692 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8693
8694 // up when BTN_TOUCH is released, hover restored
8695 processKey(mapper, BTN_TOUCH, 0);
8696 processSync(mapper);
8697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8700 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8701
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8703 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8705 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8706
8707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8708 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8710 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8711
8712 // exit hover when pointer goes away
8713 processId(mapper, -1);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8718 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8719}
8720
8721TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008723 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008725 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008726
8727 NotifyMotionArgs motionArgs;
8728
8729 // initially hovering because pressure is 0
8730 processId(mapper, 1);
8731 processPosition(mapper, 100, 200);
8732 processPressure(mapper, 0);
8733 processSync(mapper);
8734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8735 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8736 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8737 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8738
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8742 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8743
8744 // move a little
8745 processPosition(mapper, 150, 250);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8750 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8751
8752 // down when pressure becomes non-zero
8753 processPressure(mapper, RAW_PRESSURE_MAX);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8758 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8759
8760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8761 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8763 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8764
8765 // up when pressure becomes 0, hover restored
8766 processPressure(mapper, 0);
8767 processSync(mapper);
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8771 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8772
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8774 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8775 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8776 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8777
8778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8779 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8781 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8782
8783 // exit hover when pointer goes away
8784 processId(mapper, -1);
8785 processSync(mapper);
8786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8787 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8788 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8789 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8790}
8791
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008792/**
8793 * Set the input device port <--> display port associations, and check that the
8794 * events are routed to the display that matches the display port.
8795 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8796 */
8797TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008798 const std::string usb2 = "USB2";
8799 const uint8_t hdmi1 = 0;
8800 const uint8_t hdmi2 = 1;
8801 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008802 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008803
8804 addConfigurationProperty("touch.deviceType", "touchScreen");
8805 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008806 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008807
8808 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8809 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8810
8811 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8812 // for this input device is specified, and the matching viewport is not present,
8813 // the input device should be disabled (at the mapper level).
8814
8815 // Add viewport for display 2 on hdmi2
8816 prepareSecondaryDisplay(type, hdmi2);
8817 // Send a touch event
8818 processPosition(mapper, 100, 100);
8819 processSync(mapper);
8820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8821
8822 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008823 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008824 // Send a touch event again
8825 processPosition(mapper, 100, 100);
8826 processSync(mapper);
8827
8828 NotifyMotionArgs args;
8829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8830 ASSERT_EQ(DISPLAY_ID, args.displayId);
8831}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008832
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008833TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8834 addConfigurationProperty("touch.deviceType", "touchScreen");
8835 prepareAxes(POSITION);
8836 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8837
8838 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8839
Michael Wrighta9cf4192022-12-01 23:46:39 +00008840 prepareDisplay(ui::ROTATION_0);
8841 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008842
8843 // Send a touch event
8844 processPosition(mapper, 100, 100);
8845 processSync(mapper);
8846
8847 NotifyMotionArgs args;
8848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8849 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8850}
8851
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008852TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008853 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008854 std::shared_ptr<FakePointerController> fakePointerController =
8855 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008856 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008857 fakePointerController->setPosition(100, 200);
8858 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008859 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008860
Garfield Tan888a6a42020-01-09 11:39:16 -08008861 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008862 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008863
Michael Wrighta9cf4192022-12-01 23:46:39 +00008864 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008865 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008866 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008867
Josep del Río2d8c79a2023-01-23 19:33:50 +00008868 // Check source is mouse that would obtain the PointerController.
8869 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008870
8871 NotifyMotionArgs motionArgs;
8872 processPosition(mapper, 100, 100);
8873 processSync(mapper);
8874
8875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8877 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8878}
8879
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008880/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008881 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8882 */
8883TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8884 addConfigurationProperty("touch.deviceType", "touchScreen");
8885 prepareAxes(POSITION);
8886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8887
Michael Wrighta9cf4192022-12-01 23:46:39 +00008888 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008889 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8890 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8891 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8892 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008893
8894 NotifyMotionArgs args;
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8896 ASSERT_EQ(26, args.readTime);
8897
Harry Cutts33476232023-01-30 19:57:29 +00008898 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8899 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8900 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008901
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8903 ASSERT_EQ(33, args.readTime);
8904}
8905
8906/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008907 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8908 * events should not be delivered to the listener.
8909 */
8910TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8911 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008912 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008913 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008914 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008915 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8916 prepareAxes(POSITION);
8917 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8918
8919 NotifyMotionArgs motionArgs;
8920 processPosition(mapper, 100, 100);
8921 processSync(mapper);
8922
8923 mFakeListener->assertNotifyMotionWasNotCalled();
8924}
8925
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008926/**
8927 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8928 * the touch mapper can process the events and the events can be delivered to the listener.
8929 */
8930TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8931 addConfigurationProperty("touch.deviceType", "touchScreen");
8932 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008933 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008934 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008935 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8936 prepareAxes(POSITION);
8937 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8938
8939 NotifyMotionArgs motionArgs;
8940 processPosition(mapper, 100, 100);
8941 processSync(mapper);
8942
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8945}
8946
Garfield Tanc734e4f2021-01-15 20:01:39 -08008947TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8948 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008949 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008950 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008951 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008952 std::optional<DisplayViewport> optionalDisplayViewport =
8953 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8954 ASSERT_TRUE(optionalDisplayViewport.has_value());
8955 DisplayViewport displayViewport = *optionalDisplayViewport;
8956
8957 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8958 prepareAxes(POSITION);
8959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8960
8961 // Finger down
8962 int32_t x = 100, y = 100;
8963 processPosition(mapper, x, y);
8964 processSync(mapper);
8965
8966 NotifyMotionArgs motionArgs;
8967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8968 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8969
8970 // Deactivate display viewport
8971 displayViewport.isActive = false;
8972 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8973 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8974
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008975 // The ongoing touch should be canceled immediately
8976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8977 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8978
8979 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008980 x += 10, y += 10;
8981 processPosition(mapper, x, y);
8982 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008984
8985 // Reactivate display viewport
8986 displayViewport.isActive = true;
8987 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8988 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8989
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008990 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008991 x += 10, y += 10;
8992 processPosition(mapper, x, y);
8993 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8995 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008996}
8997
Arthur Hung7c645402019-01-25 17:45:42 +08008998TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8999 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009000 prepareAxes(POSITION | ID | SLOT);
9001 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009003
9004 // Create the second touch screen device, and enable multi fingers.
9005 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009006 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009007 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009008 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009009 std::shared_ptr<InputDevice> device2 =
9010 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009011 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009012
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009013 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009014 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009015 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009016 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009017 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009018 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009019 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009020 /*flat=*/0, /*fuzz=*/0);
9021 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009022 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9023 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009024
9025 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009026 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009027 std::list<NotifyArgs> unused =
9028 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009029 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009030 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009031
9032 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009033 std::shared_ptr<FakePointerController> fakePointerController =
9034 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009035 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009036
9037 // Setup policy for associated displays and show touches.
9038 const uint8_t hdmi1 = 0;
9039 const uint8_t hdmi2 = 1;
9040 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9041 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9042 mFakePolicy->setShowTouches(true);
9043
9044 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009045 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009046 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009047
9048 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009049 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9050 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9051 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009052
9053 // Two fingers down at default display.
9054 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9055 processPosition(mapper, x1, y1);
9056 processId(mapper, 1);
9057 processSlot(mapper, 1);
9058 processPosition(mapper, x2, y2);
9059 processId(mapper, 2);
9060 processSync(mapper);
9061
9062 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9063 fakePointerController->getSpots().find(DISPLAY_ID);
9064 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9065 ASSERT_EQ(size_t(2), iter->second.size());
9066
9067 // Two fingers down at second display.
9068 processPosition(mapper2, x1, y1);
9069 processId(mapper2, 1);
9070 processSlot(mapper2, 1);
9071 processPosition(mapper2, x2, y2);
9072 processId(mapper2, 2);
9073 processSync(mapper2);
9074
9075 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9076 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9077 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009078
9079 // Disable the show touches configuration and ensure the spots are cleared.
9080 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009081 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9082 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009083
9084 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009085}
9086
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009087TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009088 prepareAxes(POSITION);
9089 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009090 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009091 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009092
9093 NotifyMotionArgs motionArgs;
9094 // Unrotated video frame
9095 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9096 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009097 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009098 processPosition(mapper, 100, 200);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9101 ASSERT_EQ(frames, motionArgs.videoFrames);
9102
9103 // Subsequent touch events should not have any videoframes
9104 // This is implemented separately in FakeEventHub,
9105 // but that should match the behaviour of TouchVideoDevice.
9106 processPosition(mapper, 200, 200);
9107 processSync(mapper);
9108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9109 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9110}
9111
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009112TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009113 prepareAxes(POSITION);
9114 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009115 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009116 // Unrotated video frame
9117 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9118 NotifyMotionArgs motionArgs;
9119
9120 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009121 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009122 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9123 clearViewports();
9124 prepareDisplay(orientation);
9125 std::vector<TouchVideoFrame> frames{frame};
9126 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9127 processPosition(mapper, 100, 200);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9130 ASSERT_EQ(frames, motionArgs.videoFrames);
9131 }
9132}
9133
9134TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9135 prepareAxes(POSITION);
9136 addConfigurationProperty("touch.deviceType", "touchScreen");
9137 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9138 // orientation-aware are affected by display rotation.
9139 addConfigurationProperty("touch.orientationAware", "0");
9140 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9141 // Unrotated video frame
9142 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9143 NotifyMotionArgs motionArgs;
9144
9145 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009146 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009147 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9148 clearViewports();
9149 prepareDisplay(orientation);
9150 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009151 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009152 processPosition(mapper, 100, 200);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009155 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9156 // compared to the display. This is so that when the window transform (which contains the
9157 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9158 // window's coordinate space.
9159 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009160 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009161
9162 // Release finger.
9163 processSync(mapper);
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009165 }
9166}
9167
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009168TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009169 prepareAxes(POSITION);
9170 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009171 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009172 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9173 // so mix these.
9174 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9175 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9176 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9177 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9178 NotifyMotionArgs motionArgs;
9179
Michael Wrighta9cf4192022-12-01 23:46:39 +00009180 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009181 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009182 processPosition(mapper, 100, 200);
9183 processSync(mapper);
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009185 ASSERT_EQ(frames, motionArgs.videoFrames);
9186}
9187
9188TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9189 prepareAxes(POSITION);
9190 addConfigurationProperty("touch.deviceType", "touchScreen");
9191 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9192 // orientation-aware are affected by display rotation.
9193 addConfigurationProperty("touch.orientationAware", "0");
9194 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9195 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9196 // so mix these.
9197 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9198 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9199 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9200 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9201 NotifyMotionArgs motionArgs;
9202
Michael Wrighta9cf4192022-12-01 23:46:39 +00009203 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009204 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9205 processPosition(mapper, 100, 200);
9206 processSync(mapper);
9207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9208 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9209 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9210 // compared to the display. This is so that when the window transform (which contains the
9211 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9212 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009213 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009214 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009215 ASSERT_EQ(frames, motionArgs.videoFrames);
9216}
9217
Arthur Hung9da14732019-09-02 16:16:58 +08009218/**
9219 * If we had defined port associations, but the viewport is not ready, the touch device would be
9220 * expected to be disabled, and it should be enabled after the viewport has found.
9221 */
9222TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009223 constexpr uint8_t hdmi2 = 1;
9224 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009225 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009226
9227 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9228
9229 addConfigurationProperty("touch.deviceType", "touchScreen");
9230 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009231 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009232
9233 ASSERT_EQ(mDevice->isEnabled(), false);
9234
9235 // Add display on hdmi2, the device should be enabled and can receive touch event.
9236 prepareSecondaryDisplay(type, hdmi2);
9237 ASSERT_EQ(mDevice->isEnabled(), true);
9238
9239 // Send a touch event.
9240 processPosition(mapper, 100, 100);
9241 processSync(mapper);
9242
9243 NotifyMotionArgs args;
9244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9245 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9246}
9247
Arthur Hung421eb1c2020-01-16 00:09:42 +08009248TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009249 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009250 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009251 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009252 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009253
9254 NotifyMotionArgs motionArgs;
9255
9256 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9257 // finger down
9258 processId(mapper, 1);
9259 processPosition(mapper, x1, y1);
9260 processSync(mapper);
9261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9262 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9264
9265 // finger move
9266 processId(mapper, 1);
9267 processPosition(mapper, x2, y2);
9268 processSync(mapper);
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9271 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9272
9273 // finger up.
9274 processId(mapper, -1);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9279
9280 // new finger down
9281 processId(mapper, 1);
9282 processPosition(mapper, x3, y3);
9283 processSync(mapper);
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9287}
9288
9289/**
arthurhungcc7f9802020-04-30 17:55:40 +08009290 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9291 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009292 */
arthurhungcc7f9802020-04-30 17:55:40 +08009293TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009295 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009296 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009297 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009298
9299 NotifyMotionArgs motionArgs;
9300
9301 // default tool type is finger
9302 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009303 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009304 processPosition(mapper, x1, y1);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9309
9310 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9311 processToolType(mapper, MT_TOOL_PALM);
9312 processSync(mapper);
9313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9314 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9315
9316 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009317 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009318 processPosition(mapper, x2, y2);
9319 processSync(mapper);
9320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9321
9322 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009323 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9326
9327 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009328 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009329 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009330 processPosition(mapper, x3, y3);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9334 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9335}
9336
arthurhungbf89a482020-04-17 17:37:55 +08009337/**
arthurhungcc7f9802020-04-30 17:55:40 +08009338 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9339 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009340 */
arthurhungcc7f9802020-04-30 17:55:40 +08009341TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009342 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009343 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009344 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9345 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9346
9347 NotifyMotionArgs motionArgs;
9348
9349 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009350 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9351 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009352 processPosition(mapper, x1, y1);
9353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9356 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9357
9358 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009359 processSlot(mapper, SECOND_SLOT);
9360 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009361 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009362 processSync(mapper);
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009364 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009365 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9366
9367 // If the tool type of the first finger changes to MT_TOOL_PALM,
9368 // we expect to receive ACTION_POINTER_UP with cancel flag.
9369 processSlot(mapper, FIRST_SLOT);
9370 processId(mapper, FIRST_TRACKING_ID);
9371 processToolType(mapper, MT_TOOL_PALM);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009374 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009375 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9376
9377 // The following MOVE events of second finger should be processed.
9378 processSlot(mapper, SECOND_SLOT);
9379 processId(mapper, SECOND_TRACKING_ID);
9380 processPosition(mapper, x2 + 1, y2 + 1);
9381 processSync(mapper);
9382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9383 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9384 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9385
9386 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9387 // it. Second finger receive move.
9388 processSlot(mapper, FIRST_SLOT);
9389 processId(mapper, INVALID_TRACKING_ID);
9390 processSync(mapper);
9391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9393 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9394
9395 // Second finger keeps moving.
9396 processSlot(mapper, SECOND_SLOT);
9397 processId(mapper, SECOND_TRACKING_ID);
9398 processPosition(mapper, x2 + 2, y2 + 2);
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9401 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9402 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9403
9404 // Second finger up.
9405 processId(mapper, INVALID_TRACKING_ID);
9406 processSync(mapper);
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9409 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9410}
9411
9412/**
9413 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9414 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9415 */
9416TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9417 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009418 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009419 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9420 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9421
9422 NotifyMotionArgs motionArgs;
9423
9424 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9425 // First finger down.
9426 processId(mapper, FIRST_TRACKING_ID);
9427 processPosition(mapper, x1, y1);
9428 processSync(mapper);
9429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9430 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9431 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9432
9433 // Second finger down.
9434 processSlot(mapper, SECOND_SLOT);
9435 processId(mapper, SECOND_TRACKING_ID);
9436 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009437 processSync(mapper);
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009439 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009440 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9441
arthurhungcc7f9802020-04-30 17:55:40 +08009442 // If the tool type of the first finger changes to MT_TOOL_PALM,
9443 // we expect to receive ACTION_POINTER_UP with cancel flag.
9444 processSlot(mapper, FIRST_SLOT);
9445 processId(mapper, FIRST_TRACKING_ID);
9446 processToolType(mapper, MT_TOOL_PALM);
9447 processSync(mapper);
9448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009449 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009450 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9451
9452 // Second finger keeps moving.
9453 processSlot(mapper, SECOND_SLOT);
9454 processId(mapper, SECOND_TRACKING_ID);
9455 processPosition(mapper, x2 + 1, y2 + 1);
9456 processSync(mapper);
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9459
9460 // second finger becomes palm, receive cancel due to only 1 finger is active.
9461 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009462 processToolType(mapper, MT_TOOL_PALM);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9465 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9466
arthurhungcc7f9802020-04-30 17:55:40 +08009467 // third finger down.
9468 processSlot(mapper, THIRD_SLOT);
9469 processId(mapper, THIRD_TRACKING_ID);
9470 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009471 processPosition(mapper, x3, y3);
9472 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9474 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9475 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009476 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9477
9478 // third finger move
9479 processId(mapper, THIRD_TRACKING_ID);
9480 processPosition(mapper, x3 + 1, y3 + 1);
9481 processSync(mapper);
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9483 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9484
9485 // first finger up, third finger receive move.
9486 processSlot(mapper, FIRST_SLOT);
9487 processId(mapper, INVALID_TRACKING_ID);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9491 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9492
9493 // second finger up, third finger receive move.
9494 processSlot(mapper, SECOND_SLOT);
9495 processId(mapper, INVALID_TRACKING_ID);
9496 processSync(mapper);
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9499 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9500
9501 // third finger up.
9502 processSlot(mapper, THIRD_SLOT);
9503 processId(mapper, INVALID_TRACKING_ID);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9507 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9508}
9509
9510/**
9511 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9512 * and the active finger could still be allowed to receive the events
9513 */
9514TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9515 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009516 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009517 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9518 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9519
9520 NotifyMotionArgs motionArgs;
9521
9522 // default tool type is finger
9523 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9524 processId(mapper, FIRST_TRACKING_ID);
9525 processPosition(mapper, x1, y1);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9529 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9530
9531 // Second finger down.
9532 processSlot(mapper, SECOND_SLOT);
9533 processId(mapper, SECOND_TRACKING_ID);
9534 processPosition(mapper, x2, y2);
9535 processSync(mapper);
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009537 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009538 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9539
9540 // If the tool type of the second finger changes to MT_TOOL_PALM,
9541 // we expect to receive ACTION_POINTER_UP with cancel flag.
9542 processId(mapper, SECOND_TRACKING_ID);
9543 processToolType(mapper, MT_TOOL_PALM);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009546 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009547 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9548
9549 // The following MOVE event should be processed.
9550 processSlot(mapper, FIRST_SLOT);
9551 processId(mapper, FIRST_TRACKING_ID);
9552 processPosition(mapper, x1 + 1, y1 + 1);
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9555 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9556 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9557
9558 // second finger up.
9559 processSlot(mapper, SECOND_SLOT);
9560 processId(mapper, INVALID_TRACKING_ID);
9561 processSync(mapper);
9562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9564
9565 // first finger keep moving
9566 processSlot(mapper, FIRST_SLOT);
9567 processId(mapper, FIRST_TRACKING_ID);
9568 processPosition(mapper, x1 + 2, y1 + 2);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9571 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9572
9573 // first finger up.
9574 processId(mapper, INVALID_TRACKING_ID);
9575 processSync(mapper);
9576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9577 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9578 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009579}
9580
Arthur Hung9ad18942021-06-19 02:04:46 +00009581/**
9582 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9583 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9584 * cause slot be valid again.
9585 */
9586TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9587 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009588 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009589 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9590 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9591
9592 NotifyMotionArgs motionArgs;
9593
9594 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9595 // First finger down.
9596 processId(mapper, FIRST_TRACKING_ID);
9597 processPosition(mapper, x1, y1);
9598 processPressure(mapper, RAW_PRESSURE_MAX);
9599 processSync(mapper);
9600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9601 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9602 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9603
9604 // First finger move.
9605 processId(mapper, FIRST_TRACKING_ID);
9606 processPosition(mapper, x1 + 1, y1 + 1);
9607 processPressure(mapper, RAW_PRESSURE_MAX);
9608 processSync(mapper);
9609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9610 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9611 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9612
9613 // Second finger down.
9614 processSlot(mapper, SECOND_SLOT);
9615 processId(mapper, SECOND_TRACKING_ID);
9616 processPosition(mapper, x2, y2);
9617 processPressure(mapper, RAW_PRESSURE_MAX);
9618 processSync(mapper);
9619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009620 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009621 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9622
9623 // second finger up with some unexpected data.
9624 processSlot(mapper, SECOND_SLOT);
9625 processId(mapper, INVALID_TRACKING_ID);
9626 processPosition(mapper, x2, y2);
9627 processSync(mapper);
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009629 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009630 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9631
9632 // first finger up with some unexpected data.
9633 processSlot(mapper, FIRST_SLOT);
9634 processId(mapper, INVALID_TRACKING_ID);
9635 processPosition(mapper, x2, y2);
9636 processPressure(mapper, RAW_PRESSURE_MAX);
9637 processSync(mapper);
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9639 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9640 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9641}
9642
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009643TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9644 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009645 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009646 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9647 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9648
9649 // First finger down.
9650 processId(mapper, FIRST_TRACKING_ID);
9651 processPosition(mapper, 100, 200);
9652 processPressure(mapper, RAW_PRESSURE_MAX);
9653 processSync(mapper);
9654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9655 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9656
9657 // Second finger down.
9658 processSlot(mapper, SECOND_SLOT);
9659 processId(mapper, SECOND_TRACKING_ID);
9660 processPosition(mapper, 300, 400);
9661 processPressure(mapper, RAW_PRESSURE_MAX);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(
9664 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9665
9666 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009667 // preserved. Resetting should cancel the ongoing gesture.
9668 resetMapper(mapper, ARBITRARY_TIME);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9670 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009671
9672 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9673 // the existing touch state to generate a down event.
9674 processPosition(mapper, 301, 302);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9680
9681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9682}
9683
9684TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9685 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009686 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009687 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9688 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9689
9690 // First finger touches down and releases.
9691 processId(mapper, FIRST_TRACKING_ID);
9692 processPosition(mapper, 100, 200);
9693 processPressure(mapper, RAW_PRESSURE_MAX);
9694 processSync(mapper);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9696 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9697 processId(mapper, INVALID_TRACKING_ID);
9698 processSync(mapper);
9699 ASSERT_NO_FATAL_FAILURE(
9700 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9701
9702 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9703 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009704 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9706
9707 // Send an empty sync frame. Since there are no pointers, no events are generated.
9708 processSync(mapper);
9709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9710}
9711
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009712TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009713 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009714 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009715 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9716 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009718
9719 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9720 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9721 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9722 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9724
9725 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009726 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009727 processId(mapper, FIRST_TRACKING_ID);
9728 processToolType(mapper, MT_TOOL_PEN);
9729 processPosition(mapper, 100, 200);
9730 processPressure(mapper, RAW_PRESSURE_MAX);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9734 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9735 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9736
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009737 // Now that we know the device supports styluses, ensure that the device is re-configured with
9738 // the stylus source.
9739 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9740 {
9741 const auto& devices = mReader->getInputDevices();
9742 auto deviceInfo =
9743 std::find_if(devices.begin(), devices.end(),
9744 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9745 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9747 }
9748
9749 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9751
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009752 processId(mapper, INVALID_TRACKING_ID);
9753 processSync(mapper);
9754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9756 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9757 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009758}
9759
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009760// --- MultiTouchInputMapperTest_ExternalDevice ---
9761
9762class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9763protected:
Chris Yea52ade12020-08-27 16:49:20 -07009764 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009765};
9766
9767/**
9768 * Expect fallback to internal viewport if device is external and external viewport is not present.
9769 */
9770TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9771 prepareAxes(POSITION);
9772 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009773 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009774 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9775
9776 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9777
9778 NotifyMotionArgs motionArgs;
9779
9780 // Expect the event to be sent to the internal viewport,
9781 // because an external viewport is not present.
9782 processPosition(mapper, 100, 100);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9785 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9786
9787 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009788 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009789 processPosition(mapper, 100, 100);
9790 processSync(mapper);
9791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9792 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9793}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009794
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009795TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9796 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9797 std::shared_ptr<FakePointerController> fakePointerController =
9798 std::make_shared<FakePointerController>();
9799 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9800 fakePointerController->setPosition(0, 0);
9801 fakePointerController->setButtonState(0);
9802
9803 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009804 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009805 prepareAxes(POSITION | ID | SLOT);
9806 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9807 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9808 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009809 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009810 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9811
9812 // captured touchpad should be a touchpad source
9813 NotifyDeviceResetArgs resetArgs;
9814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9815 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9816
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009817 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009818
9819 const InputDeviceInfo::MotionRange* relRangeX =
9820 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9821 ASSERT_NE(relRangeX, nullptr);
9822 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9823 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9824 const InputDeviceInfo::MotionRange* relRangeY =
9825 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9826 ASSERT_NE(relRangeY, nullptr);
9827 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9828 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9829
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009830 // run captured pointer tests - note that this is unscaled, so input listener events should be
9831 // identical to what the hardware sends (accounting for any
9832 // calibration).
9833 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009834 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009835 processId(mapper, 1);
9836 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9837 processKey(mapper, BTN_TOUCH, 1);
9838 processSync(mapper);
9839
9840 // expect coord[0] to contain initial location of touch 0
9841 NotifyMotionArgs args;
9842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9843 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9844 ASSERT_EQ(1U, args.pointerCount);
9845 ASSERT_EQ(0, args.pointerProperties[0].id);
9846 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9847 ASSERT_NO_FATAL_FAILURE(
9848 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9849
9850 // FINGER 1 DOWN
9851 processSlot(mapper, 1);
9852 processId(mapper, 2);
9853 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9854 processSync(mapper);
9855
9856 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009858 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009859 ASSERT_EQ(2U, args.pointerCount);
9860 ASSERT_EQ(0, args.pointerProperties[0].id);
9861 ASSERT_EQ(1, args.pointerProperties[1].id);
9862 ASSERT_NO_FATAL_FAILURE(
9863 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9864 ASSERT_NO_FATAL_FAILURE(
9865 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9866
9867 // FINGER 1 MOVE
9868 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9869 processSync(mapper);
9870
9871 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9872 // from move
9873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9875 ASSERT_NO_FATAL_FAILURE(
9876 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9877 ASSERT_NO_FATAL_FAILURE(
9878 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9879
9880 // FINGER 0 MOVE
9881 processSlot(mapper, 0);
9882 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9883 processSync(mapper);
9884
9885 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9887 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9888 ASSERT_NO_FATAL_FAILURE(
9889 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9890 ASSERT_NO_FATAL_FAILURE(
9891 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9892
9893 // BUTTON DOWN
9894 processKey(mapper, BTN_LEFT, 1);
9895 processSync(mapper);
9896
9897 // touchinputmapper design sends a move before button press
9898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9902
9903 // BUTTON UP
9904 processKey(mapper, BTN_LEFT, 0);
9905 processSync(mapper);
9906
9907 // touchinputmapper design sends a move after button release
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9912
9913 // FINGER 0 UP
9914 processId(mapper, -1);
9915 processSync(mapper);
9916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9917 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9918
9919 // FINGER 1 MOVE
9920 processSlot(mapper, 1);
9921 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9922 processSync(mapper);
9923
9924 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9927 ASSERT_EQ(1U, args.pointerCount);
9928 ASSERT_EQ(1, args.pointerProperties[0].id);
9929 ASSERT_NO_FATAL_FAILURE(
9930 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9931
9932 // FINGER 1 UP
9933 processId(mapper, -1);
9934 processKey(mapper, BTN_TOUCH, 0);
9935 processSync(mapper);
9936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9937 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9938
Josep del Río2d8c79a2023-01-23 19:33:50 +00009939 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009940 mFakePolicy->setPointerCapture(false);
9941 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9942 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +00009943 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009944}
9945
9946TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9947 std::shared_ptr<FakePointerController> fakePointerController =
9948 std::make_shared<FakePointerController>();
9949 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9950 fakePointerController->setPosition(0, 0);
9951 fakePointerController->setButtonState(0);
9952
9953 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009954 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009955 prepareAxes(POSITION | ID | SLOT);
9956 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9957 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009958 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009959 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9960 // run uncaptured pointer tests - pushes out generic events
9961 // FINGER 0 DOWN
9962 processId(mapper, 3);
9963 processPosition(mapper, 100, 100);
9964 processKey(mapper, BTN_TOUCH, 1);
9965 processSync(mapper);
9966
9967 // start at (100,100), cursor should be at (0,0) * scale
9968 NotifyMotionArgs args;
9969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9971 ASSERT_NO_FATAL_FAILURE(
9972 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9973
9974 // FINGER 0 MOVE
9975 processPosition(mapper, 200, 200);
9976 processSync(mapper);
9977
9978 // compute scaling to help with touch position checking
9979 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9980 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9981 float scale =
9982 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9983
9984 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9988 0, 0, 0, 0, 0, 0, 0));
9989}
9990
9991TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9992 std::shared_ptr<FakePointerController> fakePointerController =
9993 std::make_shared<FakePointerController>();
9994
Michael Wrighta9cf4192022-12-01 23:46:39 +00009995 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009996 prepareAxes(POSITION | ID | SLOT);
9997 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009998 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009999 mFakePolicy->setPointerCapture(false);
10000 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10001
Josep del Río2d8c79a2023-01-23 19:33:50 +000010002 // uncaptured touchpad should be a pointer device
10003 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010004
Josep del Río2d8c79a2023-01-23 19:33:50 +000010005 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010006 mFakePolicy->setPointerCapture(true);
10007 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10008 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10009}
10010
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010011// --- BluetoothMultiTouchInputMapperTest ---
10012
10013class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10014protected:
10015 void SetUp() override {
10016 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10017 }
10018};
10019
10020TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10021 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010022 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010023 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10024 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10025
10026 nsecs_t kernelEventTime = ARBITRARY_TIME;
10027 nsecs_t expectedEventTime = ARBITRARY_TIME;
10028 // Touch down.
10029 processId(mapper, FIRST_TRACKING_ID);
10030 processPosition(mapper, 100, 200);
10031 processPressure(mapper, RAW_PRESSURE_MAX);
10032 processSync(mapper, ARBITRARY_TIME);
10033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10034 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10035
10036 // Process several events that come in quick succession, according to their timestamps.
10037 for (int i = 0; i < 3; i++) {
10038 constexpr static nsecs_t delta = ms2ns(1);
10039 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10040 kernelEventTime += delta;
10041 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10042
10043 processPosition(mapper, 101 + i, 201 + i);
10044 processSync(mapper, kernelEventTime);
10045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10047 WithEventTime(expectedEventTime))));
10048 }
10049
10050 // Release the touch.
10051 processId(mapper, INVALID_TRACKING_ID);
10052 processPressure(mapper, RAW_PRESSURE_MIN);
10053 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10055 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10056 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10057}
10058
10059// --- MultiTouchPointerModeTest ---
10060
HQ Liue6983c72022-04-19 22:14:56 +000010061class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10062protected:
10063 float mPointerMovementScale;
10064 float mPointerXZoomScale;
10065 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10066 addConfigurationProperty("touch.deviceType", "pointer");
10067 std::shared_ptr<FakePointerController> fakePointerController =
10068 std::make_shared<FakePointerController>();
10069 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10070 fakePointerController->setPosition(0, 0);
10071 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010072 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010073
10074 prepareAxes(POSITION);
10075 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10076 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10077 // needs to be disabled, and the pointer gesture needs to be enabled.
10078 mFakePolicy->setPointerCapture(false);
10079 mFakePolicy->setPointerGestureEnabled(true);
10080 mFakePolicy->setPointerController(fakePointerController);
10081
10082 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10083 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10084 mPointerMovementScale =
10085 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10086 mPointerXZoomScale =
10087 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10088 }
10089
10090 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10092 /*flat*/ 0,
10093 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10095 /*flat*/ 0,
10096 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10097 }
10098};
10099
10100/**
10101 * Two fingers down on a pointer mode touch pad. The width
10102 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10103 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10104 * be greater than the both value to be freeform gesture, so that after two
10105 * fingers start to move downwards, the gesture should be swipe.
10106 */
10107TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10108 // The min freeform gesture width is 25units/mm x 30mm = 750
10109 // which is greater than fraction of the diagnal length of the touchpad (349).
10110 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010111 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010112 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10113 NotifyMotionArgs motionArgs;
10114
10115 // Two fingers down at once.
10116 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10117 // Pointer's initial position is used the [0,0] coordinate.
10118 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10119
10120 processId(mapper, FIRST_TRACKING_ID);
10121 processPosition(mapper, x1, y1);
10122 processMTSync(mapper);
10123 processId(mapper, SECOND_TRACKING_ID);
10124 processPosition(mapper, x2, y2);
10125 processMTSync(mapper);
10126 processSync(mapper);
10127
10128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10129 ASSERT_EQ(1U, motionArgs.pointerCount);
10130 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10131 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010132 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010133 ASSERT_NO_FATAL_FAILURE(
10134 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10135
10136 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10137 // that there should be 1 pointer.
10138 int32_t movingDistance = 200;
10139 y1 += movingDistance;
10140 y2 += movingDistance;
10141
10142 processId(mapper, FIRST_TRACKING_ID);
10143 processPosition(mapper, x1, y1);
10144 processMTSync(mapper);
10145 processId(mapper, SECOND_TRACKING_ID);
10146 processPosition(mapper, x2, y2);
10147 processMTSync(mapper);
10148 processSync(mapper);
10149
10150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10151 ASSERT_EQ(1U, motionArgs.pointerCount);
10152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010154 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10156 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10157 0, 0, 0, 0));
10158}
10159
10160/**
10161 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10162 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10163 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10164 * value to be freeform gesture, so that after two fingers start to move downwards,
10165 * the gesture should be swipe.
10166 */
10167TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10168 // The min freeform gesture width is 5units/mm x 30mm = 150
10169 // which is greater than fraction of the diagnal length of the touchpad (349).
10170 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010171 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010172 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10173 NotifyMotionArgs motionArgs;
10174
10175 // Two fingers down at once.
10176 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10177 // Pointer's initial position is used the [0,0] coordinate.
10178 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10179
10180 processId(mapper, FIRST_TRACKING_ID);
10181 processPosition(mapper, x1, y1);
10182 processMTSync(mapper);
10183 processId(mapper, SECOND_TRACKING_ID);
10184 processPosition(mapper, x2, y2);
10185 processMTSync(mapper);
10186 processSync(mapper);
10187
10188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10189 ASSERT_EQ(1U, motionArgs.pointerCount);
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10191 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010192 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010193 ASSERT_NO_FATAL_FAILURE(
10194 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10195
10196 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10197 // and there should be 1 pointer.
10198 int32_t movingDistance = 200;
10199 y1 += movingDistance;
10200 y2 += movingDistance;
10201
10202 processId(mapper, FIRST_TRACKING_ID);
10203 processPosition(mapper, x1, y1);
10204 processMTSync(mapper);
10205 processId(mapper, SECOND_TRACKING_ID);
10206 processPosition(mapper, x2, y2);
10207 processMTSync(mapper);
10208 processSync(mapper);
10209
10210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10211 ASSERT_EQ(1U, motionArgs.pointerCount);
10212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010214 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010215 // New coordinate is the scaled relative coordinate from the initial coordinate.
10216 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10217 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10218 0, 0, 0, 0));
10219}
10220
10221/**
10222 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10223 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10224 * freeform gestures after two fingers start to move downwards.
10225 */
10226TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010227 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010228 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10229
10230 NotifyMotionArgs motionArgs;
10231
10232 // Two fingers down at once. Wider than the max swipe width.
10233 // The gesture is expected to be PRESS, then transformed to FREEFORM
10234 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10235
10236 processId(mapper, FIRST_TRACKING_ID);
10237 processPosition(mapper, x1, y1);
10238 processMTSync(mapper);
10239 processId(mapper, SECOND_TRACKING_ID);
10240 processPosition(mapper, x2, y2);
10241 processMTSync(mapper);
10242 processSync(mapper);
10243
10244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10245 ASSERT_EQ(1U, motionArgs.pointerCount);
10246 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10247 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010248 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010249 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10250 ASSERT_NO_FATAL_FAILURE(
10251 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10252
10253 int32_t movingDistance = 200;
10254
10255 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10256 // then two down events for two pointers.
10257 y1 += movingDistance;
10258 y2 += movingDistance;
10259
10260 processId(mapper, FIRST_TRACKING_ID);
10261 processPosition(mapper, x1, y1);
10262 processMTSync(mapper);
10263 processId(mapper, SECOND_TRACKING_ID);
10264 processPosition(mapper, x2, y2);
10265 processMTSync(mapper);
10266 processSync(mapper);
10267
10268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10269 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10270 ASSERT_EQ(1U, motionArgs.pointerCount);
10271 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10273 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10274 ASSERT_EQ(1U, motionArgs.pointerCount);
10275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010278 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010279 ASSERT_EQ(2U, motionArgs.pointerCount);
10280 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010282 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010283 // Two pointers' scaled relative coordinates from their initial centroid.
10284 // Initial y coordinates are 0 as y1 and y2 have the same value.
10285 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10286 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10287 // When pointers move, the new coordinates equal to the initial coordinates plus
10288 // scaled moving distance.
10289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10290 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10291 0, 0, 0, 0));
10292 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10293 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10294 0, 0, 0, 0));
10295
10296 // Move two fingers down again, expect one MOVE motion event.
10297 y1 += movingDistance;
10298 y2 += movingDistance;
10299
10300 processId(mapper, FIRST_TRACKING_ID);
10301 processPosition(mapper, x1, y1);
10302 processMTSync(mapper);
10303 processId(mapper, SECOND_TRACKING_ID);
10304 processPosition(mapper, x2, y2);
10305 processMTSync(mapper);
10306 processSync(mapper);
10307
10308 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10309 ASSERT_EQ(2U, motionArgs.pointerCount);
10310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010312 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10314 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10315 0, 0, 0, 0, 0));
10316 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10317 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10318 0, 0, 0, 0, 0));
10319}
10320
Harry Cutts39b7ca22022-10-05 15:55:48 +000010321TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010322 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010323 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10324 NotifyMotionArgs motionArgs;
10325
10326 // Place two fingers down.
10327 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10328
10329 processId(mapper, FIRST_TRACKING_ID);
10330 processPosition(mapper, x1, y1);
10331 processMTSync(mapper);
10332 processId(mapper, SECOND_TRACKING_ID);
10333 processPosition(mapper, x2, y2);
10334 processMTSync(mapper);
10335 processSync(mapper);
10336
10337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10338 ASSERT_EQ(1U, motionArgs.pointerCount);
10339 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10340 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10341 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10342 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10343
10344 // Move the two fingers down and to the left.
10345 int32_t movingDistance = 200;
10346 x1 -= movingDistance;
10347 y1 += movingDistance;
10348 x2 -= movingDistance;
10349 y2 += movingDistance;
10350
10351 processId(mapper, FIRST_TRACKING_ID);
10352 processPosition(mapper, x1, y1);
10353 processMTSync(mapper);
10354 processId(mapper, SECOND_TRACKING_ID);
10355 processPosition(mapper, x2, y2);
10356 processMTSync(mapper);
10357 processSync(mapper);
10358
10359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10360 ASSERT_EQ(1U, motionArgs.pointerCount);
10361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10362 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10363 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10364 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10365}
10366
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010367TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010368 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010369 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10370 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10372
10373 // Start a stylus gesture.
10374 processKey(mapper, BTN_TOOL_PEN, 1);
10375 processId(mapper, FIRST_TRACKING_ID);
10376 processPosition(mapper, 100, 200);
10377 processSync(mapper);
10378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10379 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10380 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10381 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10382 // TODO(b/257078296): Pointer mode generates extra event.
10383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10384 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10385 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10386 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10388
10389 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10390 // gesture should be disabled.
10391 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10392 viewport->isActive = false;
10393 mFakePolicy->updateViewport(*viewport);
10394 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10396 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10397 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10398 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10399 // TODO(b/257078296): Pointer mode generates extra event.
10400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10401 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10402 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10403 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10405}
10406
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010407// --- JoystickInputMapperTest ---
10408
10409class JoystickInputMapperTest : public InputMapperTest {
10410protected:
10411 static const int32_t RAW_X_MIN;
10412 static const int32_t RAW_X_MAX;
10413 static const int32_t RAW_Y_MIN;
10414 static const int32_t RAW_Y_MAX;
10415
10416 void SetUp() override {
10417 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10418 }
10419 void prepareAxes() {
10420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10421 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10422 }
10423
10424 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10425 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10426 }
10427
10428 void processSync(JoystickInputMapper& mapper) {
10429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10430 }
10431
Michael Wrighta9cf4192022-12-01 23:46:39 +000010432 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010433 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10434 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10435 NO_PORT, ViewportType::VIRTUAL);
10436 }
10437};
10438
10439const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10440const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10441const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10442const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10443
10444TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10445 prepareAxes();
10446 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10447
10448 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10449
Michael Wrighta9cf4192022-12-01 23:46:39 +000010450 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010451
10452 // Send an axis event
10453 processAxis(mapper, ABS_X, 100);
10454 processSync(mapper);
10455
10456 NotifyMotionArgs args;
10457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10458 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10459
10460 // Send another axis event
10461 processAxis(mapper, ABS_Y, 100);
10462 processSync(mapper);
10463
10464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10465 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10466}
10467
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010468// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010469
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010470class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010471protected:
10472 static const char* DEVICE_NAME;
10473 static const char* DEVICE_LOCATION;
10474 static const int32_t DEVICE_ID;
10475 static const int32_t DEVICE_GENERATION;
10476 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010477 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010478 static const int32_t EVENTHUB_ID;
10479
10480 std::shared_ptr<FakeEventHub> mFakeEventHub;
10481 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010482 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010483 std::unique_ptr<InstrumentedInputReader> mReader;
10484 std::shared_ptr<InputDevice> mDevice;
10485
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010486 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010487 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010488 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010489 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010490 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010491 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010492 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10493 }
10494
10495 void SetUp() override { SetUp(DEVICE_CLASSES); }
10496
10497 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010498 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010499 mFakePolicy.clear();
10500 }
10501
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010502 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010503 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10504 mReader->requestRefreshConfiguration(changes);
10505 mReader->loopOnce();
10506 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010507 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010508 }
10509
10510 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10511 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010512 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010513 InputDeviceIdentifier identifier;
10514 identifier.name = name;
10515 identifier.location = location;
10516 std::shared_ptr<InputDevice> device =
10517 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10518 identifier);
10519 mReader->pushNextDevice(device);
10520 mFakeEventHub->addDevice(eventHubId, name, classes);
10521 mReader->loopOnce();
10522 return device;
10523 }
10524
10525 template <class T, typename... Args>
10526 T& addControllerAndConfigure(Args... args) {
10527 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10528
10529 return controller;
10530 }
10531};
10532
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010533const char* PeripheralControllerTest::DEVICE_NAME = "device";
10534const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10535const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10536const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10537const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010538const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10539 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010540const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010541
10542// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010543class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010544protected:
10545 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010546 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010547 }
10548};
10549
10550TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010551 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010552
Harry Cuttsa5b71292022-11-28 12:56:17 +000010553 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10554 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10555 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556}
10557
10558TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010559 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010560
Harry Cuttsa5b71292022-11-28 12:56:17 +000010561 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10562 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10563 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010564}
10565
10566// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010567class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010568protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010569 void SetUp() override {
10570 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10571 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010572};
10573
Chris Ye85758332021-05-16 23:05:17 -070010574TEST_F(LightControllerTest, MonoLight) {
10575 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010576 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010577 .maxBrightness = 255,
10578 .flags = InputLightClass::BRIGHTNESS,
10579 .path = ""};
10580 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010581
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010582 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010583 InputDeviceInfo info;
10584 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010585 std::vector<InputDeviceLightInfo> lights = info.getLights();
10586 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010587 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10588 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10589
10590 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10591 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10592}
10593
10594TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10595 RawLightInfo infoMono = {.id = 1,
10596 .name = "mono_keyboard_backlight",
10597 .maxBrightness = 255,
10598 .flags = InputLightClass::BRIGHTNESS |
10599 InputLightClass::KEYBOARD_BACKLIGHT,
10600 .path = ""};
10601 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10602
10603 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10604 InputDeviceInfo info;
10605 controller.populateDeviceInfo(&info);
10606 std::vector<InputDeviceLightInfo> lights = info.getLights();
10607 ASSERT_EQ(1U, lights.size());
10608 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10609 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010610
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010611 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10612 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010613}
10614
10615TEST_F(LightControllerTest, RGBLight) {
10616 RawLightInfo infoRed = {.id = 1,
10617 .name = "red",
10618 .maxBrightness = 255,
10619 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10620 .path = ""};
10621 RawLightInfo infoGreen = {.id = 2,
10622 .name = "green",
10623 .maxBrightness = 255,
10624 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10625 .path = ""};
10626 RawLightInfo infoBlue = {.id = 3,
10627 .name = "blue",
10628 .maxBrightness = 255,
10629 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10630 .path = ""};
10631 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10632 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10633 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10634
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010635 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010636 InputDeviceInfo info;
10637 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010638 std::vector<InputDeviceLightInfo> lights = info.getLights();
10639 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010640 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10641 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10642 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10643
10644 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10645 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10646}
10647
10648TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10649 RawLightInfo infoRed = {.id = 1,
10650 .name = "red_keyboard_backlight",
10651 .maxBrightness = 255,
10652 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10653 InputLightClass::KEYBOARD_BACKLIGHT,
10654 .path = ""};
10655 RawLightInfo infoGreen = {.id = 2,
10656 .name = "green_keyboard_backlight",
10657 .maxBrightness = 255,
10658 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10659 InputLightClass::KEYBOARD_BACKLIGHT,
10660 .path = ""};
10661 RawLightInfo infoBlue = {.id = 3,
10662 .name = "blue_keyboard_backlight",
10663 .maxBrightness = 255,
10664 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10665 InputLightClass::KEYBOARD_BACKLIGHT,
10666 .path = ""};
10667 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10668 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10669 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10670
10671 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10672 InputDeviceInfo info;
10673 controller.populateDeviceInfo(&info);
10674 std::vector<InputDeviceLightInfo> lights = info.getLights();
10675 ASSERT_EQ(1U, lights.size());
10676 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10677 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10678 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10679
10680 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10681 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10682}
10683
10684TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10685 RawLightInfo infoRed = {.id = 1,
10686 .name = "red",
10687 .maxBrightness = 255,
10688 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10689 .path = ""};
10690 RawLightInfo infoGreen = {.id = 2,
10691 .name = "green",
10692 .maxBrightness = 255,
10693 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10694 .path = ""};
10695 RawLightInfo infoBlue = {.id = 3,
10696 .name = "blue",
10697 .maxBrightness = 255,
10698 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10699 .path = ""};
10700 RawLightInfo infoGlobal = {.id = 3,
10701 .name = "global_keyboard_backlight",
10702 .maxBrightness = 255,
10703 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10704 InputLightClass::KEYBOARD_BACKLIGHT,
10705 .path = ""};
10706 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10707 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10708 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10709 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10710
10711 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10712 InputDeviceInfo info;
10713 controller.populateDeviceInfo(&info);
10714 std::vector<InputDeviceLightInfo> lights = info.getLights();
10715 ASSERT_EQ(1U, lights.size());
10716 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10717 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10718 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010719
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010720 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10721 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010722}
10723
10724TEST_F(LightControllerTest, MultiColorRGBLight) {
10725 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010726 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010727 .maxBrightness = 255,
10728 .flags = InputLightClass::BRIGHTNESS |
10729 InputLightClass::MULTI_INTENSITY |
10730 InputLightClass::MULTI_INDEX,
10731 .path = ""};
10732
10733 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10734
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010735 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010736 InputDeviceInfo info;
10737 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010738 std::vector<InputDeviceLightInfo> lights = info.getLights();
10739 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010740 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10741 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10742 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10743
10744 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10745 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10746}
10747
10748TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10749 RawLightInfo infoColor = {.id = 1,
10750 .name = "multi_color_keyboard_backlight",
10751 .maxBrightness = 255,
10752 .flags = InputLightClass::BRIGHTNESS |
10753 InputLightClass::MULTI_INTENSITY |
10754 InputLightClass::MULTI_INDEX |
10755 InputLightClass::KEYBOARD_BACKLIGHT,
10756 .path = ""};
10757
10758 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10759
10760 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10761 InputDeviceInfo info;
10762 controller.populateDeviceInfo(&info);
10763 std::vector<InputDeviceLightInfo> lights = info.getLights();
10764 ASSERT_EQ(1U, lights.size());
10765 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10766 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10767 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010768
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010769 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10770 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010771}
10772
10773TEST_F(LightControllerTest, PlayerIdLight) {
10774 RawLightInfo info1 = {.id = 1,
10775 .name = "player1",
10776 .maxBrightness = 255,
10777 .flags = InputLightClass::BRIGHTNESS,
10778 .path = ""};
10779 RawLightInfo info2 = {.id = 2,
10780 .name = "player2",
10781 .maxBrightness = 255,
10782 .flags = InputLightClass::BRIGHTNESS,
10783 .path = ""};
10784 RawLightInfo info3 = {.id = 3,
10785 .name = "player3",
10786 .maxBrightness = 255,
10787 .flags = InputLightClass::BRIGHTNESS,
10788 .path = ""};
10789 RawLightInfo info4 = {.id = 4,
10790 .name = "player4",
10791 .maxBrightness = 255,
10792 .flags = InputLightClass::BRIGHTNESS,
10793 .path = ""};
10794 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10795 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10796 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10797 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10798
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010799 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010800 InputDeviceInfo info;
10801 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010802 std::vector<InputDeviceLightInfo> lights = info.getLights();
10803 ASSERT_EQ(1U, lights.size());
10804 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010805 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10806 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010807
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010808 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10809 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10810 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010811}
10812
Michael Wrightd02c5b62014-02-10 15:10:22 -080010813} // namespace android