blob: 989269726e5b8c7ad18677d0b96032def0273672 [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
Seunghwan Choi356026c2023-02-01 14:37:25 +09006681TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6682 std::shared_ptr<FakePointerController> fakePointerController =
6683 std::make_shared<FakePointerController>();
6684 addConfigurationProperty("touch.deviceType", "touchScreen");
6685 prepareDisplay(ui::ROTATION_0);
6686 prepareButtons();
6687 prepareAxes(POSITION);
6688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6689 mFakePolicy->setPointerController(fakePointerController);
6690 mFakePolicy->setStylusPointerIconEnabled(true);
6691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6692
6693 processKey(mapper, BTN_TOOL_PEN, 1);
6694 processMove(mapper, 100, 200);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6698 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6699 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6700 ASSERT_TRUE(fakePointerController->isPointerShown());
6701 ASSERT_NO_FATAL_FAILURE(
6702 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6703}
6704
6705TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6706 std::shared_ptr<FakePointerController> fakePointerController =
6707 std::make_shared<FakePointerController>();
6708 addConfigurationProperty("touch.deviceType", "touchScreen");
6709 prepareDisplay(ui::ROTATION_0);
6710 prepareButtons();
6711 prepareAxes(POSITION);
6712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6713 mFakePolicy->setPointerController(fakePointerController);
6714 mFakePolicy->setStylusPointerIconEnabled(false);
6715 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6716
6717 processKey(mapper, BTN_TOOL_PEN, 1);
6718 processMove(mapper, 100, 200);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6721 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6722 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6723 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6724 ASSERT_FALSE(fakePointerController->isPointerShown());
6725}
6726
Prabir Pradhan5632d622021-09-06 07:57:20 -07006727// --- TouchDisplayProjectionTest ---
6728
6729class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6730public:
6731 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6732 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6733 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006734 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6735 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6736 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006737 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006738 auto rotatedWidth = naturalDisplayWidth;
6739 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006740 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006741 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006742 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006743 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006744 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006745 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006746 inverseRotationFlags = ui::Transform::ROT_180;
6747 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006748 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006749 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006750 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006752 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006753 inverseRotationFlags = ui::Transform::ROT_0;
6754 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006755 }
6756
Prabir Pradhana9df3162022-12-05 23:57:27 +00006757 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006758 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6759
6760 std::optional<DisplayViewport> internalViewport =
6761 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6762 DisplayViewport& v = *internalViewport;
6763 v.displayId = DISPLAY_ID;
6764 v.orientation = orientation;
6765
6766 v.logicalLeft = 0;
6767 v.logicalTop = 0;
6768 v.logicalRight = 100;
6769 v.logicalBottom = 100;
6770
6771 v.physicalLeft = rotatedPhysicalDisplay.left;
6772 v.physicalTop = rotatedPhysicalDisplay.top;
6773 v.physicalRight = rotatedPhysicalDisplay.right;
6774 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6775
Prabir Pradhana9df3162022-12-05 23:57:27 +00006776 v.deviceWidth = rotatedWidth;
6777 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006778
6779 v.isActive = true;
6780 v.uniqueId = UNIQUE_ID;
6781 v.type = ViewportType::INTERNAL;
6782 mFakePolicy->updateViewport(v);
6783 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6784 }
6785
6786 void assertReceivedMove(const Point& point) {
6787 NotifyMotionArgs motionArgs;
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6792 1, 0, 0, 0, 0, 0, 0, 0));
6793 }
6794};
6795
6796TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6797 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006798 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006799
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803
6804 NotifyMotionArgs motionArgs;
6805
6806 // Configure the DisplayViewport such that the logical display maps to a subsection of
6807 // the display panel called the physical display. Here, the physical display is bounded by the
6808 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6809 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6810 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6811 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6812
Michael Wrighta9cf4192022-12-01 23:46:39 +00006813 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006814 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6815
6816 // Touches outside the physical display should be ignored, and should not generate any
6817 // events. Ensure touches at the following points that lie outside of the physical display
6818 // area do not generate any events.
6819 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6820 processDown(mapper, toRawX(point.x), toRawY(point.y));
6821 processSync(mapper);
6822 processUp(mapper);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6825 << "Unexpected event generated for touch outside physical display at point: "
6826 << point.x << ", " << point.y;
6827 }
6828 }
6829}
6830
6831TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6832 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006833 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006834
6835 prepareButtons();
6836 prepareAxes(POSITION);
6837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6838
6839 NotifyMotionArgs motionArgs;
6840
6841 // Configure the DisplayViewport such that the logical display maps to a subsection of
6842 // the display panel called the physical display. Here, the physical display is bounded by the
6843 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6844 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6845
Michael Wrighta9cf4192022-12-01 23:46:39 +00006846 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006847 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6848
6849 // Touches that start outside the physical display should be ignored until it enters the
6850 // physical display bounds, at which point it should generate a down event. Start a touch at
6851 // the point (5, 100), which is outside the physical display bounds.
6852 static const Point kOutsidePoint{5, 100};
6853 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6854 processSync(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6856
6857 // Move the touch into the physical display area. This should generate a pointer down.
6858 processMove(mapper, toRawX(11), toRawY(21));
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6863 ASSERT_NO_FATAL_FAILURE(
6864 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // Move the touch inside the physical display area. This should generate a pointer move.
6867 processMove(mapper, toRawX(69), toRawY(159));
6868 processSync(mapper);
6869 assertReceivedMove({69, 159});
6870
6871 // Move outside the physical display area. Since the pointer is already down, this should
6872 // now continue generating events.
6873 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6874 processSync(mapper);
6875 assertReceivedMove(kOutsidePoint);
6876
6877 // Release. This should generate a pointer up.
6878 processUp(mapper);
6879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6881 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6883 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6884
6885 // Ensure no more events were generated.
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6888 }
6889}
6890
Prabir Pradhana9df3162022-12-05 23:57:27 +00006891// --- TouchscreenPrecisionTests ---
6892
6893// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6894// in various orientations and with different display rotations. We configure the touchscreen to
6895// have a higher resolution than that of the display by an integer scale factor in each axis so that
6896// we can enforce that coordinates match precisely as expected.
6897class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6898 public ::testing::WithParamInterface<ui::Rotation> {
6899public:
6900 void SetUp() override {
6901 SingleTouchInputMapperTest::SetUp();
6902
6903 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6904 // four times the resolution of the display in the Y axis.
6905 prepareButtons();
6906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
6907 0, 0);
6908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
6909 0, 0);
6910 }
6911
6912 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6913 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6914 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6915 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6916
6917 static const std::array<Point, 4> kRawCorners;
6918};
6919
6920const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6921 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6922 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6923 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6924 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6925}};
6926
6927// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6928// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6929// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6930TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6931 enum class Orientation {
6932 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6933 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6934 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6935 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6936 ftl_last = ORIENTATION_270,
6937 };
6938 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6939 Orientation::ORIENTATION_270;
6940 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6941 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6942 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6943 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6944 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6945 };
6946
6947 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6948
6949 // Configure the touchscreen as being installed in the one of the four different orientations
6950 // relative to the display.
6951 addConfigurationProperty("touch.deviceType", "touchScreen");
6952 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6953 prepareDisplay(ui::ROTATION_0);
6954
6955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6956
6957 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6958 // orientations of either 90 or 270) this means the display's natural resolution will be
6959 // flipped.
6960 const bool displayRotated =
6961 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6962 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6963 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6964 const Rect physicalFrame{0, 0, width, height};
6965 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6966
6967 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6968 const float expectedPrecisionX = displayRotated ? 4 : 2;
6969 const float expectedPrecisionY = displayRotated ? 2 : 4;
6970
6971 // Test all four corners.
6972 for (int i = 0; i < 4; i++) {
6973 const auto& raw = kRawCorners[i];
6974 processDown(mapper, raw.x, raw.y);
6975 processSync(mapper);
6976 const auto& expected = expectedPoints[i];
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6979 WithCoords(expected.x, expected.y),
6980 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6981 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6982 << "with touchscreen orientation "
6983 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6984 << expected.x << ", " << expected.y << ").";
6985 processUp(mapper);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6988 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6989 WithCoords(expected.x, expected.y))));
6990 }
6991}
6992
Prabir Pradhan82687402022-12-06 01:32:53 +00006993TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6994 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6995 kMappedCorners = {
6996 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6997 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6998 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6999 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7000 };
7001
7002 const ui::Rotation displayRotation = GetParam();
7003
7004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareDisplay(displayRotation);
7006
7007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7008
7009 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7010
7011 // Test all four corners.
7012 for (int i = 0; i < 4; i++) {
7013 const auto& expected = expectedPoints[i];
7014 const auto& raw = kRawCorners[i];
7015 processDown(mapper, raw.x, raw.y);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7019 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7020 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7021 << "with display rotation " << ui::toCString(displayRotation)
7022 << ", expected point (" << expected.x << ", " << expected.y << ").";
7023 processUp(mapper);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7027 WithCoords(expected.x, expected.y))));
7028 }
7029}
7030
Prabir Pradhana9df3162022-12-05 23:57:27 +00007031// Run the precision tests for all rotations.
7032INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7033 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7034 ui::ROTATION_270),
7035 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7036 return ftl::enum_string(testParamInfo.param);
7037 });
7038
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007039// --- ExternalStylusFusionTest ---
7040
7041class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7042public:
7043 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7044 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007045 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007046 prepareButtons();
7047 prepareAxes(POSITION);
7048 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7049
7050 mStylusState.when = ARBITRARY_TIME;
7051 mStylusState.pressure = 0.f;
7052 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7053 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7054 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7055 processExternalStylusState(mapper);
7056 return mapper;
7057 }
7058
7059 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7060 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7061 for (const NotifyArgs& args : generatedArgs) {
7062 mFakeListener->notify(args);
7063 }
7064 // Loop the reader to flush the input listener queue.
7065 mReader->loopOnce();
7066 return generatedArgs;
7067 }
7068
7069protected:
7070 StylusState mStylusState{};
7071 static constexpr uint32_t EXPECTED_SOURCE =
7072 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7073
7074 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7075 auto toolTypeSource =
7076 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7077
7078 // The first pointer is withheld.
7079 processDown(mapper, 100, 200);
7080 processSync(mapper);
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7082 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7083 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7084
7085 // The external stylus reports pressure. The withheld finger pointer is released as a
7086 // stylus.
7087 mStylusState.pressure = 1.f;
7088 processExternalStylusState(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7090 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7091 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7092
7093 // Subsequent pointer events are not withheld.
7094 processMove(mapper, 101, 201);
7095 processSync(mapper);
7096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7097 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7098
7099 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7101 }
7102
7103 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7104 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7105
7106 // Releasing the touch pointer ends the gesture.
7107 processUp(mapper);
7108 processSync(mapper);
7109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7110 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7111 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7112
7113 mStylusState.pressure = 0.f;
7114 processExternalStylusState(mapper);
7115 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7117 }
7118
7119 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7120 auto toolTypeSource =
7121 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7122
7123 // The first pointer is withheld when an external stylus is connected,
7124 // and a timeout is requested.
7125 processDown(mapper, 100, 200);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7128 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7129 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7130
7131 // If the timeout expires early, it is requested again.
7132 handleTimeout(mapper, ARBITRARY_TIME + 1);
7133 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7134 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7135
7136 // When the timeout expires, the withheld touch is released as a finger pointer.
7137 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7139 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7140
7141 // Subsequent pointer events are not withheld.
7142 processMove(mapper, 101, 201);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7145 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7146 processUp(mapper);
7147 processSync(mapper);
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7149 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7150
7151 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7153 }
7154
7155private:
7156 InputDeviceInfo mExternalStylusDeviceInfo{};
7157};
7158
7159TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7160 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7161 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7162}
7163
7164TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7165 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7166 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7167}
7168
7169TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7170 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7171 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7172}
7173
7174// Test a successful stylus fusion gesture where the pressure is reported by the external
7175// before the touch is reported by the touchscreen.
7176TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7177 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7178 auto toolTypeSource =
7179 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7180
7181 // The external stylus reports pressure first. It is ignored for now.
7182 mStylusState.pressure = 1.f;
7183 processExternalStylusState(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7185 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7186
7187 // When the touch goes down afterwards, it is reported as a stylus pointer.
7188 processDown(mapper, 100, 200);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7191 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7192 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7193
7194 processMove(mapper, 101, 201);
7195 processSync(mapper);
7196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7197 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7198 processUp(mapper);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7201 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7202
7203 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7205}
7206
7207TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7208 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7209
7210 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7211 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7212
7213 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7214 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7215 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7216 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7217}
7218
7219TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7220 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7221 auto toolTypeSource =
7222 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7223
7224 mStylusState.pressure = 0.8f;
7225 processExternalStylusState(mapper);
7226 processDown(mapper, 100, 200);
7227 processSync(mapper);
7228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7229 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7230 WithPressure(0.8f))));
7231 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7232
7233 // The external stylus reports a pressure change. We wait for some time for a touch event.
7234 mStylusState.pressure = 0.6f;
7235 processExternalStylusState(mapper);
7236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7237 ASSERT_NO_FATAL_FAILURE(
7238 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7239
7240 // If a touch is reported within the timeout, it reports the updated pressure.
7241 processMove(mapper, 101, 201);
7242 processSync(mapper);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7244 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7245 WithPressure(0.6f))));
7246 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7247
7248 // There is another pressure change.
7249 mStylusState.pressure = 0.5f;
7250 processExternalStylusState(mapper);
7251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7252 ASSERT_NO_FATAL_FAILURE(
7253 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7254
7255 // If a touch is not reported within the timeout, a move event is generated to report
7256 // the new pressure.
7257 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7259 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7260 WithPressure(0.5f))));
7261
7262 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7263 // repeated indefinitely.
7264 mStylusState.pressure = 0.0f;
7265 processExternalStylusState(mapper);
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7267 ASSERT_NO_FATAL_FAILURE(
7268 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7269 processMove(mapper, 102, 202);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7272 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7273 WithPressure(0.5f))));
7274 processMove(mapper, 103, 203);
7275 processSync(mapper);
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7277 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7278 WithPressure(0.5f))));
7279
7280 processUp(mapper);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7283 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7284 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7285
7286 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7288}
7289
7290TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7291 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7292 auto source = WithSource(EXPECTED_SOURCE);
7293
7294 mStylusState.pressure = 1.f;
7295 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7296 processExternalStylusState(mapper);
7297 processDown(mapper, 100, 200);
7298 processSync(mapper);
7299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7300 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7301 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7302 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7303
7304 // The external stylus reports a tool change. We wait for some time for a touch event.
7305 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7306 processExternalStylusState(mapper);
7307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7308 ASSERT_NO_FATAL_FAILURE(
7309 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7310
7311 // If a touch is reported within the timeout, it reports the updated pressure.
7312 processMove(mapper, 101, 201);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7315 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7316 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7317 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7318
7319 // There is another tool type change.
7320 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7321 processExternalStylusState(mapper);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7323 ASSERT_NO_FATAL_FAILURE(
7324 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7325
7326 // If a touch is not reported within the timeout, a move event is generated to report
7327 // the new tool type.
7328 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7330 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7331 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7332
7333 processUp(mapper);
7334 processSync(mapper);
7335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7336 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7337 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7338
7339 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7341}
7342
7343TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7344 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7345 auto toolTypeSource =
7346 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7347
7348 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7349
7350 // The external stylus reports a button change. We wait for some time for a touch event.
7351 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7352 processExternalStylusState(mapper);
7353 ASSERT_NO_FATAL_FAILURE(
7354 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7355
7356 // If a touch is reported within the timeout, it reports the updated button state.
7357 processMove(mapper, 101, 201);
7358 processSync(mapper);
7359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7360 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7361 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7363 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7364 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7365 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7366
7367 // The button is now released.
7368 mStylusState.buttons = 0;
7369 processExternalStylusState(mapper);
7370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7371 ASSERT_NO_FATAL_FAILURE(
7372 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7373
7374 // If a touch is not reported within the timeout, a move event is generated to report
7375 // the new button state.
7376 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7378 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7379 WithButtonState(0))));
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007381 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7382 WithButtonState(0))));
7383
7384 processUp(mapper);
7385 processSync(mapper);
7386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007387 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7388
7389 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7391}
7392
Michael Wrightd02c5b62014-02-10 15:10:22 -08007393// --- MultiTouchInputMapperTest ---
7394
7395class MultiTouchInputMapperTest : public TouchInputMapperTest {
7396protected:
7397 void prepareAxes(int axes);
7398
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007399 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7400 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7401 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7402 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7403 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7404 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7405 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7406 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7407 void processId(MultiTouchInputMapper& mapper, int32_t id);
7408 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7409 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7410 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007411 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007412 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007413 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7414 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007415};
7416
7417void MultiTouchInputMapperTest::prepareAxes(int axes) {
7418 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007419 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7420 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007421 }
7422 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7424 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007425 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007426 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7427 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007428 }
7429 }
7430 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7432 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007433 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007435 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 }
7437 }
7438 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7440 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007441 }
7442 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7444 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007445 }
7446 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7448 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007449 }
7450 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7452 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007453 }
7454 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7456 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 }
7458 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007460 }
7461}
7462
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007463void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7464 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007467}
7468
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007469void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7470 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472}
7473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007474void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7475 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477}
7478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007479void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007481}
7482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007483void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007485}
7486
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007487void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7488 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490}
7491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007492void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007494}
7495
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007496void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007498}
7499
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007500void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502}
7503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007504void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007506}
7507
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007508void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510}
7511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007512void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7513 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515}
7516
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007517void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7518 int32_t value) {
7519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7521}
7522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007523void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525}
7526
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007527void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7528 nsecs_t readTime) {
7529 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007530}
7531
Michael Wrightd02c5b62014-02-10 15:10:22 -08007532TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007534 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007535 prepareAxes(POSITION);
7536 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007537 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007538
arthurhungdcef2dc2020-08-11 14:47:50 +08007539 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007540
7541 NotifyMotionArgs motionArgs;
7542
7543 // Two fingers down at once.
7544 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7545 processPosition(mapper, x1, y1);
7546 processMTSync(mapper);
7547 processPosition(mapper, x2, y2);
7548 processMTSync(mapper);
7549 processSync(mapper);
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7553 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7554 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7555 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7556 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7557 ASSERT_EQ(0, motionArgs.flags);
7558 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7559 ASSERT_EQ(0, motionArgs.buttonState);
7560 ASSERT_EQ(0, motionArgs.edgeFlags);
7561 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7562 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7563 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7564 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7565 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7566 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7567 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7568 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7569
7570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7571 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7572 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7573 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7574 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007575 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576 ASSERT_EQ(0, motionArgs.flags);
7577 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7578 ASSERT_EQ(0, motionArgs.buttonState);
7579 ASSERT_EQ(0, motionArgs.edgeFlags);
7580 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7581 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7582 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7583 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7586 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7588 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7589 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7590 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7591 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7592
7593 // Move.
7594 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7595 processPosition(mapper, x1, y1);
7596 processMTSync(mapper);
7597 processPosition(mapper, x2, y2);
7598 processMTSync(mapper);
7599 processSync(mapper);
7600
7601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7603 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7604 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7605 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7607 ASSERT_EQ(0, motionArgs.flags);
7608 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7609 ASSERT_EQ(0, motionArgs.buttonState);
7610 ASSERT_EQ(0, motionArgs.edgeFlags);
7611 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7613 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7615 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7620 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7621 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7622 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7623
7624 // First finger up.
7625 x2 += 15; y2 -= 20;
7626 processPosition(mapper, x2, y2);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7632 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7633 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7634 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007635 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007636 ASSERT_EQ(0, motionArgs.flags);
7637 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7638 ASSERT_EQ(0, motionArgs.buttonState);
7639 ASSERT_EQ(0, motionArgs.edgeFlags);
7640 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7641 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7642 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7643 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7644 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7645 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7646 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7648 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7649 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7650 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7651 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7652
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7654 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7655 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7656 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7657 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7658 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7659 ASSERT_EQ(0, motionArgs.flags);
7660 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7661 ASSERT_EQ(0, motionArgs.buttonState);
7662 ASSERT_EQ(0, motionArgs.edgeFlags);
7663 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7664 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7665 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7668 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7669 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7670 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7671
7672 // Move.
7673 x2 += 20; y2 -= 25;
7674 processPosition(mapper, x2, y2);
7675 processMTSync(mapper);
7676 processSync(mapper);
7677
7678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7680 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7681 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7682 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7684 ASSERT_EQ(0, motionArgs.flags);
7685 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7686 ASSERT_EQ(0, motionArgs.buttonState);
7687 ASSERT_EQ(0, motionArgs.edgeFlags);
7688 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7689 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7690 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7691 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7692 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7693 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7694 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7695 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7696
7697 // New finger down.
7698 int32_t x3 = 700, y3 = 300;
7699 processPosition(mapper, x2, y2);
7700 processMTSync(mapper);
7701 processPosition(mapper, x3, y3);
7702 processMTSync(mapper);
7703 processSync(mapper);
7704
7705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7707 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7708 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7709 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007710 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 ASSERT_EQ(0, motionArgs.flags);
7712 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7713 ASSERT_EQ(0, motionArgs.buttonState);
7714 ASSERT_EQ(0, motionArgs.edgeFlags);
7715 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7716 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7718 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7719 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7720 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7721 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7722 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7723 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7724 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7725 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7726 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7727
7728 // Second finger up.
7729 x3 += 30; y3 -= 20;
7730 processPosition(mapper, x3, y3);
7731 processMTSync(mapper);
7732 processSync(mapper);
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7736 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7737 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7738 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007739 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007740 ASSERT_EQ(0, motionArgs.flags);
7741 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7742 ASSERT_EQ(0, motionArgs.buttonState);
7743 ASSERT_EQ(0, motionArgs.edgeFlags);
7744 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7747 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7748 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7749 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7750 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7752 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7753 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7754 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7756
7757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7759 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7760 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7761 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7762 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7763 ASSERT_EQ(0, motionArgs.flags);
7764 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7765 ASSERT_EQ(0, motionArgs.buttonState);
7766 ASSERT_EQ(0, motionArgs.edgeFlags);
7767 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7768 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7769 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7770 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7771 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7772 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7773 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7774 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7775
7776 // Last finger up.
7777 processMTSync(mapper);
7778 processSync(mapper);
7779
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7781 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7782 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7783 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7784 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7785 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7786 ASSERT_EQ(0, motionArgs.flags);
7787 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7788 ASSERT_EQ(0, motionArgs.buttonState);
7789 ASSERT_EQ(0, motionArgs.edgeFlags);
7790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7791 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7793 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7794 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7795 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7796 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7797 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7798
7799 // Should not have sent any more keys or motions.
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7802}
7803
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007804TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7805 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007806 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007807
7808 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7809 /*fuzz*/ 0, /*resolution*/ 10);
7810 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7811 /*fuzz*/ 0, /*resolution*/ 11);
7812 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7813 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7814 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7815 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7816 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7817 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7818 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7819 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7820
7821 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7822
7823 // X and Y axes
7824 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7825 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7826 // Touch major and minor
7827 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7828 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7829 // Tool major and minor
7830 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7831 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7832}
7833
7834TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7835 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007836 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007837
7838 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7839 /*fuzz*/ 0, /*resolution*/ 10);
7840 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7841 /*fuzz*/ 0, /*resolution*/ 11);
7842
7843 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7844
7845 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7846
7847 // Touch major and minor
7848 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7849 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7850 // Tool major and minor
7851 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7852 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7853}
7854
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007857 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858 prepareAxes(POSITION | ID);
7859 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007860 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861
arthurhungdcef2dc2020-08-11 14:47:50 +08007862 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007863
7864 NotifyMotionArgs motionArgs;
7865
7866 // Two fingers down at once.
7867 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7868 processPosition(mapper, x1, y1);
7869 processId(mapper, 1);
7870 processMTSync(mapper);
7871 processPosition(mapper, x2, y2);
7872 processId(mapper, 2);
7873 processMTSync(mapper);
7874 processSync(mapper);
7875
7876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7877 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7878 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7879 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7880 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7881 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7882 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7883
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007885 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007886 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7887 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7888 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7889 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7890 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7892 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7894 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7895
7896 // Move.
7897 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7898 processPosition(mapper, x1, y1);
7899 processId(mapper, 1);
7900 processMTSync(mapper);
7901 processPosition(mapper, x2, y2);
7902 processId(mapper, 2);
7903 processMTSync(mapper);
7904 processSync(mapper);
7905
7906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7908 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7909 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7910 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7911 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7912 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7914 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7916 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7917
7918 // First finger up.
7919 x2 += 15; y2 -= 20;
7920 processPosition(mapper, x2, y2);
7921 processId(mapper, 2);
7922 processMTSync(mapper);
7923 processSync(mapper);
7924
7925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007926 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007927 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7928 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7929 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7930 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7933 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7935 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7936
7937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7939 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7940 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7941 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7942 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7943 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7944
7945 // Move.
7946 x2 += 20; y2 -= 25;
7947 processPosition(mapper, x2, y2);
7948 processId(mapper, 2);
7949 processMTSync(mapper);
7950 processSync(mapper);
7951
7952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7954 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7955 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7956 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7957 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7958 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7959
7960 // New finger down.
7961 int32_t x3 = 700, y3 = 300;
7962 processPosition(mapper, x2, y2);
7963 processId(mapper, 2);
7964 processMTSync(mapper);
7965 processPosition(mapper, x3, y3);
7966 processId(mapper, 3);
7967 processMTSync(mapper);
7968 processSync(mapper);
7969
7970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007971 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007972 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7973 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7975 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7976 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7978 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7979 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7980 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7981
7982 // Second finger up.
7983 x3 += 30; y3 -= 20;
7984 processPosition(mapper, x3, y3);
7985 processId(mapper, 3);
7986 processMTSync(mapper);
7987 processSync(mapper);
7988
7989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007990 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7993 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7994 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7995 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7997 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7998 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7999 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8000
8001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8002 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8003 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8004 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8005 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8007 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8008
8009 // Last finger up.
8010 processMTSync(mapper);
8011 processSync(mapper);
8012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8015 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8019 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8020
8021 // Should not have sent any more keys or motions.
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8024}
8025
8026TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008027 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008028 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029 prepareAxes(POSITION | ID | SLOT);
8030 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008031 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008032
arthurhungdcef2dc2020-08-11 14:47:50 +08008033 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034
8035 NotifyMotionArgs motionArgs;
8036
8037 // Two fingers down at once.
8038 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8039 processPosition(mapper, x1, y1);
8040 processId(mapper, 1);
8041 processSlot(mapper, 1);
8042 processPosition(mapper, x2, y2);
8043 processId(mapper, 2);
8044 processSync(mapper);
8045
8046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8048 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8049 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8050 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8051 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8052 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8053
8054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008055 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008056 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8057 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8058 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8059 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8062 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8063 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8064 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8065
8066 // Move.
8067 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8068 processSlot(mapper, 0);
8069 processPosition(mapper, x1, y1);
8070 processSlot(mapper, 1);
8071 processPosition(mapper, x2, y2);
8072 processSync(mapper);
8073
8074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8075 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8076 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8078 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8079 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8080 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8081 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8082 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8083 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8084 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8085
8086 // First finger up.
8087 x2 += 15; y2 -= 20;
8088 processSlot(mapper, 0);
8089 processId(mapper, -1);
8090 processSlot(mapper, 1);
8091 processPosition(mapper, x2, y2);
8092 processSync(mapper);
8093
8094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008095 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8097 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8098 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8099 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8100 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8101 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8102 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8103 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8104 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8108 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8109 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8110 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8111 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8112 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8113
8114 // Move.
8115 x2 += 20; y2 -= 25;
8116 processPosition(mapper, x2, y2);
8117 processSync(mapper);
8118
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8121 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8122 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8125 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8126
8127 // New finger down.
8128 int32_t x3 = 700, y3 = 300;
8129 processPosition(mapper, x2, y2);
8130 processSlot(mapper, 0);
8131 processId(mapper, 3);
8132 processPosition(mapper, x3, y3);
8133 processSync(mapper);
8134
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008136 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8138 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8140 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8142 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8143 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8145 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8146
8147 // Second finger up.
8148 x3 += 30; y3 -= 20;
8149 processSlot(mapper, 1);
8150 processId(mapper, -1);
8151 processSlot(mapper, 0);
8152 processPosition(mapper, x3, y3);
8153 processSync(mapper);
8154
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008156 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8158 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8159 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8160 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8163 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8164 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8165 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8166
8167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8169 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8170 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8171 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8172 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8173 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8174
8175 // Last finger up.
8176 processId(mapper, -1);
8177 processSync(mapper);
8178
8179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8180 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8181 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8182 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8183 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8185 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8186
8187 // Should not have sent any more keys or motions.
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8190}
8191
8192TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008194 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008196 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197
8198 // These calculations are based on the input device calibration documentation.
8199 int32_t rawX = 100;
8200 int32_t rawY = 200;
8201 int32_t rawTouchMajor = 7;
8202 int32_t rawTouchMinor = 6;
8203 int32_t rawToolMajor = 9;
8204 int32_t rawToolMinor = 8;
8205 int32_t rawPressure = 11;
8206 int32_t rawDistance = 0;
8207 int32_t rawOrientation = 3;
8208 int32_t id = 5;
8209
8210 float x = toDisplayX(rawX);
8211 float y = toDisplayY(rawY);
8212 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8213 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8214 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8215 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8216 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8217 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8218 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8219 float distance = float(rawDistance);
8220
8221 processPosition(mapper, rawX, rawY);
8222 processTouchMajor(mapper, rawTouchMajor);
8223 processTouchMinor(mapper, rawTouchMinor);
8224 processToolMajor(mapper, rawToolMajor);
8225 processToolMinor(mapper, rawToolMinor);
8226 processPressure(mapper, rawPressure);
8227 processOrientation(mapper, rawOrientation);
8228 processDistance(mapper, rawDistance);
8229 processId(mapper, id);
8230 processMTSync(mapper);
8231 processSync(mapper);
8232
8233 NotifyMotionArgs args;
8234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8235 ASSERT_EQ(0, args.pointerProperties[0].id);
8236 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8237 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8238 orientation, distance));
8239}
8240
8241TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008243 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8245 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008246 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247
8248 // These calculations are based on the input device calibration documentation.
8249 int32_t rawX = 100;
8250 int32_t rawY = 200;
8251 int32_t rawTouchMajor = 140;
8252 int32_t rawTouchMinor = 120;
8253 int32_t rawToolMajor = 180;
8254 int32_t rawToolMinor = 160;
8255
8256 float x = toDisplayX(rawX);
8257 float y = toDisplayY(rawY);
8258 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8259 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8260 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8261 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8262 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8263
8264 processPosition(mapper, rawX, rawY);
8265 processTouchMajor(mapper, rawTouchMajor);
8266 processTouchMinor(mapper, rawTouchMinor);
8267 processToolMajor(mapper, rawToolMajor);
8268 processToolMinor(mapper, rawToolMinor);
8269 processMTSync(mapper);
8270 processSync(mapper);
8271
8272 NotifyMotionArgs args;
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8275 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8276}
8277
8278TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008280 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008281 prepareAxes(POSITION | TOUCH | TOOL);
8282 addConfigurationProperty("touch.size.calibration", "diameter");
8283 addConfigurationProperty("touch.size.scale", "10");
8284 addConfigurationProperty("touch.size.bias", "160");
8285 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008286 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287
8288 // These calculations are based on the input device calibration documentation.
8289 // Note: We only provide a single common touch/tool value because the device is assumed
8290 // not to emit separate values for each pointer (isSummed = 1).
8291 int32_t rawX = 100;
8292 int32_t rawY = 200;
8293 int32_t rawX2 = 150;
8294 int32_t rawY2 = 250;
8295 int32_t rawTouchMajor = 5;
8296 int32_t rawToolMajor = 8;
8297
8298 float x = toDisplayX(rawX);
8299 float y = toDisplayY(rawY);
8300 float x2 = toDisplayX(rawX2);
8301 float y2 = toDisplayY(rawY2);
8302 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8303 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8304 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8305
8306 processPosition(mapper, rawX, rawY);
8307 processTouchMajor(mapper, rawTouchMajor);
8308 processToolMajor(mapper, rawToolMajor);
8309 processMTSync(mapper);
8310 processPosition(mapper, rawX2, rawY2);
8311 processTouchMajor(mapper, rawTouchMajor);
8312 processToolMajor(mapper, rawToolMajor);
8313 processMTSync(mapper);
8314 processSync(mapper);
8315
8316 NotifyMotionArgs args;
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8318 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8319
8320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008321 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 ASSERT_EQ(size_t(2), args.pointerCount);
8323 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8324 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8325 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8326 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8327}
8328
8329TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008331 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332 prepareAxes(POSITION | TOUCH | TOOL);
8333 addConfigurationProperty("touch.size.calibration", "area");
8334 addConfigurationProperty("touch.size.scale", "43");
8335 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008337
8338 // These calculations are based on the input device calibration documentation.
8339 int32_t rawX = 100;
8340 int32_t rawY = 200;
8341 int32_t rawTouchMajor = 5;
8342 int32_t rawToolMajor = 8;
8343
8344 float x = toDisplayX(rawX);
8345 float y = toDisplayY(rawY);
8346 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8347 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8348 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8349
8350 processPosition(mapper, rawX, rawY);
8351 processTouchMajor(mapper, rawTouchMajor);
8352 processToolMajor(mapper, rawToolMajor);
8353 processMTSync(mapper);
8354 processSync(mapper);
8355
8356 NotifyMotionArgs args;
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8358 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8359 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8360}
8361
8362TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008364 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 prepareAxes(POSITION | PRESSURE);
8366 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8367 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008368 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008369
Michael Wrightaa449c92017-12-13 21:21:43 +00008370 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008371 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008372 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8373 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8374 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8375
Michael Wrightd02c5b62014-02-10 15:10:22 -08008376 // These calculations are based on the input device calibration documentation.
8377 int32_t rawX = 100;
8378 int32_t rawY = 200;
8379 int32_t rawPressure = 60;
8380
8381 float x = toDisplayX(rawX);
8382 float y = toDisplayY(rawY);
8383 float pressure = float(rawPressure) * 0.01f;
8384
8385 processPosition(mapper, rawX, rawY);
8386 processPressure(mapper, rawPressure);
8387 processMTSync(mapper);
8388 processSync(mapper);
8389
8390 NotifyMotionArgs args;
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8392 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8393 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8394}
8395
8396TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008398 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008399 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008400 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008401
8402 NotifyMotionArgs motionArgs;
8403 NotifyKeyArgs keyArgs;
8404
8405 processId(mapper, 1);
8406 processPosition(mapper, 100, 200);
8407 processSync(mapper);
8408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8409 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8410 ASSERT_EQ(0, motionArgs.buttonState);
8411
8412 // press BTN_LEFT, release BTN_LEFT
8413 processKey(mapper, BTN_LEFT, 1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8417 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8418
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8421 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8422
Michael Wrightd02c5b62014-02-10 15:10:22 -08008423 processKey(mapper, BTN_LEFT, 0);
8424 processSync(mapper);
8425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008426 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008428
8429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008431 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432
8433 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8434 processKey(mapper, BTN_RIGHT, 1);
8435 processKey(mapper, BTN_MIDDLE, 1);
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8439 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8440 motionArgs.buttonState);
8441
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8443 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8444 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8445
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8448 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8449 motionArgs.buttonState);
8450
Michael Wrightd02c5b62014-02-10 15:10:22 -08008451 processKey(mapper, BTN_RIGHT, 0);
8452 processSync(mapper);
8453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008455 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008456
8457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008459 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460
8461 processKey(mapper, BTN_MIDDLE, 0);
8462 processSync(mapper);
8463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008466
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008469 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470
8471 // press BTN_BACK, release BTN_BACK
8472 processKey(mapper, BTN_BACK, 1);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8475 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8476 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008477
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008480 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8481
8482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8484 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008485
8486 processKey(mapper, BTN_BACK, 0);
8487 processSync(mapper);
8488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008489 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008491
8492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008494 ASSERT_EQ(0, motionArgs.buttonState);
8495
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8497 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8498 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8499
8500 // press BTN_SIDE, release BTN_SIDE
8501 processKey(mapper, BTN_SIDE, 1);
8502 processSync(mapper);
8503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8504 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8505 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008506
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008509 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8510
8511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8512 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8513 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008514
8515 processKey(mapper, BTN_SIDE, 0);
8516 processSync(mapper);
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008518 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008520
8521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008523 ASSERT_EQ(0, motionArgs.buttonState);
8524
Michael Wrightd02c5b62014-02-10 15:10:22 -08008525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8526 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8527 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8528
8529 // press BTN_FORWARD, release BTN_FORWARD
8530 processKey(mapper, BTN_FORWARD, 1);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8533 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8534 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008535
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008538 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8539
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8542 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543
8544 processKey(mapper, BTN_FORWARD, 0);
8545 processSync(mapper);
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008548 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008549
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008552 ASSERT_EQ(0, motionArgs.buttonState);
8553
Michael Wrightd02c5b62014-02-10 15:10:22 -08008554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8555 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8556 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8557
8558 // press BTN_EXTRA, release BTN_EXTRA
8559 processKey(mapper, BTN_EXTRA, 1);
8560 processSync(mapper);
8561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8562 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8563 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008564
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008566 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008567 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8568
8569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8570 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8571 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008572
8573 processKey(mapper, BTN_EXTRA, 0);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008576 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008577 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008578
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008581 ASSERT_EQ(0, motionArgs.buttonState);
8582
Michael Wrightd02c5b62014-02-10 15:10:22 -08008583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8584 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8585 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8586
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8588
Michael Wrightd02c5b62014-02-10 15:10:22 -08008589 // press BTN_STYLUS, release BTN_STYLUS
8590 processKey(mapper, BTN_STYLUS, 1);
8591 processSync(mapper);
8592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8593 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008594 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8595
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8598 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008599
8600 processKey(mapper, BTN_STYLUS, 0);
8601 processSync(mapper);
8602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008603 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008604 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008605
8606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008608 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609
8610 // press BTN_STYLUS2, release BTN_STYLUS2
8611 processKey(mapper, BTN_STYLUS2, 1);
8612 processSync(mapper);
8613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008615 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8616
8617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8618 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8619 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620
8621 processKey(mapper, BTN_STYLUS2, 0);
8622 processSync(mapper);
8623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008624 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008626
8627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008629 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630
8631 // release touch
8632 processId(mapper, -1);
8633 processSync(mapper);
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8636 ASSERT_EQ(0, motionArgs.buttonState);
8637}
8638
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008639TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8640 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008641 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008642 prepareAxes(POSITION | ID | SLOT);
8643 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8644
8645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8646 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8647
8648 // Touch down.
8649 processId(mapper, 1);
8650 processPosition(mapper, 100, 200);
8651 processSync(mapper);
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8654
8655 // Press and release button mapped to the primary stylus button.
8656 processKey(mapper, BTN_A, 1);
8657 processSync(mapper);
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8659 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8660 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8662 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8663 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8664
8665 processKey(mapper, BTN_A, 0);
8666 processSync(mapper);
8667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8668 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8670 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8671
8672 // Press and release the HID usage mapped to the secondary stylus button.
8673 processHidUsage(mapper, 0xabcd, 1);
8674 processSync(mapper);
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8676 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8677 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8680 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8681
8682 processHidUsage(mapper, 0xabcd, 0);
8683 processSync(mapper);
8684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8688
8689 // Release touch.
8690 processId(mapper, -1);
8691 processSync(mapper);
8692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8693 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8694}
8695
Michael Wrightd02c5b62014-02-10 15:10:22 -08008696TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008697 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008698 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008699 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008700 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008701
8702 NotifyMotionArgs motionArgs;
8703
8704 // default tool type is finger
8705 processId(mapper, 1);
8706 processPosition(mapper, 100, 200);
8707 processSync(mapper);
8708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8710 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8711
8712 // eraser
8713 processKey(mapper, BTN_TOOL_RUBBER, 1);
8714 processSync(mapper);
8715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8717 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8718
8719 // stylus
8720 processKey(mapper, BTN_TOOL_RUBBER, 0);
8721 processKey(mapper, BTN_TOOL_PEN, 1);
8722 processSync(mapper);
8723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8726
8727 // brush
8728 processKey(mapper, BTN_TOOL_PEN, 0);
8729 processKey(mapper, BTN_TOOL_BRUSH, 1);
8730 processSync(mapper);
8731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8734
8735 // pencil
8736 processKey(mapper, BTN_TOOL_BRUSH, 0);
8737 processKey(mapper, BTN_TOOL_PENCIL, 1);
8738 processSync(mapper);
8739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8741 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8742
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008743 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744 processKey(mapper, BTN_TOOL_PENCIL, 0);
8745 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8746 processSync(mapper);
8747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8749 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8750
8751 // mouse
8752 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8753 processKey(mapper, BTN_TOOL_MOUSE, 1);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8757 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8758
8759 // lens
8760 processKey(mapper, BTN_TOOL_MOUSE, 0);
8761 processKey(mapper, BTN_TOOL_LENS, 1);
8762 processSync(mapper);
8763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8765 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8766
8767 // double-tap
8768 processKey(mapper, BTN_TOOL_LENS, 0);
8769 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8770 processSync(mapper);
8771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8773 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8774
8775 // triple-tap
8776 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8777 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8778 processSync(mapper);
8779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8781 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8782
8783 // quad-tap
8784 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8785 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8786 processSync(mapper);
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8788 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8790
8791 // finger
8792 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8793 processKey(mapper, BTN_TOOL_FINGER, 1);
8794 processSync(mapper);
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8797 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8798
8799 // stylus trumps finger
8800 processKey(mapper, BTN_TOOL_PEN, 1);
8801 processSync(mapper);
8802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8804 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8805
8806 // eraser trumps stylus
8807 processKey(mapper, BTN_TOOL_RUBBER, 1);
8808 processSync(mapper);
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8811 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8812
8813 // mouse trumps eraser
8814 processKey(mapper, BTN_TOOL_MOUSE, 1);
8815 processSync(mapper);
8816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8817 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8818 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8819
8820 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8821 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8822 processSync(mapper);
8823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8824 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8825 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8826
8827 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8828 processToolType(mapper, MT_TOOL_PEN);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8833
8834 // back to default tool type
8835 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8836 processKey(mapper, BTN_TOOL_MOUSE, 0);
8837 processKey(mapper, BTN_TOOL_RUBBER, 0);
8838 processKey(mapper, BTN_TOOL_PEN, 0);
8839 processKey(mapper, BTN_TOOL_FINGER, 0);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8843 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8844}
8845
8846TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008847 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008848 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008849 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008850 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008852
8853 NotifyMotionArgs motionArgs;
8854
8855 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8856 processId(mapper, 1);
8857 processPosition(mapper, 100, 200);
8858 processSync(mapper);
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8860 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8862 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8863
8864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8865 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8866 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8867 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8868
8869 // move a little
8870 processPosition(mapper, 150, 250);
8871 processSync(mapper);
8872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8876
8877 // down when BTN_TOUCH is pressed, pressure defaults to 1
8878 processKey(mapper, BTN_TOUCH, 1);
8879 processSync(mapper);
8880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8881 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8883 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8884
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8887 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8888 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8889
8890 // up when BTN_TOUCH is released, hover restored
8891 processKey(mapper, BTN_TOUCH, 0);
8892 processSync(mapper);
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8894 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8895 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8896 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8897
8898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8899 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8901 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8902
8903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8904 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8906 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8907
8908 // exit hover when pointer goes away
8909 processId(mapper, -1);
8910 processSync(mapper);
8911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8912 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8913 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8914 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8915}
8916
8917TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008919 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008921 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922
8923 NotifyMotionArgs motionArgs;
8924
8925 // initially hovering because pressure is 0
8926 processId(mapper, 1);
8927 processPosition(mapper, 100, 200);
8928 processPressure(mapper, 0);
8929 processSync(mapper);
8930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8931 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8933 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8934
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8936 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8938 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8939
8940 // move a little
8941 processPosition(mapper, 150, 250);
8942 processSync(mapper);
8943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8947
8948 // down when pressure becomes non-zero
8949 processPressure(mapper, RAW_PRESSURE_MAX);
8950 processSync(mapper);
8951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8954 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8955
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8959 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8960
8961 // up when pressure becomes 0, hover restored
8962 processPressure(mapper, 0);
8963 processSync(mapper);
8964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8965 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8967 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8968
8969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8970 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8972 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8973
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8975 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8976 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8977 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8978
8979 // exit hover when pointer goes away
8980 processId(mapper, -1);
8981 processSync(mapper);
8982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8983 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8984 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8985 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8986}
8987
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008988/**
8989 * Set the input device port <--> display port associations, and check that the
8990 * events are routed to the display that matches the display port.
8991 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8992 */
8993TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008994 const std::string usb2 = "USB2";
8995 const uint8_t hdmi1 = 0;
8996 const uint8_t hdmi2 = 1;
8997 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008998 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008999
9000 addConfigurationProperty("touch.deviceType", "touchScreen");
9001 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009002 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009003
9004 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9005 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9006
9007 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9008 // for this input device is specified, and the matching viewport is not present,
9009 // the input device should be disabled (at the mapper level).
9010
9011 // Add viewport for display 2 on hdmi2
9012 prepareSecondaryDisplay(type, hdmi2);
9013 // Send a touch event
9014 processPosition(mapper, 100, 100);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9017
9018 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009019 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009020 // Send a touch event again
9021 processPosition(mapper, 100, 100);
9022 processSync(mapper);
9023
9024 NotifyMotionArgs args;
9025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9026 ASSERT_EQ(DISPLAY_ID, args.displayId);
9027}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009028
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009029TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9030 addConfigurationProperty("touch.deviceType", "touchScreen");
9031 prepareAxes(POSITION);
9032 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9033
9034 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9035
Michael Wrighta9cf4192022-12-01 23:46:39 +00009036 prepareDisplay(ui::ROTATION_0);
9037 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009038
9039 // Send a touch event
9040 processPosition(mapper, 100, 100);
9041 processSync(mapper);
9042
9043 NotifyMotionArgs args;
9044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9045 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9046}
9047
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009048TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009049 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009050 std::shared_ptr<FakePointerController> fakePointerController =
9051 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009052 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009053 fakePointerController->setPosition(100, 200);
9054 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009055 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009056
Garfield Tan888a6a42020-01-09 11:39:16 -08009057 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009058 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009059
Michael Wrighta9cf4192022-12-01 23:46:39 +00009060 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009061 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009062 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009063
Josep del Río2d8c79a2023-01-23 19:33:50 +00009064 // Check source is mouse that would obtain the PointerController.
9065 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009066
9067 NotifyMotionArgs motionArgs;
9068 processPosition(mapper, 100, 100);
9069 processSync(mapper);
9070
9071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9072 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9073 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9074}
9075
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009076/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009077 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9078 */
9079TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9080 addConfigurationProperty("touch.deviceType", "touchScreen");
9081 prepareAxes(POSITION);
9082 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9083
Michael Wrighta9cf4192022-12-01 23:46:39 +00009084 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009085 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9086 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9087 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9088 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009089
9090 NotifyMotionArgs args;
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9092 ASSERT_EQ(26, args.readTime);
9093
Harry Cutts33476232023-01-30 19:57:29 +00009094 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9095 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9096 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009097
9098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9099 ASSERT_EQ(33, args.readTime);
9100}
9101
9102/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009103 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9104 * events should not be delivered to the listener.
9105 */
9106TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9107 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009108 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009109 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009110 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009111 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9112 prepareAxes(POSITION);
9113 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9114
9115 NotifyMotionArgs motionArgs;
9116 processPosition(mapper, 100, 100);
9117 processSync(mapper);
9118
9119 mFakeListener->assertNotifyMotionWasNotCalled();
9120}
9121
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009122/**
9123 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9124 * the touch mapper can process the events and the events can be delivered to the listener.
9125 */
9126TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9127 addConfigurationProperty("touch.deviceType", "touchScreen");
9128 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009129 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009130 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009131 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9132 prepareAxes(POSITION);
9133 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9134
9135 NotifyMotionArgs motionArgs;
9136 processPosition(mapper, 100, 100);
9137 processSync(mapper);
9138
9139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9140 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9141}
9142
Garfield Tanc734e4f2021-01-15 20:01:39 -08009143TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9144 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009145 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009146 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009147 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009148 std::optional<DisplayViewport> optionalDisplayViewport =
9149 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9150 ASSERT_TRUE(optionalDisplayViewport.has_value());
9151 DisplayViewport displayViewport = *optionalDisplayViewport;
9152
9153 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9154 prepareAxes(POSITION);
9155 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9156
9157 // Finger down
9158 int32_t x = 100, y = 100;
9159 processPosition(mapper, x, y);
9160 processSync(mapper);
9161
9162 NotifyMotionArgs motionArgs;
9163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9164 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9165
9166 // Deactivate display viewport
9167 displayViewport.isActive = false;
9168 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9169 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9170
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009171 // The ongoing touch should be canceled immediately
9172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9173 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9174
9175 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009176 x += 10, y += 10;
9177 processPosition(mapper, x, y);
9178 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009180
9181 // Reactivate display viewport
9182 displayViewport.isActive = true;
9183 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9184 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9185
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009186 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009187 x += 10, y += 10;
9188 processPosition(mapper, x, y);
9189 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009192}
9193
Arthur Hung7c645402019-01-25 17:45:42 +08009194TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9195 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009196 prepareAxes(POSITION | ID | SLOT);
9197 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009199
9200 // Create the second touch screen device, and enable multi fingers.
9201 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009202 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009203 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009204 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009205 std::shared_ptr<InputDevice> device2 =
9206 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009207 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009208
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009209 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009210 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009211 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009212 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009213 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009214 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009215 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009216 /*flat=*/0, /*fuzz=*/0);
9217 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009218 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9219 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009220
9221 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009222 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009223 std::list<NotifyArgs> unused =
9224 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009225 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009226 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009227
9228 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009229 std::shared_ptr<FakePointerController> fakePointerController =
9230 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009231 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009232
9233 // Setup policy for associated displays and show touches.
9234 const uint8_t hdmi1 = 0;
9235 const uint8_t hdmi2 = 1;
9236 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9237 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9238 mFakePolicy->setShowTouches(true);
9239
9240 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009241 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009242 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009243
9244 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009245 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9246 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9247 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009248
9249 // Two fingers down at default display.
9250 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9251 processPosition(mapper, x1, y1);
9252 processId(mapper, 1);
9253 processSlot(mapper, 1);
9254 processPosition(mapper, x2, y2);
9255 processId(mapper, 2);
9256 processSync(mapper);
9257
9258 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9259 fakePointerController->getSpots().find(DISPLAY_ID);
9260 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9261 ASSERT_EQ(size_t(2), iter->second.size());
9262
9263 // Two fingers down at second display.
9264 processPosition(mapper2, x1, y1);
9265 processId(mapper2, 1);
9266 processSlot(mapper2, 1);
9267 processPosition(mapper2, x2, y2);
9268 processId(mapper2, 2);
9269 processSync(mapper2);
9270
9271 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9272 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9273 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009274
9275 // Disable the show touches configuration and ensure the spots are cleared.
9276 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009277 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9278 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009279
9280 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009281}
9282
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009283TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009284 prepareAxes(POSITION);
9285 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009286 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009287 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009288
9289 NotifyMotionArgs motionArgs;
9290 // Unrotated video frame
9291 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9292 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009293 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009294 processPosition(mapper, 100, 200);
9295 processSync(mapper);
9296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9297 ASSERT_EQ(frames, motionArgs.videoFrames);
9298
9299 // Subsequent touch events should not have any videoframes
9300 // This is implemented separately in FakeEventHub,
9301 // but that should match the behaviour of TouchVideoDevice.
9302 processPosition(mapper, 200, 200);
9303 processSync(mapper);
9304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9305 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9306}
9307
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009308TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009309 prepareAxes(POSITION);
9310 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009312 // Unrotated video frame
9313 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9314 NotifyMotionArgs motionArgs;
9315
9316 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009317 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009318 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9319 clearViewports();
9320 prepareDisplay(orientation);
9321 std::vector<TouchVideoFrame> frames{frame};
9322 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9323 processPosition(mapper, 100, 200);
9324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9326 ASSERT_EQ(frames, motionArgs.videoFrames);
9327 }
9328}
9329
9330TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9331 prepareAxes(POSITION);
9332 addConfigurationProperty("touch.deviceType", "touchScreen");
9333 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9334 // orientation-aware are affected by display rotation.
9335 addConfigurationProperty("touch.orientationAware", "0");
9336 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9337 // Unrotated video frame
9338 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9339 NotifyMotionArgs motionArgs;
9340
9341 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009342 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009343 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9344 clearViewports();
9345 prepareDisplay(orientation);
9346 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009347 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009348 processPosition(mapper, 100, 200);
9349 processSync(mapper);
9350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009351 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9352 // compared to the display. This is so that when the window transform (which contains the
9353 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9354 // window's coordinate space.
9355 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009356 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009357
9358 // Release finger.
9359 processSync(mapper);
9360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009361 }
9362}
9363
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009364TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009365 prepareAxes(POSITION);
9366 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009367 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009368 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9369 // so mix these.
9370 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9371 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9372 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9373 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9374 NotifyMotionArgs motionArgs;
9375
Michael Wrighta9cf4192022-12-01 23:46:39 +00009376 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009377 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009378 processPosition(mapper, 100, 200);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009381 ASSERT_EQ(frames, motionArgs.videoFrames);
9382}
9383
9384TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9385 prepareAxes(POSITION);
9386 addConfigurationProperty("touch.deviceType", "touchScreen");
9387 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9388 // orientation-aware are affected by display rotation.
9389 addConfigurationProperty("touch.orientationAware", "0");
9390 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9391 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9392 // so mix these.
9393 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9394 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9395 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9396 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9397 NotifyMotionArgs motionArgs;
9398
Michael Wrighta9cf4192022-12-01 23:46:39 +00009399 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009400 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9401 processPosition(mapper, 100, 200);
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9404 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9405 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9406 // compared to the display. This is so that when the window transform (which contains the
9407 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9408 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009409 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009410 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009411 ASSERT_EQ(frames, motionArgs.videoFrames);
9412}
9413
Arthur Hung9da14732019-09-02 16:16:58 +08009414/**
9415 * If we had defined port associations, but the viewport is not ready, the touch device would be
9416 * expected to be disabled, and it should be enabled after the viewport has found.
9417 */
9418TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009419 constexpr uint8_t hdmi2 = 1;
9420 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009421 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009422
9423 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9424
9425 addConfigurationProperty("touch.deviceType", "touchScreen");
9426 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009427 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009428
9429 ASSERT_EQ(mDevice->isEnabled(), false);
9430
9431 // Add display on hdmi2, the device should be enabled and can receive touch event.
9432 prepareSecondaryDisplay(type, hdmi2);
9433 ASSERT_EQ(mDevice->isEnabled(), true);
9434
9435 // Send a touch event.
9436 processPosition(mapper, 100, 100);
9437 processSync(mapper);
9438
9439 NotifyMotionArgs args;
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9441 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9442}
9443
Arthur Hung421eb1c2020-01-16 00:09:42 +08009444TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009446 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009447 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009448 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009449
9450 NotifyMotionArgs motionArgs;
9451
9452 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9453 // finger down
9454 processId(mapper, 1);
9455 processPosition(mapper, x1, y1);
9456 processSync(mapper);
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9460
9461 // finger move
9462 processId(mapper, 1);
9463 processPosition(mapper, x2, y2);
9464 processSync(mapper);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9466 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9467 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9468
9469 // finger up.
9470 processId(mapper, -1);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9473 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9475
9476 // new finger down
9477 processId(mapper, 1);
9478 processPosition(mapper, x3, y3);
9479 processSync(mapper);
9480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9481 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9482 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9483}
9484
9485/**
arthurhungcc7f9802020-04-30 17:55:40 +08009486 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9487 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009488 */
arthurhungcc7f9802020-04-30 17:55:40 +08009489TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009490 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009491 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009492 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009493 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009494
9495 NotifyMotionArgs motionArgs;
9496
9497 // default tool type is finger
9498 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009499 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009500 processPosition(mapper, x1, y1);
9501 processSync(mapper);
9502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9503 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9504 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9505
9506 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9507 processToolType(mapper, MT_TOOL_PALM);
9508 processSync(mapper);
9509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9510 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9511
9512 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009513 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009514 processPosition(mapper, x2, y2);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9517
9518 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009519 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9522
9523 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009524 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009525 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009526 processPosition(mapper, x3, y3);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9530 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9531}
9532
arthurhungbf89a482020-04-17 17:37:55 +08009533/**
arthurhungcc7f9802020-04-30 17:55:40 +08009534 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9535 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009536 */
arthurhungcc7f9802020-04-30 17:55:40 +08009537TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009538 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009539 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009540 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9541 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9542
9543 NotifyMotionArgs motionArgs;
9544
9545 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009546 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9547 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009548 processPosition(mapper, x1, y1);
9549 processSync(mapper);
9550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9551 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9552 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9553
9554 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009555 processSlot(mapper, SECOND_SLOT);
9556 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009557 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009558 processSync(mapper);
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009560 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009561 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9562
9563 // If the tool type of the first finger changes to MT_TOOL_PALM,
9564 // we expect to receive ACTION_POINTER_UP with cancel flag.
9565 processSlot(mapper, FIRST_SLOT);
9566 processId(mapper, FIRST_TRACKING_ID);
9567 processToolType(mapper, MT_TOOL_PALM);
9568 processSync(mapper);
9569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009570 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009571 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9572
9573 // The following MOVE events of second finger should be processed.
9574 processSlot(mapper, SECOND_SLOT);
9575 processId(mapper, SECOND_TRACKING_ID);
9576 processPosition(mapper, x2 + 1, y2 + 1);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9580 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9581
9582 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9583 // it. Second finger receive move.
9584 processSlot(mapper, FIRST_SLOT);
9585 processId(mapper, INVALID_TRACKING_ID);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9588 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9589 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9590
9591 // Second finger keeps moving.
9592 processSlot(mapper, SECOND_SLOT);
9593 processId(mapper, SECOND_TRACKING_ID);
9594 processPosition(mapper, x2 + 2, y2 + 2);
9595 processSync(mapper);
9596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9597 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9598 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9599
9600 // Second finger up.
9601 processId(mapper, INVALID_TRACKING_ID);
9602 processSync(mapper);
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9604 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9605 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9606}
9607
9608/**
9609 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9610 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9611 */
9612TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9613 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009614 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009615 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9616 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9617
9618 NotifyMotionArgs motionArgs;
9619
9620 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9621 // First finger down.
9622 processId(mapper, FIRST_TRACKING_ID);
9623 processPosition(mapper, x1, y1);
9624 processSync(mapper);
9625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9626 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9628
9629 // Second finger down.
9630 processSlot(mapper, SECOND_SLOT);
9631 processId(mapper, SECOND_TRACKING_ID);
9632 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009635 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009636 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9637
arthurhungcc7f9802020-04-30 17:55:40 +08009638 // If the tool type of the first finger changes to MT_TOOL_PALM,
9639 // we expect to receive ACTION_POINTER_UP with cancel flag.
9640 processSlot(mapper, FIRST_SLOT);
9641 processId(mapper, FIRST_TRACKING_ID);
9642 processToolType(mapper, MT_TOOL_PALM);
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009645 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009646 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9647
9648 // Second finger keeps moving.
9649 processSlot(mapper, SECOND_SLOT);
9650 processId(mapper, SECOND_TRACKING_ID);
9651 processPosition(mapper, x2 + 1, y2 + 1);
9652 processSync(mapper);
9653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9655
9656 // second finger becomes palm, receive cancel due to only 1 finger is active.
9657 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009658 processToolType(mapper, MT_TOOL_PALM);
9659 processSync(mapper);
9660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9661 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9662
arthurhungcc7f9802020-04-30 17:55:40 +08009663 // third finger down.
9664 processSlot(mapper, THIRD_SLOT);
9665 processId(mapper, THIRD_TRACKING_ID);
9666 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009667 processPosition(mapper, x3, y3);
9668 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9670 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9671 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009672 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9673
9674 // third finger move
9675 processId(mapper, THIRD_TRACKING_ID);
9676 processPosition(mapper, x3 + 1, y3 + 1);
9677 processSync(mapper);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9680
9681 // first finger up, third finger receive move.
9682 processSlot(mapper, FIRST_SLOT);
9683 processId(mapper, INVALID_TRACKING_ID);
9684 processSync(mapper);
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9686 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9687 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9688
9689 // second finger up, third finger receive move.
9690 processSlot(mapper, SECOND_SLOT);
9691 processId(mapper, INVALID_TRACKING_ID);
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9695 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9696
9697 // third finger up.
9698 processSlot(mapper, THIRD_SLOT);
9699 processId(mapper, INVALID_TRACKING_ID);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9702 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9703 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9704}
9705
9706/**
9707 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9708 * and the active finger could still be allowed to receive the events
9709 */
9710TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9711 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009712 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009713 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9714 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9715
9716 NotifyMotionArgs motionArgs;
9717
9718 // default tool type is finger
9719 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9720 processId(mapper, FIRST_TRACKING_ID);
9721 processPosition(mapper, x1, y1);
9722 processSync(mapper);
9723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9724 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9725 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9726
9727 // Second finger down.
9728 processSlot(mapper, SECOND_SLOT);
9729 processId(mapper, SECOND_TRACKING_ID);
9730 processPosition(mapper, x2, y2);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009733 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009734 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9735
9736 // If the tool type of the second finger changes to MT_TOOL_PALM,
9737 // we expect to receive ACTION_POINTER_UP with cancel flag.
9738 processId(mapper, SECOND_TRACKING_ID);
9739 processToolType(mapper, MT_TOOL_PALM);
9740 processSync(mapper);
9741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009742 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009743 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9744
9745 // The following MOVE event should be processed.
9746 processSlot(mapper, FIRST_SLOT);
9747 processId(mapper, FIRST_TRACKING_ID);
9748 processPosition(mapper, x1 + 1, y1 + 1);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9752 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9753
9754 // second finger up.
9755 processSlot(mapper, SECOND_SLOT);
9756 processId(mapper, INVALID_TRACKING_ID);
9757 processSync(mapper);
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9760
9761 // first finger keep moving
9762 processSlot(mapper, FIRST_SLOT);
9763 processId(mapper, FIRST_TRACKING_ID);
9764 processPosition(mapper, x1 + 2, y1 + 2);
9765 processSync(mapper);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9768
9769 // first finger up.
9770 processId(mapper, INVALID_TRACKING_ID);
9771 processSync(mapper);
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9774 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009775}
9776
Arthur Hung9ad18942021-06-19 02:04:46 +00009777/**
9778 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9779 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9780 * cause slot be valid again.
9781 */
9782TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9783 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009784 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009785 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9786 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9787
9788 NotifyMotionArgs motionArgs;
9789
9790 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9791 // First finger down.
9792 processId(mapper, FIRST_TRACKING_ID);
9793 processPosition(mapper, x1, y1);
9794 processPressure(mapper, RAW_PRESSURE_MAX);
9795 processSync(mapper);
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9798 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9799
9800 // First finger move.
9801 processId(mapper, FIRST_TRACKING_ID);
9802 processPosition(mapper, x1 + 1, y1 + 1);
9803 processPressure(mapper, RAW_PRESSURE_MAX);
9804 processSync(mapper);
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9807 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9808
9809 // Second finger down.
9810 processSlot(mapper, SECOND_SLOT);
9811 processId(mapper, SECOND_TRACKING_ID);
9812 processPosition(mapper, x2, y2);
9813 processPressure(mapper, RAW_PRESSURE_MAX);
9814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009816 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009817 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9818
9819 // second finger up with some unexpected data.
9820 processSlot(mapper, SECOND_SLOT);
9821 processId(mapper, INVALID_TRACKING_ID);
9822 processPosition(mapper, x2, y2);
9823 processSync(mapper);
9824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009825 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009826 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9827
9828 // first finger up with some unexpected data.
9829 processSlot(mapper, FIRST_SLOT);
9830 processId(mapper, INVALID_TRACKING_ID);
9831 processPosition(mapper, x2, y2);
9832 processPressure(mapper, RAW_PRESSURE_MAX);
9833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9836 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9837}
9838
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009839TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9840 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009841 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009842 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9843 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9844
9845 // First finger down.
9846 processId(mapper, FIRST_TRACKING_ID);
9847 processPosition(mapper, 100, 200);
9848 processPressure(mapper, RAW_PRESSURE_MAX);
9849 processSync(mapper);
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9851 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9852
9853 // Second finger down.
9854 processSlot(mapper, SECOND_SLOT);
9855 processId(mapper, SECOND_TRACKING_ID);
9856 processPosition(mapper, 300, 400);
9857 processPressure(mapper, RAW_PRESSURE_MAX);
9858 processSync(mapper);
9859 ASSERT_NO_FATAL_FAILURE(
9860 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9861
9862 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009863 // preserved. Resetting should cancel the ongoing gesture.
9864 resetMapper(mapper, ARBITRARY_TIME);
9865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9866 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009867
9868 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9869 // the existing touch state to generate a down event.
9870 processPosition(mapper, 301, 302);
9871 processSync(mapper);
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9875 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9876
9877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9878}
9879
9880TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9881 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009882 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009883 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9884 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9885
9886 // First finger touches down and releases.
9887 processId(mapper, FIRST_TRACKING_ID);
9888 processPosition(mapper, 100, 200);
9889 processPressure(mapper, RAW_PRESSURE_MAX);
9890 processSync(mapper);
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9892 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9893 processId(mapper, INVALID_TRACKING_ID);
9894 processSync(mapper);
9895 ASSERT_NO_FATAL_FAILURE(
9896 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9897
9898 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9899 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009900 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9902
9903 // Send an empty sync frame. Since there are no pointers, no events are generated.
9904 processSync(mapper);
9905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9906}
9907
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009908TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009909 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009910 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009911 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9912 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009914
9915 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9916 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9917 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9918 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9919 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9920
9921 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009922 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009923 processId(mapper, FIRST_TRACKING_ID);
9924 processToolType(mapper, MT_TOOL_PEN);
9925 processPosition(mapper, 100, 200);
9926 processPressure(mapper, RAW_PRESSURE_MAX);
9927 processSync(mapper);
9928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9929 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9930 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9931 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9932
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009933 // Now that we know the device supports styluses, ensure that the device is re-configured with
9934 // the stylus source.
9935 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9936 {
9937 const auto& devices = mReader->getInputDevices();
9938 auto deviceInfo =
9939 std::find_if(devices.begin(), devices.end(),
9940 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9941 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9942 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9943 }
9944
9945 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9947
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009948 processId(mapper, INVALID_TRACKING_ID);
9949 processSync(mapper);
9950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9951 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9952 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9953 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009954}
9955
Seunghwan Choi356026c2023-02-01 14:37:25 +09009956TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9957 addConfigurationProperty("touch.deviceType", "touchScreen");
9958 prepareDisplay(ui::ROTATION_0);
9959 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9960 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9961 // indicate stylus presence dynamically.
9962 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9963 std::shared_ptr<FakePointerController> fakePointerController =
9964 std::make_shared<FakePointerController>();
9965 mFakePolicy->setPointerController(fakePointerController);
9966 mFakePolicy->setStylusPointerIconEnabled(true);
9967 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9968
9969 processId(mapper, FIRST_TRACKING_ID);
9970 processPressure(mapper, RAW_PRESSURE_MIN);
9971 processPosition(mapper, 100, 200);
9972 processToolType(mapper, MT_TOOL_PEN);
9973 processSync(mapper);
9974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9975 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
9976 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
9977 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9978 ASSERT_TRUE(fakePointerController->isPointerShown());
9979 ASSERT_NO_FATAL_FAILURE(
9980 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9981}
9982
9983TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9984 addConfigurationProperty("touch.deviceType", "touchScreen");
9985 prepareDisplay(ui::ROTATION_0);
9986 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9987 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9988 // indicate stylus presence dynamically.
9989 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9990 std::shared_ptr<FakePointerController> fakePointerController =
9991 std::make_shared<FakePointerController>();
9992 mFakePolicy->setPointerController(fakePointerController);
9993 mFakePolicy->setStylusPointerIconEnabled(false);
9994 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9995
9996 processId(mapper, FIRST_TRACKING_ID);
9997 processPressure(mapper, RAW_PRESSURE_MIN);
9998 processPosition(mapper, 100, 200);
9999 processToolType(mapper, MT_TOOL_PEN);
10000 processSync(mapper);
10001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10002 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10003 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10004 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10005 ASSERT_FALSE(fakePointerController->isPointerShown());
10006}
10007
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010008// --- MultiTouchInputMapperTest_ExternalDevice ---
10009
10010class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10011protected:
Chris Yea52ade12020-08-27 16:49:20 -070010012 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010013};
10014
10015/**
10016 * Expect fallback to internal viewport if device is external and external viewport is not present.
10017 */
10018TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10019 prepareAxes(POSITION);
10020 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010021 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010022 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10023
10024 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10025
10026 NotifyMotionArgs motionArgs;
10027
10028 // Expect the event to be sent to the internal viewport,
10029 // because an external viewport is not present.
10030 processPosition(mapper, 100, 100);
10031 processSync(mapper);
10032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10033 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10034
10035 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010036 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010037 processPosition(mapper, 100, 100);
10038 processSync(mapper);
10039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10040 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10041}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010042
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010043TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10044 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10045 std::shared_ptr<FakePointerController> fakePointerController =
10046 std::make_shared<FakePointerController>();
10047 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10048 fakePointerController->setPosition(0, 0);
10049 fakePointerController->setButtonState(0);
10050
10051 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010052 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010053 prepareAxes(POSITION | ID | SLOT);
10054 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10055 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10056 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010057 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010058 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10059
10060 // captured touchpad should be a touchpad source
10061 NotifyDeviceResetArgs resetArgs;
10062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10063 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10064
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010065 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010066
10067 const InputDeviceInfo::MotionRange* relRangeX =
10068 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10069 ASSERT_NE(relRangeX, nullptr);
10070 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10071 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10072 const InputDeviceInfo::MotionRange* relRangeY =
10073 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10074 ASSERT_NE(relRangeY, nullptr);
10075 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10076 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10077
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010078 // run captured pointer tests - note that this is unscaled, so input listener events should be
10079 // identical to what the hardware sends (accounting for any
10080 // calibration).
10081 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010082 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010083 processId(mapper, 1);
10084 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10085 processKey(mapper, BTN_TOUCH, 1);
10086 processSync(mapper);
10087
10088 // expect coord[0] to contain initial location of touch 0
10089 NotifyMotionArgs args;
10090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10091 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10092 ASSERT_EQ(1U, args.pointerCount);
10093 ASSERT_EQ(0, args.pointerProperties[0].id);
10094 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10095 ASSERT_NO_FATAL_FAILURE(
10096 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10097
10098 // FINGER 1 DOWN
10099 processSlot(mapper, 1);
10100 processId(mapper, 2);
10101 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10102 processSync(mapper);
10103
10104 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010106 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010107 ASSERT_EQ(2U, args.pointerCount);
10108 ASSERT_EQ(0, args.pointerProperties[0].id);
10109 ASSERT_EQ(1, args.pointerProperties[1].id);
10110 ASSERT_NO_FATAL_FAILURE(
10111 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10112 ASSERT_NO_FATAL_FAILURE(
10113 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10114
10115 // FINGER 1 MOVE
10116 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10117 processSync(mapper);
10118
10119 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10120 // from move
10121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10123 ASSERT_NO_FATAL_FAILURE(
10124 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10125 ASSERT_NO_FATAL_FAILURE(
10126 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10127
10128 // FINGER 0 MOVE
10129 processSlot(mapper, 0);
10130 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10131 processSync(mapper);
10132
10133 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10136 ASSERT_NO_FATAL_FAILURE(
10137 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10138 ASSERT_NO_FATAL_FAILURE(
10139 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10140
10141 // BUTTON DOWN
10142 processKey(mapper, BTN_LEFT, 1);
10143 processSync(mapper);
10144
10145 // touchinputmapper design sends a move before button press
10146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10147 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10150
10151 // BUTTON UP
10152 processKey(mapper, BTN_LEFT, 0);
10153 processSync(mapper);
10154
10155 // touchinputmapper design sends a move after button release
10156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10157 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10160
10161 // FINGER 0 UP
10162 processId(mapper, -1);
10163 processSync(mapper);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10166
10167 // FINGER 1 MOVE
10168 processSlot(mapper, 1);
10169 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10170 processSync(mapper);
10171
10172 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10174 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10175 ASSERT_EQ(1U, args.pointerCount);
10176 ASSERT_EQ(1, args.pointerProperties[0].id);
10177 ASSERT_NO_FATAL_FAILURE(
10178 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10179
10180 // FINGER 1 UP
10181 processId(mapper, -1);
10182 processKey(mapper, BTN_TOUCH, 0);
10183 processSync(mapper);
10184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10185 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10186
Josep del Río2d8c79a2023-01-23 19:33:50 +000010187 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010188 mFakePolicy->setPointerCapture(false);
10189 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010191 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010192}
10193
10194TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10195 std::shared_ptr<FakePointerController> fakePointerController =
10196 std::make_shared<FakePointerController>();
10197 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10198 fakePointerController->setPosition(0, 0);
10199 fakePointerController->setButtonState(0);
10200
10201 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010202 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010203 prepareAxes(POSITION | ID | SLOT);
10204 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10205 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010206 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010207 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10208 // run uncaptured pointer tests - pushes out generic events
10209 // FINGER 0 DOWN
10210 processId(mapper, 3);
10211 processPosition(mapper, 100, 100);
10212 processKey(mapper, BTN_TOUCH, 1);
10213 processSync(mapper);
10214
10215 // start at (100,100), cursor should be at (0,0) * scale
10216 NotifyMotionArgs args;
10217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10219 ASSERT_NO_FATAL_FAILURE(
10220 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10221
10222 // FINGER 0 MOVE
10223 processPosition(mapper, 200, 200);
10224 processSync(mapper);
10225
10226 // compute scaling to help with touch position checking
10227 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10228 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10229 float scale =
10230 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10231
10232 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10234 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10236 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010237
10238 // BUTTON DOWN
10239 processKey(mapper, BTN_LEFT, 1);
10240 processSync(mapper);
10241
10242 // touchinputmapper design sends a move before button press
10243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10246 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10247
10248 // BUTTON UP
10249 processKey(mapper, BTN_LEFT, 0);
10250 processSync(mapper);
10251
10252 // touchinputmapper design sends a move after button release
10253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10254 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10256 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010257}
10258
10259TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10260 std::shared_ptr<FakePointerController> fakePointerController =
10261 std::make_shared<FakePointerController>();
10262
Michael Wrighta9cf4192022-12-01 23:46:39 +000010263 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010264 prepareAxes(POSITION | ID | SLOT);
10265 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010266 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010267 mFakePolicy->setPointerCapture(false);
10268 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10269
Josep del Río2d8c79a2023-01-23 19:33:50 +000010270 // uncaptured touchpad should be a pointer device
10271 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010272
Josep del Río2d8c79a2023-01-23 19:33:50 +000010273 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010274 mFakePolicy->setPointerCapture(true);
10275 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10276 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10277}
10278
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010279// --- BluetoothMultiTouchInputMapperTest ---
10280
10281class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10282protected:
10283 void SetUp() override {
10284 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10285 }
10286};
10287
10288TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10289 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010290 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010291 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10292 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10293
10294 nsecs_t kernelEventTime = ARBITRARY_TIME;
10295 nsecs_t expectedEventTime = ARBITRARY_TIME;
10296 // Touch down.
10297 processId(mapper, FIRST_TRACKING_ID);
10298 processPosition(mapper, 100, 200);
10299 processPressure(mapper, RAW_PRESSURE_MAX);
10300 processSync(mapper, ARBITRARY_TIME);
10301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10303
10304 // Process several events that come in quick succession, according to their timestamps.
10305 for (int i = 0; i < 3; i++) {
10306 constexpr static nsecs_t delta = ms2ns(1);
10307 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10308 kernelEventTime += delta;
10309 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10310
10311 processPosition(mapper, 101 + i, 201 + i);
10312 processSync(mapper, kernelEventTime);
10313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10314 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10315 WithEventTime(expectedEventTime))));
10316 }
10317
10318 // Release the touch.
10319 processId(mapper, INVALID_TRACKING_ID);
10320 processPressure(mapper, RAW_PRESSURE_MIN);
10321 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10323 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10324 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10325}
10326
10327// --- MultiTouchPointerModeTest ---
10328
HQ Liue6983c72022-04-19 22:14:56 +000010329class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10330protected:
10331 float mPointerMovementScale;
10332 float mPointerXZoomScale;
10333 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10334 addConfigurationProperty("touch.deviceType", "pointer");
10335 std::shared_ptr<FakePointerController> fakePointerController =
10336 std::make_shared<FakePointerController>();
10337 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10338 fakePointerController->setPosition(0, 0);
10339 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010340 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010341
10342 prepareAxes(POSITION);
10343 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10344 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10345 // needs to be disabled, and the pointer gesture needs to be enabled.
10346 mFakePolicy->setPointerCapture(false);
10347 mFakePolicy->setPointerGestureEnabled(true);
10348 mFakePolicy->setPointerController(fakePointerController);
10349
10350 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10351 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10352 mPointerMovementScale =
10353 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10354 mPointerXZoomScale =
10355 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10356 }
10357
10358 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10359 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10360 /*flat*/ 0,
10361 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10362 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10363 /*flat*/ 0,
10364 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10365 }
10366};
10367
10368/**
10369 * Two fingers down on a pointer mode touch pad. The width
10370 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10371 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10372 * be greater than the both value to be freeform gesture, so that after two
10373 * fingers start to move downwards, the gesture should be swipe.
10374 */
10375TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10376 // The min freeform gesture width is 25units/mm x 30mm = 750
10377 // which is greater than fraction of the diagnal length of the touchpad (349).
10378 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010379 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010380 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10381 NotifyMotionArgs motionArgs;
10382
10383 // Two fingers down at once.
10384 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10385 // Pointer's initial position is used the [0,0] coordinate.
10386 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10387
10388 processId(mapper, FIRST_TRACKING_ID);
10389 processPosition(mapper, x1, y1);
10390 processMTSync(mapper);
10391 processId(mapper, SECOND_TRACKING_ID);
10392 processPosition(mapper, x2, y2);
10393 processMTSync(mapper);
10394 processSync(mapper);
10395
10396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10397 ASSERT_EQ(1U, motionArgs.pointerCount);
10398 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010400 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010401 ASSERT_NO_FATAL_FAILURE(
10402 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10403
10404 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10405 // that there should be 1 pointer.
10406 int32_t movingDistance = 200;
10407 y1 += movingDistance;
10408 y2 += movingDistance;
10409
10410 processId(mapper, FIRST_TRACKING_ID);
10411 processPosition(mapper, x1, y1);
10412 processMTSync(mapper);
10413 processId(mapper, SECOND_TRACKING_ID);
10414 processPosition(mapper, x2, y2);
10415 processMTSync(mapper);
10416 processSync(mapper);
10417
10418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10419 ASSERT_EQ(1U, motionArgs.pointerCount);
10420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010422 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010423 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10424 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10425 0, 0, 0, 0));
10426}
10427
10428/**
10429 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10430 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10431 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10432 * value to be freeform gesture, so that after two fingers start to move downwards,
10433 * the gesture should be swipe.
10434 */
10435TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10436 // The min freeform gesture width is 5units/mm x 30mm = 150
10437 // which is greater than fraction of the diagnal length of the touchpad (349).
10438 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010439 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010440 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10441 NotifyMotionArgs motionArgs;
10442
10443 // Two fingers down at once.
10444 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10445 // Pointer's initial position is used the [0,0] coordinate.
10446 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10447
10448 processId(mapper, FIRST_TRACKING_ID);
10449 processPosition(mapper, x1, y1);
10450 processMTSync(mapper);
10451 processId(mapper, SECOND_TRACKING_ID);
10452 processPosition(mapper, x2, y2);
10453 processMTSync(mapper);
10454 processSync(mapper);
10455
10456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10457 ASSERT_EQ(1U, motionArgs.pointerCount);
10458 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10459 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010460 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010461 ASSERT_NO_FATAL_FAILURE(
10462 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10463
10464 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10465 // and there should be 1 pointer.
10466 int32_t movingDistance = 200;
10467 y1 += movingDistance;
10468 y2 += movingDistance;
10469
10470 processId(mapper, FIRST_TRACKING_ID);
10471 processPosition(mapper, x1, y1);
10472 processMTSync(mapper);
10473 processId(mapper, SECOND_TRACKING_ID);
10474 processPosition(mapper, x2, y2);
10475 processMTSync(mapper);
10476 processSync(mapper);
10477
10478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10479 ASSERT_EQ(1U, motionArgs.pointerCount);
10480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10481 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010482 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010483 // New coordinate is the scaled relative coordinate from the initial coordinate.
10484 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10485 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10486 0, 0, 0, 0));
10487}
10488
10489/**
10490 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10491 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10492 * freeform gestures after two fingers start to move downwards.
10493 */
10494TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010495 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010496 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10497
10498 NotifyMotionArgs motionArgs;
10499
10500 // Two fingers down at once. Wider than the max swipe width.
10501 // The gesture is expected to be PRESS, then transformed to FREEFORM
10502 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10503
10504 processId(mapper, FIRST_TRACKING_ID);
10505 processPosition(mapper, x1, y1);
10506 processMTSync(mapper);
10507 processId(mapper, SECOND_TRACKING_ID);
10508 processPosition(mapper, x2, y2);
10509 processMTSync(mapper);
10510 processSync(mapper);
10511
10512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10513 ASSERT_EQ(1U, motionArgs.pointerCount);
10514 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10515 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010516 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010517 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10518 ASSERT_NO_FATAL_FAILURE(
10519 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10520
10521 int32_t movingDistance = 200;
10522
10523 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10524 // then two down events for two pointers.
10525 y1 += movingDistance;
10526 y2 += movingDistance;
10527
10528 processId(mapper, FIRST_TRACKING_ID);
10529 processPosition(mapper, x1, y1);
10530 processMTSync(mapper);
10531 processId(mapper, SECOND_TRACKING_ID);
10532 processPosition(mapper, x2, y2);
10533 processMTSync(mapper);
10534 processSync(mapper);
10535
10536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10537 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10538 ASSERT_EQ(1U, motionArgs.pointerCount);
10539 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10541 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10542 ASSERT_EQ(1U, motionArgs.pointerCount);
10543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10545 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010546 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010547 ASSERT_EQ(2U, motionArgs.pointerCount);
10548 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10549 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010550 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010551 // Two pointers' scaled relative coordinates from their initial centroid.
10552 // Initial y coordinates are 0 as y1 and y2 have the same value.
10553 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10554 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10555 // When pointers move, the new coordinates equal to the initial coordinates plus
10556 // scaled moving distance.
10557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10558 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10559 0, 0, 0, 0));
10560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10561 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10562 0, 0, 0, 0));
10563
10564 // Move two fingers down again, expect one MOVE motion event.
10565 y1 += movingDistance;
10566 y2 += movingDistance;
10567
10568 processId(mapper, FIRST_TRACKING_ID);
10569 processPosition(mapper, x1, y1);
10570 processMTSync(mapper);
10571 processId(mapper, SECOND_TRACKING_ID);
10572 processPosition(mapper, x2, y2);
10573 processMTSync(mapper);
10574 processSync(mapper);
10575
10576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10577 ASSERT_EQ(2U, motionArgs.pointerCount);
10578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10579 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010580 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10582 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10583 0, 0, 0, 0, 0));
10584 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10585 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10586 0, 0, 0, 0, 0));
10587}
10588
Harry Cutts39b7ca22022-10-05 15:55:48 +000010589TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010590 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010591 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10592 NotifyMotionArgs motionArgs;
10593
10594 // Place two fingers down.
10595 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10596
10597 processId(mapper, FIRST_TRACKING_ID);
10598 processPosition(mapper, x1, y1);
10599 processMTSync(mapper);
10600 processId(mapper, SECOND_TRACKING_ID);
10601 processPosition(mapper, x2, y2);
10602 processMTSync(mapper);
10603 processSync(mapper);
10604
10605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10606 ASSERT_EQ(1U, motionArgs.pointerCount);
10607 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10608 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10609 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10610 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10611
10612 // Move the two fingers down and to the left.
10613 int32_t movingDistance = 200;
10614 x1 -= movingDistance;
10615 y1 += movingDistance;
10616 x2 -= movingDistance;
10617 y2 += movingDistance;
10618
10619 processId(mapper, FIRST_TRACKING_ID);
10620 processPosition(mapper, x1, y1);
10621 processMTSync(mapper);
10622 processId(mapper, SECOND_TRACKING_ID);
10623 processPosition(mapper, x2, y2);
10624 processMTSync(mapper);
10625 processSync(mapper);
10626
10627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10628 ASSERT_EQ(1U, motionArgs.pointerCount);
10629 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10630 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10631 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10632 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10633}
10634
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010635TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010636 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010637 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10638 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10640
10641 // Start a stylus gesture.
10642 processKey(mapper, BTN_TOOL_PEN, 1);
10643 processId(mapper, FIRST_TRACKING_ID);
10644 processPosition(mapper, 100, 200);
10645 processSync(mapper);
10646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10647 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10648 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10649 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10650 // TODO(b/257078296): Pointer mode generates extra event.
10651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10652 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10653 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10654 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10656
10657 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10658 // gesture should be disabled.
10659 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10660 viewport->isActive = false;
10661 mFakePolicy->updateViewport(*viewport);
10662 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10664 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10665 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10666 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10667 // TODO(b/257078296): Pointer mode generates extra event.
10668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10669 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10670 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10671 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10673}
10674
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010675// --- JoystickInputMapperTest ---
10676
10677class JoystickInputMapperTest : public InputMapperTest {
10678protected:
10679 static const int32_t RAW_X_MIN;
10680 static const int32_t RAW_X_MAX;
10681 static const int32_t RAW_Y_MIN;
10682 static const int32_t RAW_Y_MAX;
10683
10684 void SetUp() override {
10685 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10686 }
10687 void prepareAxes() {
10688 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10689 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10690 }
10691
10692 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10694 }
10695
10696 void processSync(JoystickInputMapper& mapper) {
10697 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10698 }
10699
Michael Wrighta9cf4192022-12-01 23:46:39 +000010700 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010701 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10702 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10703 NO_PORT, ViewportType::VIRTUAL);
10704 }
10705};
10706
10707const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10708const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10709const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10710const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10711
10712TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10713 prepareAxes();
10714 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10715
10716 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10717
Michael Wrighta9cf4192022-12-01 23:46:39 +000010718 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010719
10720 // Send an axis event
10721 processAxis(mapper, ABS_X, 100);
10722 processSync(mapper);
10723
10724 NotifyMotionArgs args;
10725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10726 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10727
10728 // Send another axis event
10729 processAxis(mapper, ABS_Y, 100);
10730 processSync(mapper);
10731
10732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10733 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10734}
10735
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010736// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010737
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010738class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010739protected:
10740 static const char* DEVICE_NAME;
10741 static const char* DEVICE_LOCATION;
10742 static const int32_t DEVICE_ID;
10743 static const int32_t DEVICE_GENERATION;
10744 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010745 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010746 static const int32_t EVENTHUB_ID;
10747
10748 std::shared_ptr<FakeEventHub> mFakeEventHub;
10749 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010750 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010751 std::unique_ptr<InstrumentedInputReader> mReader;
10752 std::shared_ptr<InputDevice> mDevice;
10753
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010754 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010755 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010756 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010757 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010758 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010759 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010760 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10761 }
10762
10763 void SetUp() override { SetUp(DEVICE_CLASSES); }
10764
10765 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010766 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010767 mFakePolicy.clear();
10768 }
10769
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010770 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010771 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10772 mReader->requestRefreshConfiguration(changes);
10773 mReader->loopOnce();
10774 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010775 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010776 }
10777
10778 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10779 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010780 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010781 InputDeviceIdentifier identifier;
10782 identifier.name = name;
10783 identifier.location = location;
10784 std::shared_ptr<InputDevice> device =
10785 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10786 identifier);
10787 mReader->pushNextDevice(device);
10788 mFakeEventHub->addDevice(eventHubId, name, classes);
10789 mReader->loopOnce();
10790 return device;
10791 }
10792
10793 template <class T, typename... Args>
10794 T& addControllerAndConfigure(Args... args) {
10795 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10796
10797 return controller;
10798 }
10799};
10800
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010801const char* PeripheralControllerTest::DEVICE_NAME = "device";
10802const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10803const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10804const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10805const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010806const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10807 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010808const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010809
10810// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010811class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010812protected:
10813 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010814 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010815 }
10816};
10817
10818TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010819 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010820
Harry Cuttsa5b71292022-11-28 12:56:17 +000010821 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10822 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10823 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010824}
10825
10826TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010827 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010828
Harry Cuttsa5b71292022-11-28 12:56:17 +000010829 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10830 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10831 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010832}
10833
10834// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010835class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010836protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010837 void SetUp() override {
10838 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10839 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010840};
10841
Chris Ye85758332021-05-16 23:05:17 -070010842TEST_F(LightControllerTest, MonoLight) {
10843 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010844 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010845 .maxBrightness = 255,
10846 .flags = InputLightClass::BRIGHTNESS,
10847 .path = ""};
10848 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010849
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010850 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010851 InputDeviceInfo info;
10852 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010853 std::vector<InputDeviceLightInfo> lights = info.getLights();
10854 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010855 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10856 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10857
10858 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10859 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10860}
10861
10862TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10863 RawLightInfo infoMono = {.id = 1,
10864 .name = "mono_keyboard_backlight",
10865 .maxBrightness = 255,
10866 .flags = InputLightClass::BRIGHTNESS |
10867 InputLightClass::KEYBOARD_BACKLIGHT,
10868 .path = ""};
10869 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10870
10871 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10872 InputDeviceInfo info;
10873 controller.populateDeviceInfo(&info);
10874 std::vector<InputDeviceLightInfo> lights = info.getLights();
10875 ASSERT_EQ(1U, lights.size());
10876 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10877 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010878
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010879 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10880 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010881}
10882
10883TEST_F(LightControllerTest, RGBLight) {
10884 RawLightInfo infoRed = {.id = 1,
10885 .name = "red",
10886 .maxBrightness = 255,
10887 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10888 .path = ""};
10889 RawLightInfo infoGreen = {.id = 2,
10890 .name = "green",
10891 .maxBrightness = 255,
10892 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10893 .path = ""};
10894 RawLightInfo infoBlue = {.id = 3,
10895 .name = "blue",
10896 .maxBrightness = 255,
10897 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10898 .path = ""};
10899 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10900 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10901 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10902
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010903 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010904 InputDeviceInfo info;
10905 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010906 std::vector<InputDeviceLightInfo> lights = info.getLights();
10907 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010908 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10909 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10910 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10911
10912 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10913 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10914}
10915
10916TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10917 RawLightInfo infoRed = {.id = 1,
10918 .name = "red_keyboard_backlight",
10919 .maxBrightness = 255,
10920 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10921 InputLightClass::KEYBOARD_BACKLIGHT,
10922 .path = ""};
10923 RawLightInfo infoGreen = {.id = 2,
10924 .name = "green_keyboard_backlight",
10925 .maxBrightness = 255,
10926 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10927 InputLightClass::KEYBOARD_BACKLIGHT,
10928 .path = ""};
10929 RawLightInfo infoBlue = {.id = 3,
10930 .name = "blue_keyboard_backlight",
10931 .maxBrightness = 255,
10932 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10933 InputLightClass::KEYBOARD_BACKLIGHT,
10934 .path = ""};
10935 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10936 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10937 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10938
10939 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10940 InputDeviceInfo info;
10941 controller.populateDeviceInfo(&info);
10942 std::vector<InputDeviceLightInfo> lights = info.getLights();
10943 ASSERT_EQ(1U, lights.size());
10944 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10945 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10946 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10947
10948 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10949 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10950}
10951
10952TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10953 RawLightInfo infoRed = {.id = 1,
10954 .name = "red",
10955 .maxBrightness = 255,
10956 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10957 .path = ""};
10958 RawLightInfo infoGreen = {.id = 2,
10959 .name = "green",
10960 .maxBrightness = 255,
10961 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10962 .path = ""};
10963 RawLightInfo infoBlue = {.id = 3,
10964 .name = "blue",
10965 .maxBrightness = 255,
10966 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10967 .path = ""};
10968 RawLightInfo infoGlobal = {.id = 3,
10969 .name = "global_keyboard_backlight",
10970 .maxBrightness = 255,
10971 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10972 InputLightClass::KEYBOARD_BACKLIGHT,
10973 .path = ""};
10974 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10975 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10976 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10977 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10978
10979 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10980 InputDeviceInfo info;
10981 controller.populateDeviceInfo(&info);
10982 std::vector<InputDeviceLightInfo> lights = info.getLights();
10983 ASSERT_EQ(1U, lights.size());
10984 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10985 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10986 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010987
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010988 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10989 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010990}
10991
10992TEST_F(LightControllerTest, MultiColorRGBLight) {
10993 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010994 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010995 .maxBrightness = 255,
10996 .flags = InputLightClass::BRIGHTNESS |
10997 InputLightClass::MULTI_INTENSITY |
10998 InputLightClass::MULTI_INDEX,
10999 .path = ""};
11000
11001 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11002
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011003 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011004 InputDeviceInfo info;
11005 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011006 std::vector<InputDeviceLightInfo> lights = info.getLights();
11007 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011008 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11009 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11010 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11011
11012 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11013 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11014}
11015
11016TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11017 RawLightInfo infoColor = {.id = 1,
11018 .name = "multi_color_keyboard_backlight",
11019 .maxBrightness = 255,
11020 .flags = InputLightClass::BRIGHTNESS |
11021 InputLightClass::MULTI_INTENSITY |
11022 InputLightClass::MULTI_INDEX |
11023 InputLightClass::KEYBOARD_BACKLIGHT,
11024 .path = ""};
11025
11026 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11027
11028 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11029 InputDeviceInfo info;
11030 controller.populateDeviceInfo(&info);
11031 std::vector<InputDeviceLightInfo> lights = info.getLights();
11032 ASSERT_EQ(1U, lights.size());
11033 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11034 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11035 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011036
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011037 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11038 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011039}
11040
11041TEST_F(LightControllerTest, PlayerIdLight) {
11042 RawLightInfo info1 = {.id = 1,
11043 .name = "player1",
11044 .maxBrightness = 255,
11045 .flags = InputLightClass::BRIGHTNESS,
11046 .path = ""};
11047 RawLightInfo info2 = {.id = 2,
11048 .name = "player2",
11049 .maxBrightness = 255,
11050 .flags = InputLightClass::BRIGHTNESS,
11051 .path = ""};
11052 RawLightInfo info3 = {.id = 3,
11053 .name = "player3",
11054 .maxBrightness = 255,
11055 .flags = InputLightClass::BRIGHTNESS,
11056 .path = ""};
11057 RawLightInfo info4 = {.id = 4,
11058 .name = "player4",
11059 .maxBrightness = 255,
11060 .flags = InputLightClass::BRIGHTNESS,
11061 .path = ""};
11062 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11063 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11064 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11065 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11066
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011067 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011068 InputDeviceInfo info;
11069 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011070 std::vector<InputDeviceLightInfo> lights = info.getLights();
11071 ASSERT_EQ(1U, lights.size());
11072 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011073 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11074 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011075
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011076 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11077 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11078 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011079}
11080
Michael Wrightd02c5b62014-02-10 15:10:22 -080011081} // namespace android