blob: e407638b0a3662598013bccad98e8259b47153ee [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000039#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050041#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070044#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000046#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000047#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000048#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000049#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000050#include "TestConstants.h"
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000051#include "android/hardware/input/InputDeviceCountryCode.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +000055using android::hardware::input::InputDeviceCountryCode;
56
Michael Wrightd02c5b62014-02-10 15:10:22 -080057namespace android {
58
Dominik Laskowski2f01d772022-03-23 16:01:29 -070059using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000060using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061using std::chrono_literals::operator""ms;
62
Michael Wrightd02c5b62014-02-10 15:10:22 -080063// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080064static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000065static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080066static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000067static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t DISPLAY_WIDTH = 480;
69static constexpr int32_t DISPLAY_HEIGHT = 800;
70static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
71static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
72static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070073static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070074static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080075
arthurhungcc7f9802020-04-30 17:55:40 +080076static constexpr int32_t FIRST_SLOT = 0;
77static constexpr int32_t SECOND_SLOT = 1;
78static constexpr int32_t THIRD_SLOT = 2;
79static constexpr int32_t INVALID_TRACKING_ID = -1;
80static constexpr int32_t FIRST_TRACKING_ID = 0;
81static constexpr int32_t SECOND_TRACKING_ID = 1;
82static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080083static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
84static constexpr int32_t LIGHT_COLOR = 0x7F448866;
85static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080086
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080087static constexpr int32_t ACTION_POINTER_0_DOWN =
88 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
89static constexpr int32_t ACTION_POINTER_0_UP =
90 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
91static constexpr int32_t ACTION_POINTER_1_DOWN =
92 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
93static constexpr int32_t ACTION_POINTER_1_UP =
94 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
95
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000096// Minimum timestamp separation between subsequent input events from a Bluetooth device.
97static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
98// Maximum smoothing time delta so that we don't generate events too far into the future.
99constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117 case ui::ROTATION_90:
118 return ui::ROTATION_270;
119 case ui::ROTATION_270:
120 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
128 mapper.populateDeviceInfo(&info);
129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
137 mapper.populateDeviceInfo(&info);
138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700144[[maybe_unused]] static void dumpReader(InputReader& reader) {
145 std::string dump;
146 reader.dump(dump);
147 std::istringstream iss(dump);
148 for (std::string line; std::getline(iss, line);) {
149 ALOGE("%s", line.c_str());
150 std::this_thread::sleep_for(std::chrono::milliseconds(1));
151 }
152}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154// --- FakeInputMapper ---
155
156class FakeInputMapper : public InputMapper {
157 uint32_t mSources;
158 int32_t mKeyboardType;
159 int32_t mMetaState;
160 KeyedVector<int32_t, int32_t> mKeyCodeStates;
161 KeyedVector<int32_t, int32_t> mScanCodeStates;
162 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100163 // fake mapping which would normally come from keyCharacterMap
164 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800165 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800166
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700167 std::mutex mLock;
168 std::condition_variable mStateChangedCondition;
169 bool mConfigureWasCalled GUARDED_BY(mLock);
170 bool mResetWasCalled GUARDED_BY(mLock);
171 bool mProcessWasCalled GUARDED_BY(mLock);
172 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800173
Arthur Hungc23540e2018-11-29 20:42:11 +0800174 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800175public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800176 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
177 : InputMapper(deviceContext),
178 mSources(sources),
179 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800181 mConfigureWasCalled(false),
182 mResetWasCalled(false),
183 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184
Chris Yea52ade12020-08-27 16:49:20 -0700185 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
187 void setKeyboardType(int32_t keyboardType) {
188 mKeyboardType = keyboardType;
189 }
190
191 void setMetaState(int32_t metaState) {
192 mMetaState = metaState;
193 }
194
195 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700196 std::unique_lock<std::mutex> lock(mLock);
197 base::ScopedLockAssertion assumeLocked(mLock);
198 const bool configureCalled =
199 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
200 return mConfigureWasCalled;
201 });
202 if (!configureCalled) {
203 FAIL() << "Expected configure() to have been called.";
204 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 mConfigureWasCalled = false;
206 }
207
208 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700209 std::unique_lock<std::mutex> lock(mLock);
210 base::ScopedLockAssertion assumeLocked(mLock);
211 const bool resetCalled =
212 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
213 return mResetWasCalled;
214 });
215 if (!resetCalled) {
216 FAIL() << "Expected reset() to have been called.";
217 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800218 mResetWasCalled = false;
219 }
220
Yi Kong9b14ac62018-07-17 13:48:38 -0700221 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700222 std::unique_lock<std::mutex> lock(mLock);
223 base::ScopedLockAssertion assumeLocked(mLock);
224 const bool processCalled =
225 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
226 return mProcessWasCalled;
227 });
228 if (!processCalled) {
229 FAIL() << "Expected process() to have been called.";
230 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800231 if (outLastEvent) {
232 *outLastEvent = mLastEvent;
233 }
234 mProcessWasCalled = false;
235 }
236
237 void setKeyCodeState(int32_t keyCode, int32_t state) {
238 mKeyCodeStates.replaceValueFor(keyCode, state);
239 }
240
241 void setScanCodeState(int32_t scanCode, int32_t state) {
242 mScanCodeStates.replaceValueFor(scanCode, state);
243 }
244
245 void setSwitchState(int32_t switchCode, int32_t state) {
246 mSwitchStates.replaceValueFor(switchCode, state);
247 }
248
249 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800250 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800251 }
252
Philip Junker4af3b3d2021-12-14 10:36:55 +0100253 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
254 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
255 }
256
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100258 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259
Chris Yea52ade12020-08-27 16:49:20 -0700260 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 InputMapper::populateDeviceInfo(deviceInfo);
262
263 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
264 deviceInfo->setKeyboardType(mKeyboardType);
265 }
266 }
267
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700268 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
269 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700270 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800272
273 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800274 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800275 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
276 mViewport = config->getDisplayViewportByPort(*displayPort);
277 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700278
279 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700280 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 }
282
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700283 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700286 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700287 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800288 }
289
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700290 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800292 mLastEvent = *rawEvent;
293 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700295 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 }
297
Chris Yea52ade12020-08-27 16:49:20 -0700298 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
300 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
301 }
302
Philip Junker4af3b3d2021-12-14 10:36:55 +0100303 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
304 auto it = mKeyCodeMapping.find(locationKeyCode);
305 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
306 }
307
Chris Yea52ade12020-08-27 16:49:20 -0700308 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
310 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
311 }
312
Chris Yea52ade12020-08-27 16:49:20 -0700313 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 ssize_t index = mSwitchStates.indexOfKey(switchCode);
315 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
316 }
317
Chris Yea52ade12020-08-27 16:49:20 -0700318 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700319 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700320 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700321 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
323 if (keyCodes[i] == mSupportedKeyCodes[j]) {
324 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800325 }
326 }
327 }
Chris Yea52ade12020-08-27 16:49:20 -0700328 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 return result;
330 }
331
332 virtual int32_t getMetaState() {
333 return mMetaState;
334 }
335
336 virtual void fadePointer() {
337 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800338
339 virtual std::optional<int32_t> getAssociatedDisplay() {
340 if (mViewport) {
341 return std::make_optional(mViewport->displayId);
342 }
343 return std::nullopt;
344 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345};
346
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700347// --- InputReaderPolicyTest ---
348class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700349protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700350 sp<FakeInputReaderPolicy> mFakePolicy;
351
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700352 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700353 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700354};
355
356/**
357 * Check that empty set of viewports is an acceptable configuration.
358 * Also try to get internal viewport two different ways - by type and by uniqueId.
359 *
360 * There will be confusion if two viewports with empty uniqueId and identical type are present.
361 * Such configuration is not currently allowed.
362 */
363TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700364 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700365
366 // We didn't add any viewports yet, so there shouldn't be any.
367 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100368 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369 ASSERT_FALSE(internalViewport);
370
371 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000372 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
373 true /*isActive*/, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374
375 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700376 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700377 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100378 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379
380 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100381 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700382 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700383 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700384
385 mFakePolicy->clearViewports();
386 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700387 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100389 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700390 ASSERT_FALSE(internalViewport);
391}
392
393TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
394 const std::string internalUniqueId = "local:0";
395 const std::string externalUniqueId = "local:1";
396 const std::string virtualUniqueId1 = "virtual:2";
397 const std::string virtualUniqueId2 = "virtual:3";
398 constexpr int32_t virtualDisplayId1 = 2;
399 constexpr int32_t virtualDisplayId2 = 3;
400
401 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000402 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
403 true /*isActive*/, internalUniqueId, NO_PORT,
404 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700405 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000406 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
407 true /*isActive*/, externalUniqueId, NO_PORT,
408 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700409 // Add an virtual viewport
410 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000411 ui::ROTATION_0, true /*isActive*/, virtualUniqueId1, NO_PORT,
412 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700413 // Add another virtual viewport
414 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000415 ui::ROTATION_0, true /*isActive*/, virtualUniqueId2, NO_PORT,
416 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700417
418 // Check matching by type for internal
419 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100420 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700421 ASSERT_TRUE(internalViewport);
422 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
423
424 // Check matching by type for external
425 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100426 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 ASSERT_TRUE(externalViewport);
428 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
429
430 // Check matching by uniqueId for virtual viewport #1
431 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700432 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
436 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
437
438 // Check matching by uniqueId for virtual viewport #2
439 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700440 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
444 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
445}
446
447
448/**
449 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
450 * that lookup works by checking display id.
451 * Check that 2 viewports of each kind is possible, for all existing viewport types.
452 */
453TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
454 const std::string uniqueId1 = "uniqueId1";
455 const std::string uniqueId2 = "uniqueId2";
456 constexpr int32_t displayId1 = 2;
457 constexpr int32_t displayId2 = 3;
458
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100459 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
460 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700461 for (const ViewportType& type : types) {
462 mFakePolicy->clearViewports();
463 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000464 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
465 true /*isActive*/, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000467 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
468 true /*isActive*/, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700469
470 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700471 std::optional<DisplayViewport> viewport1 =
472 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700473 ASSERT_TRUE(viewport1);
474 ASSERT_EQ(displayId1, viewport1->displayId);
475 ASSERT_EQ(type, viewport1->type);
476
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700477 std::optional<DisplayViewport> viewport2 =
478 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700479 ASSERT_TRUE(viewport2);
480 ASSERT_EQ(displayId2, viewport2->displayId);
481 ASSERT_EQ(type, viewport2->type);
482
483 // When there are multiple viewports of the same kind, and uniqueId is not specified
484 // in the call to getDisplayViewport, then that situation is not supported.
485 // The viewports can be stored in any order, so we cannot rely on the order, since that
486 // is just implementation detail.
487 // However, we can check that it still returns *a* viewport, we just cannot assert
488 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700489 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490 ASSERT_TRUE(someViewport);
491 }
492}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800493
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700494/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000495 * When we have multiple internal displays make sure we always return the default display when
496 * querying by type.
497 */
498TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
499 const std::string uniqueId1 = "uniqueId1";
500 const std::string uniqueId2 = "uniqueId2";
501 constexpr int32_t nonDefaultDisplayId = 2;
502 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
503 "Test display ID should not be ADISPLAY_ID_DEFAULT");
504
505 // Add the default display first and ensure it gets returned.
506 mFakePolicy->clearViewports();
507 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000508 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 ViewportType::INTERNAL);
510 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000511 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000512 ViewportType::INTERNAL);
513
514 std::optional<DisplayViewport> viewport =
515 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
516 ASSERT_TRUE(viewport);
517 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
518 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
519
520 // Add the default display second to make sure order doesn't matter.
521 mFakePolicy->clearViewports();
522 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000523 ui::ROTATION_0, true /*isActive*/, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000524 ViewportType::INTERNAL);
525 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000526 ui::ROTATION_0, true /*isActive*/, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000527 ViewportType::INTERNAL);
528
529 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
530 ASSERT_TRUE(viewport);
531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
533}
534
535/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700536 * Check getDisplayViewportByPort
537 */
538TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100539 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700540 const std::string uniqueId1 = "uniqueId1";
541 const std::string uniqueId2 = "uniqueId2";
542 constexpr int32_t displayId1 = 1;
543 constexpr int32_t displayId2 = 2;
544 const uint8_t hdmi1 = 0;
545 const uint8_t hdmi2 = 1;
546 const uint8_t hdmi3 = 2;
547
548 mFakePolicy->clearViewports();
549 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000550 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
551 true /*isActive*/, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000553 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
554 true /*isActive*/, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700555
556 // Check that correct display viewport was returned by comparing the display ports.
557 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
558 ASSERT_TRUE(hdmi1Viewport);
559 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
560 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
561
562 // Check that we can still get the same viewport using the uniqueId
563 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
564 ASSERT_TRUE(hdmi1Viewport);
565 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
566 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
567 ASSERT_EQ(type, hdmi1Viewport->type);
568
569 // Check that we cannot find a port with "HDMI2", because we never added one
570 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
571 ASSERT_FALSE(hdmi2Viewport);
572}
573
Michael Wrightd02c5b62014-02-10 15:10:22 -0800574// --- InputReaderTest ---
575
576class InputReaderTest : public testing::Test {
577protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700578 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700580 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000581 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800582
Chris Yea52ade12020-08-27 16:49:20 -0700583 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700584 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700585 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587
Prabir Pradhan28efc192019-11-05 01:10:04 +0000588 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700589 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800590 }
591
Chris Yea52ade12020-08-27 16:49:20 -0700592 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700593 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800595 }
596
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700597 void addDevice(int32_t eventHubId, const std::string& name,
598 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600
601 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800602 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 }
604 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000605 mReader->loopOnce();
606 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700607 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
608 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800611 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700612 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000613 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 }
615
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800616 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700617 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000618 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700619 }
620
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700622 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700623 ftl::Flags<InputDeviceClass> classes,
624 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800625 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
627 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800628 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800629 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800630 return mapper;
631 }
632};
633
Chris Ye98d3f532020-10-01 21:48:59 -0700634TEST_F(InputReaderTest, PolicyGetInputDevices) {
635 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700636 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700637 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638
639 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700640 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800642 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100643 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
645 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000646 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647}
648
Chris Yee7310032020-09-22 15:36:28 -0700649TEST_F(InputReaderTest, GetMergedInputDevices) {
650 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
651 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
652 // Add two subdevices to device
653 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
654 // Must add at least one mapper or the device will be ignored!
655 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
656 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
657
658 // Push same device instance for next device to be added, so they'll have same identifier.
659 mReader->pushNextDevice(device);
660 mReader->pushNextDevice(device);
661 ASSERT_NO_FATAL_FAILURE(
662 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
663 ASSERT_NO_FATAL_FAILURE(
664 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
665
666 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000667 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700668}
669
Chris Yee14523a2020-12-19 13:46:00 -0800670TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
671 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
672 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
673 // Add two subdevices to device
674 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
675 // Must add at least one mapper or the device will be ignored!
676 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
677 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
678
679 // Push same device instance for next device to be added, so they'll have same identifier.
680 mReader->pushNextDevice(device);
681 mReader->pushNextDevice(device);
682 // Sensor device is initially disabled
683 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
684 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
685 nullptr));
686 // Device is disabled because the only sub device is a sensor device and disabled initially.
687 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
688 ASSERT_FALSE(device->isEnabled());
689 ASSERT_NO_FATAL_FAILURE(
690 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
691 // The merged device is enabled if any sub device is enabled
692 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
693 ASSERT_TRUE(device->isEnabled());
694}
695
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700696TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800697 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700698 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800699 constexpr int32_t eventHubId = 1;
700 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700701 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800702 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800703 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800704 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700705
Yi Kong9b14ac62018-07-17 13:48:38 -0700706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700707
708 NotifyDeviceResetArgs resetArgs;
709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700710 ASSERT_EQ(deviceId, resetArgs.deviceId);
711
712 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800713 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000714 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700715
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700717 ASSERT_EQ(deviceId, resetArgs.deviceId);
718 ASSERT_EQ(device->isEnabled(), false);
719
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800720 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000721 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700724 ASSERT_EQ(device->isEnabled(), false);
725
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800726 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000727 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700729 ASSERT_EQ(deviceId, resetArgs.deviceId);
730 ASSERT_EQ(device->isEnabled(), true);
731}
732
Michael Wrightd02c5b62014-02-10 15:10:22 -0800733TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800734 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700735 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800736 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800737 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800738 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800739 AINPUT_SOURCE_KEYBOARD, nullptr);
740 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800741
742 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
743 AINPUT_SOURCE_ANY, AKEYCODE_A))
744 << "Should return unknown when the device id is >= 0 but unknown.";
745
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800746 ASSERT_EQ(AKEY_STATE_UNKNOWN,
747 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
748 << "Should return unknown when the device id is valid but the sources are not "
749 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800750
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800751 ASSERT_EQ(AKEY_STATE_DOWN,
752 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
753 AKEYCODE_A))
754 << "Should return value provided by mapper when device id is valid and the device "
755 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800756
757 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
758 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
759 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
760
761 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
762 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
763 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
764}
765
Philip Junker4af3b3d2021-12-14 10:36:55 +0100766TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
767 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
768 constexpr int32_t eventHubId = 1;
769 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
770 InputDeviceClass::KEYBOARD,
771 AINPUT_SOURCE_KEYBOARD, nullptr);
772 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
773
774 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
775 << "Should return unknown when the device with the specified id is not found.";
776
777 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
778 << "Should return correct mapping when device id is valid and mapping exists.";
779
780 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
781 << "Should return the location key code when device id is valid and there's no "
782 "mapping.";
783}
784
785TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
786 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
787 constexpr int32_t eventHubId = 1;
788 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
789 InputDeviceClass::JOYSTICK,
790 AINPUT_SOURCE_GAMEPAD, nullptr);
791 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
792
793 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
794 << "Should return unknown when the device id is valid but there is no keyboard mapper";
795}
796
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800798 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700799 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800801 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800802 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800803 AINPUT_SOURCE_KEYBOARD, nullptr);
804 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800805
806 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
807 AINPUT_SOURCE_ANY, KEY_A))
808 << "Should return unknown when the device id is >= 0 but unknown.";
809
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800810 ASSERT_EQ(AKEY_STATE_UNKNOWN,
811 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
812 << "Should return unknown when the device id is valid but the sources are not "
813 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800814
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800815 ASSERT_EQ(AKEY_STATE_DOWN,
816 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
817 KEY_A))
818 << "Should return value provided by mapper when device id is valid and the device "
819 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800820
821 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
822 AINPUT_SOURCE_TRACKBALL, KEY_A))
823 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
824
825 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
826 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
827 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
828}
829
830TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800831 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700832 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800833 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800834 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800835 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800836 AINPUT_SOURCE_KEYBOARD, nullptr);
837 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800838
839 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
840 AINPUT_SOURCE_ANY, SW_LID))
841 << "Should return unknown when the device id is >= 0 but unknown.";
842
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800843 ASSERT_EQ(AKEY_STATE_UNKNOWN,
844 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
845 << "Should return unknown when the device id is valid but the sources are not "
846 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800847
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 ASSERT_EQ(AKEY_STATE_DOWN,
849 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
850 SW_LID))
851 << "Should return value provided by mapper when device id is valid and the device "
852 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853
854 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
855 AINPUT_SOURCE_TRACKBALL, SW_LID))
856 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
857
858 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
859 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
860 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
861}
862
863TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700865 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800867 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800868 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800869 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100870
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800871 mapper.addSupportedKeyCode(AKEYCODE_A);
872 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800873
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700874 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 uint8_t flags[4] = { 0, 0, 0, 1 };
876
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700877 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800878 << "Should return false when device id is >= 0 but unknown.";
879 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
880
881 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700882 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 << "Should return false when device id is valid but the sources are not supported by "
884 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800885 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
886
887 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700888 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 keyCodes, flags))
890 << "Should return value provided by mapper when device id is valid and the device "
891 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
893
894 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700895 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
896 << "Should return false when the device id is < 0 but the sources are not supported by "
897 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800898 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
899
900 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700901 ASSERT_TRUE(
902 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
903 << "Should return value provided by mapper when device id is < 0 and one of the "
904 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800905 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
906}
907
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000908TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800909 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700910 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800911
912 NotifyConfigurationChangedArgs args;
913
914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
915 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
916}
917
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000918TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700920 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000921 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000923 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800924 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800925 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800926 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000928 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000929 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800930 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
931
932 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800933 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000934 ASSERT_EQ(when, event.when);
935 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800936 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800937 ASSERT_EQ(EV_KEY, event.type);
938 ASSERT_EQ(KEY_A, event.code);
939 ASSERT_EQ(1, event.value);
940}
941
Garfield Tan1c7bc862020-01-28 13:24:04 -0800942TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700944 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 constexpr int32_t eventHubId = 1;
946 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800947 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800948 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800949 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800950 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800951
952 NotifyDeviceResetArgs resetArgs;
953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800954 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800955
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800956 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000957 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800959 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800960 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800962 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000963 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800965 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800966 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800968 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000969 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800971 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800972 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800973}
974
Garfield Tan1c7bc862020-01-28 13:24:04 -0800975TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
976 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700977 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800978 constexpr int32_t eventHubId = 1;
979 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
980 // Must add at least one mapper or the device will be ignored!
981 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800982 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800983 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
984
985 NotifyDeviceResetArgs resetArgs;
986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
987 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
988}
989
Arthur Hungc23540e2018-11-29 20:42:11 +0800990TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700992 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800993 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800994 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800995 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
996 FakeInputMapper& mapper =
997 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800998 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800999
1000 const uint8_t hdmi1 = 1;
1001
1002 // Associated touch screen with second display.
1003 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1004
1005 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001006 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001007 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1008 true /*isActive*/, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001009 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001010 ui::ROTATION_0, true /*isActive*/, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001011 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001012 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001013 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001014
1015 // Add the device, and make sure all of the callbacks are triggered.
1016 // The device is added after the input port associations are processed since
1017 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001018 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001021 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001022
Arthur Hung2c9a3342019-07-23 14:18:59 +08001023 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001024 ASSERT_EQ(deviceId, device->getId());
1025 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1026 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001027
1028 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001029 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001030 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001031 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001032}
1033
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001034TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1035 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001036 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001037 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1038 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1039 // Must add at least one mapper or the device will be ignored!
1040 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1041 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1042 mReader->pushNextDevice(device);
1043 mReader->pushNextDevice(device);
1044 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1045 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1046
1047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1048
1049 NotifyDeviceResetArgs resetArgs;
1050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1051 ASSERT_EQ(deviceId, resetArgs.deviceId);
1052 ASSERT_TRUE(device->isEnabled());
1053 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1054 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1055
1056 disableDevice(deviceId);
1057 mReader->loopOnce();
1058
1059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1060 ASSERT_EQ(deviceId, resetArgs.deviceId);
1061 ASSERT_FALSE(device->isEnabled());
1062 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1063 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1064
1065 enableDevice(deviceId);
1066 mReader->loopOnce();
1067
1068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1069 ASSERT_EQ(deviceId, resetArgs.deviceId);
1070 ASSERT_TRUE(device->isEnabled());
1071 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1072 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1073}
1074
1075TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1076 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001077 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001078 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1079 // Add two subdevices to device
1080 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1081 FakeInputMapper& mapperDevice1 =
1082 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1083 FakeInputMapper& mapperDevice2 =
1084 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1085 mReader->pushNextDevice(device);
1086 mReader->pushNextDevice(device);
1087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1088 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1089
1090 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1091 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1092
1093 ASSERT_EQ(AKEY_STATE_DOWN,
1094 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1095 ASSERT_EQ(AKEY_STATE_DOWN,
1096 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1097 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1098 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1099}
1100
Prabir Pradhan7e186182020-11-10 13:56:45 -08001101TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1102 NotifyPointerCaptureChangedArgs args;
1103
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001104 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001105 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1106 mReader->loopOnce();
1107 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001108 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1109 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001110
1111 mFakePolicy->setPointerCapture(false);
1112 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1113 mReader->loopOnce();
1114 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001115 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001116
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001117 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001118 // does not change.
1119 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1120 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001121 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001122}
1123
Chris Ye87143712020-11-10 05:05:58 +00001124class FakeVibratorInputMapper : public FakeInputMapper {
1125public:
1126 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1127 : FakeInputMapper(deviceContext, sources) {}
1128
1129 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1130};
1131
1132TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1133 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001134 ftl::Flags<InputDeviceClass> deviceClass =
1135 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001136 constexpr int32_t eventHubId = 1;
1137 const char* DEVICE_LOCATION = "BLUETOOTH";
1138 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1139 FakeVibratorInputMapper& mapper =
1140 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1141 mReader->pushNextDevice(device);
1142
1143 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1144 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1145
1146 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1147 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1148}
1149
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001150// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001151
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001152class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001153public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001154 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001155
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001156 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001157
Andy Chenf9f1a022022-08-29 20:07:10 -04001158 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1159
Chris Yee2b1e5c2021-03-10 22:45:12 -08001160 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1161
1162 void dump(std::string& dump) override {}
1163
1164 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1165 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001166 }
1167
Chris Yee2b1e5c2021-03-10 22:45:12 -08001168 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1169 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001170 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001171
1172 bool setLightColor(int32_t lightId, int32_t color) override {
1173 getDeviceContext().setLightBrightness(lightId, color >> 24);
1174 return true;
1175 }
1176
1177 std::optional<int32_t> getLightColor(int32_t lightId) override {
1178 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1179 if (!result.has_value()) {
1180 return std::nullopt;
1181 }
1182 return result.value() << 24;
1183 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001184
1185 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1186
1187 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1188
1189private:
1190 InputDeviceContext& mDeviceContext;
1191 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1192 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001193 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001194};
1195
Chris Yee2b1e5c2021-03-10 22:45:12 -08001196TEST_F(InputReaderTest, BatteryGetCapacity) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001203 FakePeripheralController& controller =
1204 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001205 mReader->pushNextDevice(device);
1206
1207 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1208
Harry Cuttsa5b71292022-11-28 12:56:17 +00001209 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1210 FakeEventHub::BATTERY_CAPACITY);
1211 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001212}
1213
1214TEST_F(InputReaderTest, BatteryGetStatus) {
1215 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001216 ftl::Flags<InputDeviceClass> deviceClass =
1217 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001218 constexpr int32_t eventHubId = 1;
1219 const char* DEVICE_LOCATION = "BLUETOOTH";
1220 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001221 FakePeripheralController& controller =
1222 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223 mReader->pushNextDevice(device);
1224
1225 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1226
Harry Cuttsa5b71292022-11-28 12:56:17 +00001227 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1228 FakeEventHub::BATTERY_STATUS);
1229 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001230}
1231
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001232TEST_F(InputReaderTest, BatteryGetDevicePath) {
1233 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1234 ftl::Flags<InputDeviceClass> deviceClass =
1235 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1236 constexpr int32_t eventHubId = 1;
1237 const char* DEVICE_LOCATION = "BLUETOOTH";
1238 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1239 device->addController<FakePeripheralController>(eventHubId);
1240 mReader->pushNextDevice(device);
1241
1242 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1243
Harry Cuttsa5b71292022-11-28 12:56:17 +00001244 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001245}
1246
Chris Ye3fdbfef2021-01-06 18:45:18 -08001247TEST_F(InputReaderTest, LightGetColor) {
1248 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001249 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001250 constexpr int32_t eventHubId = 1;
1251 const char* DEVICE_LOCATION = "BLUETOOTH";
1252 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001253 FakePeripheralController& controller =
1254 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001255 mReader->pushNextDevice(device);
1256 RawLightInfo info = {.id = 1,
1257 .name = "Mono",
1258 .maxBrightness = 255,
1259 .flags = InputLightClass::BRIGHTNESS,
1260 .path = ""};
1261 mFakeEventHub->addRawLightInfo(1 /* rawId */, std::move(info));
1262 mFakeEventHub->fakeLightBrightness(1 /* rawId */, 0x55);
1263
1264 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001265
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 ASSERT_TRUE(controller.setLightColor(1 /* lightId */, LIGHT_BRIGHTNESS));
1267 ASSERT_EQ(controller.getLightColor(1 /* lightId */), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001268 ASSERT_TRUE(mReader->setLightColor(deviceId, 1 /* lightId */, LIGHT_BRIGHTNESS));
1269 ASSERT_EQ(mReader->getLightColor(deviceId, 1 /* lightId */), LIGHT_BRIGHTNESS);
1270}
1271
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001272// --- InputReaderIntegrationTest ---
1273
1274// These tests create and interact with the InputReader only through its interface.
1275// The InputReader is started during SetUp(), which starts its processing in its own
1276// thread. The tests use linux uinput to emulate input devices.
1277// NOTE: Interacting with the physical device while these tests are running may cause
1278// the tests to fail.
1279class InputReaderIntegrationTest : public testing::Test {
1280protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001281 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001282 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001283 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001284
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001285 std::shared_ptr<FakePointerController> mFakePointerController;
1286
Chris Yea52ade12020-08-27 16:49:20 -07001287 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001288#if !defined(__ANDROID__)
1289 GTEST_SKIP();
1290#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001291 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001292 mFakePointerController = std::make_shared<FakePointerController>();
1293 mFakePolicy->setPointerController(mFakePointerController);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001294 mTestListener = std::make_unique<TestInputListener>(2000ms /*eventHappenedTimeout*/,
1295 30ms /*eventDidNotHappenTimeout*/);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001296
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001297 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1298 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001299 ASSERT_EQ(mReader->start(), OK);
1300
1301 // Since this test is run on a real device, all the input devices connected
1302 // to the test device will show up in mReader. We wait for those input devices to
1303 // show up before beginning the tests.
1304 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1305 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1306 }
1307
Chris Yea52ade12020-08-27 16:49:20 -07001308 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001309#if !defined(__ANDROID__)
1310 return;
1311#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001312 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001313 mReader.reset();
1314 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001315 mFakePolicy.clear();
1316 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001317
1318 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1319 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1320 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1321 [&name](const InputDeviceInfo& info) {
1322 return info.getIdentifier().name == name;
1323 });
1324 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1325 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001326};
1327
1328TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1329 // An invalid input device that is only used for this test.
1330 class InvalidUinputDevice : public UinputDevice {
1331 public:
Prabir Pradhanb7d434e2022-10-14 22:41:38 +00001332 InvalidUinputDevice() : UinputDevice("Invalid Device", 99 /*productId*/) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001333
1334 private:
1335 void configureDevice(int fd, uinput_user_dev* device) override {}
1336 };
1337
1338 const size_t numDevices = mFakePolicy->getInputDevices().size();
1339
1340 // UinputDevice does not set any event or key bits, so InputReader should not
1341 // consider it as a valid device.
1342 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1343 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1344 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1345 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1346
1347 invalidDevice.reset();
1348 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1349 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1350 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1351}
1352
1353TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1354 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1355
1356 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1357 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1358 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1359 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1360
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001361 const auto device = findDeviceByName(keyboard->getName());
1362 ASSERT_TRUE(device.has_value());
1363 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1364 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1365 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001366
1367 keyboard.reset();
1368 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1370 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1371}
1372
1373TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1374 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1375 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1376
1377 NotifyConfigurationChangedArgs configChangedArgs;
1378 ASSERT_NO_FATAL_FAILURE(
1379 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001380 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001381 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1382
1383 NotifyKeyArgs keyArgs;
1384 keyboard->pressAndReleaseHomeKey();
1385 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1386 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001387 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001388 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001389 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001390 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001391 prevTimestamp = keyArgs.eventTime;
1392
1393 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1394 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001395 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001396 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001397 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001398}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001399
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001400TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1401 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1402 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1403
1404 const auto device = findDeviceByName(stylus->getName());
1405 ASSERT_TRUE(device.has_value());
1406
Prabir Pradhana3621852022-10-14 18:57:23 +00001407 // An external stylus with buttons should also be recognized as a keyboard.
1408 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001409 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1410 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1411
1412 const auto DOWN =
1413 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1414 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1415
1416 stylus->pressAndReleaseKey(BTN_STYLUS);
1417 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1418 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1420 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1421
1422 stylus->pressAndReleaseKey(BTN_STYLUS2);
1423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1424 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1426 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1427
1428 stylus->pressAndReleaseKey(BTN_STYLUS3);
1429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1430 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1432 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1433}
1434
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001435/**
1436 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1437 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1438 * are passed to the listener.
1439 */
1440static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1441TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1442 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 NotifyKeyArgs keyArgs;
1445
1446 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1447 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1448 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1449 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1450
1451 controller->pressAndReleaseKey(BTN_GEAR_UP);
1452 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1454 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1455}
1456
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001457// --- TouchIntegrationTest ---
1458
Arthur Hungaab25622020-01-16 11:22:11 +08001459class TouchIntegrationTest : public InputReaderIntegrationTest {
1460protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001461 const std::string UNIQUE_ID = "local:0";
1462
Chris Yea52ade12020-08-27 16:49:20 -07001463 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001464#if !defined(__ANDROID__)
1465 GTEST_SKIP();
1466#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001467 InputReaderIntegrationTest::SetUp();
1468 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001469 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1470 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001471
1472 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1473 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001475 const auto info = findDeviceByName(mDevice->getName());
1476 ASSERT_TRUE(info);
1477 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001478 }
1479
1480 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001481 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001482 std::optional<uint8_t> physicalPort,
1483 ViewportType viewportType) {
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001484 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, true /*isActive*/,
1485 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001486 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1487 }
1488
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001489 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1490 NotifyMotionArgs args;
1491 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1492 EXPECT_EQ(action, args.action);
1493 ASSERT_EQ(points.size(), args.pointerCount);
1494 for (size_t i = 0; i < args.pointerCount; i++) {
1495 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1496 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1497 }
1498 }
1499
Arthur Hungaab25622020-01-16 11:22:11 +08001500 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001501 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001502};
1503
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001504TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1505 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1506 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1507 // presses).
1508 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1509 mDeviceInfo.getSources());
1510}
1511
Arthur Hungaab25622020-01-16 11:22:11 +08001512TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1513 NotifyMotionArgs args;
1514 const Point centerPoint = mDevice->getCenterPoint();
1515
1516 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001517 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001518 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001519 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001520 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1521 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1522
1523 // ACTION_MOVE
1524 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001525 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1528
1529 // ACTION_UP
1530 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001531 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001532 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1533 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1534}
1535
1536TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1537 NotifyMotionArgs args;
1538 const Point centerPoint = mDevice->getCenterPoint();
1539
1540 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001541 mDevice->sendSlot(FIRST_SLOT);
1542 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001543 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001544 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001545 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1547
1548 // ACTION_POINTER_DOWN (Second slot)
1549 const Point secondPoint = centerPoint + Point(100, 100);
1550 mDevice->sendSlot(SECOND_SLOT);
1551 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001552 mDevice->sendDown(secondPoint);
1553 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001554 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001555 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001556
1557 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001558 mDevice->sendMove(secondPoint + Point(1, 1));
1559 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001560 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1561 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1562
1563 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001564 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001565 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001566 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001567 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001568
1569 // ACTION_UP
1570 mDevice->sendSlot(FIRST_SLOT);
1571 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001572 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1574 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1575}
1576
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001577/**
1578 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1579 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1580 * data?
1581 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1582 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1583 * for Pointer 0 only is generated after.
1584 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1585 * events, we will not miss any information.
1586 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1587 * event generated afterwards that contains the newest movement of pointer 0.
1588 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1589 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1590 * losing information about non-palm pointers.
1591 */
1592TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1593 NotifyMotionArgs args;
1594 const Point centerPoint = mDevice->getCenterPoint();
1595
1596 // ACTION_DOWN
1597 mDevice->sendSlot(FIRST_SLOT);
1598 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1599 mDevice->sendDown(centerPoint);
1600 mDevice->sendSync();
1601 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1602
1603 // ACTION_POINTER_DOWN (Second slot)
1604 const Point secondPoint = centerPoint + Point(100, 100);
1605 mDevice->sendSlot(SECOND_SLOT);
1606 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1607 mDevice->sendDown(secondPoint);
1608 mDevice->sendSync();
1609 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1610
1611 // ACTION_MOVE (First slot)
1612 mDevice->sendSlot(FIRST_SLOT);
1613 mDevice->sendMove(centerPoint + Point(5, 5));
1614 // ACTION_POINTER_UP (Second slot)
1615 mDevice->sendSlot(SECOND_SLOT);
1616 mDevice->sendPointerUp();
1617 // Send a single sync for the above 2 pointer updates
1618 mDevice->sendSync();
1619
1620 // First, we should get POINTER_UP for the second pointer
1621 assertReceivedMotion(ACTION_POINTER_1_UP,
1622 {/*first pointer */ centerPoint + Point(5, 5),
1623 /*second pointer*/ secondPoint});
1624
1625 // Next, the MOVE event for the first pointer
1626 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1627}
1628
1629/**
1630 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1631 * move, and then it will go up, all in the same frame.
1632 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1633 * gets sent to the listener.
1634 */
1635TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1636 NotifyMotionArgs args;
1637 const Point centerPoint = mDevice->getCenterPoint();
1638
1639 // ACTION_DOWN
1640 mDevice->sendSlot(FIRST_SLOT);
1641 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1642 mDevice->sendDown(centerPoint);
1643 mDevice->sendSync();
1644 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1645
1646 // ACTION_POINTER_DOWN (Second slot)
1647 const Point secondPoint = centerPoint + Point(100, 100);
1648 mDevice->sendSlot(SECOND_SLOT);
1649 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1650 mDevice->sendDown(secondPoint);
1651 mDevice->sendSync();
1652 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1653
1654 // ACTION_MOVE (First slot)
1655 mDevice->sendSlot(FIRST_SLOT);
1656 mDevice->sendMove(centerPoint + Point(5, 5));
1657 // ACTION_POINTER_UP (Second slot)
1658 mDevice->sendSlot(SECOND_SLOT);
1659 mDevice->sendMove(secondPoint + Point(6, 6));
1660 mDevice->sendPointerUp();
1661 // Send a single sync for the above 2 pointer updates
1662 mDevice->sendSync();
1663
1664 // First, we should get POINTER_UP for the second pointer
1665 // The movement of the second pointer during the liftoff frame is ignored.
1666 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1667 assertReceivedMotion(ACTION_POINTER_1_UP,
1668 {/*first pointer */ centerPoint + Point(5, 5),
1669 /*second pointer*/ secondPoint});
1670
1671 // Next, the MOVE event for the first pointer
1672 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1673}
1674
Arthur Hungaab25622020-01-16 11:22:11 +08001675TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1676 NotifyMotionArgs args;
1677 const Point centerPoint = mDevice->getCenterPoint();
1678
1679 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001680 mDevice->sendSlot(FIRST_SLOT);
1681 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001682 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1685 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1686
arthurhungcc7f9802020-04-30 17:55:40 +08001687 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001688 const Point secondPoint = centerPoint + Point(100, 100);
1689 mDevice->sendSlot(SECOND_SLOT);
1690 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1691 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001693 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001694 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001695
arthurhungcc7f9802020-04-30 17:55:40 +08001696 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001697 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1701
arthurhungcc7f9802020-04-30 17:55:40 +08001702 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1703 // a palm event.
1704 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001705 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001706 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001707 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001708 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001709 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001710
arthurhungcc7f9802020-04-30 17:55:40 +08001711 // Send up to second slot, expect first slot send moving.
1712 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001713 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001714 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001716
arthurhungcc7f9802020-04-30 17:55:40 +08001717 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001718 mDevice->sendSlot(FIRST_SLOT);
1719 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721
arthurhungcc7f9802020-04-30 17:55:40 +08001722 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1723 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001724}
1725
Prabir Pradhanda20b172022-09-26 17:01:18 +00001726TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1727 const Point centerPoint = mDevice->getCenterPoint();
1728
1729 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1730 mDevice->sendSlot(FIRST_SLOT);
1731 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1732 mDevice->sendToolType(MT_TOOL_PEN);
1733 mDevice->sendDown(centerPoint);
1734 mDevice->sendSync();
1735 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1737 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1738
1739 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1740
1741 // Release the stylus touch.
1742 mDevice->sendUp();
1743 mDevice->sendSync();
1744 ASSERT_NO_FATAL_FAILURE(
1745 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1746
1747 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1748
1749 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1750 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1751 mDevice->sendToolType(MT_TOOL_FINGER);
1752 mDevice->sendDown(centerPoint);
1753 mDevice->sendSync();
1754 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1755 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1756 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1757
1758 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1759
1760 mDevice->sendUp();
1761 mDevice->sendSync();
1762 ASSERT_NO_FATAL_FAILURE(
1763 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1764
1765 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1766 // The policy should be notified of the stylus presence.
1767 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1768 mDevice->sendToolType(MT_TOOL_PEN);
1769 mDevice->sendMove(centerPoint);
1770 mDevice->sendSync();
1771 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1772 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1773 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1774
1775 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1776}
1777
Prabir Pradhan124ea442022-10-28 20:27:44 +00001778// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001779
Prabir Pradhan124ea442022-10-28 20:27:44 +00001780// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1781// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1782// stylus.
1783template <typename UinputStylusDevice>
1784class StylusButtonIntegrationTest : public TouchIntegrationTest {
1785protected:
1786 void SetUp() override {
1787#if !defined(__ANDROID__)
1788 GTEST_SKIP();
1789#endif
1790 TouchIntegrationTest::SetUp();
1791 mTouchscreen = mDevice.get();
1792 mTouchscreenInfo = mDeviceInfo;
1793
1794 setUpStylusDevice();
1795 }
1796
1797 UinputStylusDevice* mStylus{nullptr};
1798 InputDeviceInfo mStylusInfo{};
1799
1800 UinputTouchScreen* mTouchscreen{nullptr};
1801 InputDeviceInfo mTouchscreenInfo{};
1802
1803private:
1804 // When we are attempting to test stylus button events that are sent from the touchscreen,
1805 // use the same Uinput device for the touchscreen and the stylus.
1806 template <typename T = UinputStylusDevice>
1807 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1808 mStylus = mDevice.get();
1809 mStylusInfo = mDeviceInfo;
1810 }
1811
1812 // When we are attempting to stylus buttons from an external stylus being merged with touches
1813 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1814 template <typename T = UinputStylusDevice>
1815 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1816 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1817 mStylus = mStylusDeviceLifecycleTracker.get();
1818 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1819 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1820 const auto info = findDeviceByName(mStylus->getName());
1821 ASSERT_TRUE(info);
1822 mStylusInfo = *info;
1823 }
1824
1825 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1826
1827 // Hide the base class's device to expose it with a different name for readability.
1828 using TouchIntegrationTest::mDevice;
1829 using TouchIntegrationTest::mDeviceInfo;
1830};
1831
1832using StylusButtonIntegrationTestTypes =
1833 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1834TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1835
1836TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1837 const auto stylusId = TestFixture::mStylusInfo.getId();
1838
1839 TestFixture::mStylus->pressKey(BTN_STYLUS);
1840 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1841 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1842 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1843
1844 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1845 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001846 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001847 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001848}
1849
Prabir Pradhan124ea442022-10-28 20:27:44 +00001850TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1851 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1852 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1853 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001854
1855 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001856 TestFixture::mStylus->pressKey(BTN_STYLUS);
1857 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001858 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001860
1861 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001862 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1863 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1864 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1865 TestFixture::mTouchscreen->sendDown(centerPoint);
1866 TestFixture::mTouchscreen->sendSync();
1867 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001868 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1869 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001870 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1871 WithDeviceId(touchscreenId))));
1872 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001873 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1874 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1876 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001877
Prabir Pradhan124ea442022-10-28 20:27:44 +00001878 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1879 TestFixture::mTouchscreen->sendSync();
1880 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001881 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001882 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1883 WithDeviceId(touchscreenId))));
1884 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001885 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001886 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1887 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001888
1889 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1891 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001892 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001893 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001894}
1895
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001896TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
1897 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1898 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1899 const auto stylusId = TestFixture::mStylusInfo.getId();
1900 auto toolTypeDevice =
1901 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1902
1903 // Press the stylus button.
1904 TestFixture::mStylus->pressKey(BTN_STYLUS);
1905 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1906 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1907 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1908
1909 // Start hovering with the stylus.
1910 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1911 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1912 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1913 TestFixture::mTouchscreen->sendMove(centerPoint);
1914 TestFixture::mTouchscreen->sendSync();
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1916 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1918 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1919 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1920 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1921 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1922 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1923 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1924
1925 // Touch down with the stylus.
1926 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1927 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1928 TestFixture::mTouchscreen->sendDown(centerPoint);
1929 TestFixture::mTouchscreen->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1931 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1932 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1933
1934 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1935 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1936 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1937
1938 // Stop touching with the stylus, and start hovering.
1939 TestFixture::mTouchscreen->sendUp();
1940 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1941 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1942 TestFixture::mTouchscreen->sendMove(centerPoint);
1943 TestFixture::mTouchscreen->sendSync();
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1945 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1948 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1949 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1950 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1951 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1952 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1953
1954 // Stop hovering.
1955 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1956 TestFixture::mTouchscreen->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1958 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1959 WithButtonState(0))));
1960 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1961 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1962 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1963 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1964
1965 // Release the stylus button.
1966 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1967 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1968 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1969 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1970}
1971
Prabir Pradhan124ea442022-10-28 20:27:44 +00001972TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
1973 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1974 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1975 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001976
1977 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001978 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1979 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1980 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1981 TestFixture::mTouchscreen->sendDown(centerPoint);
1982 TestFixture::mTouchscreen->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001984 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001985 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1986 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001987
1988 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 TestFixture::mStylus->pressKey(BTN_STYLUS);
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001991 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001992 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1993 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1995 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1997 WithDeviceId(touchscreenId))));
1998 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001999 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2000 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2002 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2008 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002010 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2011 WithDeviceId(touchscreenId))));
2012 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002014 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2015 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002016
2017 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002018 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2019 TestFixture::mTouchscreen->sendSync();
2020 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002022 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2023 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002024}
2025
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002026// --- ExternalStylusIntegrationTest ---
2027
2028// Verify the behavior of an external stylus. An external stylus can report pressure or button
2029// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2030// ongoing stylus gesture that is being emitted by the touchscreen.
2031using ExternalStylusIntegrationTest = TouchIntegrationTest;
2032
2033TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2034 const Point centerPoint = mDevice->getCenterPoint();
2035
2036 // Create an external stylus capable of reporting pressure data that
2037 // should be fused with a touch pointer.
2038 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2039 createUinputDevice<UinputExternalStylusWithPressure>();
2040 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2041 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2042 const auto stylusInfo = findDeviceByName(stylus->getName());
2043 ASSERT_TRUE(stylusInfo);
2044
2045 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2046
2047 const auto touchscreenId = mDeviceInfo.getId();
2048
2049 // Set a pressure value on the stylus. It doesn't generate any events.
2050 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2051 stylus->setPressure(100);
2052 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2053
2054 // Start a finger gesture, and ensure it shows up as stylus gesture
2055 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002056 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002057 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002058 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002059 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002060 mDevice->sendSync();
2061 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2062 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002063 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2064 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002065
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002066 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2067 // event with the updated pressure.
2068 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002069 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2070 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002071 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2072 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002073
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002074 // The external stylus did not generate any events.
2075 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2076 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2077}
2078
2079TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2080 const Point centerPoint = mDevice->getCenterPoint();
2081
2082 // Create an external stylus capable of reporting pressure data that
2083 // should be fused with a touch pointer.
2084 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2085 createUinputDevice<UinputExternalStylusWithPressure>();
2086 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2087 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2088 const auto stylusInfo = findDeviceByName(stylus->getName());
2089 ASSERT_TRUE(stylusInfo);
2090
2091 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2092
2093 const auto touchscreenId = mDeviceInfo.getId();
2094
2095 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2096 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002097 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2098 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002099 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002100 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002101
2102 // Start a finger gesture. The touch device will withhold generating any touches for
2103 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2104 mDevice->sendSlot(FIRST_SLOT);
2105 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2106 mDevice->sendToolType(MT_TOOL_FINGER);
2107 mDevice->sendDown(centerPoint);
2108 auto waitUntil = std::chrono::system_clock::now() +
2109 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002110 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002111 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002112
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 // Since the external stylus did not report a pressure value within the timeout,
2114 // it shows up as a finger pointer.
2115 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2117 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2118 WithPressure(1.f))));
2119
2120 // Change the pressure on the external stylus. Since the pressure was not present at the start
2121 // of the gesture, it is ignored for now.
2122 stylus->setPressure(200);
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2124
2125 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002126 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2127 mDevice->sendSync();
2128 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002130 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2131
2132 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2133 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2134 mDevice->sendToolType(MT_TOOL_FINGER);
2135 mDevice->sendDown(centerPoint);
2136 mDevice->sendSync();
2137 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2138 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2139 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2140 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2141
2142 // The external stylus did not generate any events.
2143 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2144 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002145}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002146
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2148 const Point centerPoint = mDevice->getCenterPoint();
2149
2150 // Create an external stylus device that does not support pressure. It should not affect any
2151 // touch pointers.
2152 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2153 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2154 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2155 const auto stylusInfo = findDeviceByName(stylus->getName());
2156 ASSERT_TRUE(stylusInfo);
2157
2158 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2159
2160 const auto touchscreenId = mDeviceInfo.getId();
2161
2162 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2163 // pressure data from the external stylus.
2164 mDevice->sendSlot(FIRST_SLOT);
2165 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2166 mDevice->sendToolType(MT_TOOL_FINGER);
2167 mDevice->sendDown(centerPoint);
2168 auto waitUntil = std::chrono::system_clock::now() +
2169 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2170 mDevice->sendSync();
2171 ASSERT_NO_FATAL_FAILURE(
2172 mTestListener
2173 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2174 WithToolType(
2175 AMOTION_EVENT_TOOL_TYPE_FINGER),
2176 WithButtonState(0),
2177 WithDeviceId(touchscreenId),
2178 WithPressure(1.f)),
2179 waitUntil));
2180
2181 // The external stylus did not generate any events.
2182 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2183 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2184}
2185
Michael Wrightd02c5b62014-02-10 15:10:22 -08002186// --- InputDeviceTest ---
2187class InputDeviceTest : public testing::Test {
2188protected:
2189 static const char* DEVICE_NAME;
2190 static const char* DEVICE_LOCATION;
2191 static const int32_t DEVICE_ID;
2192 static const int32_t DEVICE_GENERATION;
2193 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002194 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002195 static const int32_t EVENTHUB_ID;
2196 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2197
2198 std::shared_ptr<FakeEventHub> mFakeEventHub;
2199 sp<FakeInputReaderPolicy> mFakePolicy;
2200 std::unique_ptr<TestInputListener> mFakeListener;
2201 std::unique_ptr<InstrumentedInputReader> mReader;
2202 std::shared_ptr<InputDevice> mDevice;
2203
2204 void SetUp() override {
2205 mFakeEventHub = std::make_unique<FakeEventHub>();
2206 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2207 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002208 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002209 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002210 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002211 identifier.name = DEVICE_NAME;
2212 identifier.location = DEVICE_LOCATION;
2213 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2214 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2215 identifier);
2216 mReader->pushNextDevice(mDevice);
2217 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002218 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002219 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002220
2221 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002222 mFakeListener.reset();
2223 mFakePolicy.clear();
2224 }
2225};
2226
2227const char* InputDeviceTest::DEVICE_NAME = "device";
2228const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2229const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2230const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002231const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002232const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2233 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002234const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002235const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2236
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002237TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002238 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002239 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2240 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002241}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002242
Michael Wrightd02c5b62014-02-10 15:10:22 -08002243TEST_F(InputDeviceTest, CountryCodeCorrectlyMapped) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002244 mFakeEventHub->setCountryCode(EVENTHUB_ID, InputDeviceCountryCode::INTERNATIONAL);
2245
Michael Wrightd02c5b62014-02-10 15:10:22 -08002246 // Configuration
2247 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002248 InputReaderConfiguration config;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002249 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2250
Michael Wrightd02c5b62014-02-10 15:10:22 -08002251 ASSERT_EQ(InputDeviceCountryCode::INTERNATIONAL, mDevice->getDeviceInfo().getCountryCode());
2252}
2253
2254TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2255 ASSERT_EQ(mDevice->isEnabled(), false);
2256}
2257
2258TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2259 // Configuration.
2260 InputReaderConfiguration config;
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002261 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002262
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002263 // Reset.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002264 unused += mDevice->reset(ARBITRARY_TIME);
2265
2266 NotifyDeviceResetArgs resetArgs;
2267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2268 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2269 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2270
2271 // Metadata.
2272 ASSERT_TRUE(mDevice->isIgnored());
2273 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2274
2275 InputDeviceInfo info = mDevice->getDeviceInfo();
2276 ASSERT_EQ(DEVICE_ID, info.getId());
2277 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2278 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2279 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2280
2281 // State queries.
2282 ASSERT_EQ(0, mDevice->getMetaState());
2283
2284 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002285 << "Ignored device should return unknown key code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002286 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002287 << "Ignored device should return unknown scan code state.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2289 << "Ignored device should return unknown switch state.";
2290
2291 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2292 uint8_t flags[2] = { 0, 1 };
2293 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002294 << "Ignored device should never mark any key codes.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002295 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2296 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2297}
2298
2299TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2300 // Configuration.
2301 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2302
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002303 FakeInputMapper& mapper1 =
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002304 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002305 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002306 mapper1.setMetaState(AMETA_ALT_ON);
2307 mapper1.addSupportedKeyCode(AKEYCODE_A);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002308 mapper1.addSupportedKeyCode(AKEYCODE_B);
2309 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002310 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2311 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2312 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2313 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002314
Arthur Hung2c9a3342019-07-23 14:18:59 +08002315 FakeInputMapper& mapper2 =
2316 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002317 mapper2.setMetaState(AMETA_SHIFT_ON);
2318
Arthur Hung2c9a3342019-07-23 14:18:59 +08002319 InputReaderConfiguration config;
2320 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2321
2322 std::string propertyValue;
2323 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2324 << "Device should have read configuration during configuration phase.";
2325 ASSERT_EQ("value", propertyValue);
2326
2327 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002328 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
2329
Arthur Hung2c9a3342019-07-23 14:18:59 +08002330 // Reset
2331 unused += mDevice->reset(ARBITRARY_TIME);
2332 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2333 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
2334
2335 NotifyDeviceResetArgs resetArgs;
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2337 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002338 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2339
Arthur Hung2c9a3342019-07-23 14:18:59 +08002340 // Metadata.
2341 ASSERT_FALSE(mDevice->isIgnored());
2342 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2343
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002344 InputDeviceInfo info = mDevice->getDeviceInfo();
2345 ASSERT_EQ(DEVICE_ID, info.getId());
Arthur Hung2c9a3342019-07-23 14:18:59 +08002346 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2347 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2348 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002349
2350 // State queries.
Arthur Hung2c9a3342019-07-23 14:18:59 +08002351 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2352 << "Should query mappers and combine meta states.";
Michael Wrightd02c5b62014-02-10 15:10:22 -08002353
Christine Franks1ba71cc2021-04-07 14:37:42 -07002354 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2355 << "Should return unknown key code state when source not supported.";
2356 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2357 << "Should return unknown scan code state when source not supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002358 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2359 << "Should return unknown switch state when source not supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002360
2361 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2362 << "Should query mapper when source is supported.";
2363 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
Christine Franks2a2293c2022-01-18 11:51:16 -08002364 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002365 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2366 << "Should query mapper when source is supported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002367
2368 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
2369 uint8_t flags[4] = { 0, 0, 0, 1 };
2370 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
2371 << "Should do nothing when source is unsupported.";
2372 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002373 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2374 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002375 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2376
2377 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2378 << "Should query mapper when source is supported.";
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002379 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2380 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
Christine Franks1ba71cc2021-04-07 14:37:42 -07002381 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2382 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2383
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002384 // Event handling.
2385 RawEvent event;
Christine Franks1ba71cc2021-04-07 14:37:42 -07002386 event.deviceId = EVENTHUB_ID;
2387 unused += mDevice->process(&event, 1);
2388
Christine Franks2a2293c2022-01-18 11:51:16 -08002389 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2390 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
2391}
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002392
2393// A single input device is associated with a specific display. Check that:
Christine Franks2a2293c2022-01-18 11:51:16 -08002394// 1. Device is disabled if the viewport corresponding to the associated display is not found
Christine Franks2a2293c2022-01-18 11:51:16 -08002395// 2. Device is disabled when setEnabled API is called
2396TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
2397 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2398
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002399 // First Configuration.
2400 std::list<NotifyArgs> unused =
Christine Franks2a2293c2022-01-18 11:51:16 -08002401 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2402
2403 // Device should be enabled by default.
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002404 ASSERT_TRUE(mDevice->isEnabled());
2405
2406 // Prepare associated info.
2407 constexpr uint8_t hdmi = 1;
2408 const std::string UNIQUE_ID = "local:1";
2409
2410 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
2411 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2412 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2413 // Device should be disabled because it is associated with a specific display via
2414 // input port <-> display port association, but the corresponding display is not found
2415 ASSERT_FALSE(mDevice->isEnabled());
2416
2417 // Prepare displays.
2418 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002419 ui::ROTATION_0, true /*isActive*/, UNIQUE_ID, hdmi,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002420 ViewportType::INTERNAL);
2421 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2422 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2423 ASSERT_TRUE(mDevice->isEnabled());
2424
2425 // Device should be disabled after set disable.
2426 mFakePolicy->addDisabledDevice(mDevice->getId());
2427 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2428 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2429 ASSERT_FALSE(mDevice->isEnabled());
2430
2431 // Device should still be disabled even found the associated display.
2432 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2433 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2434 ASSERT_FALSE(mDevice->isEnabled());
2435}
2436
2437TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2438 // Device should be enabled by default.
2439 mFakePolicy->clearViewports();
2440 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2441 std::list<NotifyArgs> unused =
2442 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
2443 ASSERT_TRUE(mDevice->isEnabled());
2444
2445 // Device should be disabled because it is associated with a specific display, but the
2446 // corresponding display is not found.
2447 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2448 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2449 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2450 ASSERT_FALSE(mDevice->isEnabled());
2451
2452 // Device should be enabled when a display is found.
2453 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002454 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002455 NO_PORT, ViewportType::INTERNAL);
2456 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2457 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2458 ASSERT_TRUE(mDevice->isEnabled());
2459
2460 // Device should be disabled after set disable.
2461 mFakePolicy->addDisabledDevice(mDevice->getId());
2462 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2463 InputReaderConfiguration::CHANGE_ENABLED_STATE);
2464 ASSERT_FALSE(mDevice->isEnabled());
2465
2466 // Device should still be disabled even found the associated display.
2467 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2468 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
2469 ASSERT_FALSE(mDevice->isEnabled());
2470}
2471
2472TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2473 mFakePolicy->clearViewports();
2474 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2475 std::list<NotifyArgs> unused =
2476 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07002477
Michael Wrightd02c5b62014-02-10 15:10:22 -08002478 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2479 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002480 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Chris Ye1b0c7342020-07-28 21:57:03 -07002481 NO_PORT, ViewportType::INTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002482 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002484 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002485}
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002486
arthurhungdcef2dc2020-08-11 14:47:50 +08002487/**
2488 * This test reproduces a crash caused by a dangling reference that remains after device is added
Michael Wrightd02c5b62014-02-10 15:10:22 -08002489 * and removed. The reference is accessed in InputDevice::dump(..);
Chris Ye1b0c7342020-07-28 21:57:03 -07002490 */
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -07002491TEST_F(InputDeviceTest, DumpDoesNotCrash) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002492 constexpr int32_t TEST_EVENTHUB_ID = 10;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002493 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
arthurhungdcef2dc2020-08-11 14:47:50 +08002494
Siarhei Vishniakou18050092021-09-01 13:32:49 -07002495 InputDevice device(mReader->getContext(), 1 /*id*/, 2 /*generation*/, {} /*identifier*/);
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002496 device.addEventHubDevice(TEST_EVENTHUB_ID, true /*populateMappers*/);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2498 std::string dumpStr, eventHubDevStr;
2499 device.dump(dumpStr, eventHubDevStr);
2500}
2501
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002502TEST_F(InputDeviceTest, GetBluetoothAddress) {
2503 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2504 ASSERT_TRUE(address);
2505 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2506}
2507
Michael Wrightd02c5b62014-02-10 15:10:22 -08002508// --- SwitchInputMapperTest ---
2509
2510class SwitchInputMapperTest : public InputMapperTest {
2511protected:
2512};
2513
2514TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002515 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002517 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002518}
2519
2520TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002521 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002523 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002525
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002526 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002527 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002528}
2529
2530TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002531 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002532 std::list<NotifyArgs> out;
2533 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2534 ASSERT_TRUE(out.empty());
2535 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2536 ASSERT_TRUE(out.empty());
2537 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2538 ASSERT_TRUE(out.empty());
2539 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002541 ASSERT_EQ(1u, out.size());
2542 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002544 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2545 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 args.switchMask);
2547 ASSERT_EQ(uint32_t(0), args.policyFlags);
2548}
2549
Chris Ye87143712020-11-10 05:05:58 +00002550// --- VibratorInputMapperTest ---
2551class VibratorInputMapperTest : public InputMapperTest {
2552protected:
2553 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2554};
2555
2556TEST_F(VibratorInputMapperTest, GetSources) {
2557 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2558
2559 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2560}
2561
2562TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2563 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2564
2565 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2566}
2567
2568TEST_F(VibratorInputMapperTest, Vibrate) {
2569 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002570 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002571 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2572
2573 VibrationElement pattern(2);
2574 VibrationSequence sequence(2);
2575 pattern.duration = std::chrono::milliseconds(200);
2576 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 2},
2577 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2578 sequence.addElement(pattern);
2579 pattern.duration = std::chrono::milliseconds(500);
2580 pattern.channels = {{0 /* vibratorId */, DEFAULT_AMPLITUDE / 4},
2581 {1 /* vibratorId */, DEFAULT_AMPLITUDE}};
2582 sequence.addElement(pattern);
2583
2584 std::vector<int64_t> timings = {0, 1};
2585 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2586
2587 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002588 // Start vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002589 std::list<NotifyArgs> out = mapper.vibrate(sequence, -1 /* repeat */, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002590 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002591 // Verify vibrator state listener was notified.
2592 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002593 ASSERT_EQ(1u, out.size());
2594 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2595 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2596 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002597 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002598 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002599 ASSERT_FALSE(mapper.isVibrating());
2600 // Verify vibrator state listener was notified.
2601 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002602 ASSERT_EQ(1u, out.size());
2603 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2604 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2605 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002606}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002607
Chris Yef59a2f42020-10-16 12:55:26 -07002608// --- SensorInputMapperTest ---
2609
2610class SensorInputMapperTest : public InputMapperTest {
2611protected:
2612 static const int32_t ACCEL_RAW_MIN;
2613 static const int32_t ACCEL_RAW_MAX;
2614 static const int32_t ACCEL_RAW_FUZZ;
2615 static const int32_t ACCEL_RAW_FLAT;
2616 static const int32_t ACCEL_RAW_RESOLUTION;
2617
2618 static const int32_t GYRO_RAW_MIN;
2619 static const int32_t GYRO_RAW_MAX;
2620 static const int32_t GYRO_RAW_FUZZ;
2621 static const int32_t GYRO_RAW_FLAT;
2622 static const int32_t GYRO_RAW_RESOLUTION;
2623
2624 static const float GRAVITY_MS2_UNIT;
2625 static const float DEGREE_RADIAN_UNIT;
2626
2627 void prepareAccelAxes();
2628 void prepareGyroAxes();
2629 void setAccelProperties();
2630 void setGyroProperties();
2631 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2632};
2633
2634const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2635const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2636const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2637const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2638const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2639
2640const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2641const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2642const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2643const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2644const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2645
2646const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2647const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2648
2649void SensorInputMapperTest::prepareAccelAxes() {
2650 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2651 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2652 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2653 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2654 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2655 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2656}
2657
2658void SensorInputMapperTest::prepareGyroAxes() {
2659 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2660 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2661 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2662 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2663 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2664 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2665}
2666
2667void SensorInputMapperTest::setAccelProperties() {
2668 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2669 /* sensorDataIndex */ 0);
2670 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2671 /* sensorDataIndex */ 1);
2672 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2673 /* sensorDataIndex */ 2);
2674 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2675 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2676 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2677 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2678 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2679}
2680
2681void SensorInputMapperTest::setGyroProperties() {
2682 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2683 /* sensorDataIndex */ 0);
2684 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2685 /* sensorDataIndex */ 1);
2686 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2687 /* sensorDataIndex */ 2);
2688 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2689 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2690 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2691 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2692 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2693}
2694
2695TEST_F(SensorInputMapperTest, GetSources) {
2696 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2697
2698 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2699}
2700
2701TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2702 setAccelProperties();
2703 prepareAccelAxes();
2704 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2705
2706 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2707 std::chrono::microseconds(10000),
2708 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002709 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002715
2716 NotifySensorArgs args;
2717 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2718 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2719 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2720
2721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2722 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2723 ASSERT_EQ(args.deviceId, DEVICE_ID);
2724 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2725 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2726 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2727 ASSERT_EQ(args.values, values);
2728 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2729}
2730
2731TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2732 setGyroProperties();
2733 prepareGyroAxes();
2734 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2735
2736 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2737 std::chrono::microseconds(10000),
2738 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002739 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2742 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2743 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2744 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002745
2746 NotifySensorArgs args;
2747 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2748 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2749 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2750
2751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2752 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2753 ASSERT_EQ(args.deviceId, DEVICE_ID);
2754 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2755 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2756 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2757 ASSERT_EQ(args.values, values);
2758 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2759}
2760
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761// --- KeyboardInputMapperTest ---
2762
2763class KeyboardInputMapperTest : public InputMapperTest {
2764protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002765 const std::string UNIQUE_ID = "local:0";
2766
Michael Wrighta9cf4192022-12-01 23:46:39 +00002767 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002769 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002770 int32_t originalKeyCode, int32_t rotatedKeyCode,
2771 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002772};
2773
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002774/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2775 * orientation.
2776 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002777void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002778 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2779 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002780}
2781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002782void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002783 int32_t originalScanCode, int32_t originalKeyCode,
2784 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002785 NotifyKeyArgs args;
2786
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002787 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2789 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2790 ASSERT_EQ(originalScanCode, args.scanCode);
2791 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002792 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002793
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002794 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2796 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2797 ASSERT_EQ(originalScanCode, args.scanCode);
2798 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002799 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002800}
2801
Michael Wrightd02c5b62014-02-10 15:10:22 -08002802TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002803 KeyboardInputMapper& mapper =
2804 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002806
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002807 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808}
2809
2810TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2811 const int32_t USAGE_A = 0x070004;
2812 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002813 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2814 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002815 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2816 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2817 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002818
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002819 KeyboardInputMapper& mapper =
2820 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2821 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002822 // Initial metastate is AMETA_NONE.
2823 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824
2825 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002827 NotifyKeyArgs args;
2828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2829 ASSERT_EQ(DEVICE_ID, args.deviceId);
2830 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2831 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2832 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2833 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2834 ASSERT_EQ(KEY_HOME, args.scanCode);
2835 ASSERT_EQ(AMETA_NONE, args.metaState);
2836 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2837 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2838 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2839
2840 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002841 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2843 ASSERT_EQ(DEVICE_ID, args.deviceId);
2844 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2845 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2846 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2847 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2848 ASSERT_EQ(KEY_HOME, args.scanCode);
2849 ASSERT_EQ(AMETA_NONE, args.metaState);
2850 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2851 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2853
2854 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002855 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002857 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2858 ASSERT_EQ(DEVICE_ID, args.deviceId);
2859 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2860 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2861 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2862 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2863 ASSERT_EQ(0, args.scanCode);
2864 ASSERT_EQ(AMETA_NONE, args.metaState);
2865 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2866 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2867 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2868
2869 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002870 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2871 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2873 ASSERT_EQ(DEVICE_ID, args.deviceId);
2874 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2875 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2876 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2877 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2878 ASSERT_EQ(0, args.scanCode);
2879 ASSERT_EQ(AMETA_NONE, args.metaState);
2880 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2881 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2882 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2883
2884 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002885 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2888 ASSERT_EQ(DEVICE_ID, args.deviceId);
2889 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2890 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2891 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2892 ASSERT_EQ(0, args.keyCode);
2893 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2894 ASSERT_EQ(AMETA_NONE, args.metaState);
2895 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2896 ASSERT_EQ(0U, args.policyFlags);
2897 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2898
2899 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2901 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2903 ASSERT_EQ(DEVICE_ID, args.deviceId);
2904 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2906 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2907 ASSERT_EQ(0, args.keyCode);
2908 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2909 ASSERT_EQ(AMETA_NONE, args.metaState);
2910 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2911 ASSERT_EQ(0U, args.policyFlags);
2912 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2913}
2914
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002915TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2916 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2917 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2918 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2919
2920 KeyboardInputMapper& mapper =
2921 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2922 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2923
2924 // Key down by scan code.
2925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2926 NotifyKeyArgs args;
2927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2928 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2929
2930 // Key up by scan code.
2931 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2933 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2934}
2935
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936/**
2937 * Ensure that the readTime is set to the time when the EV_KEY is received.
2938 */
2939TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2940 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2941
2942 KeyboardInputMapper& mapper =
2943 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2944 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2945 NotifyKeyArgs args;
2946
2947 // Key down
2948 process(mapper, ARBITRARY_TIME, 12 /*readTime*/, EV_KEY, KEY_HOME, 1);
2949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2950 ASSERT_EQ(12, args.readTime);
2951
2952 // Key up
2953 process(mapper, ARBITRARY_TIME, 15 /*readTime*/, EV_KEY, KEY_HOME, 1);
2954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2955 ASSERT_EQ(15, args.readTime);
2956}
2957
Michael Wrightd02c5b62014-02-10 15:10:22 -08002958TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002961 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2962 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2963 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002965 KeyboardInputMapper& mapper =
2966 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2967 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968
Arthur Hung95f68612022-04-07 14:08:22 +08002969 // Initial metastate is AMETA_NONE.
2970 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
2972 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974 NotifyKeyArgs args;
2975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2976 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002977 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002979
2980 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002981 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002984 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002985
2986 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002987 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2989 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002990 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002991
2992 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002993 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2995 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08002997 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998}
2999
3000TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003001 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3002 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3003 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3004 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003006 KeyboardInputMapper& mapper =
3007 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3008 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003009
Michael Wrighta9cf4192022-12-01 23:46:39 +00003010 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003011 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3012 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3013 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3014 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3015 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3016 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3017 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3018 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3019}
3020
3021TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003022 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3023 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3024 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3025 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003026
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003028 KeyboardInputMapper& mapper =
3029 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3030 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003031
Michael Wrighta9cf4192022-12-01 23:46:39 +00003032 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003033 ASSERT_NO_FATAL_FAILURE(
3034 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3035 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3036 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3037 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3038 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3039 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3040 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003043 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003044 ASSERT_NO_FATAL_FAILURE(
3045 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3046 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3047 AKEYCODE_DPAD_UP, DISPLAY_ID));
3048 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3049 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3050 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3051 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003052
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003053 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003054 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003055 ASSERT_NO_FATAL_FAILURE(
3056 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3057 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3058 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3059 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3060 AKEYCODE_DPAD_UP, DISPLAY_ID));
3061 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3062 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003064 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003065 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003066 ASSERT_NO_FATAL_FAILURE(
3067 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3068 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3069 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3070 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3071 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3072 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3073 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003074
3075 // Special case: if orientation changes while key is down, we still emit the same keycode
3076 // in the key up as we did in the key down.
3077 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3082 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3083 ASSERT_EQ(KEY_UP, args.scanCode);
3084 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3085
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003086 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003087 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003088 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3090 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3091 ASSERT_EQ(KEY_UP, args.scanCode);
3092 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3093}
3094
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003095TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3096 // If the keyboard is not orientation aware,
3097 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003098 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003099
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003100 KeyboardInputMapper& mapper =
3101 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3102 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003103 NotifyKeyArgs args;
3104
3105 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003106 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3110 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3111
Michael Wrighta9cf4192022-12-01 23:46:39 +00003112 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3117 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3118}
3119
3120TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3121 // If the keyboard is orientation aware,
3122 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003123 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003124
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003125 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003126 KeyboardInputMapper& mapper =
3127 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3128 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003129 NotifyKeyArgs args;
3130
3131 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3132 // ^--- already checked by the previous test
3133
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003135 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3140 ASSERT_EQ(DISPLAY_ID, args.displayId);
3141
3142 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003143 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003144 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003145 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3150 ASSERT_EQ(newDisplayId, args.displayId);
3151}
3152
Michael Wrightd02c5b62014-02-10 15:10:22 -08003153TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003154 KeyboardInputMapper& mapper =
3155 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3156 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003158 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003159 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003161 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163}
3164
Philip Junker4af3b3d2021-12-14 10:36:55 +01003165TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3166 KeyboardInputMapper& mapper =
3167 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3168 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3169
3170 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3171 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3172 << "If a mapping is available, the result is equal to the mapping";
3173
3174 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3175 << "If no mapping is available, the result is the key location";
3176}
3177
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003179 KeyboardInputMapper& mapper =
3180 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3181 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003182
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003183 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003184 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003186 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003187 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003188}
3189
3190TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003191 KeyboardInputMapper& mapper =
3192 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3193 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003195 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Michael Wrightd02c5b62014-02-10 15:10:22 -08003197 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003198 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199 ASSERT_TRUE(flags[0]);
3200 ASSERT_FALSE(flags[1]);
3201}
3202
3203TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003204 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3205 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3206 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003210
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003211 KeyboardInputMapper& mapper =
3212 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3213 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003214 // Initial metastate is AMETA_NONE.
3215 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003216
3217 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003218 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3219 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3220 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
3222 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3224 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003225 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3226 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3227 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003229
3230 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003231 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3232 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003233 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3234 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3235 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003236 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237
3238 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003239 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003241 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3242 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3243 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003244 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003245
3246 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003249 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3250 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3251 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003252 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003253
3254 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003255 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3256 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003257 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3258 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3259 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003260 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261
3262 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003265 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3266 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3267 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003268 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269}
3270
Chris Yea52ade12020-08-27 16:49:20 -07003271TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3272 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3273 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3274 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3276
3277 KeyboardInputMapper& mapper =
3278 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3279 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3280
Chris Yea52ade12020-08-27 16:49:20 -07003281 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003282 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003283 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3284 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3285 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3286 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3287
3288 NotifyKeyArgs args;
3289 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003290 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3292 ASSERT_EQ(AMETA_NONE, args.metaState);
3293 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3294 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3295 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3296
3297 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AMETA_NONE, args.metaState);
3301 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3302 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3303 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3304}
3305
Arthur Hung2c9a3342019-07-23 14:18:59 +08003306TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3307 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003308 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003312
3313 // keyboard 2.
3314 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003315 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003316 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003317 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003318 std::shared_ptr<InputDevice> device2 =
3319 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003320 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003321
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003322 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3323 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3324 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3325 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003327 KeyboardInputMapper& mapper =
3328 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3329 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003330
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003331 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003332 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003333 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003334 std::list<NotifyArgs> unused =
3335 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3336 0 /*changes*/);
3337 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003338
3339 // Prepared displays and associated info.
3340 constexpr uint8_t hdmi1 = 0;
3341 constexpr uint8_t hdmi2 = 1;
3342 const std::string SECONDARY_UNIQUE_ID = "local:1";
3343
3344 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3345 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3346
3347 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003348 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3349 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003350 ASSERT_FALSE(device2->isEnabled());
3351
3352 // Prepare second display.
3353 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003354 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003355 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003356 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003357 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003358 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003359 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3360 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003361
3362 // Device should be enabled after the associated display is found.
3363 ASSERT_TRUE(mDevice->isEnabled());
3364 ASSERT_TRUE(device2->isEnabled());
3365
3366 // Test pad key events
3367 ASSERT_NO_FATAL_FAILURE(
3368 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3369 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3370 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3371 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3372 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3373 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3374 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3375
3376 ASSERT_NO_FATAL_FAILURE(
3377 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3379 AKEYCODE_DPAD_RIGHT, newDisplayId));
3380 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3381 AKEYCODE_DPAD_DOWN, newDisplayId));
3382 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3383 AKEYCODE_DPAD_LEFT, newDisplayId));
3384}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003385
arthurhungc903df12020-08-11 15:08:42 +08003386TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3387 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3388 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3389 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3393
3394 KeyboardInputMapper& mapper =
3395 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3396 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003397 // Initial metastate is AMETA_NONE.
3398 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003399
3400 // Initialization should have turned all of the lights off.
3401 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3402 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3403 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3404
3405 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003408 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3409 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3410
3411 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003414 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3415 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3416
3417 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003420 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3421 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3422
3423 mFakeEventHub->removeDevice(EVENTHUB_ID);
3424 mReader->loopOnce();
3425
3426 // keyboard 2 should default toggle keys.
3427 const std::string USB2 = "USB2";
3428 const std::string DEVICE_NAME2 = "KEYBOARD2";
3429 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3430 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3431 std::shared_ptr<InputDevice> device2 =
3432 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003433 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003434 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3435 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3436 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3437 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3438 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3439 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3440
arthurhung6fe95782020-10-05 22:41:16 +08003441 KeyboardInputMapper& mapper2 =
3442 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003444 std::list<NotifyArgs> unused =
3445 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3446 0 /*changes*/);
3447 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003448
3449 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3450 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3451 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003452 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3453 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003454}
3455
Arthur Hungcb40a002021-08-03 14:31:01 +00003456TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3457 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3458 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3459 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3460
3461 // Suppose we have two mappers. (DPAD + KEYBOARD)
3462 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3463 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3464 KeyboardInputMapper& mapper =
3465 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3466 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003467 // Initial metastate is AMETA_NONE.
3468 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003469
3470 mReader->toggleCapsLockState(DEVICE_ID);
3471 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3472}
3473
Arthur Hungfb3cc112022-04-13 07:39:50 +00003474TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3475 // keyboard 1.
3476 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3477 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3478 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3482
3483 KeyboardInputMapper& mapper1 =
3484 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3486
3487 // keyboard 2.
3488 const std::string USB2 = "USB2";
3489 const std::string DEVICE_NAME2 = "KEYBOARD2";
3490 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3491 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3492 std::shared_ptr<InputDevice> device2 =
3493 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3494 ftl::Flags<InputDeviceClass>(0));
3495 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3496 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3497 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3498 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3499 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3500 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3501
3502 KeyboardInputMapper& mapper2 =
3503 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3504 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003505 std::list<NotifyArgs> unused =
3506 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3507 0 /*changes*/);
3508 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003509
Arthur Hung95f68612022-04-07 14:08:22 +08003510 // Initial metastate is AMETA_NONE.
3511 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3512 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3513
3514 // Toggle num lock on and off.
3515 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3516 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003517 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3518 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3519 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3520
3521 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3522 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3523 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3524 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3525 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3526
3527 // Toggle caps lock on and off.
3528 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3529 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3530 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3531 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3532 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3533
3534 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3535 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3536 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3537 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3538 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3539
3540 // Toggle scroll lock on and off.
3541 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3542 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3543 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3544 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3545 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3546
3547 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3548 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3549 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3550 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3551 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3552}
3553
Arthur Hung2141d542022-08-23 07:45:21 +00003554TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3555 const int32_t USAGE_A = 0x070004;
3556 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3557 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3558
3559 KeyboardInputMapper& mapper =
3560 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3561 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3562 // Key down by scan code.
3563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3564 NotifyKeyArgs args;
3565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(DEVICE_ID, args.deviceId);
3567 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3568 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3569 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3570 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3571 ASSERT_EQ(KEY_HOME, args.scanCode);
3572 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3573
3574 // Disable device, it should synthesize cancellation events for down events.
3575 mFakePolicy->addDisabledDevice(DEVICE_ID);
3576 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3577
3578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3579 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3580 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3581 ASSERT_EQ(KEY_HOME, args.scanCode);
3582 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3583}
3584
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585// --- KeyboardInputMapperTest_ExternalDevice ---
3586
3587class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3588protected:
Chris Yea52ade12020-08-27 16:49:20 -07003589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003590};
3591
3592TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003593 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3594 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003595
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003596 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3597 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3598 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3599 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003600
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003601 KeyboardInputMapper& mapper =
3602 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3603 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003604
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003605 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003606 NotifyKeyArgs args;
3607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3608 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3609
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(uint32_t(0), args.policyFlags);
3613
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(uint32_t(0), args.policyFlags);
3617
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3620 ASSERT_EQ(uint32_t(0), args.policyFlags);
3621
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3624 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3625
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003626 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3628 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3629}
3630
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003631TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003632 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3635 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3636 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003637
Powei Fengd041c5d2019-05-03 17:11:33 -07003638 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 KeyboardInputMapper& mapper =
3640 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3641 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003642
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003644 NotifyKeyArgs args;
3645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3646 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3647
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3650 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3651
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003652 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3654 ASSERT_EQ(uint32_t(0), args.policyFlags);
3655
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003656 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3658 ASSERT_EQ(uint32_t(0), args.policyFlags);
3659
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003660 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3662 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3663
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003664 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3666 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3667}
3668
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669// --- CursorInputMapperTest ---
3670
3671class CursorInputMapperTest : public InputMapperTest {
3672protected:
3673 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3674
Michael Wright17db18e2020-06-26 20:51:44 +01003675 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676
Chris Yea52ade12020-08-27 16:49:20 -07003677 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003678 InputMapperTest::SetUp();
3679
Michael Wright17db18e2020-06-26 20:51:44 +01003680 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003681 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682 }
3683
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003684 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3685 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003686
Michael Wrighta9cf4192022-12-01 23:46:39 +00003687 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003688 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3689 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3690 }
3691
3692 void prepareSecondaryDisplay() {
3693 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003694 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003695 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003696 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003697
3698 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3699 float pressure) {
3700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3701 0.0f, 0.0f, 0.0f, EPSILON));
3702 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003703};
3704
3705const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3708 int32_t originalY, int32_t rotatedX,
3709 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003710 NotifyMotionArgs args;
3711
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003717 ASSERT_NO_FATAL_FAILURE(
3718 assertCursorPointerCoords(args.pointerCoords[0],
3719 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3720 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721}
3722
3723TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003724 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003725 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003726
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003727 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003728}
3729
3730TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003731 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003734 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003735}
3736
3737TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003739 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003740
3741 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003742 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003743
3744 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003745 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3746 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003747 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3748 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3749
3750 // When the bounds are set, then there should be a valid motion range.
3751 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3752
3753 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003754 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003755
3756 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3757 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3758 1, 800 - 1, 0.0f, 0.0f));
3759 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3760 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3761 2, 480 - 1, 0.0f, 0.0f));
3762 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3763 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3764 0.0f, 1.0f, 0.0f, 0.0f));
3765}
3766
3767TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003768 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003769 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003770
3771 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003772 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003773
3774 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3775 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3776 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3777 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3778 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3779 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3780 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3781 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3782 0.0f, 1.0f, 0.0f, 0.0f));
3783}
3784
3785TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003786 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003787 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003788
arthurhungdcef2dc2020-08-11 14:47:50 +08003789 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003790
3791 NotifyMotionArgs args;
3792
3793 // Button press.
3794 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3796 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3798 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3799 ASSERT_EQ(DEVICE_ID, args.deviceId);
3800 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3801 ASSERT_EQ(uint32_t(0), args.policyFlags);
3802 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3803 ASSERT_EQ(0, args.flags);
3804 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3805 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3806 ASSERT_EQ(0, args.edgeFlags);
3807 ASSERT_EQ(uint32_t(1), args.pointerCount);
3808 ASSERT_EQ(0, args.pointerProperties[0].id);
3809 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003810 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3812 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3813 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3814
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3816 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3817 ASSERT_EQ(DEVICE_ID, args.deviceId);
3818 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3819 ASSERT_EQ(uint32_t(0), args.policyFlags);
3820 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3821 ASSERT_EQ(0, args.flags);
3822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3823 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3824 ASSERT_EQ(0, args.edgeFlags);
3825 ASSERT_EQ(uint32_t(1), args.pointerCount);
3826 ASSERT_EQ(0, args.pointerProperties[0].id);
3827 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003828 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003829 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3830 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3831 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3832
Michael Wrightd02c5b62014-02-10 15:10:22 -08003833 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003834 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3835 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3837 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3838 ASSERT_EQ(DEVICE_ID, args.deviceId);
3839 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3840 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003841 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3842 ASSERT_EQ(0, args.flags);
3843 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3844 ASSERT_EQ(0, args.buttonState);
3845 ASSERT_EQ(0, args.edgeFlags);
3846 ASSERT_EQ(uint32_t(1), args.pointerCount);
3847 ASSERT_EQ(0, args.pointerProperties[0].id);
3848 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003849 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003850 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3851 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3852 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3853
3854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3855 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3856 ASSERT_EQ(DEVICE_ID, args.deviceId);
3857 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3858 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003859 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3860 ASSERT_EQ(0, args.flags);
3861 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3862 ASSERT_EQ(0, args.buttonState);
3863 ASSERT_EQ(0, args.edgeFlags);
3864 ASSERT_EQ(uint32_t(1), args.pointerCount);
3865 ASSERT_EQ(0, args.pointerProperties[0].id);
3866 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003867 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003868 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3869 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3870 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3871}
3872
3873TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003874 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003875 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003876
3877 NotifyMotionArgs args;
3878
3879 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003884 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3885 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3886 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003887
3888 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003893 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3894 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003895}
3896
3897TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003898 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003899 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003900
3901 NotifyMotionArgs args;
3902
3903 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3907 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003908 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003909
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3911 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003912 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003913
Michael Wrightd02c5b62014-02-10 15:10:22 -08003914 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3916 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003919 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003920
3921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003922 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003923 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003924}
3925
3926TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003928 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003929
3930 NotifyMotionArgs args;
3931
3932 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003933 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3934 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3938 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003939 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3940 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3941 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3944 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003945 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3946 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3947 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003948
Michael Wrightd02c5b62014-02-10 15:10:22 -08003949 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003950 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
3951 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
3952 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003955 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3956 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
3957 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003958
3959 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003960 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003964 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003965
3966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003967 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003968 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003969}
3970
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003971TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003972 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003973 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003974 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
3975 // need to be rotated.
3976 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003977 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003978
Michael Wrighta9cf4192022-12-01 23:46:39 +00003979 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003980 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
3981 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
3982 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
3983 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
3984 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
3985 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
3986 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
3987 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
3988}
3989
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003990TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003991 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07003993 // Since InputReader works in the un-rotated coordinate space, only devices that are not
3994 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003995 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003996
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003997 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003998 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003999 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4000 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4001 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4002 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4003 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4004 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4005 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4006 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4007
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004008 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004009 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004010 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4011 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4012 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4013 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4014 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4015 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4016 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4017 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004018
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004019 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004020 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4022 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4023 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4024 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4025 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4026 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4027 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4028 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4029
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004030 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004031 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004032 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4033 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4034 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4035 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4036 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4037 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4038 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4039 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004040}
4041
4042TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004043 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004044 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004045
4046 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4047 mFakePointerController->setPosition(100, 200);
4048 mFakePointerController->setButtonState(0);
4049
4050 NotifyMotionArgs motionArgs;
4051 NotifyKeyArgs keyArgs;
4052
4053 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004054 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4055 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4057 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4058 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4059 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004060 ASSERT_NO_FATAL_FAILURE(
4061 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004062
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4064 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4065 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4066 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004067 ASSERT_NO_FATAL_FAILURE(
4068 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004069
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004070 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4071 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004074 ASSERT_EQ(0, motionArgs.buttonState);
4075 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004076 ASSERT_NO_FATAL_FAILURE(
4077 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004078
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004081 ASSERT_EQ(0, motionArgs.buttonState);
4082 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004083 ASSERT_NO_FATAL_FAILURE(
4084 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004085
4086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004087 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004088 ASSERT_EQ(0, motionArgs.buttonState);
4089 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004090 ASSERT_NO_FATAL_FAILURE(
4091 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004092
4093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4100 motionArgs.buttonState);
4101 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4102 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004103 ASSERT_NO_FATAL_FAILURE(
4104 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004105
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4108 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4109 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4110 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004111 ASSERT_NO_FATAL_FAILURE(
4112 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004113
4114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4115 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4116 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4117 motionArgs.buttonState);
4118 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4119 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004120 ASSERT_NO_FATAL_FAILURE(
4121 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4128 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004129 ASSERT_NO_FATAL_FAILURE(
4130 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131
4132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004133 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004134 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4135 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004136 ASSERT_NO_FATAL_FAILURE(
4137 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004142 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4143 ASSERT_EQ(0, motionArgs.buttonState);
4144 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004145 ASSERT_NO_FATAL_FAILURE(
4146 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004149
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004151 ASSERT_EQ(0, motionArgs.buttonState);
4152 ASSERT_EQ(0, mFakePointerController->getButtonState());
4153 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004154 ASSERT_NO_FATAL_FAILURE(
4155 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156
Michael Wrightd02c5b62014-02-10 15:10:22 -08004157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4158 ASSERT_EQ(0, motionArgs.buttonState);
4159 ASSERT_EQ(0, mFakePointerController->getButtonState());
4160 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004161 ASSERT_NO_FATAL_FAILURE(
4162 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004163
4164 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4168 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4169 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004170
Michael Wrightd02c5b62014-02-10 15:10:22 -08004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004172 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4174 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004175 ASSERT_NO_FATAL_FAILURE(
4176 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004177
4178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4181 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004182 ASSERT_NO_FATAL_FAILURE(
4183 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004188 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189 ASSERT_EQ(0, motionArgs.buttonState);
4190 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004191 ASSERT_NO_FATAL_FAILURE(
4192 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004193
4194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004196 ASSERT_EQ(0, motionArgs.buttonState);
4197 ASSERT_EQ(0, mFakePointerController->getButtonState());
4198
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004199 ASSERT_NO_FATAL_FAILURE(
4200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4202 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4203 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4204
4205 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004206 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4209 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4210 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004211
Michael Wrightd02c5b62014-02-10 15:10:22 -08004212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004214 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4215 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004216 ASSERT_NO_FATAL_FAILURE(
4217 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004218
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4220 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4221 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4222 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004223 ASSERT_NO_FATAL_FAILURE(
4224 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004229 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004230 ASSERT_EQ(0, motionArgs.buttonState);
4231 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004232 ASSERT_NO_FATAL_FAILURE(
4233 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004234
4235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4236 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4237 ASSERT_EQ(0, motionArgs.buttonState);
4238 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004239 ASSERT_NO_FATAL_FAILURE(
4240 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004241
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4244 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4245
4246 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4248 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4250 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4251 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004252
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4256 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004257 ASSERT_NO_FATAL_FAILURE(
4258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004259
4260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4261 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4262 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4263 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004264 ASSERT_NO_FATAL_FAILURE(
4265 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004270 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271 ASSERT_EQ(0, motionArgs.buttonState);
4272 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(
4274 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004275
4276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4278 ASSERT_EQ(0, motionArgs.buttonState);
4279 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004280 ASSERT_NO_FATAL_FAILURE(
4281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004282
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4284 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4285 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4286
4287 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004288 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4289 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4291 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4292 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004293
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4297 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004298 ASSERT_NO_FATAL_FAILURE(
4299 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004300
4301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4302 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4303 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4304 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004305 ASSERT_NO_FATAL_FAILURE(
4306 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004308 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004311 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312 ASSERT_EQ(0, motionArgs.buttonState);
4313 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004314 ASSERT_NO_FATAL_FAILURE(
4315 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004316
4317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4318 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4319 ASSERT_EQ(0, motionArgs.buttonState);
4320 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004321 ASSERT_NO_FATAL_FAILURE(
4322 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004323
Michael Wrightd02c5b62014-02-10 15:10:22 -08004324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4325 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4326 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4327}
4328
4329TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004330 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004331 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4334 mFakePointerController->setPosition(100, 200);
4335 mFakePointerController->setButtonState(0);
4336
4337 NotifyMotionArgs args;
4338
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4341 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004343 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4345 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4346 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004347 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004348}
4349
4350TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004351 addConfigurationProperty("cursor.mode", "pointer");
4352 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004353 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004354
4355 NotifyDeviceResetArgs resetArgs;
4356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4357 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4358 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4359
4360 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4361 mFakePointerController->setPosition(100, 200);
4362 mFakePointerController->setButtonState(0);
4363
4364 NotifyMotionArgs args;
4365
4366 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004367 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4368 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4369 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4371 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4372 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4373 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4374 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004375 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004376
4377 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4381 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4382 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4384 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4386 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4388 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4389 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4390
4391 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004392 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4393 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4395 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4396 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4397 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4398 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4400 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4401 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4402 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4403 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4404
4405 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4410 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4412 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4413 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004414 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004415
4416 // Disable pointer capture and check that the device generation got bumped
4417 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004418 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 mFakePolicy->setPointerCapture(false);
4420 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004421 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004424 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4425
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004426 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4427 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4430 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4433 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004434 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004437/**
4438 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4439 * pointer acceleration or speed processing should not be applied.
4440 */
4441TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4442 addConfigurationProperty("cursor.mode", "pointer");
4443 const VelocityControlParameters testParams(5.f /*scale*/, 0.f /*low threshold*/,
4444 100.f /*high threshold*/, 10.f /*acceleration*/);
4445 mFakePolicy->setVelocityControlParams(testParams);
4446 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4447
4448 NotifyDeviceResetArgs resetArgs;
4449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4450 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4451 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4452
4453 NotifyMotionArgs args;
4454
4455 // Move and verify scale is applied.
4456 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4460 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4461 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4462 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4463 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4464 ASSERT_GT(relX, 10);
4465 ASSERT_GT(relY, 20);
4466
4467 // Enable Pointer Capture
4468 mFakePolicy->setPointerCapture(true);
4469 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4470 NotifyPointerCaptureChangedArgs captureArgs;
4471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4472 ASSERT_TRUE(captureArgs.request.enable);
4473
4474 // Move and verify scale is not applied.
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4479 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4480 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4481 ASSERT_EQ(10, args.pointerCoords[0].getX());
4482 ASSERT_EQ(20, args.pointerCoords[0].getY());
4483}
4484
Prabir Pradhan208360b2022-06-24 18:37:04 +00004485TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4486 addConfigurationProperty("cursor.mode", "pointer");
4487 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4488
4489 NotifyDeviceResetArgs resetArgs;
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4491 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
4494 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004495 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004496
4497 NotifyMotionArgs args;
4498
4499 // Verify that the coordinates are rotated.
4500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4504 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4505 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4506 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4507 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4508
4509 // Enable Pointer Capture.
4510 mFakePolicy->setPointerCapture(true);
4511 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4512 NotifyPointerCaptureChangedArgs captureArgs;
4513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4514 ASSERT_TRUE(captureArgs.request.enable);
4515
4516 // Move and verify rotation is not applied.
4517 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4521 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4523 ASSERT_EQ(10, args.pointerCoords[0].getX());
4524 ASSERT_EQ(20, args.pointerCoords[0].getY());
4525}
4526
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004527TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004528 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004529
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004530 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004531 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004532
4533 // Set up the secondary display as the display on which the pointer should be shown.
4534 // The InputDevice is not associated with any display.
4535 prepareSecondaryDisplay();
4536 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004537 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4538
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004539 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004540 mFakePointerController->setPosition(100, 200);
4541 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004542
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004543 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4546 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4549 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4550 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004551 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004552}
4553
4554TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004558 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004559
4560 // Set up the secondary display as the display on which the pointer should be shown,
4561 // and associate the InputDevice with the secondary display.
4562 prepareSecondaryDisplay();
4563 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4564 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4565 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4566
4567 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4568 mFakePointerController->setPosition(100, 200);
4569 mFakePointerController->setButtonState(0);
4570
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4572 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4573 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4576 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4577 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004578 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004579}
4580
4581TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4582 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4583
4584 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004585 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004586 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4587
4588 // Associate the InputDevice with the secondary display.
4589 prepareSecondaryDisplay();
4590 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4591 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4592
4593 // The mapper should not generate any events because it is associated with a display that is
4594 // different from the pointer display.
4595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4596 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4597 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004599}
4600
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004601// --- BluetoothCursorInputMapperTest ---
4602
4603class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4604protected:
4605 void SetUp() override {
4606 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4607
4608 mFakePointerController = std::make_shared<FakePointerController>();
4609 mFakePolicy->setPointerController(mFakePointerController);
4610 }
4611};
4612
4613TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4614 addConfigurationProperty("cursor.mode", "pointer");
4615 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4616
4617 nsecs_t kernelEventTime = ARBITRARY_TIME;
4618 nsecs_t expectedEventTime = ARBITRARY_TIME;
4619 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4620 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4622 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4623 WithEventTime(expectedEventTime))));
4624
4625 // Process several events that come in quick succession, according to their timestamps.
4626 for (int i = 0; i < 3; i++) {
4627 constexpr static nsecs_t delta = ms2ns(1);
4628 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4629 kernelEventTime += delta;
4630 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4631
4632 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4633 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4635 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4636 WithEventTime(expectedEventTime))));
4637 }
4638}
4639
4640TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4641 addConfigurationProperty("cursor.mode", "pointer");
4642 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4643
4644 nsecs_t expectedEventTime = ARBITRARY_TIME;
4645 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4646 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4648 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4649 WithEventTime(expectedEventTime))));
4650
4651 // Process several events with the same timestamp from the kernel.
4652 // Ensure that we do not generate events too far into the future.
4653 constexpr static int32_t numEvents =
4654 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4655 for (int i = 0; i < numEvents; i++) {
4656 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4657
4658 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4659 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4661 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4662 WithEventTime(expectedEventTime))));
4663 }
4664
4665 // By processing more events with the same timestamp, we should not generate events with a
4666 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4667 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4668 for (int i = 0; i < 3; i++) {
4669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4672 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4673 WithEventTime(cappedEventTime))));
4674 }
4675}
4676
4677TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4678 addConfigurationProperty("cursor.mode", "pointer");
4679 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4680
4681 nsecs_t kernelEventTime = ARBITRARY_TIME;
4682 nsecs_t expectedEventTime = ARBITRARY_TIME;
4683 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4684 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4686 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4687 WithEventTime(expectedEventTime))));
4688
4689 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4690 // smoothening is not needed, its timestamp is not affected.
4691 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4692 expectedEventTime = kernelEventTime;
4693
4694 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4695 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4698 WithEventTime(expectedEventTime))));
4699}
4700
Michael Wrightd02c5b62014-02-10 15:10:22 -08004701// --- TouchInputMapperTest ---
4702
4703class TouchInputMapperTest : public InputMapperTest {
4704protected:
4705 static const int32_t RAW_X_MIN;
4706 static const int32_t RAW_X_MAX;
4707 static const int32_t RAW_Y_MIN;
4708 static const int32_t RAW_Y_MAX;
4709 static const int32_t RAW_TOUCH_MIN;
4710 static const int32_t RAW_TOUCH_MAX;
4711 static const int32_t RAW_TOOL_MIN;
4712 static const int32_t RAW_TOOL_MAX;
4713 static const int32_t RAW_PRESSURE_MIN;
4714 static const int32_t RAW_PRESSURE_MAX;
4715 static const int32_t RAW_ORIENTATION_MIN;
4716 static const int32_t RAW_ORIENTATION_MAX;
4717 static const int32_t RAW_DISTANCE_MIN;
4718 static const int32_t RAW_DISTANCE_MAX;
4719 static const int32_t RAW_TILT_MIN;
4720 static const int32_t RAW_TILT_MAX;
4721 static const int32_t RAW_ID_MIN;
4722 static const int32_t RAW_ID_MAX;
4723 static const int32_t RAW_SLOT_MIN;
4724 static const int32_t RAW_SLOT_MAX;
4725 static const float X_PRECISION;
4726 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004727 static const float X_PRECISION_VIRTUAL;
4728 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729
4730 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004731 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732
4733 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4734
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004735 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004736 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004737
Michael Wrightd02c5b62014-02-10 15:10:22 -08004738 enum Axes {
4739 POSITION = 1 << 0,
4740 TOUCH = 1 << 1,
4741 TOOL = 1 << 2,
4742 PRESSURE = 1 << 3,
4743 ORIENTATION = 1 << 4,
4744 MINOR = 1 << 5,
4745 ID = 1 << 6,
4746 DISTANCE = 1 << 7,
4747 TILT = 1 << 8,
4748 SLOT = 1 << 9,
4749 TOOL_TYPE = 1 << 10,
4750 };
4751
Michael Wrighta9cf4192022-12-01 23:46:39 +00004752 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004753 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004754 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004756 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004757 int32_t toRawX(float displayX);
4758 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004759 int32_t toRotatedRawX(float displayX);
4760 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004761 float toCookedX(float rawX, float rawY);
4762 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004764 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004766 float toDisplayY(int32_t rawY, int32_t displayHeight);
4767
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768};
4769
4770const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4771const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4772const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4773const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4774const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4775const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4776const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4777const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004778const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4779const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004780const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4781const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4782const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4783const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4784const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4785const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4786const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4787const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4788const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4789const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4790const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4791const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004792const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4793 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4794const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4795 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004796const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4797 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004798
4799const float TouchInputMapperTest::GEOMETRIC_SCALE =
4800 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4801 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4802
4803const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4804 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4805 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4806};
4807
Michael Wrighta9cf4192022-12-01 23:46:39 +00004808void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004809 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4810 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004811}
4812
4813void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4814 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004815 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004816}
4817
Michael Wrighta9cf4192022-12-01 23:46:39 +00004818void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004819 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4820 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4821 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004822}
4823
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004825 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4826 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4827 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4828 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829}
4830
Jason Gerecke489fda82012-09-07 17:19:40 -07004831void TouchInputMapperTest::prepareLocationCalibration() {
4832 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4833}
4834
Michael Wrightd02c5b62014-02-10 15:10:22 -08004835int32_t TouchInputMapperTest::toRawX(float displayX) {
4836 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4837}
4838
4839int32_t TouchInputMapperTest::toRawY(float displayY) {
4840 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4841}
4842
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004843int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4844 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4845}
4846
4847int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4848 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4849}
4850
Jason Gerecke489fda82012-09-07 17:19:40 -07004851float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4852 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4853 return rawX;
4854}
4855
4856float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4857 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4858 return rawY;
4859}
4860
Michael Wrightd02c5b62014-02-10 15:10:22 -08004861float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004862 return toDisplayX(rawX, DISPLAY_WIDTH);
4863}
4864
4865float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4866 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004867}
4868
4869float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004870 return toDisplayY(rawY, DISPLAY_HEIGHT);
4871}
4872
4873float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4874 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004875}
4876
4877
4878// --- SingleTouchInputMapperTest ---
4879
4880class SingleTouchInputMapperTest : public TouchInputMapperTest {
4881protected:
4882 void prepareButtons();
4883 void prepareAxes(int axes);
4884
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004885 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4886 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4887 void processUp(SingleTouchInputMapper& mappery);
4888 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4889 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4890 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4891 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4892 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4893 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894};
4895
4896void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004897 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004898}
4899
4900void SingleTouchInputMapperTest::prepareAxes(int axes) {
4901 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4903 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004904 }
4905 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4907 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 }
4909 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4911 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004912 }
4913 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004914 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4915 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004916 }
4917 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004918 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4919 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004920 }
4921}
4922
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004923void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004924 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4925 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004927}
4928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004929void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4931 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932}
4933
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004934void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004935 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936}
4937
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004938void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004939 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940}
4941
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004942void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4943 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004944 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945}
4946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004947void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004949}
4950
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004951void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4952 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004953 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4954 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004955}
4956
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004957void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4958 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004959 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960}
4961
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004962void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004963 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964}
4965
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 prepareButtons();
4968 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004969 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004970
Harry Cutts16a24cc2022-10-26 15:22:19 +00004971 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972}
4973
Michael Wrightd02c5b62014-02-10 15:10:22 -08004974TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004975 prepareButtons();
4976 prepareAxes(POSITION);
4977 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004978 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004979
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004980 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004981}
4982
4983TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004985 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004986 prepareButtons();
4987 prepareAxes(POSITION);
4988 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004989 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004990
4991 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004992 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993
4994 // Virtual key is down.
4995 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4996 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4997 processDown(mapper, x, y);
4998 processSync(mapper);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005001 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005002
5003 // Virtual key is up.
5004 processUp(mapper);
5005 processSync(mapper);
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5007
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005008 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005009}
5010
5011TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005013 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 prepareButtons();
5015 prepareAxes(POSITION);
5016 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005017 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005018
5019 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005020 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005021
5022 // Virtual key is down.
5023 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5024 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5025 processDown(mapper, x, y);
5026 processSync(mapper);
5027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5028
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005029 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 // Virtual key is up.
5032 processUp(mapper);
5033 processSync(mapper);
5034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5035
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005036 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005037}
5038
5039TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005041 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042 prepareButtons();
5043 prepareAxes(POSITION);
5044 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005046
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005048 ASSERT_TRUE(
5049 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005050 ASSERT_TRUE(flags[0]);
5051 ASSERT_FALSE(flags[1]);
5052}
5053
5054TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005056 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005057 prepareButtons();
5058 prepareAxes(POSITION);
5059 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
arthurhungdcef2dc2020-08-11 14:47:50 +08005062 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005063
5064 NotifyKeyArgs args;
5065
5066 // Press virtual key.
5067 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5068 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5069 processDown(mapper, x, y);
5070 processSync(mapper);
5071
5072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5073 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5074 ASSERT_EQ(DEVICE_ID, args.deviceId);
5075 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5076 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5077 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5078 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5079 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5080 ASSERT_EQ(KEY_HOME, args.scanCode);
5081 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5082 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5083
5084 // Release virtual key.
5085 processUp(mapper);
5086 processSync(mapper);
5087
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5089 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5090 ASSERT_EQ(DEVICE_ID, args.deviceId);
5091 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5092 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5093 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5094 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5095 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5096 ASSERT_EQ(KEY_HOME, args.scanCode);
5097 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5098 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5099
5100 // Should not have sent any motions.
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5102}
5103
5104TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005106 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005107 prepareButtons();
5108 prepareAxes(POSITION);
5109 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005110 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005111
arthurhungdcef2dc2020-08-11 14:47:50 +08005112 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005113
5114 NotifyKeyArgs keyArgs;
5115
5116 // Press virtual key.
5117 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5118 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5119 processDown(mapper, x, y);
5120 processSync(mapper);
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5123 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5124 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5125 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5126 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5127 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5128 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5129 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5130 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5131 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5132 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5133
5134 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5135 // into the display area.
5136 y -= 100;
5137 processMove(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5141 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5142 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5147 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5148 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5149 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5150 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5151 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5152
5153 NotifyMotionArgs motionArgs;
5154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5155 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5156 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5157 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5158 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5160 ASSERT_EQ(0, motionArgs.flags);
5161 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5162 ASSERT_EQ(0, motionArgs.buttonState);
5163 ASSERT_EQ(0, motionArgs.edgeFlags);
5164 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5165 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5168 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5169 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5170 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5171 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5172
5173 // Keep moving out of bounds. Should generate a pointer move.
5174 y -= 50;
5175 processMove(mapper, x, y);
5176 processSync(mapper);
5177
5178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5180 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5181 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5182 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5183 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5184 ASSERT_EQ(0, motionArgs.flags);
5185 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5186 ASSERT_EQ(0, motionArgs.buttonState);
5187 ASSERT_EQ(0, motionArgs.edgeFlags);
5188 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5189 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5192 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5193 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5194 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5195 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5196
5197 // Release out of bounds. Should generate a pointer up.
5198 processUp(mapper);
5199 processSync(mapper);
5200
5201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5202 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5203 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5204 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5205 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5206 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5207 ASSERT_EQ(0, motionArgs.flags);
5208 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5209 ASSERT_EQ(0, motionArgs.buttonState);
5210 ASSERT_EQ(0, motionArgs.edgeFlags);
5211 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5212 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5215 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5216 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5217 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5218 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5219
5220 // Should not have sent any more keys or motions.
5221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5223}
5224
5225TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005226 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005227 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005228 prepareButtons();
5229 prepareAxes(POSITION);
5230 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005231 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005232
arthurhungdcef2dc2020-08-11 14:47:50 +08005233 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005234
5235 NotifyMotionArgs motionArgs;
5236
5237 // Initially go down out of bounds.
5238 int32_t x = -10;
5239 int32_t y = -10;
5240 processDown(mapper, x, y);
5241 processSync(mapper);
5242
5243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5244
5245 // Move into the display area. Should generate a pointer down.
5246 x = 50;
5247 y = 75;
5248 processMove(mapper, x, y);
5249 processSync(mapper);
5250
5251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5253 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5254 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5255 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5256 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5257 ASSERT_EQ(0, motionArgs.flags);
5258 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5259 ASSERT_EQ(0, motionArgs.buttonState);
5260 ASSERT_EQ(0, motionArgs.edgeFlags);
5261 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5262 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5263 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5265 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5266 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5267 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5269
5270 // Release. Should generate a pointer up.
5271 processUp(mapper);
5272 processSync(mapper);
5273
5274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5276 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5277 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5278 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5279 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5280 ASSERT_EQ(0, motionArgs.flags);
5281 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5282 ASSERT_EQ(0, motionArgs.buttonState);
5283 ASSERT_EQ(0, motionArgs.edgeFlags);
5284 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5285 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5286 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5287 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5288 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5289 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5290 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5292
5293 // Should not have sent any more keys or motions.
5294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5296}
5297
Santos Cordonfa5cf462017-04-05 10:37:00 -07005298TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005299 addConfigurationProperty("touch.deviceType", "touchScreen");
5300 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5301
Michael Wrighta9cf4192022-12-01 23:46:39 +00005302 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005303 prepareButtons();
5304 prepareAxes(POSITION);
5305 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005306 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005307
arthurhungdcef2dc2020-08-11 14:47:50 +08005308 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005309
5310 NotifyMotionArgs motionArgs;
5311
5312 // Down.
5313 int32_t x = 100;
5314 int32_t y = 125;
5315 processDown(mapper, x, y);
5316 processSync(mapper);
5317
5318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5320 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5321 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5325 ASSERT_EQ(0, motionArgs.flags);
5326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5327 ASSERT_EQ(0, motionArgs.buttonState);
5328 ASSERT_EQ(0, motionArgs.edgeFlags);
5329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5334 1, 0, 0, 0, 0, 0, 0, 0));
5335 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5336 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5337 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5338
5339 // Move.
5340 x += 50;
5341 y += 75;
5342 processMove(mapper, x, y);
5343 processSync(mapper);
5344
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5346 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5347 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5348 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5349 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5350 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5351 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5352 ASSERT_EQ(0, motionArgs.flags);
5353 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5354 ASSERT_EQ(0, motionArgs.buttonState);
5355 ASSERT_EQ(0, motionArgs.edgeFlags);
5356 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5357 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5358 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5359 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5360 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5361 1, 0, 0, 0, 0, 0, 0, 0));
5362 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5363 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5364 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5365
5366 // Up.
5367 processUp(mapper);
5368 processSync(mapper);
5369
5370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5371 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5372 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5373 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5375 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5376 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5377 ASSERT_EQ(0, motionArgs.flags);
5378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5379 ASSERT_EQ(0, motionArgs.buttonState);
5380 ASSERT_EQ(0, motionArgs.edgeFlags);
5381 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5382 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5383 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5385 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5386 1, 0, 0, 0, 0, 0, 0, 0));
5387 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5388 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5390
5391 // Should not have sent any more keys or motions.
5392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5394}
5395
Michael Wrightd02c5b62014-02-10 15:10:22 -08005396TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005398 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005399 prepareButtons();
5400 prepareAxes(POSITION);
5401 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005402 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005403
arthurhungdcef2dc2020-08-11 14:47:50 +08005404 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005405
5406 NotifyMotionArgs motionArgs;
5407
5408 // Down.
5409 int32_t x = 100;
5410 int32_t y = 125;
5411 processDown(mapper, x, y);
5412 processSync(mapper);
5413
5414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5416 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5429 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5430 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5432
5433 // Move.
5434 x += 50;
5435 y += 75;
5436 processMove(mapper, x, y);
5437 processSync(mapper);
5438
5439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5440 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5441 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5454 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5455 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5456 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5457
5458 // Up.
5459 processUp(mapper);
5460 processSync(mapper);
5461
5462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5463 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5464 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5465 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5466 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5467 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5468 ASSERT_EQ(0, motionArgs.flags);
5469 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5470 ASSERT_EQ(0, motionArgs.buttonState);
5471 ASSERT_EQ(0, motionArgs.edgeFlags);
5472 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5473 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5474 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5476 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5477 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5478 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5479 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5480
5481 // Should not have sent any more keys or motions.
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5484}
5485
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005486TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005487 addConfigurationProperty("touch.deviceType", "touchScreen");
5488 prepareButtons();
5489 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005490 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5491 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005492 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005493
5494 NotifyMotionArgs args;
5495
5496 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005497 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 processDown(mapper, toRawX(50), toRawY(75));
5499 processSync(mapper);
5500
5501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5502 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5503 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5504
5505 processUp(mapper);
5506 processSync(mapper);
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5508}
5509
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005510TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511 addConfigurationProperty("touch.deviceType", "touchScreen");
5512 prepareButtons();
5513 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005514 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5515 // orientation-aware are affected by display rotation.
5516 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005517 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518
5519 NotifyMotionArgs args;
5520
5521 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005522 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005523 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005524 processDown(mapper, toRawX(50), toRawY(75));
5525 processSync(mapper);
5526
5527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5528 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5529 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5530
5531 processUp(mapper);
5532 processSync(mapper);
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5534
5535 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005537 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005538 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005539 processSync(mapper);
5540
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5542 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5543 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5544
5545 processUp(mapper);
5546 processSync(mapper);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5548
5549 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005550 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005551 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005552 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5553 processSync(mapper);
5554
5555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5556 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5557 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5558
5559 processUp(mapper);
5560 processSync(mapper);
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5562
5563 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005564 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005566 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5571 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5572
5573 processUp(mapper);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5576}
5577
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
5582 addConfigurationProperty("touch.orientationAware", "1");
5583 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5584 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005585 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005586 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5587 NotifyMotionArgs args;
5588
5589 // Orientation 0.
5590 processDown(mapper, toRawX(50), toRawY(75));
5591 processSync(mapper);
5592
5593 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5594 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5595 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5596
5597 processUp(mapper);
5598 processSync(mapper);
5599 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5600}
5601
5602TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5603 addConfigurationProperty("touch.deviceType", "touchScreen");
5604 prepareButtons();
5605 prepareAxes(POSITION);
5606 addConfigurationProperty("touch.orientationAware", "1");
5607 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5608 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005609 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005610 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5611 NotifyMotionArgs args;
5612
5613 // Orientation 90.
5614 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5615 processSync(mapper);
5616
5617 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5618 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5619 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5620
5621 processUp(mapper);
5622 processSync(mapper);
5623 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5624}
5625
5626TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5627 addConfigurationProperty("touch.deviceType", "touchScreen");
5628 prepareButtons();
5629 prepareAxes(POSITION);
5630 addConfigurationProperty("touch.orientationAware", "1");
5631 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005634 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5635 NotifyMotionArgs args;
5636
5637 // Orientation 180.
5638 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5639 processSync(mapper);
5640
5641 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5642 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5643 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5644
5645 processUp(mapper);
5646 processSync(mapper);
5647 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5648}
5649
5650TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5651 addConfigurationProperty("touch.deviceType", "touchScreen");
5652 prepareButtons();
5653 prepareAxes(POSITION);
5654 addConfigurationProperty("touch.orientationAware", "1");
5655 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5656 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005657 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005658 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5659 NotifyMotionArgs args;
5660
5661 // Orientation 270.
5662 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5663 processSync(mapper);
5664
5665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5666 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5667 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5668
5669 processUp(mapper);
5670 processSync(mapper);
5671 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5672}
5673
5674TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5675 addConfigurationProperty("touch.deviceType", "touchScreen");
5676 prepareButtons();
5677 prepareAxes(POSITION);
5678 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5679 // orientation-aware are affected by display rotation.
5680 addConfigurationProperty("touch.orientationAware", "0");
5681 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5682 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5683
5684 NotifyMotionArgs args;
5685
5686 // Orientation 90, Rotation 0.
5687 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005688 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005689 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5690 processSync(mapper);
5691
5692 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5693 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5694 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5695
5696 processUp(mapper);
5697 processSync(mapper);
5698 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5699
5700 // Orientation 90, Rotation 90.
5701 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005702 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005703 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005704 processSync(mapper);
5705
5706 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5707 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5708 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5709
5710 processUp(mapper);
5711 processSync(mapper);
5712 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5713
5714 // Orientation 90, Rotation 180.
5715 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005716 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005717 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5718 processSync(mapper);
5719
5720 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5721 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5722 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5723
5724 processUp(mapper);
5725 processSync(mapper);
5726 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5727
5728 // Orientation 90, Rotation 270.
5729 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005730 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005731 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 -07005732 processSync(mapper);
5733
5734 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5735 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5736 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5737
5738 processUp(mapper);
5739 processSync(mapper);
5740 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5741}
5742
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005743TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5744 addConfigurationProperty("touch.deviceType", "touchScreen");
5745 prepareButtons();
5746 prepareAxes(POSITION);
5747 addConfigurationProperty("touch.orientationAware", "1");
5748 prepareDisplay(ui::ROTATION_0);
5749 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5750
5751 // Set a physical frame in the display viewport.
5752 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5753 viewport->physicalLeft = 20;
5754 viewport->physicalTop = 600;
5755 viewport->physicalRight = 30;
5756 viewport->physicalBottom = 610;
5757 mFakePolicy->updateViewport(*viewport);
5758 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5759
5760 // Start the touch.
5761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5762 processSync(mapper);
5763
5764 // Expect all input starting outside the physical frame to be ignored.
5765 const std::array<Point, 6> outsidePoints = {
5766 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5767 for (const auto& p : outsidePoints) {
5768 processMove(mapper, toRawX(p.x), toRawY(p.y));
5769 processSync(mapper);
5770 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5771 }
5772
5773 // Move the touch into the physical frame.
5774 processMove(mapper, toRawX(25), toRawY(605));
5775 processSync(mapper);
5776 NotifyMotionArgs args;
5777 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5778 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5779 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5780 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5781
5782 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5783 for (const auto& p : outsidePoints) {
5784 processMove(mapper, toRawX(p.x), toRawY(p.y));
5785 processSync(mapper);
5786 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5787 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5788 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5789 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5790 }
5791
5792 processUp(mapper);
5793 processSync(mapper);
5794 EXPECT_NO_FATAL_FAILURE(
5795 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5796}
5797
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005799 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005800 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005801 prepareButtons();
5802 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005803 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804
5805 // These calculations are based on the input device calibration documentation.
5806 int32_t rawX = 100;
5807 int32_t rawY = 200;
5808 int32_t rawPressure = 10;
5809 int32_t rawToolMajor = 12;
5810 int32_t rawDistance = 2;
5811 int32_t rawTiltX = 30;
5812 int32_t rawTiltY = 110;
5813
5814 float x = toDisplayX(rawX);
5815 float y = toDisplayY(rawY);
5816 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5817 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5818 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5819 float distance = float(rawDistance);
5820
5821 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5822 float tiltScale = M_PI / 180;
5823 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5824 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5825 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5826 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5827
5828 processDown(mapper, rawX, rawY);
5829 processPressure(mapper, rawPressure);
5830 processToolMajor(mapper, rawToolMajor);
5831 processDistance(mapper, rawDistance);
5832 processTilt(mapper, rawTiltX, rawTiltY);
5833 processSync(mapper);
5834
5835 NotifyMotionArgs args;
5836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5837 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5838 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5839 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5840}
5841
Jason Gerecke489fda82012-09-07 17:19:40 -07005842TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005843 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005844 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005845 prepareLocationCalibration();
5846 prepareButtons();
5847 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005848 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005849
5850 int32_t rawX = 100;
5851 int32_t rawY = 200;
5852
5853 float x = toDisplayX(toCookedX(rawX, rawY));
5854 float y = toDisplayY(toCookedY(rawX, rawY));
5855
5856 processDown(mapper, rawX, rawY);
5857 processSync(mapper);
5858
5859 NotifyMotionArgs args;
5860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5861 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5862 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5863}
5864
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005867 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005868 prepareButtons();
5869 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005870 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871
5872 NotifyMotionArgs motionArgs;
5873 NotifyKeyArgs keyArgs;
5874
5875 processDown(mapper, 100, 200);
5876 processSync(mapper);
5877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5878 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5879 ASSERT_EQ(0, motionArgs.buttonState);
5880
5881 // press BTN_LEFT, release BTN_LEFT
5882 processKey(mapper, BTN_LEFT, 1);
5883 processSync(mapper);
5884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5886 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5887
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5889 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5890 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5891
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892 processKey(mapper, BTN_LEFT, 0);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005896 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005897
5898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005900 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005901
5902 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5903 processKey(mapper, BTN_RIGHT, 1);
5904 processKey(mapper, BTN_MIDDLE, 1);
5905 processSync(mapper);
5906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5908 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5909 motionArgs.buttonState);
5910
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5913 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5914
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5916 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5917 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5918 motionArgs.buttonState);
5919
Michael Wrightd02c5b62014-02-10 15:10:22 -08005920 processKey(mapper, BTN_RIGHT, 0);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005923 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005924 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005925
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005928 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005929
5930 processKey(mapper, BTN_MIDDLE, 0);
5931 processSync(mapper);
5932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005933 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005935
5936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005938 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939
5940 // press BTN_BACK, release BTN_BACK
5941 processKey(mapper, BTN_BACK, 1);
5942 processSync(mapper);
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5944 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5945 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005946
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005949 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5950
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5953 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005954
5955 processKey(mapper, BTN_BACK, 0);
5956 processSync(mapper);
5957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005958 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005959 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005960
5961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(0, motionArgs.buttonState);
5964
Michael Wrightd02c5b62014-02-10 15:10:22 -08005965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5966 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5967 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5968
5969 // press BTN_SIDE, release BTN_SIDE
5970 processKey(mapper, BTN_SIDE, 1);
5971 processSync(mapper);
5972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5973 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5974 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005975
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005977 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005978 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5979
5980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5981 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5982 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005983
5984 processKey(mapper, BTN_SIDE, 0);
5985 processSync(mapper);
5986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005989
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005992 ASSERT_EQ(0, motionArgs.buttonState);
5993
Michael Wrightd02c5b62014-02-10 15:10:22 -08005994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5995 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5996 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5997
5998 // press BTN_FORWARD, release BTN_FORWARD
5999 processKey(mapper, BTN_FORWARD, 1);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6002 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6003 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006004
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006006 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006007 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6008
6009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6010 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6011 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006012
6013 processKey(mapper, BTN_FORWARD, 0);
6014 processSync(mapper);
6015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006020 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006021 ASSERT_EQ(0, motionArgs.buttonState);
6022
Michael Wrightd02c5b62014-02-10 15:10:22 -08006023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6024 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6025 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6026
6027 // press BTN_EXTRA, release BTN_EXTRA
6028 processKey(mapper, BTN_EXTRA, 1);
6029 processSync(mapper);
6030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6031 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6032 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006033
Michael Wrightd02c5b62014-02-10 15:10:22 -08006034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006035 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006036 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6037
6038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6040 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006041
6042 processKey(mapper, BTN_EXTRA, 0);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006046 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006049 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006050 ASSERT_EQ(0, motionArgs.buttonState);
6051
Michael Wrightd02c5b62014-02-10 15:10:22 -08006052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6053 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6054 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6055
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6057
Michael Wrightd02c5b62014-02-10 15:10:22 -08006058 // press BTN_STYLUS, release BTN_STYLUS
6059 processKey(mapper, BTN_STYLUS, 1);
6060 processSync(mapper);
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6062 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006063 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6064
6065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6066 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6067 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006068
6069 processKey(mapper, BTN_STYLUS, 0);
6070 processSync(mapper);
6071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006074
6075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006077 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
6079 // press BTN_STYLUS2, release BTN_STYLUS2
6080 processKey(mapper, BTN_STYLUS2, 1);
6081 processSync(mapper);
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6085
6086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6087 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6088 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006089
6090 processKey(mapper, BTN_STYLUS2, 0);
6091 processSync(mapper);
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006093 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006094 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006095
6096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006097 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006098 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006099
6100 // release touch
6101 processUp(mapper);
6102 processSync(mapper);
6103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6104 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6105 ASSERT_EQ(0, motionArgs.buttonState);
6106}
6107
6108TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006110 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006111 prepareButtons();
6112 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114
6115 NotifyMotionArgs motionArgs;
6116
6117 // default tool type is finger
6118 processDown(mapper, 100, 200);
6119 processSync(mapper);
6120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6121 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6122 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6123
6124 // eraser
6125 processKey(mapper, BTN_TOOL_RUBBER, 1);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6129 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6130
6131 // stylus
6132 processKey(mapper, BTN_TOOL_RUBBER, 0);
6133 processKey(mapper, BTN_TOOL_PEN, 1);
6134 processSync(mapper);
6135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6137 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6138
6139 // brush
6140 processKey(mapper, BTN_TOOL_PEN, 0);
6141 processKey(mapper, BTN_TOOL_BRUSH, 1);
6142 processSync(mapper);
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6145 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6146
6147 // pencil
6148 processKey(mapper, BTN_TOOL_BRUSH, 0);
6149 processKey(mapper, BTN_TOOL_PENCIL, 1);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6154
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006155 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 processKey(mapper, BTN_TOOL_PENCIL, 0);
6157 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6158 processSync(mapper);
6159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6160 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6161 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6162
6163 // mouse
6164 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6165 processKey(mapper, BTN_TOOL_MOUSE, 1);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6170
6171 // lens
6172 processKey(mapper, BTN_TOOL_MOUSE, 0);
6173 processKey(mapper, BTN_TOOL_LENS, 1);
6174 processSync(mapper);
6175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6176 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6177 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6178
6179 // double-tap
6180 processKey(mapper, BTN_TOOL_LENS, 0);
6181 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6182 processSync(mapper);
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6184 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6185 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6186
6187 // triple-tap
6188 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6189 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6190 processSync(mapper);
6191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6193 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6194
6195 // quad-tap
6196 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6197 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6201 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6202
6203 // finger
6204 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6205 processKey(mapper, BTN_TOOL_FINGER, 1);
6206 processSync(mapper);
6207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6209 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6210
6211 // stylus trumps finger
6212 processKey(mapper, BTN_TOOL_PEN, 1);
6213 processSync(mapper);
6214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6216 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6217
6218 // eraser trumps stylus
6219 processKey(mapper, BTN_TOOL_RUBBER, 1);
6220 processSync(mapper);
6221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6223 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6224
6225 // mouse trumps eraser
6226 processKey(mapper, BTN_TOOL_MOUSE, 1);
6227 processSync(mapper);
6228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6230 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6231
6232 // back to default tool type
6233 processKey(mapper, BTN_TOOL_MOUSE, 0);
6234 processKey(mapper, BTN_TOOL_RUBBER, 0);
6235 processKey(mapper, BTN_TOOL_PEN, 0);
6236 processKey(mapper, BTN_TOOL_FINGER, 0);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6240 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6241}
6242
6243TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006244 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006245 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006246 prepareButtons();
6247 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006248 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006249 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006250
6251 NotifyMotionArgs motionArgs;
6252
6253 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6254 processKey(mapper, BTN_TOOL_FINGER, 1);
6255 processMove(mapper, 100, 200);
6256 processSync(mapper);
6257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6258 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6260 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6261
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6265 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6266
6267 // move a little
6268 processMove(mapper, 150, 250);
6269 processSync(mapper);
6270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6271 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6273 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6274
6275 // down when BTN_TOUCH is pressed, pressure defaults to 1
6276 processKey(mapper, BTN_TOUCH, 1);
6277 processSync(mapper);
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6279 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6281 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6282
6283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6284 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6286 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6287
6288 // up when BTN_TOUCH is released, hover restored
6289 processKey(mapper, BTN_TOUCH, 0);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6294 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6295
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6298 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6299 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6300
6301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6302 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6303 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6304 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6305
6306 // exit hover when pointer goes away
6307 processKey(mapper, BTN_TOOL_FINGER, 0);
6308 processSync(mapper);
6309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6310 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6311 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6312 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6313}
6314
6315TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006316 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006317 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318 prepareButtons();
6319 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006320 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006321
6322 NotifyMotionArgs motionArgs;
6323
6324 // initially hovering because pressure is 0
6325 processDown(mapper, 100, 200);
6326 processPressure(mapper, 0);
6327 processSync(mapper);
6328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6329 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6330 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6331 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6332
6333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6334 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6336 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6337
6338 // move a little
6339 processMove(mapper, 150, 250);
6340 processSync(mapper);
6341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6342 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6344 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6345
6346 // down when pressure is non-zero
6347 processPressure(mapper, RAW_PRESSURE_MAX);
6348 processSync(mapper);
6349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6350 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6352 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6353
6354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6356 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6357 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6358
6359 // up when pressure becomes 0, hover restored
6360 processPressure(mapper, 0);
6361 processSync(mapper);
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6365 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6366
6367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6368 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6370 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6371
6372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6373 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6374 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6375 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6376
6377 // exit hover when pointer goes away
6378 processUp(mapper);
6379 processSync(mapper);
6380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6383 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6384}
6385
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006386TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6387 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006388 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006389 prepareButtons();
6390 prepareAxes(POSITION | PRESSURE);
6391 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6392
6393 // Touch down.
6394 processDown(mapper, 100, 200);
6395 processPressure(mapper, 1);
6396 processSync(mapper);
6397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6398 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6399
6400 // Reset the mapper. This should cancel the ongoing gesture.
6401 resetMapper(mapper, ARBITRARY_TIME);
6402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6403 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6404
6405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6406}
6407
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006408TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6409 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006410 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006411 prepareButtons();
6412 prepareAxes(POSITION | PRESSURE);
6413 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6414
6415 // Set the initial state for the touch pointer.
6416 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6417 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6418 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6419 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6420
6421 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006422 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6423 // does not generate any events.
6424 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006425
6426 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6427 // the recreated touch state to generate a down event.
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6430 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6431
6432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6433}
6434
lilinnan687e58f2022-07-19 16:00:50 +08006435TEST_F(SingleTouchInputMapperTest,
6436 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006438 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006439 prepareButtons();
6440 prepareAxes(POSITION);
6441 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6442 NotifyMotionArgs motionArgs;
6443
6444 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006445 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006446 processSync(mapper);
6447
6448 // We should receive a down event
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6451
6452 // Change display id
6453 clearViewports();
6454 prepareSecondaryDisplay(ViewportType::INTERNAL);
6455
6456 // We should receive a cancel event
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6458 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6459 // Then receive reset called
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6461}
6462
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006463TEST_F(SingleTouchInputMapperTest,
6464 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006466 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006467 prepareButtons();
6468 prepareAxes(POSITION);
6469 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6471 NotifyMotionArgs motionArgs;
6472
6473 // Start a new gesture.
6474 processDown(mapper, 100, 200);
6475 processSync(mapper);
6476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6477 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6478
6479 // Make the viewport inactive. This will put the device in disabled mode.
6480 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6481 viewport->isActive = false;
6482 mFakePolicy->updateViewport(*viewport);
6483 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6484
6485 // We should receive a cancel event for the ongoing gesture.
6486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6487 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6488 // Then we should be notified that the device was reset.
6489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6490
6491 // No events are generated while the viewport is inactive.
6492 processMove(mapper, 101, 201);
6493 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006494 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006495 processSync(mapper);
6496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6497
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006498 // Start a new gesture while the viewport is still inactive.
6499 processDown(mapper, 300, 400);
6500 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6501 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6502 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6503 processSync(mapper);
6504
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006505 // Make the viewport active again. The device should resume processing events.
6506 viewport->isActive = true;
6507 mFakePolicy->updateViewport(*viewport);
6508 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6509
6510 // The device is reset because it changes back to direct mode, without generating any events.
6511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6513
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006514 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006515 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6517 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006518
6519 // No more events.
6520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6522}
6523
Prabir Pradhan211ba622022-10-31 21:09:21 +00006524TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006526 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006527 prepareButtons();
6528 prepareAxes(POSITION);
6529 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6531
6532 // Press a stylus button.
6533 processKey(mapper, BTN_STYLUS, 1);
6534 processSync(mapper);
6535
6536 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6537 processDown(mapper, 100, 200);
6538 processSync(mapper);
6539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6540 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6541 WithCoords(toDisplayX(100), toDisplayY(200)),
6542 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6544 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6545 WithCoords(toDisplayX(100), toDisplayY(200)),
6546 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6547
6548 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6549 // the button has not actually been released, since there will be no pointers through which the
6550 // button state can be reported. The event is generated at the location of the pointer before
6551 // it went up.
6552 processUp(mapper);
6553 processSync(mapper);
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6555 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6556 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6558 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6559 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6560}
6561
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006562TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6563 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6564 prepareDisplay(ui::ROTATION_0);
6565 prepareButtons();
6566 prepareAxes(POSITION);
6567 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6569
6570 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6571}
6572
Prabir Pradhan5632d622021-09-06 07:57:20 -07006573// --- TouchDisplayProjectionTest ---
6574
6575class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6576public:
6577 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6578 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6579 // rotated equivalent of the given un-rotated physical display bounds.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006580 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006581 uint32_t inverseRotationFlags;
6582 auto width = DISPLAY_WIDTH;
6583 auto height = DISPLAY_HEIGHT;
6584 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006585 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006586 inverseRotationFlags = ui::Transform::ROT_270;
6587 std::swap(width, height);
6588 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006589 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006590 inverseRotationFlags = ui::Transform::ROT_180;
6591 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006592 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006593 inverseRotationFlags = ui::Transform::ROT_90;
6594 std::swap(width, height);
6595 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006596 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006597 inverseRotationFlags = ui::Transform::ROT_0;
6598 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006599 }
6600
6601 const ui::Transform rotation(inverseRotationFlags, width, height);
6602 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6603
6604 std::optional<DisplayViewport> internalViewport =
6605 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6606 DisplayViewport& v = *internalViewport;
6607 v.displayId = DISPLAY_ID;
6608 v.orientation = orientation;
6609
6610 v.logicalLeft = 0;
6611 v.logicalTop = 0;
6612 v.logicalRight = 100;
6613 v.logicalBottom = 100;
6614
6615 v.physicalLeft = rotatedPhysicalDisplay.left;
6616 v.physicalTop = rotatedPhysicalDisplay.top;
6617 v.physicalRight = rotatedPhysicalDisplay.right;
6618 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6619
6620 v.deviceWidth = width;
6621 v.deviceHeight = height;
6622
6623 v.isActive = true;
6624 v.uniqueId = UNIQUE_ID;
6625 v.type = ViewportType::INTERNAL;
6626 mFakePolicy->updateViewport(v);
6627 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6628 }
6629
6630 void assertReceivedMove(const Point& point) {
6631 NotifyMotionArgs motionArgs;
6632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6634 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6636 1, 0, 0, 0, 0, 0, 0, 0));
6637 }
6638};
6639
6640TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6641 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006642 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006643
6644 prepareButtons();
6645 prepareAxes(POSITION);
6646 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6647
6648 NotifyMotionArgs motionArgs;
6649
6650 // Configure the DisplayViewport such that the logical display maps to a subsection of
6651 // the display panel called the physical display. Here, the physical display is bounded by the
6652 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6653 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6654 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6655 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6656
Michael Wrighta9cf4192022-12-01 23:46:39 +00006657 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006658 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6659
6660 // Touches outside the physical display should be ignored, and should not generate any
6661 // events. Ensure touches at the following points that lie outside of the physical display
6662 // area do not generate any events.
6663 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6664 processDown(mapper, toRawX(point.x), toRawY(point.y));
6665 processSync(mapper);
6666 processUp(mapper);
6667 processSync(mapper);
6668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6669 << "Unexpected event generated for touch outside physical display at point: "
6670 << point.x << ", " << point.y;
6671 }
6672 }
6673}
6674
6675TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6676 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006677 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006678
6679 prepareButtons();
6680 prepareAxes(POSITION);
6681 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6682
6683 NotifyMotionArgs motionArgs;
6684
6685 // Configure the DisplayViewport such that the logical display maps to a subsection of
6686 // the display panel called the physical display. Here, the physical display is bounded by the
6687 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6688 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6689
Michael Wrighta9cf4192022-12-01 23:46:39 +00006690 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006691 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6692
6693 // Touches that start outside the physical display should be ignored until it enters the
6694 // physical display bounds, at which point it should generate a down event. Start a touch at
6695 // the point (5, 100), which is outside the physical display bounds.
6696 static const Point kOutsidePoint{5, 100};
6697 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6698 processSync(mapper);
6699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6700
6701 // Move the touch into the physical display area. This should generate a pointer down.
6702 processMove(mapper, toRawX(11), toRawY(21));
6703 processSync(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6705 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6706 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6707 ASSERT_NO_FATAL_FAILURE(
6708 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6709
6710 // Move the touch inside the physical display area. This should generate a pointer move.
6711 processMove(mapper, toRawX(69), toRawY(159));
6712 processSync(mapper);
6713 assertReceivedMove({69, 159});
6714
6715 // Move outside the physical display area. Since the pointer is already down, this should
6716 // now continue generating events.
6717 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6718 processSync(mapper);
6719 assertReceivedMove(kOutsidePoint);
6720
6721 // Release. This should generate a pointer up.
6722 processUp(mapper);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6725 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6727 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6728
6729 // Ensure no more events were generated.
6730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6732 }
6733}
6734
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006735// --- ExternalStylusFusionTest ---
6736
6737class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6738public:
6739 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6740 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006741 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006742 prepareButtons();
6743 prepareAxes(POSITION);
6744 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6745
6746 mStylusState.when = ARBITRARY_TIME;
6747 mStylusState.pressure = 0.f;
6748 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
6749 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
6750 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
6751 processExternalStylusState(mapper);
6752 return mapper;
6753 }
6754
6755 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6756 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6757 for (const NotifyArgs& args : generatedArgs) {
6758 mFakeListener->notify(args);
6759 }
6760 // Loop the reader to flush the input listener queue.
6761 mReader->loopOnce();
6762 return generatedArgs;
6763 }
6764
6765protected:
6766 StylusState mStylusState{};
6767 static constexpr uint32_t EXPECTED_SOURCE =
6768 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
6769
6770 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6771 auto toolTypeSource =
6772 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6773
6774 // The first pointer is withheld.
6775 processDown(mapper, 100, 200);
6776 processSync(mapper);
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6778 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6779 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6780
6781 // The external stylus reports pressure. The withheld finger pointer is released as a
6782 // stylus.
6783 mStylusState.pressure = 1.f;
6784 processExternalStylusState(mapper);
6785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6786 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6787 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6788
6789 // Subsequent pointer events are not withheld.
6790 processMove(mapper, 101, 201);
6791 processSync(mapper);
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6793 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6794
6795 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6797 }
6798
6799 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6800 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6801
6802 // Releasing the touch pointer ends the gesture.
6803 processUp(mapper);
6804 processSync(mapper);
6805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6806 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6807 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6808
6809 mStylusState.pressure = 0.f;
6810 processExternalStylusState(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6813 }
6814
6815 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6816 auto toolTypeSource =
6817 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
6818
6819 // The first pointer is withheld when an external stylus is connected,
6820 // and a timeout is requested.
6821 processDown(mapper, 100, 200);
6822 processSync(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6824 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6825 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6826
6827 // If the timeout expires early, it is requested again.
6828 handleTimeout(mapper, ARBITRARY_TIME + 1);
6829 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6830 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6831
6832 // When the timeout expires, the withheld touch is released as a finger pointer.
6833 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6835 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6836
6837 // Subsequent pointer events are not withheld.
6838 processMove(mapper, 101, 201);
6839 processSync(mapper);
6840 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6841 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6842 processUp(mapper);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6845 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6846
6847 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6849 }
6850
6851private:
6852 InputDeviceInfo mExternalStylusDeviceInfo{};
6853};
6854
6855TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6856 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6857 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
6858}
6859
6860TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6861 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6862 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6863}
6864
6865TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6866 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6867 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6868}
6869
6870// Test a successful stylus fusion gesture where the pressure is reported by the external
6871// before the touch is reported by the touchscreen.
6872TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6873 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6874 auto toolTypeSource =
6875 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6876
6877 // The external stylus reports pressure first. It is ignored for now.
6878 mStylusState.pressure = 1.f;
6879 processExternalStylusState(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6881 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6882
6883 // When the touch goes down afterwards, it is reported as a stylus pointer.
6884 processDown(mapper, 100, 200);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6887 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6888 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6889
6890 processMove(mapper, 101, 201);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6894 processUp(mapper);
6895 processSync(mapper);
6896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6897 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6898
6899 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6901}
6902
6903TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6904 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6905
6906 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6907 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6908
6909 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6910 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6911 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6912 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6913}
6914
6915TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6916 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6917 auto toolTypeSource =
6918 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
6919
6920 mStylusState.pressure = 0.8f;
6921 processExternalStylusState(mapper);
6922 processDown(mapper, 100, 200);
6923 processSync(mapper);
6924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6925 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6926 WithPressure(0.8f))));
6927 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6928
6929 // The external stylus reports a pressure change. We wait for some time for a touch event.
6930 mStylusState.pressure = 0.6f;
6931 processExternalStylusState(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6933 ASSERT_NO_FATAL_FAILURE(
6934 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6935
6936 // If a touch is reported within the timeout, it reports the updated pressure.
6937 processMove(mapper, 101, 201);
6938 processSync(mapper);
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6940 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6941 WithPressure(0.6f))));
6942 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6943
6944 // There is another pressure change.
6945 mStylusState.pressure = 0.5f;
6946 processExternalStylusState(mapper);
6947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6948 ASSERT_NO_FATAL_FAILURE(
6949 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6950
6951 // If a touch is not reported within the timeout, a move event is generated to report
6952 // the new pressure.
6953 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6955 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6956 WithPressure(0.5f))));
6957
6958 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6959 // repeated indefinitely.
6960 mStylusState.pressure = 0.0f;
6961 processExternalStylusState(mapper);
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6963 ASSERT_NO_FATAL_FAILURE(
6964 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6965 processMove(mapper, 102, 202);
6966 processSync(mapper);
6967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6968 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6969 WithPressure(0.5f))));
6970 processMove(mapper, 103, 203);
6971 processSync(mapper);
6972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6973 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6974 WithPressure(0.5f))));
6975
6976 processUp(mapper);
6977 processSync(mapper);
6978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6979 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
6980 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
6981
6982 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6984}
6985
6986TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6987 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6988 auto source = WithSource(EXPECTED_SOURCE);
6989
6990 mStylusState.pressure = 1.f;
6991 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
6992 processExternalStylusState(mapper);
6993 processDown(mapper, 100, 200);
6994 processSync(mapper);
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6996 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6997 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
6998 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6999
7000 // The external stylus reports a tool change. We wait for some time for a touch event.
7001 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7002 processExternalStylusState(mapper);
7003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7004 ASSERT_NO_FATAL_FAILURE(
7005 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7006
7007 // If a touch is reported within the timeout, it reports the updated pressure.
7008 processMove(mapper, 101, 201);
7009 processSync(mapper);
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7011 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7012 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7013 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7014
7015 // There is another tool type change.
7016 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7017 processExternalStylusState(mapper);
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7019 ASSERT_NO_FATAL_FAILURE(
7020 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7021
7022 // If a touch is not reported within the timeout, a move event is generated to report
7023 // the new tool type.
7024 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7026 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7027 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7028
7029 processUp(mapper);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7032 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7033 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7034
7035 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7037}
7038
7039TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7040 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7041 auto toolTypeSource =
7042 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7043
7044 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7045
7046 // The external stylus reports a button change. We wait for some time for a touch event.
7047 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7048 processExternalStylusState(mapper);
7049 ASSERT_NO_FATAL_FAILURE(
7050 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7051
7052 // If a touch is reported within the timeout, it reports the updated button state.
7053 processMove(mapper, 101, 201);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7056 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7057 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7059 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7060 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7061 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7062
7063 // The button is now released.
7064 mStylusState.buttons = 0;
7065 processExternalStylusState(mapper);
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7067 ASSERT_NO_FATAL_FAILURE(
7068 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7069
7070 // If a touch is not reported within the timeout, a move event is generated to report
7071 // the new button state.
7072 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7074 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7075 WithButtonState(0))));
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007077 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7078 WithButtonState(0))));
7079
7080 processUp(mapper);
7081 processSync(mapper);
7082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007083 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7084
7085 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7086 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7087}
7088
Michael Wrightd02c5b62014-02-10 15:10:22 -08007089// --- MultiTouchInputMapperTest ---
7090
7091class MultiTouchInputMapperTest : public TouchInputMapperTest {
7092protected:
7093 void prepareAxes(int axes);
7094
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007095 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7096 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7097 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7098 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7099 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7100 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7101 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7102 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7103 void processId(MultiTouchInputMapper& mapper, int32_t id);
7104 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7105 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7106 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007107 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007108 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007109 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7110 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007111};
7112
7113void MultiTouchInputMapperTest::prepareAxes(int axes) {
7114 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7116 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 }
7118 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7120 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7123 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124 }
7125 }
7126 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007127 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7128 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007131 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132 }
7133 }
7134 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7136 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137 }
7138 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7140 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141 }
7142 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007143 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7144 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145 }
7146 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007147 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7148 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149 }
7150 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007151 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7152 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153 }
7154 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007155 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 }
7157}
7158
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007159void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7160 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007165void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7166 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168}
7169
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007170void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7171 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007173}
7174
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007175void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177}
7178
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007179void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7184 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007186}
7187
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007188void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190}
7191
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007192void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007194}
7195
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007196void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007197 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198}
7199
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007200void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007201 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007202}
7203
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007204void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206}
7207
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007208void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7209 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211}
7212
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007213void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7214 int32_t value) {
7215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7217}
7218
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007219void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007221}
7222
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007223void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7224 nsecs_t readTime) {
7225 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226}
7227
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007230 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 prepareAxes(POSITION);
7232 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007233 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007234
arthurhungdcef2dc2020-08-11 14:47:50 +08007235 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236
7237 NotifyMotionArgs motionArgs;
7238
7239 // Two fingers down at once.
7240 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7241 processPosition(mapper, x1, y1);
7242 processMTSync(mapper);
7243 processPosition(mapper, x2, y2);
7244 processMTSync(mapper);
7245 processSync(mapper);
7246
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7248 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7249 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7250 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7251 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7252 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7253 ASSERT_EQ(0, motionArgs.flags);
7254 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7255 ASSERT_EQ(0, motionArgs.buttonState);
7256 ASSERT_EQ(0, motionArgs.edgeFlags);
7257 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7258 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7259 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7263 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7264 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7265
7266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7267 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7268 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7269 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7270 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007271 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(0, motionArgs.flags);
7273 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7274 ASSERT_EQ(0, motionArgs.buttonState);
7275 ASSERT_EQ(0, motionArgs.edgeFlags);
7276 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7277 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7278 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7279 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7280 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7284 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7285 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7286 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7287 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7288
7289 // Move.
7290 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7291 processPosition(mapper, x1, y1);
7292 processMTSync(mapper);
7293 processPosition(mapper, x2, y2);
7294 processMTSync(mapper);
7295 processSync(mapper);
7296
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7299 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7300 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7301 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7302 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7303 ASSERT_EQ(0, motionArgs.flags);
7304 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7305 ASSERT_EQ(0, motionArgs.buttonState);
7306 ASSERT_EQ(0, motionArgs.edgeFlags);
7307 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7308 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7309 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7310 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7311 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7314 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7315 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7316 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7317 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7318 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7319
7320 // First finger up.
7321 x2 += 15; y2 -= 20;
7322 processPosition(mapper, x2, y2);
7323 processMTSync(mapper);
7324 processSync(mapper);
7325
7326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7327 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7328 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7329 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7330 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007331 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 ASSERT_EQ(0, motionArgs.flags);
7333 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7334 ASSERT_EQ(0, motionArgs.buttonState);
7335 ASSERT_EQ(0, motionArgs.edgeFlags);
7336 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7337 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7338 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7339 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7340 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7341 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7342 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7343 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7344 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7345 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7346 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7347 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
7359 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7360 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7361 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7362 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7363 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7364 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7365 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7367
7368 // Move.
7369 x2 += 20; y2 -= 25;
7370 processPosition(mapper, x2, y2);
7371 processMTSync(mapper);
7372 processSync(mapper);
7373
7374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7375 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7376 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7377 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7378 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7380 ASSERT_EQ(0, motionArgs.flags);
7381 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7382 ASSERT_EQ(0, motionArgs.buttonState);
7383 ASSERT_EQ(0, motionArgs.edgeFlags);
7384 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7385 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7386 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7387 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7388 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7389 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7390 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7391 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7392
7393 // New finger down.
7394 int32_t x3 = 700, y3 = 300;
7395 processPosition(mapper, x2, y2);
7396 processMTSync(mapper);
7397 processPosition(mapper, x3, y3);
7398 processMTSync(mapper);
7399 processSync(mapper);
7400
7401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7402 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7403 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7404 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7405 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007406 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007407 ASSERT_EQ(0, motionArgs.flags);
7408 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7409 ASSERT_EQ(0, motionArgs.buttonState);
7410 ASSERT_EQ(0, motionArgs.edgeFlags);
7411 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7412 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7414 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7415 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7417 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7418 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7419 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7420 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7421 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7422 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7423
7424 // Second finger up.
7425 x3 += 30; y3 -= 20;
7426 processPosition(mapper, x3, y3);
7427 processMTSync(mapper);
7428 processSync(mapper);
7429
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7431 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7432 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7433 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7434 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007435 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436 ASSERT_EQ(0, motionArgs.flags);
7437 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7438 ASSERT_EQ(0, motionArgs.buttonState);
7439 ASSERT_EQ(0, motionArgs.edgeFlags);
7440 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7441 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7443 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7444 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7445 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7446 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7448 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7449 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7450 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7451 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7452
7453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7454 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7455 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7456 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7457 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7458 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7459 ASSERT_EQ(0, motionArgs.flags);
7460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7461 ASSERT_EQ(0, motionArgs.buttonState);
7462 ASSERT_EQ(0, motionArgs.edgeFlags);
7463 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7464 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7465 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7467 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7468 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7469 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7470 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7471
7472 // Last finger up.
7473 processMTSync(mapper);
7474 processSync(mapper);
7475
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7477 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7478 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7479 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7480 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7482 ASSERT_EQ(0, motionArgs.flags);
7483 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7484 ASSERT_EQ(0, motionArgs.buttonState);
7485 ASSERT_EQ(0, motionArgs.edgeFlags);
7486 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7487 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7488 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7490 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7491 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7492 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7493 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7494
7495 // Should not have sent any more keys or motions.
7496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7498}
7499
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007500TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7501 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007502 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007503
7504 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7505 /*fuzz*/ 0, /*resolution*/ 10);
7506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7507 /*fuzz*/ 0, /*resolution*/ 11);
7508 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7509 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7511 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7512 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7513 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7514 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7515 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7516
7517 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7518
7519 // X and Y axes
7520 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7521 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7522 // Touch major and minor
7523 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7524 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7525 // Tool major and minor
7526 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7527 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7528}
7529
7530TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7531 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007532 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007533
7534 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7535 /*fuzz*/ 0, /*resolution*/ 10);
7536 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7537 /*fuzz*/ 0, /*resolution*/ 11);
7538
7539 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7540
7541 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7542
7543 // Touch major and minor
7544 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7545 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7546 // Tool major and minor
7547 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7548 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7549}
7550
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007552 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007553 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554 prepareAxes(POSITION | ID);
7555 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007556 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557
arthurhungdcef2dc2020-08-11 14:47:50 +08007558 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007559
7560 NotifyMotionArgs motionArgs;
7561
7562 // Two fingers down at once.
7563 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7564 processPosition(mapper, x1, y1);
7565 processId(mapper, 1);
7566 processMTSync(mapper);
7567 processPosition(mapper, x2, y2);
7568 processId(mapper, 2);
7569 processMTSync(mapper);
7570 processSync(mapper);
7571
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7573 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7574 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7575 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7576 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7577 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7578 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7579
7580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007581 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007582 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7583 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7585 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7586 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7587 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7588 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7589 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7590 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7591
7592 // Move.
7593 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7594 processPosition(mapper, x1, y1);
7595 processId(mapper, 1);
7596 processMTSync(mapper);
7597 processPosition(mapper, x2, y2);
7598 processId(mapper, 2);
7599 processMTSync(mapper);
7600 processSync(mapper);
7601
7602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7603 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7604 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7607 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7608 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7610 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7612 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7613
7614 // First finger up.
7615 x2 += 15; y2 -= 20;
7616 processPosition(mapper, x2, y2);
7617 processId(mapper, 2);
7618 processMTSync(mapper);
7619 processSync(mapper);
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007622 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007623 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7624 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7625 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7626 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7627 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7629 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7630 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7631 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7632
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7635 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7636 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7637 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7638 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7639 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7640
7641 // Move.
7642 x2 += 20; y2 -= 25;
7643 processPosition(mapper, x2, y2);
7644 processId(mapper, 2);
7645 processMTSync(mapper);
7646 processSync(mapper);
7647
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7650 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7651 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7652 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7653 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7654 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7655
7656 // New finger down.
7657 int32_t x3 = 700, y3 = 300;
7658 processPosition(mapper, x2, y2);
7659 processId(mapper, 2);
7660 processMTSync(mapper);
7661 processPosition(mapper, x3, y3);
7662 processId(mapper, 3);
7663 processMTSync(mapper);
7664 processSync(mapper);
7665
7666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007667 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007668 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7669 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7671 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7672 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7673 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7674 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7676 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7677
7678 // Second finger up.
7679 x3 += 30; y3 -= 20;
7680 processPosition(mapper, x3, y3);
7681 processId(mapper, 3);
7682 processMTSync(mapper);
7683 processSync(mapper);
7684
7685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007686 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7688 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7689 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7690 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7691 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7692 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7693 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7694 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7695 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7696
7697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7698 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7699 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7700 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7701 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7703 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7704
7705 // Last finger up.
7706 processMTSync(mapper);
7707 processSync(mapper);
7708
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7710 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7711 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7713 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7715 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7716
7717 // Should not have sent any more keys or motions.
7718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7720}
7721
7722TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007724 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725 prepareAxes(POSITION | ID | SLOT);
7726 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007727 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007728
arthurhungdcef2dc2020-08-11 14:47:50 +08007729 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007730
7731 NotifyMotionArgs motionArgs;
7732
7733 // Two fingers down at once.
7734 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7735 processPosition(mapper, x1, y1);
7736 processId(mapper, 1);
7737 processSlot(mapper, 1);
7738 processPosition(mapper, x2, y2);
7739 processId(mapper, 2);
7740 processSync(mapper);
7741
7742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7743 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7744 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7745 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7746 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7748 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007751 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7754 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7756 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7758 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7761
7762 // Move.
7763 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7764 processSlot(mapper, 0);
7765 processPosition(mapper, x1, y1);
7766 processSlot(mapper, 1);
7767 processPosition(mapper, x2, y2);
7768 processSync(mapper);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7772 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7774 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 // First finger up.
7783 x2 += 15; y2 -= 20;
7784 processSlot(mapper, 0);
7785 processId(mapper, -1);
7786 processSlot(mapper, 1);
7787 processPosition(mapper, x2, y2);
7788 processSync(mapper);
7789
7790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007791 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007792 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7793 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7794 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7795 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7796 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7798 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7800 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7801
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7804 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7805 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7806 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7808 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7809
7810 // Move.
7811 x2 += 20; y2 -= 25;
7812 processPosition(mapper, x2, y2);
7813 processSync(mapper);
7814
7815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7816 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7817 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7818 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7819 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7820 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7821 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7822
7823 // New finger down.
7824 int32_t x3 = 700, y3 = 300;
7825 processPosition(mapper, x2, y2);
7826 processSlot(mapper, 0);
7827 processId(mapper, 3);
7828 processPosition(mapper, x3, y3);
7829 processSync(mapper);
7830
7831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007832 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007833 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7836 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7837 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7839 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7840 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7841 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7842
7843 // Second finger up.
7844 x3 += 30; y3 -= 20;
7845 processSlot(mapper, 1);
7846 processId(mapper, -1);
7847 processSlot(mapper, 0);
7848 processPosition(mapper, x3, y3);
7849 processSync(mapper);
7850
7851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007852 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7854 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7855 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7856 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7857 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7858 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7859 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7861 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7862
7863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7864 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7865 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7866 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7867 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7868 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7869 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7870
7871 // Last finger up.
7872 processId(mapper, -1);
7873 processSync(mapper);
7874
7875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7876 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7877 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7878 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7879 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7880 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7881 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7882
7883 // Should not have sent any more keys or motions.
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7886}
7887
7888TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007889 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007890 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007891 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893
7894 // These calculations are based on the input device calibration documentation.
7895 int32_t rawX = 100;
7896 int32_t rawY = 200;
7897 int32_t rawTouchMajor = 7;
7898 int32_t rawTouchMinor = 6;
7899 int32_t rawToolMajor = 9;
7900 int32_t rawToolMinor = 8;
7901 int32_t rawPressure = 11;
7902 int32_t rawDistance = 0;
7903 int32_t rawOrientation = 3;
7904 int32_t id = 5;
7905
7906 float x = toDisplayX(rawX);
7907 float y = toDisplayY(rawY);
7908 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7909 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7910 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7911 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7912 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7913 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7914 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7915 float distance = float(rawDistance);
7916
7917 processPosition(mapper, rawX, rawY);
7918 processTouchMajor(mapper, rawTouchMajor);
7919 processTouchMinor(mapper, rawTouchMinor);
7920 processToolMajor(mapper, rawToolMajor);
7921 processToolMinor(mapper, rawToolMinor);
7922 processPressure(mapper, rawPressure);
7923 processOrientation(mapper, rawOrientation);
7924 processDistance(mapper, rawDistance);
7925 processId(mapper, id);
7926 processMTSync(mapper);
7927 processSync(mapper);
7928
7929 NotifyMotionArgs args;
7930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7931 ASSERT_EQ(0, args.pointerProperties[0].id);
7932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7933 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7934 orientation, distance));
7935}
7936
7937TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007939 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007940 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7941 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007942 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943
7944 // These calculations are based on the input device calibration documentation.
7945 int32_t rawX = 100;
7946 int32_t rawY = 200;
7947 int32_t rawTouchMajor = 140;
7948 int32_t rawTouchMinor = 120;
7949 int32_t rawToolMajor = 180;
7950 int32_t rawToolMinor = 160;
7951
7952 float x = toDisplayX(rawX);
7953 float y = toDisplayY(rawY);
7954 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7955 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7956 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7957 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7958 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7959
7960 processPosition(mapper, rawX, rawY);
7961 processTouchMajor(mapper, rawTouchMajor);
7962 processTouchMinor(mapper, rawTouchMinor);
7963 processToolMajor(mapper, rawToolMajor);
7964 processToolMinor(mapper, rawToolMinor);
7965 processMTSync(mapper);
7966 processSync(mapper);
7967
7968 NotifyMotionArgs args;
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7970 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7971 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7972}
7973
7974TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007975 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007976 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 prepareAxes(POSITION | TOUCH | TOOL);
7978 addConfigurationProperty("touch.size.calibration", "diameter");
7979 addConfigurationProperty("touch.size.scale", "10");
7980 addConfigurationProperty("touch.size.bias", "160");
7981 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007982 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983
7984 // These calculations are based on the input device calibration documentation.
7985 // Note: We only provide a single common touch/tool value because the device is assumed
7986 // not to emit separate values for each pointer (isSummed = 1).
7987 int32_t rawX = 100;
7988 int32_t rawY = 200;
7989 int32_t rawX2 = 150;
7990 int32_t rawY2 = 250;
7991 int32_t rawTouchMajor = 5;
7992 int32_t rawToolMajor = 8;
7993
7994 float x = toDisplayX(rawX);
7995 float y = toDisplayY(rawY);
7996 float x2 = toDisplayX(rawX2);
7997 float y2 = toDisplayY(rawY2);
7998 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7999 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8000 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8001
8002 processPosition(mapper, rawX, rawY);
8003 processTouchMajor(mapper, rawTouchMajor);
8004 processToolMajor(mapper, rawToolMajor);
8005 processMTSync(mapper);
8006 processPosition(mapper, rawX2, rawY2);
8007 processTouchMajor(mapper, rawTouchMajor);
8008 processToolMajor(mapper, rawToolMajor);
8009 processMTSync(mapper);
8010 processSync(mapper);
8011
8012 NotifyMotionArgs args;
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8014 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8015
8016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008017 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008018 ASSERT_EQ(size_t(2), args.pointerCount);
8019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8020 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8021 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8022 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8023}
8024
8025TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008026 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008027 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008028 prepareAxes(POSITION | TOUCH | TOOL);
8029 addConfigurationProperty("touch.size.calibration", "area");
8030 addConfigurationProperty("touch.size.scale", "43");
8031 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008032 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008033
8034 // These calculations are based on the input device calibration documentation.
8035 int32_t rawX = 100;
8036 int32_t rawY = 200;
8037 int32_t rawTouchMajor = 5;
8038 int32_t rawToolMajor = 8;
8039
8040 float x = toDisplayX(rawX);
8041 float y = toDisplayY(rawY);
8042 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8043 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8044 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8045
8046 processPosition(mapper, rawX, rawY);
8047 processTouchMajor(mapper, rawTouchMajor);
8048 processToolMajor(mapper, rawToolMajor);
8049 processMTSync(mapper);
8050 processSync(mapper);
8051
8052 NotifyMotionArgs args;
8053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8054 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8055 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8056}
8057
8058TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008060 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061 prepareAxes(POSITION | PRESSURE);
8062 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8063 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065
Michael Wrightaa449c92017-12-13 21:21:43 +00008066 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008067 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008068 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8069 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8070 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8071
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 // These calculations are based on the input device calibration documentation.
8073 int32_t rawX = 100;
8074 int32_t rawY = 200;
8075 int32_t rawPressure = 60;
8076
8077 float x = toDisplayX(rawX);
8078 float y = toDisplayY(rawY);
8079 float pressure = float(rawPressure) * 0.01f;
8080
8081 processPosition(mapper, rawX, rawY);
8082 processPressure(mapper, rawPressure);
8083 processMTSync(mapper);
8084 processSync(mapper);
8085
8086 NotifyMotionArgs args;
8087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8089 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8090}
8091
8092TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008094 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008095 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008096 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008097
8098 NotifyMotionArgs motionArgs;
8099 NotifyKeyArgs keyArgs;
8100
8101 processId(mapper, 1);
8102 processPosition(mapper, 100, 200);
8103 processSync(mapper);
8104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8105 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8106 ASSERT_EQ(0, motionArgs.buttonState);
8107
8108 // press BTN_LEFT, release BTN_LEFT
8109 processKey(mapper, BTN_LEFT, 1);
8110 processSync(mapper);
8111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8113 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8114
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8116 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8117 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8118
Michael Wrightd02c5b62014-02-10 15:10:22 -08008119 processKey(mapper, BTN_LEFT, 0);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008122 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008127 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128
8129 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8130 processKey(mapper, BTN_RIGHT, 1);
8131 processKey(mapper, BTN_MIDDLE, 1);
8132 processSync(mapper);
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8136 motionArgs.buttonState);
8137
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8139 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8140 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8141
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8145 motionArgs.buttonState);
8146
Michael Wrightd02c5b62014-02-10 15:10:22 -08008147 processKey(mapper, BTN_RIGHT, 0);
8148 processSync(mapper);
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152
8153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008155 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156
8157 processKey(mapper, BTN_MIDDLE, 0);
8158 processSync(mapper);
8159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008161 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008164 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008165 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166
8167 // press BTN_BACK, release BTN_BACK
8168 processKey(mapper, BTN_BACK, 1);
8169 processSync(mapper);
8170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8171 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8172 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008173
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008175 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008176 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8177
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8179 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8180 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181
8182 processKey(mapper, BTN_BACK, 0);
8183 processSync(mapper);
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008187
8188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008189 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190 ASSERT_EQ(0, motionArgs.buttonState);
8191
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8193 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8194 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8195
8196 // press BTN_SIDE, release BTN_SIDE
8197 processKey(mapper, BTN_SIDE, 1);
8198 processSync(mapper);
8199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8200 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8201 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008205 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8206
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8209 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008210
8211 processKey(mapper, BTN_SIDE, 0);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008214 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008216
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
8220
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8222 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8223 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8224
8225 // press BTN_FORWARD, release BTN_FORWARD
8226 processKey(mapper, BTN_FORWARD, 1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8229 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8230 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008231
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008233 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008234 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8235
8236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8237 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8238 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239
8240 processKey(mapper, BTN_FORWARD, 0);
8241 processSync(mapper);
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008245
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008247 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008248 ASSERT_EQ(0, motionArgs.buttonState);
8249
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8251 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8252 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8253
8254 // press BTN_EXTRA, release BTN_EXTRA
8255 processKey(mapper, BTN_EXTRA, 1);
8256 processSync(mapper);
8257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8258 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8259 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008260
Michael Wrightd02c5b62014-02-10 15:10:22 -08008261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008262 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8264
8265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8266 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8267 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008268
8269 processKey(mapper, BTN_EXTRA, 0);
8270 processSync(mapper);
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008272 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008273 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008274
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008277 ASSERT_EQ(0, motionArgs.buttonState);
8278
Michael Wrightd02c5b62014-02-10 15:10:22 -08008279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8280 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8281 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8282
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8284
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 // press BTN_STYLUS, release BTN_STYLUS
8286 processKey(mapper, BTN_STYLUS, 1);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8289 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008290 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8291
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8293 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8294 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008295
8296 processKey(mapper, BTN_STYLUS, 0);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008299 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008300 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008301
8302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008303 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008304 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008305
8306 // press BTN_STYLUS2, release BTN_STYLUS2
8307 processKey(mapper, BTN_STYLUS2, 1);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8310 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008311 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8312
8313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8314 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8315 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316
8317 processKey(mapper, BTN_STYLUS2, 0);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008320 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008322
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008325 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326
8327 // release touch
8328 processId(mapper, -1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8332 ASSERT_EQ(0, motionArgs.buttonState);
8333}
8334
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008335TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8336 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008337 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008338 prepareAxes(POSITION | ID | SLOT);
8339 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8340
8341 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8342 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8343
8344 // Touch down.
8345 processId(mapper, 1);
8346 processPosition(mapper, 100, 200);
8347 processSync(mapper);
8348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8349 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8350
8351 // Press and release button mapped to the primary stylus button.
8352 processKey(mapper, BTN_A, 1);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8355 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8356 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8358 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8359 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8360
8361 processKey(mapper, BTN_A, 0);
8362 processSync(mapper);
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8364 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8367
8368 // Press and release the HID usage mapped to the secondary stylus button.
8369 processHidUsage(mapper, 0xabcd, 1);
8370 processSync(mapper);
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8372 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8373 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8376 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8377
8378 processHidUsage(mapper, 0xabcd, 0);
8379 processSync(mapper);
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8384
8385 // Release touch.
8386 processId(mapper, -1);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8389 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8390}
8391
Michael Wrightd02c5b62014-02-10 15:10:22 -08008392TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008393 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008394 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008395 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008396 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397
8398 NotifyMotionArgs motionArgs;
8399
8400 // default tool type is finger
8401 processId(mapper, 1);
8402 processPosition(mapper, 100, 200);
8403 processSync(mapper);
8404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8405 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8406 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8407
8408 // eraser
8409 processKey(mapper, BTN_TOOL_RUBBER, 1);
8410 processSync(mapper);
8411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8412 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8413 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8414
8415 // stylus
8416 processKey(mapper, BTN_TOOL_RUBBER, 0);
8417 processKey(mapper, BTN_TOOL_PEN, 1);
8418 processSync(mapper);
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8422
8423 // brush
8424 processKey(mapper, BTN_TOOL_PEN, 0);
8425 processKey(mapper, BTN_TOOL_BRUSH, 1);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8429 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8430
8431 // pencil
8432 processKey(mapper, BTN_TOOL_BRUSH, 0);
8433 processKey(mapper, BTN_TOOL_PENCIL, 1);
8434 processSync(mapper);
8435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8436 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8437 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8438
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008439 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440 processKey(mapper, BTN_TOOL_PENCIL, 0);
8441 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8442 processSync(mapper);
8443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8444 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8445 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8446
8447 // mouse
8448 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8449 processKey(mapper, BTN_TOOL_MOUSE, 1);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8453 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8454
8455 // lens
8456 processKey(mapper, BTN_TOOL_MOUSE, 0);
8457 processKey(mapper, BTN_TOOL_LENS, 1);
8458 processSync(mapper);
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8461 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8462
8463 // double-tap
8464 processKey(mapper, BTN_TOOL_LENS, 0);
8465 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8469 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8470
8471 // triple-tap
8472 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8473 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8474 processSync(mapper);
8475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8476 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8477 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8478
8479 // quad-tap
8480 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8481 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8485 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8486
8487 // finger
8488 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8489 processKey(mapper, BTN_TOOL_FINGER, 1);
8490 processSync(mapper);
8491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8492 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8493 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8494
8495 // stylus trumps finger
8496 processKey(mapper, BTN_TOOL_PEN, 1);
8497 processSync(mapper);
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8500 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8501
8502 // eraser trumps stylus
8503 processKey(mapper, BTN_TOOL_RUBBER, 1);
8504 processSync(mapper);
8505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8507 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8508
8509 // mouse trumps eraser
8510 processKey(mapper, BTN_TOOL_MOUSE, 1);
8511 processSync(mapper);
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8514 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8515
8516 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8517 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8521 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8522
8523 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8524 processToolType(mapper, MT_TOOL_PEN);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8528 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8529
8530 // back to default tool type
8531 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8532 processKey(mapper, BTN_TOOL_MOUSE, 0);
8533 processKey(mapper, BTN_TOOL_RUBBER, 0);
8534 processKey(mapper, BTN_TOOL_PEN, 0);
8535 processKey(mapper, BTN_TOOL_FINGER, 0);
8536 processSync(mapper);
8537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8538 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8539 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8540}
8541
8542TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008544 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008545 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008546 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008547 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008548
8549 NotifyMotionArgs motionArgs;
8550
8551 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8552 processId(mapper, 1);
8553 processPosition(mapper, 100, 200);
8554 processSync(mapper);
8555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8556 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8558 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8559
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8562 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8563 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8564
8565 // move a little
8566 processPosition(mapper, 150, 250);
8567 processSync(mapper);
8568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8569 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8571 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8572
8573 // down when BTN_TOUCH is pressed, pressure defaults to 1
8574 processKey(mapper, BTN_TOUCH, 1);
8575 processSync(mapper);
8576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8577 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8579 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8580
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8583 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8584 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8585
8586 // up when BTN_TOUCH is released, hover restored
8587 processKey(mapper, BTN_TOUCH, 0);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8593
8594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8595 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8598
8599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8601 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8602 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8603
8604 // exit hover when pointer goes away
8605 processId(mapper, -1);
8606 processSync(mapper);
8607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8608 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8609 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8610 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8611}
8612
8613TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008614 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008615 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008616 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008617 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008618
8619 NotifyMotionArgs motionArgs;
8620
8621 // initially hovering because pressure is 0
8622 processId(mapper, 1);
8623 processPosition(mapper, 100, 200);
8624 processPressure(mapper, 0);
8625 processSync(mapper);
8626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8627 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8629 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8630
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8632 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8634 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8635
8636 // move a little
8637 processPosition(mapper, 150, 250);
8638 processSync(mapper);
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8641 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8642 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8643
8644 // down when pressure becomes non-zero
8645 processPressure(mapper, RAW_PRESSURE_MAX);
8646 processSync(mapper);
8647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8648 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8650 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8651
8652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8653 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8656
8657 // up when pressure becomes 0, hover restored
8658 processPressure(mapper, 0);
8659 processSync(mapper);
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8662 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8663 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8664
8665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8666 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8667 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8668 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8669
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8671 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8672 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8673 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8674
8675 // exit hover when pointer goes away
8676 processId(mapper, -1);
8677 processSync(mapper);
8678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8680 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8681 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8682}
8683
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008684/**
8685 * Set the input device port <--> display port associations, and check that the
8686 * events are routed to the display that matches the display port.
8687 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8688 */
8689TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008690 const std::string usb2 = "USB2";
8691 const uint8_t hdmi1 = 0;
8692 const uint8_t hdmi2 = 1;
8693 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008694 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008695
8696 addConfigurationProperty("touch.deviceType", "touchScreen");
8697 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008698 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008699
8700 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8701 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8702
8703 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8704 // for this input device is specified, and the matching viewport is not present,
8705 // the input device should be disabled (at the mapper level).
8706
8707 // Add viewport for display 2 on hdmi2
8708 prepareSecondaryDisplay(type, hdmi2);
8709 // Send a touch event
8710 processPosition(mapper, 100, 100);
8711 processSync(mapper);
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8713
8714 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008715 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008716 // Send a touch event again
8717 processPosition(mapper, 100, 100);
8718 processSync(mapper);
8719
8720 NotifyMotionArgs args;
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8722 ASSERT_EQ(DISPLAY_ID, args.displayId);
8723}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008724
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008725TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8726 addConfigurationProperty("touch.deviceType", "touchScreen");
8727 prepareAxes(POSITION);
8728 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8729
8730 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8731
Michael Wrighta9cf4192022-12-01 23:46:39 +00008732 prepareDisplay(ui::ROTATION_0);
8733 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008734
8735 // Send a touch event
8736 processPosition(mapper, 100, 100);
8737 processSync(mapper);
8738
8739 NotifyMotionArgs args;
8740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8741 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8742}
8743
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008744TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008745 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008746 std::shared_ptr<FakePointerController> fakePointerController =
8747 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008748 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008749 fakePointerController->setPosition(100, 200);
8750 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008751 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008752
Garfield Tan888a6a42020-01-09 11:39:16 -08008753 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008754 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008755
Michael Wrighta9cf4192022-12-01 23:46:39 +00008756 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008757 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008758 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008759
Harry Cutts16a24cc2022-10-26 15:22:19 +00008760 // Check source is a touchpad that would obtain the PointerController.
8761 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008762
8763 NotifyMotionArgs motionArgs;
8764 processPosition(mapper, 100, 100);
8765 processSync(mapper);
8766
8767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8768 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8769 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8770}
8771
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008772/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008773 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8774 */
8775TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8776 addConfigurationProperty("touch.deviceType", "touchScreen");
8777 prepareAxes(POSITION);
8778 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8779
Michael Wrighta9cf4192022-12-01 23:46:39 +00008780 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008781 process(mapper, 10, 11 /*readTime*/, EV_ABS, ABS_MT_TRACKING_ID, 1);
8782 process(mapper, 15, 16 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 100);
8783 process(mapper, 20, 21 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 100);
8784 process(mapper, 25, 26 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8785
8786 NotifyMotionArgs args;
8787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8788 ASSERT_EQ(26, args.readTime);
8789
8790 process(mapper, 30, 31 /*readTime*/, EV_ABS, ABS_MT_POSITION_X, 110);
8791 process(mapper, 30, 32 /*readTime*/, EV_ABS, ABS_MT_POSITION_Y, 220);
8792 process(mapper, 30, 33 /*readTime*/, EV_SYN, SYN_REPORT, 0);
8793
8794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8795 ASSERT_EQ(33, args.readTime);
8796}
8797
8798/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008799 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8800 * events should not be delivered to the listener.
8801 */
8802TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8803 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008804 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008805 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8806 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008807 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8808 prepareAxes(POSITION);
8809 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8810
8811 NotifyMotionArgs motionArgs;
8812 processPosition(mapper, 100, 100);
8813 processSync(mapper);
8814
8815 mFakeListener->assertNotifyMotionWasNotCalled();
8816}
8817
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008818/**
8819 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8820 * the touch mapper can process the events and the events can be delivered to the listener.
8821 */
8822TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8823 addConfigurationProperty("touch.deviceType", "touchScreen");
8824 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008825 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8826 false /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008827 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8828 prepareAxes(POSITION);
8829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8830
8831 NotifyMotionArgs motionArgs;
8832 processPosition(mapper, 100, 100);
8833 processSync(mapper);
8834
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8836 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8837}
8838
Garfield Tanc734e4f2021-01-15 20:01:39 -08008839TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8840 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008841 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008842 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8843 true /*isActive*/, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008844 std::optional<DisplayViewport> optionalDisplayViewport =
8845 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8846 ASSERT_TRUE(optionalDisplayViewport.has_value());
8847 DisplayViewport displayViewport = *optionalDisplayViewport;
8848
8849 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8850 prepareAxes(POSITION);
8851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8852
8853 // Finger down
8854 int32_t x = 100, y = 100;
8855 processPosition(mapper, x, y);
8856 processSync(mapper);
8857
8858 NotifyMotionArgs motionArgs;
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8860 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8861
8862 // Deactivate display viewport
8863 displayViewport.isActive = false;
8864 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8865 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8866
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008867 // The ongoing touch should be canceled immediately
8868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8869 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8870
8871 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008872 x += 10, y += 10;
8873 processPosition(mapper, x, y);
8874 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008876
8877 // Reactivate display viewport
8878 displayViewport.isActive = true;
8879 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8880 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
8881
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008882 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008883 x += 10, y += 10;
8884 processPosition(mapper, x, y);
8885 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8887 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008888}
8889
Arthur Hung7c645402019-01-25 17:45:42 +08008890TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8891 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008892 prepareAxes(POSITION | ID | SLOT);
8893 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008895
8896 // Create the second touch screen device, and enable multi fingers.
8897 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008898 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008899 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008900 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008901 std::shared_ptr<InputDevice> device2 =
8902 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008903 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008904
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008905 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
8906 0 /*flat*/, 0 /*fuzz*/);
8907 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
8908 0 /*flat*/, 0 /*fuzz*/);
8909 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
8910 0 /*flat*/, 0 /*fuzz*/);
8911 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
8912 0 /*flat*/, 0 /*fuzz*/);
8913 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, 0 /*value*/);
8914 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8915 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008916
8917 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008918 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008919 std::list<NotifyArgs> unused =
8920 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8921 0 /*changes*/);
8922 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008923
8924 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008925 std::shared_ptr<FakePointerController> fakePointerController =
8926 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008927 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008928
8929 // Setup policy for associated displays and show touches.
8930 const uint8_t hdmi1 = 0;
8931 const uint8_t hdmi2 = 1;
8932 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8933 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8934 mFakePolicy->setShowTouches(true);
8935
8936 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008937 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008938 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008939
8940 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008941 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8942 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
8943 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008944
8945 // Two fingers down at default display.
8946 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8947 processPosition(mapper, x1, y1);
8948 processId(mapper, 1);
8949 processSlot(mapper, 1);
8950 processPosition(mapper, x2, y2);
8951 processId(mapper, 2);
8952 processSync(mapper);
8953
8954 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8955 fakePointerController->getSpots().find(DISPLAY_ID);
8956 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8957 ASSERT_EQ(size_t(2), iter->second.size());
8958
8959 // Two fingers down at second display.
8960 processPosition(mapper2, x1, y1);
8961 processId(mapper2, 1);
8962 processSlot(mapper2, 1);
8963 processPosition(mapper2, x2, y2);
8964 processId(mapper2, 2);
8965 processSync(mapper2);
8966
8967 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8968 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8969 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008970
8971 // Disable the show touches configuration and ensure the spots are cleared.
8972 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008973 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
8974 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008975
8976 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08008977}
8978
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008979TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008980 prepareAxes(POSITION);
8981 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008982 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008983 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008984
8985 NotifyMotionArgs motionArgs;
8986 // Unrotated video frame
8987 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8988 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008989 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008990 processPosition(mapper, 100, 200);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8993 ASSERT_EQ(frames, motionArgs.videoFrames);
8994
8995 // Subsequent touch events should not have any videoframes
8996 // This is implemented separately in FakeEventHub,
8997 // but that should match the behaviour of TouchVideoDevice.
8998 processPosition(mapper, 200, 200);
8999 processSync(mapper);
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9001 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9002}
9003
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009004TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009005 prepareAxes(POSITION);
9006 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009007 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009008 // Unrotated video frame
9009 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9010 NotifyMotionArgs motionArgs;
9011
9012 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009013 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009014 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9015 clearViewports();
9016 prepareDisplay(orientation);
9017 std::vector<TouchVideoFrame> frames{frame};
9018 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9019 processPosition(mapper, 100, 200);
9020 processSync(mapper);
9021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9022 ASSERT_EQ(frames, motionArgs.videoFrames);
9023 }
9024}
9025
9026TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9027 prepareAxes(POSITION);
9028 addConfigurationProperty("touch.deviceType", "touchScreen");
9029 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9030 // orientation-aware are affected by display rotation.
9031 addConfigurationProperty("touch.orientationAware", "0");
9032 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9033 // Unrotated video frame
9034 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9035 NotifyMotionArgs motionArgs;
9036
9037 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009038 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009039 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9040 clearViewports();
9041 prepareDisplay(orientation);
9042 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009043 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009044 processPosition(mapper, 100, 200);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009047 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9048 // compared to the display. This is so that when the window transform (which contains the
9049 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9050 // window's coordinate space.
9051 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009052 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009053
9054 // Release finger.
9055 processSync(mapper);
9056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009057 }
9058}
9059
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009060TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009061 prepareAxes(POSITION);
9062 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009063 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009064 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9065 // so mix these.
9066 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9067 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9068 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9069 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9070 NotifyMotionArgs motionArgs;
9071
Michael Wrighta9cf4192022-12-01 23:46:39 +00009072 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009073 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009074 processPosition(mapper, 100, 200);
9075 processSync(mapper);
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009077 ASSERT_EQ(frames, motionArgs.videoFrames);
9078}
9079
9080TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9081 prepareAxes(POSITION);
9082 addConfigurationProperty("touch.deviceType", "touchScreen");
9083 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9084 // orientation-aware are affected by display rotation.
9085 addConfigurationProperty("touch.orientationAware", "0");
9086 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9087 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9088 // so mix these.
9089 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9090 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9091 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9092 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9093 NotifyMotionArgs motionArgs;
9094
Michael Wrighta9cf4192022-12-01 23:46:39 +00009095 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009096 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9097 processPosition(mapper, 100, 200);
9098 processSync(mapper);
9099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9100 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9101 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9102 // compared to the display. This is so that when the window transform (which contains the
9103 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9104 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009105 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009106 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009107 ASSERT_EQ(frames, motionArgs.videoFrames);
9108}
9109
Arthur Hung9da14732019-09-02 16:16:58 +08009110/**
9111 * If we had defined port associations, but the viewport is not ready, the touch device would be
9112 * expected to be disabled, and it should be enabled after the viewport has found.
9113 */
9114TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009115 constexpr uint8_t hdmi2 = 1;
9116 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009117 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009118
9119 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9120
9121 addConfigurationProperty("touch.deviceType", "touchScreen");
9122 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009123 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009124
9125 ASSERT_EQ(mDevice->isEnabled(), false);
9126
9127 // Add display on hdmi2, the device should be enabled and can receive touch event.
9128 prepareSecondaryDisplay(type, hdmi2);
9129 ASSERT_EQ(mDevice->isEnabled(), true);
9130
9131 // Send a touch event.
9132 processPosition(mapper, 100, 100);
9133 processSync(mapper);
9134
9135 NotifyMotionArgs args;
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9137 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9138}
9139
Arthur Hung421eb1c2020-01-16 00:09:42 +08009140TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009141 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009142 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009143 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009144 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009145
9146 NotifyMotionArgs motionArgs;
9147
9148 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9149 // finger down
9150 processId(mapper, 1);
9151 processPosition(mapper, x1, y1);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9155 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9156
9157 // finger move
9158 processId(mapper, 1);
9159 processPosition(mapper, x2, y2);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9163 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9164
9165 // finger up.
9166 processId(mapper, -1);
9167 processSync(mapper);
9168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9169 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9170 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9171
9172 // new finger down
9173 processId(mapper, 1);
9174 processPosition(mapper, x3, y3);
9175 processSync(mapper);
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9178 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9179}
9180
9181/**
arthurhungcc7f9802020-04-30 17:55:40 +08009182 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9183 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009184 */
arthurhungcc7f9802020-04-30 17:55:40 +08009185TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009186 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009187 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009188 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009189 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009190
9191 NotifyMotionArgs motionArgs;
9192
9193 // default tool type is finger
9194 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009195 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009196 processPosition(mapper, x1, y1);
9197 processSync(mapper);
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9199 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9200 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9201
9202 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9203 processToolType(mapper, MT_TOOL_PALM);
9204 processSync(mapper);
9205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9206 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9207
9208 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009209 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009210 processPosition(mapper, x2, y2);
9211 processSync(mapper);
9212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9213
9214 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009215 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9218
9219 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009220 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009221 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009222 processPosition(mapper, x3, y3);
9223 processSync(mapper);
9224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9225 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9227}
9228
arthurhungbf89a482020-04-17 17:37:55 +08009229/**
arthurhungcc7f9802020-04-30 17:55:40 +08009230 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9231 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009232 */
arthurhungcc7f9802020-04-30 17:55:40 +08009233TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009234 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009235 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009236 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9237 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9238
9239 NotifyMotionArgs motionArgs;
9240
9241 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009242 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9243 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009244 processPosition(mapper, x1, y1);
9245 processSync(mapper);
9246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9248 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9249
9250 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009251 processSlot(mapper, SECOND_SLOT);
9252 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009253 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009254 processSync(mapper);
9255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009256 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009257 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9258
9259 // If the tool type of the first finger changes to MT_TOOL_PALM,
9260 // we expect to receive ACTION_POINTER_UP with cancel flag.
9261 processSlot(mapper, FIRST_SLOT);
9262 processId(mapper, FIRST_TRACKING_ID);
9263 processToolType(mapper, MT_TOOL_PALM);
9264 processSync(mapper);
9265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009266 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009267 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9268
9269 // The following MOVE events of second finger should be processed.
9270 processSlot(mapper, SECOND_SLOT);
9271 processId(mapper, SECOND_TRACKING_ID);
9272 processPosition(mapper, x2 + 1, y2 + 1);
9273 processSync(mapper);
9274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9275 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9276 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9277
9278 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9279 // it. Second finger receive move.
9280 processSlot(mapper, FIRST_SLOT);
9281 processId(mapper, INVALID_TRACKING_ID);
9282 processSync(mapper);
9283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9285 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9286
9287 // Second finger keeps moving.
9288 processSlot(mapper, SECOND_SLOT);
9289 processId(mapper, SECOND_TRACKING_ID);
9290 processPosition(mapper, x2 + 2, y2 + 2);
9291 processSync(mapper);
9292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9293 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9294 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9295
9296 // Second finger up.
9297 processId(mapper, INVALID_TRACKING_ID);
9298 processSync(mapper);
9299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9301 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9302}
9303
9304/**
9305 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9306 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9307 */
9308TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9309 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009310 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009311 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9312 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9313
9314 NotifyMotionArgs motionArgs;
9315
9316 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9317 // First finger down.
9318 processId(mapper, FIRST_TRACKING_ID);
9319 processPosition(mapper, x1, y1);
9320 processSync(mapper);
9321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9322 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9323 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9324
9325 // Second finger down.
9326 processSlot(mapper, SECOND_SLOT);
9327 processId(mapper, SECOND_TRACKING_ID);
9328 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009329 processSync(mapper);
9330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009331 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009332 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9333
arthurhungcc7f9802020-04-30 17:55:40 +08009334 // If the tool type of the first finger changes to MT_TOOL_PALM,
9335 // we expect to receive ACTION_POINTER_UP with cancel flag.
9336 processSlot(mapper, FIRST_SLOT);
9337 processId(mapper, FIRST_TRACKING_ID);
9338 processToolType(mapper, MT_TOOL_PALM);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009341 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009342 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9343
9344 // Second finger keeps moving.
9345 processSlot(mapper, SECOND_SLOT);
9346 processId(mapper, SECOND_TRACKING_ID);
9347 processPosition(mapper, x2 + 1, y2 + 1);
9348 processSync(mapper);
9349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9350 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9351
9352 // second finger becomes palm, receive cancel due to only 1 finger is active.
9353 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009354 processToolType(mapper, MT_TOOL_PALM);
9355 processSync(mapper);
9356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9357 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9358
arthurhungcc7f9802020-04-30 17:55:40 +08009359 // third finger down.
9360 processSlot(mapper, THIRD_SLOT);
9361 processId(mapper, THIRD_TRACKING_ID);
9362 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009363 processPosition(mapper, x3, y3);
9364 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9366 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9367 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009368 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9369
9370 // third finger move
9371 processId(mapper, THIRD_TRACKING_ID);
9372 processPosition(mapper, x3 + 1, y3 + 1);
9373 processSync(mapper);
9374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9375 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9376
9377 // first finger up, third finger receive move.
9378 processSlot(mapper, FIRST_SLOT);
9379 processId(mapper, INVALID_TRACKING_ID);
9380 processSync(mapper);
9381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9382 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9383 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9384
9385 // second finger up, third finger receive move.
9386 processSlot(mapper, SECOND_SLOT);
9387 processId(mapper, INVALID_TRACKING_ID);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9391 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9392
9393 // third finger up.
9394 processSlot(mapper, THIRD_SLOT);
9395 processId(mapper, INVALID_TRACKING_ID);
9396 processSync(mapper);
9397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9398 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9399 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9400}
9401
9402/**
9403 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9404 * and the active finger could still be allowed to receive the events
9405 */
9406TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9407 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009408 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009409 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9411
9412 NotifyMotionArgs motionArgs;
9413
9414 // default tool type is finger
9415 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9416 processId(mapper, FIRST_TRACKING_ID);
9417 processPosition(mapper, x1, y1);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9421 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9422
9423 // Second finger down.
9424 processSlot(mapper, SECOND_SLOT);
9425 processId(mapper, SECOND_TRACKING_ID);
9426 processPosition(mapper, x2, y2);
9427 processSync(mapper);
9428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009429 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009430 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9431
9432 // If the tool type of the second finger changes to MT_TOOL_PALM,
9433 // we expect to receive ACTION_POINTER_UP with cancel flag.
9434 processId(mapper, SECOND_TRACKING_ID);
9435 processToolType(mapper, MT_TOOL_PALM);
9436 processSync(mapper);
9437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009438 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009439 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9440
9441 // The following MOVE event should be processed.
9442 processSlot(mapper, FIRST_SLOT);
9443 processId(mapper, FIRST_TRACKING_ID);
9444 processPosition(mapper, x1 + 1, y1 + 1);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9448 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9449
9450 // second finger up.
9451 processSlot(mapper, SECOND_SLOT);
9452 processId(mapper, INVALID_TRACKING_ID);
9453 processSync(mapper);
9454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9455 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9456
9457 // first finger keep moving
9458 processSlot(mapper, FIRST_SLOT);
9459 processId(mapper, FIRST_TRACKING_ID);
9460 processPosition(mapper, x1 + 2, y1 + 2);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9464
9465 // first finger up.
9466 processId(mapper, INVALID_TRACKING_ID);
9467 processSync(mapper);
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9470 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009471}
9472
Arthur Hung9ad18942021-06-19 02:04:46 +00009473/**
9474 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9475 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9476 * cause slot be valid again.
9477 */
9478TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9479 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009480 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009481 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9482 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9483
9484 NotifyMotionArgs motionArgs;
9485
9486 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9487 // First finger down.
9488 processId(mapper, FIRST_TRACKING_ID);
9489 processPosition(mapper, x1, y1);
9490 processPressure(mapper, RAW_PRESSURE_MAX);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9493 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9494 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9495
9496 // First finger move.
9497 processId(mapper, FIRST_TRACKING_ID);
9498 processPosition(mapper, x1 + 1, y1 + 1);
9499 processPressure(mapper, RAW_PRESSURE_MAX);
9500 processSync(mapper);
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9503 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9504
9505 // Second finger down.
9506 processSlot(mapper, SECOND_SLOT);
9507 processId(mapper, SECOND_TRACKING_ID);
9508 processPosition(mapper, x2, y2);
9509 processPressure(mapper, RAW_PRESSURE_MAX);
9510 processSync(mapper);
9511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009512 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009513 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9514
9515 // second finger up with some unexpected data.
9516 processSlot(mapper, SECOND_SLOT);
9517 processId(mapper, INVALID_TRACKING_ID);
9518 processPosition(mapper, x2, y2);
9519 processSync(mapper);
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009521 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009522 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9523
9524 // first finger up with some unexpected data.
9525 processSlot(mapper, FIRST_SLOT);
9526 processId(mapper, INVALID_TRACKING_ID);
9527 processPosition(mapper, x2, y2);
9528 processPressure(mapper, RAW_PRESSURE_MAX);
9529 processSync(mapper);
9530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9531 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9532 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9533}
9534
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009535TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9536 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009537 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009538 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9540
9541 // First finger down.
9542 processId(mapper, FIRST_TRACKING_ID);
9543 processPosition(mapper, 100, 200);
9544 processPressure(mapper, RAW_PRESSURE_MAX);
9545 processSync(mapper);
9546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9547 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9548
9549 // Second finger down.
9550 processSlot(mapper, SECOND_SLOT);
9551 processId(mapper, SECOND_TRACKING_ID);
9552 processPosition(mapper, 300, 400);
9553 processPressure(mapper, RAW_PRESSURE_MAX);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(
9556 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9557
9558 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009559 // preserved. Resetting should cancel the ongoing gesture.
9560 resetMapper(mapper, ARBITRARY_TIME);
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9562 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009563
9564 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9565 // the existing touch state to generate a down event.
9566 processPosition(mapper, 301, 302);
9567 processSync(mapper);
9568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9569 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9571 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9572
9573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9574}
9575
9576TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9577 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009578 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009579 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9581
9582 // First finger touches down and releases.
9583 processId(mapper, FIRST_TRACKING_ID);
9584 processPosition(mapper, 100, 200);
9585 processPressure(mapper, RAW_PRESSURE_MAX);
9586 processSync(mapper);
9587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9588 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9589 processId(mapper, INVALID_TRACKING_ID);
9590 processSync(mapper);
9591 ASSERT_NO_FATAL_FAILURE(
9592 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9593
9594 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9595 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009596 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9598
9599 // Send an empty sync frame. Since there are no pointers, no events are generated.
9600 processSync(mapper);
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9602}
9603
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009604TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009605 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009606 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009607 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9608 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009610
9611 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9612 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9613 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9614 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9616
9617 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009618 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009619 processId(mapper, FIRST_TRACKING_ID);
9620 processToolType(mapper, MT_TOOL_PEN);
9621 processPosition(mapper, 100, 200);
9622 processPressure(mapper, RAW_PRESSURE_MAX);
9623 processSync(mapper);
9624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9625 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9626 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9627 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9628
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009629 // Now that we know the device supports styluses, ensure that the device is re-configured with
9630 // the stylus source.
9631 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9632 {
9633 const auto& devices = mReader->getInputDevices();
9634 auto deviceInfo =
9635 std::find_if(devices.begin(), devices.end(),
9636 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9637 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9638 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9639 }
9640
9641 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9643
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009644 processId(mapper, INVALID_TRACKING_ID);
9645 processSync(mapper);
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9647 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9648 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9649 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009650}
9651
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009652// --- MultiTouchInputMapperTest_ExternalDevice ---
9653
9654class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9655protected:
Chris Yea52ade12020-08-27 16:49:20 -07009656 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009657};
9658
9659/**
9660 * Expect fallback to internal viewport if device is external and external viewport is not present.
9661 */
9662TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9663 prepareAxes(POSITION);
9664 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009665 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009666 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9667
9668 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9669
9670 NotifyMotionArgs motionArgs;
9671
9672 // Expect the event to be sent to the internal viewport,
9673 // because an external viewport is not present.
9674 processPosition(mapper, 100, 100);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9677 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9678
9679 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009680 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009681 processPosition(mapper, 100, 100);
9682 processSync(mapper);
9683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9684 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9685}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009686
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009687TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9688 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9689 std::shared_ptr<FakePointerController> fakePointerController =
9690 std::make_shared<FakePointerController>();
9691 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9692 fakePointerController->setPosition(0, 0);
9693 fakePointerController->setButtonState(0);
9694
9695 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009696 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009697 prepareAxes(POSITION | ID | SLOT);
9698 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9699 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9700 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009701 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009702 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9703
9704 // captured touchpad should be a touchpad source
9705 NotifyDeviceResetArgs resetArgs;
9706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9707 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9708
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009709 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009710
9711 const InputDeviceInfo::MotionRange* relRangeX =
9712 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9713 ASSERT_NE(relRangeX, nullptr);
9714 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9715 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9716 const InputDeviceInfo::MotionRange* relRangeY =
9717 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9718 ASSERT_NE(relRangeY, nullptr);
9719 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9720 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9721
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009722 // run captured pointer tests - note that this is unscaled, so input listener events should be
9723 // identical to what the hardware sends (accounting for any
9724 // calibration).
9725 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009726 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009727 processId(mapper, 1);
9728 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9729 processKey(mapper, BTN_TOUCH, 1);
9730 processSync(mapper);
9731
9732 // expect coord[0] to contain initial location of touch 0
9733 NotifyMotionArgs args;
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9735 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9736 ASSERT_EQ(1U, args.pointerCount);
9737 ASSERT_EQ(0, args.pointerProperties[0].id);
9738 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9739 ASSERT_NO_FATAL_FAILURE(
9740 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9741
9742 // FINGER 1 DOWN
9743 processSlot(mapper, 1);
9744 processId(mapper, 2);
9745 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9746 processSync(mapper);
9747
9748 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009750 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009751 ASSERT_EQ(2U, args.pointerCount);
9752 ASSERT_EQ(0, args.pointerProperties[0].id);
9753 ASSERT_EQ(1, args.pointerProperties[1].id);
9754 ASSERT_NO_FATAL_FAILURE(
9755 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9756 ASSERT_NO_FATAL_FAILURE(
9757 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9758
9759 // FINGER 1 MOVE
9760 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9761 processSync(mapper);
9762
9763 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9764 // from move
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9766 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9767 ASSERT_NO_FATAL_FAILURE(
9768 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9769 ASSERT_NO_FATAL_FAILURE(
9770 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9771
9772 // FINGER 0 MOVE
9773 processSlot(mapper, 0);
9774 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9775 processSync(mapper);
9776
9777 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9780 ASSERT_NO_FATAL_FAILURE(
9781 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9782 ASSERT_NO_FATAL_FAILURE(
9783 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9784
9785 // BUTTON DOWN
9786 processKey(mapper, BTN_LEFT, 1);
9787 processSync(mapper);
9788
9789 // touchinputmapper design sends a move before button press
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9793 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9794
9795 // BUTTON UP
9796 processKey(mapper, BTN_LEFT, 0);
9797 processSync(mapper);
9798
9799 // touchinputmapper design sends a move after button release
9800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9801 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9803 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9804
9805 // FINGER 0 UP
9806 processId(mapper, -1);
9807 processSync(mapper);
9808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9809 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9810
9811 // FINGER 1 MOVE
9812 processSlot(mapper, 1);
9813 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9814 processSync(mapper);
9815
9816 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9818 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9819 ASSERT_EQ(1U, args.pointerCount);
9820 ASSERT_EQ(1, args.pointerProperties[0].id);
9821 ASSERT_NO_FATAL_FAILURE(
9822 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9823
9824 // FINGER 1 UP
9825 processId(mapper, -1);
9826 processKey(mapper, BTN_TOUCH, 0);
9827 processSync(mapper);
9828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9829 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9830
Harry Cutts16a24cc2022-10-26 15:22:19 +00009831 // A non captured touchpad should have a mouse and touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009832 mFakePolicy->setPointerCapture(false);
9833 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Harry Cutts16a24cc2022-10-26 15:22:19 +00009835 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009836}
9837
9838TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9839 std::shared_ptr<FakePointerController> fakePointerController =
9840 std::make_shared<FakePointerController>();
9841 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9842 fakePointerController->setPosition(0, 0);
9843 fakePointerController->setButtonState(0);
9844
9845 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009846 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009847 prepareAxes(POSITION | ID | SLOT);
9848 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9849 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009850 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009851 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9852 // run uncaptured pointer tests - pushes out generic events
9853 // FINGER 0 DOWN
9854 processId(mapper, 3);
9855 processPosition(mapper, 100, 100);
9856 processKey(mapper, BTN_TOUCH, 1);
9857 processSync(mapper);
9858
9859 // start at (100,100), cursor should be at (0,0) * scale
9860 NotifyMotionArgs args;
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9863 ASSERT_NO_FATAL_FAILURE(
9864 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9865
9866 // FINGER 0 MOVE
9867 processPosition(mapper, 200, 200);
9868 processSync(mapper);
9869
9870 // compute scaling to help with touch position checking
9871 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9872 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9873 float scale =
9874 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9875
9876 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9878 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9879 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9880 0, 0, 0, 0, 0, 0, 0));
9881}
9882
9883TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9884 std::shared_ptr<FakePointerController> fakePointerController =
9885 std::make_shared<FakePointerController>();
9886
Michael Wrighta9cf4192022-12-01 23:46:39 +00009887 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009888 prepareAxes(POSITION | ID | SLOT);
9889 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009890 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009891 mFakePolicy->setPointerCapture(false);
9892 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9893
Harry Cutts16a24cc2022-10-26 15:22:19 +00009894 // An uncaptured touchpad should be a pointer device, with additional touchpad source.
9895 ASSERT_EQ(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009896
Harry Cutts16a24cc2022-10-26 15:22:19 +00009897 // A captured touchpad should just have a touchpad source.
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009898 mFakePolicy->setPointerCapture(true);
9899 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
9900 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9901}
9902
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009903// --- BluetoothMultiTouchInputMapperTest ---
9904
9905class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9906protected:
9907 void SetUp() override {
9908 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9909 }
9910};
9911
9912TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9913 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009914 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009915 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9916 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9917
9918 nsecs_t kernelEventTime = ARBITRARY_TIME;
9919 nsecs_t expectedEventTime = ARBITRARY_TIME;
9920 // Touch down.
9921 processId(mapper, FIRST_TRACKING_ID);
9922 processPosition(mapper, 100, 200);
9923 processPressure(mapper, RAW_PRESSURE_MAX);
9924 processSync(mapper, ARBITRARY_TIME);
9925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9926 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9927
9928 // Process several events that come in quick succession, according to their timestamps.
9929 for (int i = 0; i < 3; i++) {
9930 constexpr static nsecs_t delta = ms2ns(1);
9931 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9932 kernelEventTime += delta;
9933 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9934
9935 processPosition(mapper, 101 + i, 201 + i);
9936 processSync(mapper, kernelEventTime);
9937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9938 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9939 WithEventTime(expectedEventTime))));
9940 }
9941
9942 // Release the touch.
9943 processId(mapper, INVALID_TRACKING_ID);
9944 processPressure(mapper, RAW_PRESSURE_MIN);
9945 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9947 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9948 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9949}
9950
9951// --- MultiTouchPointerModeTest ---
9952
HQ Liue6983c72022-04-19 22:14:56 +00009953class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9954protected:
9955 float mPointerMovementScale;
9956 float mPointerXZoomScale;
9957 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9958 addConfigurationProperty("touch.deviceType", "pointer");
9959 std::shared_ptr<FakePointerController> fakePointerController =
9960 std::make_shared<FakePointerController>();
9961 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9962 fakePointerController->setPosition(0, 0);
9963 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +00009964 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009965
9966 prepareAxes(POSITION);
9967 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9968 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9969 // needs to be disabled, and the pointer gesture needs to be enabled.
9970 mFakePolicy->setPointerCapture(false);
9971 mFakePolicy->setPointerGestureEnabled(true);
9972 mFakePolicy->setPointerController(fakePointerController);
9973
9974 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9975 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9976 mPointerMovementScale =
9977 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9978 mPointerXZoomScale =
9979 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9980 }
9981
9982 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9983 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9984 /*flat*/ 0,
9985 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9987 /*flat*/ 0,
9988 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9989 }
9990};
9991
9992/**
9993 * Two fingers down on a pointer mode touch pad. The width
9994 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9995 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9996 * be greater than the both value to be freeform gesture, so that after two
9997 * fingers start to move downwards, the gesture should be swipe.
9998 */
9999TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10000 // The min freeform gesture width is 25units/mm x 30mm = 750
10001 // which is greater than fraction of the diagnal length of the touchpad (349).
10002 // Thus, MaxSwipWidth is 750.
10003 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10004 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10005 NotifyMotionArgs motionArgs;
10006
10007 // Two fingers down at once.
10008 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10009 // Pointer's initial position is used the [0,0] coordinate.
10010 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10011
10012 processId(mapper, FIRST_TRACKING_ID);
10013 processPosition(mapper, x1, y1);
10014 processMTSync(mapper);
10015 processId(mapper, SECOND_TRACKING_ID);
10016 processPosition(mapper, x2, y2);
10017 processMTSync(mapper);
10018 processSync(mapper);
10019
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 ASSERT_EQ(1U, motionArgs.pointerCount);
10022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10023 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010024 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010025 ASSERT_NO_FATAL_FAILURE(
10026 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10027
10028 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10029 // that there should be 1 pointer.
10030 int32_t movingDistance = 200;
10031 y1 += movingDistance;
10032 y2 += movingDistance;
10033
10034 processId(mapper, FIRST_TRACKING_ID);
10035 processPosition(mapper, x1, y1);
10036 processMTSync(mapper);
10037 processId(mapper, SECOND_TRACKING_ID);
10038 processPosition(mapper, x2, y2);
10039 processMTSync(mapper);
10040 processSync(mapper);
10041
10042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10043 ASSERT_EQ(1U, motionArgs.pointerCount);
10044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10045 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010046 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10048 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10049 0, 0, 0, 0));
10050}
10051
10052/**
10053 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10054 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10055 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10056 * value to be freeform gesture, so that after two fingers start to move downwards,
10057 * the gesture should be swipe.
10058 */
10059TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10060 // The min freeform gesture width is 5units/mm x 30mm = 150
10061 // which is greater than fraction of the diagnal length of the touchpad (349).
10062 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
10063 preparePointerMode(5 /*xResolution*/, 5 /*yResolution*/);
10064 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10065 NotifyMotionArgs motionArgs;
10066
10067 // Two fingers down at once.
10068 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10069 // Pointer's initial position is used the [0,0] coordinate.
10070 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10071
10072 processId(mapper, FIRST_TRACKING_ID);
10073 processPosition(mapper, x1, y1);
10074 processMTSync(mapper);
10075 processId(mapper, SECOND_TRACKING_ID);
10076 processPosition(mapper, x2, y2);
10077 processMTSync(mapper);
10078 processSync(mapper);
10079
10080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10081 ASSERT_EQ(1U, motionArgs.pointerCount);
10082 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10083 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010084 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010085 ASSERT_NO_FATAL_FAILURE(
10086 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10087
10088 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10089 // and there should be 1 pointer.
10090 int32_t movingDistance = 200;
10091 y1 += movingDistance;
10092 y2 += movingDistance;
10093
10094 processId(mapper, FIRST_TRACKING_ID);
10095 processPosition(mapper, x1, y1);
10096 processMTSync(mapper);
10097 processId(mapper, SECOND_TRACKING_ID);
10098 processPosition(mapper, x2, y2);
10099 processMTSync(mapper);
10100 processSync(mapper);
10101
10102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10103 ASSERT_EQ(1U, motionArgs.pointerCount);
10104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10105 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010106 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010107 // New coordinate is the scaled relative coordinate from the initial coordinate.
10108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10109 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10110 0, 0, 0, 0));
10111}
10112
10113/**
10114 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10115 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10116 * freeform gestures after two fingers start to move downwards.
10117 */
10118TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
10119 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10120 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10121
10122 NotifyMotionArgs motionArgs;
10123
10124 // Two fingers down at once. Wider than the max swipe width.
10125 // The gesture is expected to be PRESS, then transformed to FREEFORM
10126 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10127
10128 processId(mapper, FIRST_TRACKING_ID);
10129 processPosition(mapper, x1, y1);
10130 processMTSync(mapper);
10131 processId(mapper, SECOND_TRACKING_ID);
10132 processPosition(mapper, x2, y2);
10133 processMTSync(mapper);
10134 processSync(mapper);
10135
10136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10137 ASSERT_EQ(1U, motionArgs.pointerCount);
10138 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10139 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010140 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010141 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10142 ASSERT_NO_FATAL_FAILURE(
10143 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10144
10145 int32_t movingDistance = 200;
10146
10147 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10148 // then two down events for two pointers.
10149 y1 += movingDistance;
10150 y2 += movingDistance;
10151
10152 processId(mapper, FIRST_TRACKING_ID);
10153 processPosition(mapper, x1, y1);
10154 processMTSync(mapper);
10155 processId(mapper, SECOND_TRACKING_ID);
10156 processPosition(mapper, x2, y2);
10157 processMTSync(mapper);
10158 processSync(mapper);
10159
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10161 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10162 ASSERT_EQ(1U, motionArgs.pointerCount);
10163 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10165 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10166 ASSERT_EQ(1U, motionArgs.pointerCount);
10167 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10169 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010170 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010171 ASSERT_EQ(2U, motionArgs.pointerCount);
10172 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10173 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010174 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010175 // Two pointers' scaled relative coordinates from their initial centroid.
10176 // Initial y coordinates are 0 as y1 and y2 have the same value.
10177 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10178 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10179 // When pointers move, the new coordinates equal to the initial coordinates plus
10180 // scaled moving distance.
10181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10182 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10183 0, 0, 0, 0));
10184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10185 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10186 0, 0, 0, 0));
10187
10188 // Move two fingers down again, expect one MOVE motion event.
10189 y1 += movingDistance;
10190 y2 += movingDistance;
10191
10192 processId(mapper, FIRST_TRACKING_ID);
10193 processPosition(mapper, x1, y1);
10194 processMTSync(mapper);
10195 processId(mapper, SECOND_TRACKING_ID);
10196 processPosition(mapper, x2, y2);
10197 processMTSync(mapper);
10198 processSync(mapper);
10199
10200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10201 ASSERT_EQ(2U, motionArgs.pointerCount);
10202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10203 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010204 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10206 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10207 0, 0, 0, 0, 0));
10208 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10209 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10210 0, 0, 0, 0, 0));
10211}
10212
Harry Cutts39b7ca22022-10-05 15:55:48 +000010213TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
10214 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10215 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10216 NotifyMotionArgs motionArgs;
10217
10218 // Place two fingers down.
10219 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10220
10221 processId(mapper, FIRST_TRACKING_ID);
10222 processPosition(mapper, x1, y1);
10223 processMTSync(mapper);
10224 processId(mapper, SECOND_TRACKING_ID);
10225 processPosition(mapper, x2, y2);
10226 processMTSync(mapper);
10227 processSync(mapper);
10228
10229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10230 ASSERT_EQ(1U, motionArgs.pointerCount);
10231 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10232 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10233 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10234 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10235
10236 // Move the two fingers down and to the left.
10237 int32_t movingDistance = 200;
10238 x1 -= movingDistance;
10239 y1 += movingDistance;
10240 x2 -= movingDistance;
10241 y2 += movingDistance;
10242
10243 processId(mapper, FIRST_TRACKING_ID);
10244 processPosition(mapper, x1, y1);
10245 processMTSync(mapper);
10246 processId(mapper, SECOND_TRACKING_ID);
10247 processPosition(mapper, x2, y2);
10248 processMTSync(mapper);
10249 processSync(mapper);
10250
10251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10252 ASSERT_EQ(1U, motionArgs.pointerCount);
10253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10254 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10255 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10256 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10257}
10258
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010259TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
10260 preparePointerMode(25 /*xResolution*/, 25 /*yResolution*/);
10261 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10262 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10264
10265 // Start a stylus gesture.
10266 processKey(mapper, BTN_TOOL_PEN, 1);
10267 processId(mapper, FIRST_TRACKING_ID);
10268 processPosition(mapper, 100, 200);
10269 processSync(mapper);
10270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10271 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10272 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10273 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10274 // TODO(b/257078296): Pointer mode generates extra event.
10275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10277 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10278 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10280
10281 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10282 // gesture should be disabled.
10283 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10284 viewport->isActive = false;
10285 mFakePolicy->updateViewport(*viewport);
10286 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10289 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10290 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10291 // TODO(b/257078296): Pointer mode generates extra event.
10292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10294 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10295 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10297}
10298
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010299// --- JoystickInputMapperTest ---
10300
10301class JoystickInputMapperTest : public InputMapperTest {
10302protected:
10303 static const int32_t RAW_X_MIN;
10304 static const int32_t RAW_X_MAX;
10305 static const int32_t RAW_Y_MIN;
10306 static const int32_t RAW_Y_MAX;
10307
10308 void SetUp() override {
10309 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10310 }
10311 void prepareAxes() {
10312 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10313 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10314 }
10315
10316 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10317 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10318 }
10319
10320 void processSync(JoystickInputMapper& mapper) {
10321 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10322 }
10323
Michael Wrighta9cf4192022-12-01 23:46:39 +000010324 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010325 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10326 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10327 NO_PORT, ViewportType::VIRTUAL);
10328 }
10329};
10330
10331const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10332const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10333const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10334const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10335
10336TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10337 prepareAxes();
10338 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10339
10340 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10341
Michael Wrighta9cf4192022-12-01 23:46:39 +000010342 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010343
10344 // Send an axis event
10345 processAxis(mapper, ABS_X, 100);
10346 processSync(mapper);
10347
10348 NotifyMotionArgs args;
10349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10350 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10351
10352 // Send another axis event
10353 processAxis(mapper, ABS_Y, 100);
10354 processSync(mapper);
10355
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10357 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10358}
10359
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010360// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010361
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010362class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010363protected:
10364 static const char* DEVICE_NAME;
10365 static const char* DEVICE_LOCATION;
10366 static const int32_t DEVICE_ID;
10367 static const int32_t DEVICE_GENERATION;
10368 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010369 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370 static const int32_t EVENTHUB_ID;
10371
10372 std::shared_ptr<FakeEventHub> mFakeEventHub;
10373 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010374 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010375 std::unique_ptr<InstrumentedInputReader> mReader;
10376 std::shared_ptr<InputDevice> mDevice;
10377
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010378 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010379 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010380 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010381 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010382 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010383 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010384 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10385 }
10386
10387 void SetUp() override { SetUp(DEVICE_CLASSES); }
10388
10389 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010390 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010391 mFakePolicy.clear();
10392 }
10393
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010394 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010395 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10396 mReader->requestRefreshConfiguration(changes);
10397 mReader->loopOnce();
10398 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010399 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010400 }
10401
10402 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10403 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010404 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010405 InputDeviceIdentifier identifier;
10406 identifier.name = name;
10407 identifier.location = location;
10408 std::shared_ptr<InputDevice> device =
10409 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10410 identifier);
10411 mReader->pushNextDevice(device);
10412 mFakeEventHub->addDevice(eventHubId, name, classes);
10413 mReader->loopOnce();
10414 return device;
10415 }
10416
10417 template <class T, typename... Args>
10418 T& addControllerAndConfigure(Args... args) {
10419 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10420
10421 return controller;
10422 }
10423};
10424
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010425const char* PeripheralControllerTest::DEVICE_NAME = "device";
10426const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10427const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10428const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10429const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010430const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10431 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010432const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010433
10434// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010435class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010436protected:
10437 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010438 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010439 }
10440};
10441
10442TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010443 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010444
Harry Cuttsa5b71292022-11-28 12:56:17 +000010445 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10446 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10447 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010448}
10449
10450TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010451 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010452
Harry Cuttsa5b71292022-11-28 12:56:17 +000010453 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10454 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10455 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010456}
10457
10458// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010459class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010460protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010461 void SetUp() override {
10462 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10463 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010464};
10465
Chris Ye85758332021-05-16 23:05:17 -070010466TEST_F(LightControllerTest, MonoLight) {
10467 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010468 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010469 .maxBrightness = 255,
10470 .flags = InputLightClass::BRIGHTNESS,
10471 .path = ""};
10472 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010473
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010474 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010475 InputDeviceInfo info;
10476 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010477 std::vector<InputDeviceLightInfo> lights = info.getLights();
10478 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010479 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10480 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10481
10482 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10483 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10484}
10485
10486TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10487 RawLightInfo infoMono = {.id = 1,
10488 .name = "mono_keyboard_backlight",
10489 .maxBrightness = 255,
10490 .flags = InputLightClass::BRIGHTNESS |
10491 InputLightClass::KEYBOARD_BACKLIGHT,
10492 .path = ""};
10493 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10494
10495 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10496 InputDeviceInfo info;
10497 controller.populateDeviceInfo(&info);
10498 std::vector<InputDeviceLightInfo> lights = info.getLights();
10499 ASSERT_EQ(1U, lights.size());
10500 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10501 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010502
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010503 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10504 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010505}
10506
10507TEST_F(LightControllerTest, RGBLight) {
10508 RawLightInfo infoRed = {.id = 1,
10509 .name = "red",
10510 .maxBrightness = 255,
10511 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10512 .path = ""};
10513 RawLightInfo infoGreen = {.id = 2,
10514 .name = "green",
10515 .maxBrightness = 255,
10516 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10517 .path = ""};
10518 RawLightInfo infoBlue = {.id = 3,
10519 .name = "blue",
10520 .maxBrightness = 255,
10521 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10522 .path = ""};
10523 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10524 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10525 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10526
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010527 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010528 InputDeviceInfo info;
10529 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010530 std::vector<InputDeviceLightInfo> lights = info.getLights();
10531 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010532 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10533 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10534 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10535
10536 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10537 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10538}
10539
10540TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10541 RawLightInfo infoRed = {.id = 1,
10542 .name = "red_keyboard_backlight",
10543 .maxBrightness = 255,
10544 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10545 InputLightClass::KEYBOARD_BACKLIGHT,
10546 .path = ""};
10547 RawLightInfo infoGreen = {.id = 2,
10548 .name = "green_keyboard_backlight",
10549 .maxBrightness = 255,
10550 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10551 InputLightClass::KEYBOARD_BACKLIGHT,
10552 .path = ""};
10553 RawLightInfo infoBlue = {.id = 3,
10554 .name = "blue_keyboard_backlight",
10555 .maxBrightness = 255,
10556 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10557 InputLightClass::KEYBOARD_BACKLIGHT,
10558 .path = ""};
10559 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10560 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10561 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10562
10563 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10564 InputDeviceInfo info;
10565 controller.populateDeviceInfo(&info);
10566 std::vector<InputDeviceLightInfo> lights = info.getLights();
10567 ASSERT_EQ(1U, lights.size());
10568 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10569 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10570 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10571
10572 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10573 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10574}
10575
10576TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10577 RawLightInfo infoRed = {.id = 1,
10578 .name = "red",
10579 .maxBrightness = 255,
10580 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10581 .path = ""};
10582 RawLightInfo infoGreen = {.id = 2,
10583 .name = "green",
10584 .maxBrightness = 255,
10585 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10586 .path = ""};
10587 RawLightInfo infoBlue = {.id = 3,
10588 .name = "blue",
10589 .maxBrightness = 255,
10590 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10591 .path = ""};
10592 RawLightInfo infoGlobal = {.id = 3,
10593 .name = "global_keyboard_backlight",
10594 .maxBrightness = 255,
10595 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10596 InputLightClass::KEYBOARD_BACKLIGHT,
10597 .path = ""};
10598 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10599 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10600 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10601 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10602
10603 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10604 InputDeviceInfo info;
10605 controller.populateDeviceInfo(&info);
10606 std::vector<InputDeviceLightInfo> lights = info.getLights();
10607 ASSERT_EQ(1U, lights.size());
10608 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10609 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10610 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010611
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010612 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10613 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010614}
10615
10616TEST_F(LightControllerTest, MultiColorRGBLight) {
10617 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010618 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010619 .maxBrightness = 255,
10620 .flags = InputLightClass::BRIGHTNESS |
10621 InputLightClass::MULTI_INTENSITY |
10622 InputLightClass::MULTI_INDEX,
10623 .path = ""};
10624
10625 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10626
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010627 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010628 InputDeviceInfo info;
10629 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010630 std::vector<InputDeviceLightInfo> lights = info.getLights();
10631 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010632 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10633 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10634 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10635
10636 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10637 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10638}
10639
10640TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10641 RawLightInfo infoColor = {.id = 1,
10642 .name = "multi_color_keyboard_backlight",
10643 .maxBrightness = 255,
10644 .flags = InputLightClass::BRIGHTNESS |
10645 InputLightClass::MULTI_INTENSITY |
10646 InputLightClass::MULTI_INDEX |
10647 InputLightClass::KEYBOARD_BACKLIGHT,
10648 .path = ""};
10649
10650 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10651
10652 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10653 InputDeviceInfo info;
10654 controller.populateDeviceInfo(&info);
10655 std::vector<InputDeviceLightInfo> lights = info.getLights();
10656 ASSERT_EQ(1U, lights.size());
10657 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10658 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10659 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010660
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010661 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10662 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010663}
10664
10665TEST_F(LightControllerTest, PlayerIdLight) {
10666 RawLightInfo info1 = {.id = 1,
10667 .name = "player1",
10668 .maxBrightness = 255,
10669 .flags = InputLightClass::BRIGHTNESS,
10670 .path = ""};
10671 RawLightInfo info2 = {.id = 2,
10672 .name = "player2",
10673 .maxBrightness = 255,
10674 .flags = InputLightClass::BRIGHTNESS,
10675 .path = ""};
10676 RawLightInfo info3 = {.id = 3,
10677 .name = "player3",
10678 .maxBrightness = 255,
10679 .flags = InputLightClass::BRIGHTNESS,
10680 .path = ""};
10681 RawLightInfo info4 = {.id = 4,
10682 .name = "player4",
10683 .maxBrightness = 255,
10684 .flags = InputLightClass::BRIGHTNESS,
10685 .path = ""};
10686 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10687 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10688 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10689 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10690
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010691 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010692 InputDeviceInfo info;
10693 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010694 std::vector<InputDeviceLightInfo> lights = info.getLights();
10695 ASSERT_EQ(1U, lights.size());
10696 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010697 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10698 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010699
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010700 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10701 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10702 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010703}
10704
Michael Wrightd02c5b62014-02-10 15:10:22 -080010705} // namespace android