blob: 66acf6077b831e329e78c5467ffe27746be77c6a [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
19
Prabir Pradhan2770d242019-09-02 18:07:11 -070020#include <CursorInputMapper.h>
21#include <InputDevice.h>
22#include <InputMapper.h>
23#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080024#include <InputReaderBase.h>
25#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000026#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070029#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070030#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
32#include <SwitchInputMapper.h>
33#include <TestInputListener.h>
Prabir Pradhan739dca42022-09-09 20:12:01 +000034#include <TestInputListenerMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080036#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000037#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070038#include <android-base/thread_annotations.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000039#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050041#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000042#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080043
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070044#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000045#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000046#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000047#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000048#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000049#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000050#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000051#include "input/DisplayViewport.h"
52#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010053
Michael Wrightd02c5b62014-02-10 15:10:22 -080054namespace android {
55
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000057using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070058using std::chrono_literals::operator""ms;
59
Michael Wrightd02c5b62014-02-10 15:10:22 -080060// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080061static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000062static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t DISPLAY_WIDTH = 480;
66static constexpr int32_t DISPLAY_HEIGHT = 800;
67static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
68static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
69static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070070static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070071static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
arthurhungcc7f9802020-04-30 17:55:40 +080073static constexpr int32_t FIRST_SLOT = 0;
74static constexpr int32_t SECOND_SLOT = 1;
75static constexpr int32_t THIRD_SLOT = 2;
76static constexpr int32_t INVALID_TRACKING_ID = -1;
77static constexpr int32_t FIRST_TRACKING_ID = 0;
78static constexpr int32_t SECOND_TRACKING_ID = 1;
79static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000093// Minimum timestamp separation between subsequent input events from a Bluetooth device.
94static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
95// Maximum smoothing time delta so that we don't generate events too far into the future.
96constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
97
Michael Wrightd02c5b62014-02-10 15:10:22 -080098template<typename T>
99static inline T min(T a, T b) {
100 return a < b ? a : b;
101}
102
103static inline float avg(float x, float y) {
104 return (x + y) / 2;
105}
106
Chris Ye3fdbfef2021-01-06 18:45:18 -0800107// Mapping for light color name and the light color
108const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
109 {"green", LightColor::GREEN},
110 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800111
Michael Wrighta9cf4192022-12-01 23:46:39 +0000112static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700113 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000114 case ui::ROTATION_90:
115 return ui::ROTATION_270;
116 case ui::ROTATION_270:
117 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 default:
119 return orientation;
120 }
121}
122
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800123static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
124 InputDeviceInfo info;
125 mapper.populateDeviceInfo(&info);
126
127 const InputDeviceInfo::MotionRange* motionRange =
128 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
129 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
130}
131
132static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
133 InputDeviceInfo info;
134 mapper.populateDeviceInfo(&info);
135
136 const InputDeviceInfo::MotionRange* motionRange =
137 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
138 ASSERT_EQ(nullptr, motionRange);
139}
140
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700141[[maybe_unused]] static void dumpReader(InputReader& reader) {
142 std::string dump;
143 reader.dump(dump);
144 std::istringstream iss(dump);
145 for (std::string line; std::getline(iss, line);) {
146 ALOGE("%s", line.c_str());
147 std::this_thread::sleep_for(std::chrono::milliseconds(1));
148 }
149}
150
Michael Wrightd02c5b62014-02-10 15:10:22 -0800151// --- FakeInputMapper ---
152
153class FakeInputMapper : public InputMapper {
154 uint32_t mSources;
155 int32_t mKeyboardType;
156 int32_t mMetaState;
157 KeyedVector<int32_t, int32_t> mKeyCodeStates;
158 KeyedVector<int32_t, int32_t> mScanCodeStates;
159 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100160 // fake mapping which would normally come from keyCharacterMap
161 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800162 std::vector<int32_t> mSupportedKeyCodes;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800163
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700164 std::mutex mLock;
165 std::condition_variable mStateChangedCondition;
166 bool mConfigureWasCalled GUARDED_BY(mLock);
167 bool mResetWasCalled GUARDED_BY(mLock);
168 bool mProcessWasCalled GUARDED_BY(mLock);
169 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800170
Arthur Hungc23540e2018-11-29 20:42:11 +0800171 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800172public:
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800173 FakeInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
174 : InputMapper(deviceContext),
175 mSources(sources),
176 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800177 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800178 mConfigureWasCalled(false),
179 mResetWasCalled(false),
180 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800181
Chris Yea52ade12020-08-27 16:49:20 -0700182 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800183
184 void setKeyboardType(int32_t keyboardType) {
185 mKeyboardType = keyboardType;
186 }
187
188 void setMetaState(int32_t metaState) {
189 mMetaState = metaState;
190 }
191
192 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700193 std::unique_lock<std::mutex> lock(mLock);
194 base::ScopedLockAssertion assumeLocked(mLock);
195 const bool configureCalled =
196 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
197 return mConfigureWasCalled;
198 });
199 if (!configureCalled) {
200 FAIL() << "Expected configure() to have been called.";
201 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800202 mConfigureWasCalled = false;
203 }
204
205 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 std::unique_lock<std::mutex> lock(mLock);
207 base::ScopedLockAssertion assumeLocked(mLock);
208 const bool resetCalled =
209 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
210 return mResetWasCalled;
211 });
212 if (!resetCalled) {
213 FAIL() << "Expected reset() to have been called.";
214 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 mResetWasCalled = false;
216 }
217
Yi Kong9b14ac62018-07-17 13:48:38 -0700218 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700219 std::unique_lock<std::mutex> lock(mLock);
220 base::ScopedLockAssertion assumeLocked(mLock);
221 const bool processCalled =
222 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
223 return mProcessWasCalled;
224 });
225 if (!processCalled) {
226 FAIL() << "Expected process() to have been called.";
227 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 if (outLastEvent) {
229 *outLastEvent = mLastEvent;
230 }
231 mProcessWasCalled = false;
232 }
233
234 void setKeyCodeState(int32_t keyCode, int32_t state) {
235 mKeyCodeStates.replaceValueFor(keyCode, state);
236 }
237
238 void setScanCodeState(int32_t scanCode, int32_t state) {
239 mScanCodeStates.replaceValueFor(scanCode, state);
240 }
241
242 void setSwitchState(int32_t switchCode, int32_t state) {
243 mSwitchStates.replaceValueFor(switchCode, state);
244 }
245
246 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800247 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 }
249
Philip Junker4af3b3d2021-12-14 10:36:55 +0100250 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
251 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
252 }
253
Michael Wrightd02c5b62014-02-10 15:10:22 -0800254private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100255 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800256
Chris Yea52ade12020-08-27 16:49:20 -0700257 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800258 InputMapper::populateDeviceInfo(deviceInfo);
259
260 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
261 deviceInfo->setKeyboardType(mKeyboardType);
262 }
263 }
264
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700265 std::list<NotifyArgs> configure(nsecs_t, const InputReaderConfiguration* config,
266 uint32_t changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700267 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800268 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800269
270 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800271 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Arthur Hungc23540e2018-11-29 20:42:11 +0800272 if (displayPort && (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
273 mViewport = config->getDisplayViewportByPort(*displayPort);
274 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700275
276 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700277 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800278 }
279
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700280 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700281 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800282 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700283 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700284 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 }
286
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700287 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700288 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 mLastEvent = *rawEvent;
290 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700291 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700292 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 }
294
Chris Yea52ade12020-08-27 16:49:20 -0700295 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800296 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
297 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
298 }
299
Philip Junker4af3b3d2021-12-14 10:36:55 +0100300 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
301 auto it = mKeyCodeMapping.find(locationKeyCode);
302 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
303 }
304
Chris Yea52ade12020-08-27 16:49:20 -0700305 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
307 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
308 }
309
Chris Yea52ade12020-08-27 16:49:20 -0700310 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800311 ssize_t index = mSwitchStates.indexOfKey(switchCode);
312 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
313 }
314
Chris Yea52ade12020-08-27 16:49:20 -0700315 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700316 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700317 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700318 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
320 if (keyCodes[i] == mSupportedKeyCodes[j]) {
321 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800322 }
323 }
324 }
Chris Yea52ade12020-08-27 16:49:20 -0700325 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800326 return result;
327 }
328
329 virtual int32_t getMetaState() {
330 return mMetaState;
331 }
332
333 virtual void fadePointer() {
334 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800335
336 virtual std::optional<int32_t> getAssociatedDisplay() {
337 if (mViewport) {
338 return std::make_optional(mViewport->displayId);
339 }
340 return std::nullopt;
341 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342};
343
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700344// --- InputReaderPolicyTest ---
345class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700346protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700347 sp<FakeInputReaderPolicy> mFakePolicy;
348
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700349 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700350 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700351};
352
353/**
354 * Check that empty set of viewports is an acceptable configuration.
355 * Also try to get internal viewport two different ways - by type and by uniqueId.
356 *
357 * There will be confusion if two viewports with empty uniqueId and identical type are present.
358 * Such configuration is not currently allowed.
359 */
360TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700361 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700362
363 // We didn't add any viewports yet, so there shouldn't be any.
364 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100365 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700366 ASSERT_FALSE(internalViewport);
367
368 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000369 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000370 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700371
372 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700373 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100375 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376
377 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100378 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700380 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700381
382 mFakePolicy->clearViewports();
383 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700384 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100386 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387 ASSERT_FALSE(internalViewport);
388}
389
390TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
391 const std::string internalUniqueId = "local:0";
392 const std::string externalUniqueId = "local:1";
393 const std::string virtualUniqueId1 = "virtual:2";
394 const std::string virtualUniqueId2 = "virtual:3";
395 constexpr int32_t virtualDisplayId1 = 2;
396 constexpr int32_t virtualDisplayId2 = 3;
397
398 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000399 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000400 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000401 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000403 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000404 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000405 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406 // Add an virtual viewport
407 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000408 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000409 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 // Add another virtual viewport
411 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000412 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000413 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700414
415 // Check matching by type for internal
416 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100417 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700418 ASSERT_TRUE(internalViewport);
419 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
420
421 // Check matching by type for external
422 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100423 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700424 ASSERT_TRUE(externalViewport);
425 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
426
427 // Check matching by uniqueId for virtual viewport #1
428 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700429 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700430 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100431 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700432 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
433 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
434
435 // Check matching by uniqueId for virtual viewport #2
436 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700437 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700438 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100439 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700440 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
441 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
442}
443
444
445/**
446 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
447 * that lookup works by checking display id.
448 * Check that 2 viewports of each kind is possible, for all existing viewport types.
449 */
450TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
451 const std::string uniqueId1 = "uniqueId1";
452 const std::string uniqueId2 = "uniqueId2";
453 constexpr int32_t displayId1 = 2;
454 constexpr int32_t displayId2 = 3;
455
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
457 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700458 for (const ViewportType& type : types) {
459 mFakePolicy->clearViewports();
460 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000461 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000462 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000464 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000465 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700466
467 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700468 std::optional<DisplayViewport> viewport1 =
469 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700470 ASSERT_TRUE(viewport1);
471 ASSERT_EQ(displayId1, viewport1->displayId);
472 ASSERT_EQ(type, viewport1->type);
473
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700474 std::optional<DisplayViewport> viewport2 =
475 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700476 ASSERT_TRUE(viewport2);
477 ASSERT_EQ(displayId2, viewport2->displayId);
478 ASSERT_EQ(type, viewport2->type);
479
480 // When there are multiple viewports of the same kind, and uniqueId is not specified
481 // in the call to getDisplayViewport, then that situation is not supported.
482 // The viewports can be stored in any order, so we cannot rely on the order, since that
483 // is just implementation detail.
484 // However, we can check that it still returns *a* viewport, we just cannot assert
485 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700486 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 ASSERT_TRUE(someViewport);
488 }
489}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700491/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000492 * When we have multiple internal displays make sure we always return the default display when
493 * querying by type.
494 */
495TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
496 const std::string uniqueId1 = "uniqueId1";
497 const std::string uniqueId2 = "uniqueId2";
498 constexpr int32_t nonDefaultDisplayId = 2;
499 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
500 "Test display ID should not be ADISPLAY_ID_DEFAULT");
501
502 // Add the default display first and ensure it gets returned.
503 mFakePolicy->clearViewports();
504 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000505 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000506 ViewportType::INTERNAL);
507 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000508 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 ViewportType::INTERNAL);
510
511 std::optional<DisplayViewport> viewport =
512 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
513 ASSERT_TRUE(viewport);
514 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
515 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
516
517 // Add the default display second to make sure order doesn't matter.
518 mFakePolicy->clearViewports();
519 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000520 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000521 ViewportType::INTERNAL);
522 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000523 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000524 ViewportType::INTERNAL);
525
526 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
527 ASSERT_TRUE(viewport);
528 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
529 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
530}
531
532/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700533 * Check getDisplayViewportByPort
534 */
535TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100536 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700537 const std::string uniqueId1 = "uniqueId1";
538 const std::string uniqueId2 = "uniqueId2";
539 constexpr int32_t displayId1 = 1;
540 constexpr int32_t displayId2 = 2;
541 const uint8_t hdmi1 = 0;
542 const uint8_t hdmi2 = 1;
543 const uint8_t hdmi3 = 2;
544
545 mFakePolicy->clearViewports();
546 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000547 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000548 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700549 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000550 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000551 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700552
553 // Check that correct display viewport was returned by comparing the display ports.
554 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
555 ASSERT_TRUE(hdmi1Viewport);
556 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
557 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
558
559 // Check that we can still get the same viewport using the uniqueId
560 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
561 ASSERT_TRUE(hdmi1Viewport);
562 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
563 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
564 ASSERT_EQ(type, hdmi1Viewport->type);
565
566 // Check that we cannot find a port with "HDMI2", because we never added one
567 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
568 ASSERT_FALSE(hdmi2Viewport);
569}
570
Michael Wrightd02c5b62014-02-10 15:10:22 -0800571// --- InputReaderTest ---
572
573class InputReaderTest : public testing::Test {
574protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700575 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800576 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700577 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000578 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800579
Chris Yea52ade12020-08-27 16:49:20 -0700580 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700581 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700582 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700583 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800584
Prabir Pradhan28efc192019-11-05 01:10:04 +0000585 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700586 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800587 }
588
Chris Yea52ade12020-08-27 16:49:20 -0700589 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700590 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800591 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800592 }
593
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700594 void addDevice(int32_t eventHubId, const std::string& name,
595 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800596 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800597
598 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800599 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800600 }
601 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000602 mReader->loopOnce();
603 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700604 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
605 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606 }
607
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800608 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700609 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700611 }
612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800613 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700614 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +0000615 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700616 }
617
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800618 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700619 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700620 ftl::Flags<InputDeviceClass> classes,
621 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800622 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800623 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
624 FakeInputMapper& mapper = device->addMapper<FakeInputMapper>(eventHubId, sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800625 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 return mapper;
628 }
629};
630
Chris Ye98d3f532020-10-01 21:48:59 -0700631TEST_F(InputReaderTest, PolicyGetInputDevices) {
632 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700633 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700634 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800635
636 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700637 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800638 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800639 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100640 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800641 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
642 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000643 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800644}
645
Chris Yee7310032020-09-22 15:36:28 -0700646TEST_F(InputReaderTest, GetMergedInputDevices) {
647 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
648 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
649 // Add two subdevices to device
650 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
651 // Must add at least one mapper or the device will be ignored!
652 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
653 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
654
655 // Push same device instance for next device to be added, so they'll have same identifier.
656 mReader->pushNextDevice(device);
657 mReader->pushNextDevice(device);
658 ASSERT_NO_FATAL_FAILURE(
659 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
660 ASSERT_NO_FATAL_FAILURE(
661 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
662
663 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000664 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700665}
666
Chris Yee14523a2020-12-19 13:46:00 -0800667TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
668 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
669 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
670 // Add two subdevices to device
671 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
672 // Must add at least one mapper or the device will be ignored!
673 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
674 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
675
676 // Push same device instance for next device to be added, so they'll have same identifier.
677 mReader->pushNextDevice(device);
678 mReader->pushNextDevice(device);
679 // Sensor device is initially disabled
680 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
681 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
682 nullptr));
683 // Device is disabled because the only sub device is a sensor device and disabled initially.
684 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
685 ASSERT_FALSE(device->isEnabled());
686 ASSERT_NO_FATAL_FAILURE(
687 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
688 // The merged device is enabled if any sub device is enabled
689 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
690 ASSERT_TRUE(device->isEnabled());
691}
692
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700693TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800694 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700695 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800696 constexpr int32_t eventHubId = 1;
697 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700698 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800699 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800700 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800701 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700702
Yi Kong9b14ac62018-07-17 13:48:38 -0700703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700704
705 NotifyDeviceResetArgs resetArgs;
706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700707 ASSERT_EQ(deviceId, resetArgs.deviceId);
708
709 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800710 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000711 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700712
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700714 ASSERT_EQ(deviceId, resetArgs.deviceId);
715 ASSERT_EQ(device->isEnabled(), false);
716
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800717 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000718 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700721 ASSERT_EQ(device->isEnabled(), false);
722
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800723 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000724 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700726 ASSERT_EQ(deviceId, resetArgs.deviceId);
727 ASSERT_EQ(device->isEnabled(), true);
728}
729
Michael Wrightd02c5b62014-02-10 15:10:22 -0800730TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800731 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700732 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800733 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800734 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800735 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800736 AINPUT_SOURCE_KEYBOARD, nullptr);
737 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800738
739 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
740 AINPUT_SOURCE_ANY, AKEYCODE_A))
741 << "Should return unknown when the device id is >= 0 but unknown.";
742
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800743 ASSERT_EQ(AKEY_STATE_UNKNOWN,
744 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
745 << "Should return unknown when the device id is valid but the sources are not "
746 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800747
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800748 ASSERT_EQ(AKEY_STATE_DOWN,
749 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
750 AKEYCODE_A))
751 << "Should return value provided by mapper when device id is valid and the device "
752 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800753
754 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
755 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
756 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
757
758 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
759 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
760 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
761}
762
Philip Junker4af3b3d2021-12-14 10:36:55 +0100763TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
764 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
765 constexpr int32_t eventHubId = 1;
766 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
767 InputDeviceClass::KEYBOARD,
768 AINPUT_SOURCE_KEYBOARD, nullptr);
769 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
770
771 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
772 << "Should return unknown when the device with the specified id is not found.";
773
774 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
775 << "Should return correct mapping when device id is valid and mapping exists.";
776
777 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
778 << "Should return the location key code when device id is valid and there's no "
779 "mapping.";
780}
781
782TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
783 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
784 constexpr int32_t eventHubId = 1;
785 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
786 InputDeviceClass::JOYSTICK,
787 AINPUT_SOURCE_GAMEPAD, nullptr);
788 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
789
790 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
791 << "Should return unknown when the device id is valid but there is no keyboard mapper";
792}
793
Michael Wrightd02c5b62014-02-10 15:10:22 -0800794TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800795 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700796 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800797 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800798 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800799 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800800 AINPUT_SOURCE_KEYBOARD, nullptr);
801 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802
803 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
804 AINPUT_SOURCE_ANY, KEY_A))
805 << "Should return unknown when the device id is >= 0 but unknown.";
806
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800807 ASSERT_EQ(AKEY_STATE_UNKNOWN,
808 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
809 << "Should return unknown when the device id is valid but the sources are not "
810 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800811
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800812 ASSERT_EQ(AKEY_STATE_DOWN,
813 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
814 KEY_A))
815 << "Should return value provided by mapper when device id is valid and the device "
816 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800817
818 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
819 AINPUT_SOURCE_TRACKBALL, KEY_A))
820 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
821
822 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
823 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
824 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
825}
826
827TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800828 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700829 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800830 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800831 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800832 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800833 AINPUT_SOURCE_KEYBOARD, nullptr);
834 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800835
836 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
837 AINPUT_SOURCE_ANY, SW_LID))
838 << "Should return unknown when the device id is >= 0 but unknown.";
839
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800840 ASSERT_EQ(AKEY_STATE_UNKNOWN,
841 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
842 << "Should return unknown when the device id is valid but the sources are not "
843 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800844
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800845 ASSERT_EQ(AKEY_STATE_DOWN,
846 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
847 SW_LID))
848 << "Should return value provided by mapper when device id is valid and the device "
849 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800850
851 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
852 AINPUT_SOURCE_TRACKBALL, SW_LID))
853 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
854
855 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
856 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
857 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
858}
859
860TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700862 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800863 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800864 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800865 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800866 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800868 mapper.addSupportedKeyCode(AKEYCODE_A);
869 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700871 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800872 uint8_t flags[4] = { 0, 0, 0, 1 };
873
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700874 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800875 << "Should return false when device id is >= 0 but unknown.";
876 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
877
878 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700879 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800880 << "Should return false when device id is valid but the sources are not supported by "
881 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800882 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
883
884 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700885 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800886 keyCodes, flags))
887 << "Should return value provided by mapper when device id is valid and the device "
888 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800889 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
890
891 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700892 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
893 << "Should return false when the device id is < 0 but the sources are not supported by "
894 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
896
897 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700898 ASSERT_TRUE(
899 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
900 << "Should return value provided by mapper when device id is < 0 and one of the "
901 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800902 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
903}
904
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000905TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800906 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700907 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800908
909 NotifyConfigurationChangedArgs args;
910
911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
912 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
913}
914
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000915TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800916 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700917 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000918 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800919 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000920 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000925 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000926 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
928
929 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800930 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000931 ASSERT_EQ(when, event.when);
932 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800933 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 ASSERT_EQ(EV_KEY, event.type);
935 ASSERT_EQ(KEY_A, event.code);
936 ASSERT_EQ(1, event.value);
937}
938
Garfield Tan1c7bc862020-01-28 13:24:04 -0800939TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800940 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700941 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800942 constexpr int32_t eventHubId = 1;
943 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800944 // Must add at least one mapper or the device will be ignored!
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800946 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800947 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800948
949 NotifyDeviceResetArgs resetArgs;
950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800951 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800953 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000954 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800956 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800957 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800958
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800959 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000960 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800962 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800963 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800964
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800965 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000966 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800968 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800969 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800970}
971
Garfield Tan1c7bc862020-01-28 13:24:04 -0800972TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
973 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -0800975 constexpr int32_t eventHubId = 1;
976 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
977 // Must add at least one mapper or the device will be ignored!
978 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800979 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -0800980 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
981
982 NotifyDeviceResetArgs resetArgs;
983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
984 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
985}
986
Arthur Hungc23540e2018-11-29 20:42:11 +0800987TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800988 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700989 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +0800991 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800992 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
993 FakeInputMapper& mapper =
994 device->addMapper<FakeInputMapper>(eventHubId, AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800995 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +0800996
997 const uint8_t hdmi1 = 1;
998
999 // Associated touch screen with second display.
1000 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1001
1002 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001003 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001004 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001005 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001006 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001007 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001008 ViewportType::EXTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001009 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001010 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001011
1012 // Add the device, and make sure all of the callbacks are triggered.
1013 // The device is added after the input port associations are processed since
1014 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001015 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001018 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001019
Arthur Hung2c9a3342019-07-23 14:18:59 +08001020 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001021 ASSERT_EQ(deviceId, device->getId());
1022 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1023 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001024
1025 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001026 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001027 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001028 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001029}
1030
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001031TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1032 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001033 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001034 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1035 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1036 // Must add at least one mapper or the device will be ignored!
1037 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1038 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1039 mReader->pushNextDevice(device);
1040 mReader->pushNextDevice(device);
1041 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1042 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1043
1044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1045
1046 NotifyDeviceResetArgs resetArgs;
1047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1048 ASSERT_EQ(deviceId, resetArgs.deviceId);
1049 ASSERT_TRUE(device->isEnabled());
1050 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1051 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1052
1053 disableDevice(deviceId);
1054 mReader->loopOnce();
1055
1056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1057 ASSERT_EQ(deviceId, resetArgs.deviceId);
1058 ASSERT_FALSE(device->isEnabled());
1059 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1060 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1061
1062 enableDevice(deviceId);
1063 mReader->loopOnce();
1064
1065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1066 ASSERT_EQ(deviceId, resetArgs.deviceId);
1067 ASSERT_TRUE(device->isEnabled());
1068 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1069 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1070}
1071
1072TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1073 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001074 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001075 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1076 // Add two subdevices to device
1077 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1078 FakeInputMapper& mapperDevice1 =
1079 device->addMapper<FakeInputMapper>(eventHubIds[0], AINPUT_SOURCE_KEYBOARD);
1080 FakeInputMapper& mapperDevice2 =
1081 device->addMapper<FakeInputMapper>(eventHubIds[1], AINPUT_SOURCE_KEYBOARD);
1082 mReader->pushNextDevice(device);
1083 mReader->pushNextDevice(device);
1084 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1085 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1086
1087 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1088 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1089
1090 ASSERT_EQ(AKEY_STATE_DOWN,
1091 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1092 ASSERT_EQ(AKEY_STATE_DOWN,
1093 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1094 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1095 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1096}
1097
Prabir Pradhan7e186182020-11-10 13:56:45 -08001098TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1099 NotifyPointerCaptureChangedArgs args;
1100
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001101 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001102 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1103 mReader->loopOnce();
1104 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001105 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1106 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001107
1108 mFakePolicy->setPointerCapture(false);
1109 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1110 mReader->loopOnce();
1111 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001112 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001113
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001114 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001115 // does not change.
1116 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
1117 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001118 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001119}
1120
Chris Ye87143712020-11-10 05:05:58 +00001121class FakeVibratorInputMapper : public FakeInputMapper {
1122public:
1123 FakeVibratorInputMapper(InputDeviceContext& deviceContext, uint32_t sources)
1124 : FakeInputMapper(deviceContext, sources) {}
1125
1126 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1127};
1128
1129TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1130 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001131 ftl::Flags<InputDeviceClass> deviceClass =
1132 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001133 constexpr int32_t eventHubId = 1;
1134 const char* DEVICE_LOCATION = "BLUETOOTH";
1135 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1136 FakeVibratorInputMapper& mapper =
1137 device->addMapper<FakeVibratorInputMapper>(eventHubId, AINPUT_SOURCE_KEYBOARD);
1138 mReader->pushNextDevice(device);
1139
1140 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1141 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1142
1143 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1144 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1145}
1146
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001147// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001148
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001149class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001150public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001151 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001152
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001153 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001154
Andy Chenf9f1a022022-08-29 20:07:10 -04001155 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1156
Chris Yee2b1e5c2021-03-10 22:45:12 -08001157 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1158
1159 void dump(std::string& dump) override {}
1160
1161 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1162 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001163 }
1164
Chris Yee2b1e5c2021-03-10 22:45:12 -08001165 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1166 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001167 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001168
1169 bool setLightColor(int32_t lightId, int32_t color) override {
1170 getDeviceContext().setLightBrightness(lightId, color >> 24);
1171 return true;
1172 }
1173
1174 std::optional<int32_t> getLightColor(int32_t lightId) override {
1175 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1176 if (!result.has_value()) {
1177 return std::nullopt;
1178 }
1179 return result.value() << 24;
1180 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001181
1182 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1183
1184 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1185
1186private:
1187 InputDeviceContext& mDeviceContext;
1188 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1189 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001190 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001191};
1192
Chris Yee2b1e5c2021-03-10 22:45:12 -08001193TEST_F(InputReaderTest, BatteryGetCapacity) {
1194 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001195 ftl::Flags<InputDeviceClass> deviceClass =
1196 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001197 constexpr int32_t eventHubId = 1;
1198 const char* DEVICE_LOCATION = "BLUETOOTH";
1199 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001200 FakePeripheralController& controller =
1201 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001202 mReader->pushNextDevice(device);
1203
1204 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1205
Harry Cuttsa5b71292022-11-28 12:56:17 +00001206 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1207 FakeEventHub::BATTERY_CAPACITY);
1208 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001209}
1210
1211TEST_F(InputReaderTest, BatteryGetStatus) {
1212 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001213 ftl::Flags<InputDeviceClass> deviceClass =
1214 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001215 constexpr int32_t eventHubId = 1;
1216 const char* DEVICE_LOCATION = "BLUETOOTH";
1217 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218 FakePeripheralController& controller =
1219 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001220 mReader->pushNextDevice(device);
1221
1222 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1223
Harry Cuttsa5b71292022-11-28 12:56:17 +00001224 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1225 FakeEventHub::BATTERY_STATUS);
1226 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001227}
1228
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001229TEST_F(InputReaderTest, BatteryGetDevicePath) {
1230 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1231 ftl::Flags<InputDeviceClass> deviceClass =
1232 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1233 constexpr int32_t eventHubId = 1;
1234 const char* DEVICE_LOCATION = "BLUETOOTH";
1235 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1236 device->addController<FakePeripheralController>(eventHubId);
1237 mReader->pushNextDevice(device);
1238
1239 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1240
Harry Cuttsa5b71292022-11-28 12:56:17 +00001241 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001242}
1243
Chris Ye3fdbfef2021-01-06 18:45:18 -08001244TEST_F(InputReaderTest, LightGetColor) {
1245 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001246 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001247 constexpr int32_t eventHubId = 1;
1248 const char* DEVICE_LOCATION = "BLUETOOTH";
1249 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001250 FakePeripheralController& controller =
1251 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001252 mReader->pushNextDevice(device);
1253 RawLightInfo info = {.id = 1,
1254 .name = "Mono",
1255 .maxBrightness = 255,
1256 .flags = InputLightClass::BRIGHTNESS,
1257 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001258 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1259 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260
1261 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001262
Harry Cutts33476232023-01-30 19:57:29 +00001263 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1264 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1265 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1266 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001267}
1268
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001269// --- InputReaderIntegrationTest ---
1270
1271// These tests create and interact with the InputReader only through its interface.
1272// The InputReader is started during SetUp(), which starts its processing in its own
1273// thread. The tests use linux uinput to emulate input devices.
1274// NOTE: Interacting with the physical device while these tests are running may cause
1275// the tests to fail.
1276class InputReaderIntegrationTest : public testing::Test {
1277protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001278 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001279 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001280 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001281
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001282 std::shared_ptr<FakePointerController> mFakePointerController;
1283
Chris Yea52ade12020-08-27 16:49:20 -07001284 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001285#if !defined(__ANDROID__)
1286 GTEST_SKIP();
1287#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001288 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001289 mFakePointerController = std::make_shared<FakePointerController>();
1290 mFakePolicy->setPointerController(mFakePointerController);
Harry Cutts33476232023-01-30 19:57:29 +00001291 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1292 /*eventDidNotHappenTimeout=*/30ms);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001293
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001294 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1295 *mTestListener);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001296 ASSERT_EQ(mReader->start(), OK);
1297
1298 // Since this test is run on a real device, all the input devices connected
1299 // to the test device will show up in mReader. We wait for those input devices to
1300 // show up before beginning the tests.
1301 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1302 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1303 }
1304
Chris Yea52ade12020-08-27 16:49:20 -07001305 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001306#if !defined(__ANDROID__)
1307 return;
1308#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001309 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001310 mReader.reset();
1311 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001312 mFakePolicy.clear();
1313 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001314
1315 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1316 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1317 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1318 [&name](const InputDeviceInfo& info) {
1319 return info.getIdentifier().name == name;
1320 });
1321 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1322 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001323};
1324
1325TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1326 // An invalid input device that is only used for this test.
1327 class InvalidUinputDevice : public UinputDevice {
1328 public:
Harry Cutts33476232023-01-30 19:57:29 +00001329 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001330
1331 private:
1332 void configureDevice(int fd, uinput_user_dev* device) override {}
1333 };
1334
1335 const size_t numDevices = mFakePolicy->getInputDevices().size();
1336
1337 // UinputDevice does not set any event or key bits, so InputReader should not
1338 // consider it as a valid device.
1339 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1340 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1342 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1343
1344 invalidDevice.reset();
1345 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1346 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1347 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1348}
1349
1350TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1351 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1352
1353 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1354 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1355 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1356 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1357
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001358 const auto device = findDeviceByName(keyboard->getName());
1359 ASSERT_TRUE(device.has_value());
1360 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1361 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1362 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001363
1364 keyboard.reset();
1365 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1366 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1367 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1368}
1369
1370TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1371 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1372 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1373
1374 NotifyConfigurationChangedArgs configChangedArgs;
1375 ASSERT_NO_FATAL_FAILURE(
1376 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001377 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001378 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1379
1380 NotifyKeyArgs keyArgs;
1381 keyboard->pressAndReleaseHomeKey();
1382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1383 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001384 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001385 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001386 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001387 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001388 prevTimestamp = keyArgs.eventTime;
1389
1390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1391 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001392 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001393 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001394 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001395}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001396
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001397TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1398 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400
1401 const auto device = findDeviceByName(stylus->getName());
1402 ASSERT_TRUE(device.has_value());
1403
Prabir Pradhana3621852022-10-14 18:57:23 +00001404 // An external stylus with buttons should also be recognized as a keyboard.
1405 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001406 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1407 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1408
1409 const auto DOWN =
1410 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1411 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1412
1413 stylus->pressAndReleaseKey(BTN_STYLUS);
1414 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1415 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1416 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1417 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1418
1419 stylus->pressAndReleaseKey(BTN_STYLUS2);
1420 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1421 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1423 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1424
1425 stylus->pressAndReleaseKey(BTN_STYLUS3);
1426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1427 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1429 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1430}
1431
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001432/**
1433 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1434 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1435 * are passed to the listener.
1436 */
1437static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1438TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1439 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1440 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1441 NotifyKeyArgs keyArgs;
1442
1443 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1446 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1447
1448 controller->pressAndReleaseKey(BTN_GEAR_UP);
1449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1451 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1452}
1453
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001454// --- TouchIntegrationTest ---
1455
Arthur Hungaab25622020-01-16 11:22:11 +08001456class TouchIntegrationTest : public InputReaderIntegrationTest {
1457protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001458 const std::string UNIQUE_ID = "local:0";
1459
Chris Yea52ade12020-08-27 16:49:20 -07001460 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001461#if !defined(__ANDROID__)
1462 GTEST_SKIP();
1463#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001464 InputReaderIntegrationTest::SetUp();
1465 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001466 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1467 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001468
1469 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1470 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001472 const auto info = findDeviceByName(mDevice->getName());
1473 ASSERT_TRUE(info);
1474 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001475 }
1476
1477 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001478 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001479 std::optional<uint8_t> physicalPort,
1480 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001481 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001482 uniqueId, physicalPort, viewportType);
Arthur Hungaab25622020-01-16 11:22:11 +08001483 mReader->requestRefreshConfiguration(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
1484 }
1485
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001486 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1487 NotifyMotionArgs args;
1488 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1489 EXPECT_EQ(action, args.action);
1490 ASSERT_EQ(points.size(), args.pointerCount);
1491 for (size_t i = 0; i < args.pointerCount; i++) {
1492 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1493 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1494 }
1495 }
1496
Arthur Hungaab25622020-01-16 11:22:11 +08001497 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001498 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001499};
1500
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001501TEST_F(TouchIntegrationTest, MultiTouchDeviceSource) {
1502 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1503 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1504 // presses).
1505 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1506 mDeviceInfo.getSources());
1507}
1508
Arthur Hungaab25622020-01-16 11:22:11 +08001509TEST_F(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
1510 NotifyMotionArgs args;
1511 const Point centerPoint = mDevice->getCenterPoint();
1512
1513 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001514 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001515 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001516 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001517 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1519
1520 // ACTION_MOVE
1521 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001522 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1524 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1525
1526 // ACTION_UP
1527 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001528 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001529 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1530 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1531}
1532
1533TEST_F(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
1534 NotifyMotionArgs args;
1535 const Point centerPoint = mDevice->getCenterPoint();
1536
1537 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001538 mDevice->sendSlot(FIRST_SLOT);
1539 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001540 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001541 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001542 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1543 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1544
1545 // ACTION_POINTER_DOWN (Second slot)
1546 const Point secondPoint = centerPoint + Point(100, 100);
1547 mDevice->sendSlot(SECOND_SLOT);
1548 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001549 mDevice->sendDown(secondPoint);
1550 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001552 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001553
1554 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001555 mDevice->sendMove(secondPoint + Point(1, 1));
1556 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1559
1560 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001561 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001562 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001564 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001565
1566 // ACTION_UP
1567 mDevice->sendSlot(FIRST_SLOT);
1568 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001569 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1571 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1572}
1573
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001574/**
1575 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1576 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1577 * data?
1578 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1579 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1580 * for Pointer 0 only is generated after.
1581 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1582 * events, we will not miss any information.
1583 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1584 * event generated afterwards that contains the newest movement of pointer 0.
1585 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1586 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1587 * losing information about non-palm pointers.
1588 */
1589TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
1590 NotifyMotionArgs args;
1591 const Point centerPoint = mDevice->getCenterPoint();
1592
1593 // ACTION_DOWN
1594 mDevice->sendSlot(FIRST_SLOT);
1595 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1596 mDevice->sendDown(centerPoint);
1597 mDevice->sendSync();
1598 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1599
1600 // ACTION_POINTER_DOWN (Second slot)
1601 const Point secondPoint = centerPoint + Point(100, 100);
1602 mDevice->sendSlot(SECOND_SLOT);
1603 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1604 mDevice->sendDown(secondPoint);
1605 mDevice->sendSync();
1606 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1607
1608 // ACTION_MOVE (First slot)
1609 mDevice->sendSlot(FIRST_SLOT);
1610 mDevice->sendMove(centerPoint + Point(5, 5));
1611 // ACTION_POINTER_UP (Second slot)
1612 mDevice->sendSlot(SECOND_SLOT);
1613 mDevice->sendPointerUp();
1614 // Send a single sync for the above 2 pointer updates
1615 mDevice->sendSync();
1616
1617 // First, we should get POINTER_UP for the second pointer
1618 assertReceivedMotion(ACTION_POINTER_1_UP,
1619 {/*first pointer */ centerPoint + Point(5, 5),
1620 /*second pointer*/ secondPoint});
1621
1622 // Next, the MOVE event for the first pointer
1623 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1624}
1625
1626/**
1627 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1628 * move, and then it will go up, all in the same frame.
1629 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1630 * gets sent to the listener.
1631 */
1632TEST_F(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
1633 NotifyMotionArgs args;
1634 const Point centerPoint = mDevice->getCenterPoint();
1635
1636 // ACTION_DOWN
1637 mDevice->sendSlot(FIRST_SLOT);
1638 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1639 mDevice->sendDown(centerPoint);
1640 mDevice->sendSync();
1641 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1642
1643 // ACTION_POINTER_DOWN (Second slot)
1644 const Point secondPoint = centerPoint + Point(100, 100);
1645 mDevice->sendSlot(SECOND_SLOT);
1646 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1647 mDevice->sendDown(secondPoint);
1648 mDevice->sendSync();
1649 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1650
1651 // ACTION_MOVE (First slot)
1652 mDevice->sendSlot(FIRST_SLOT);
1653 mDevice->sendMove(centerPoint + Point(5, 5));
1654 // ACTION_POINTER_UP (Second slot)
1655 mDevice->sendSlot(SECOND_SLOT);
1656 mDevice->sendMove(secondPoint + Point(6, 6));
1657 mDevice->sendPointerUp();
1658 // Send a single sync for the above 2 pointer updates
1659 mDevice->sendSync();
1660
1661 // First, we should get POINTER_UP for the second pointer
1662 // The movement of the second pointer during the liftoff frame is ignored.
1663 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1664 assertReceivedMotion(ACTION_POINTER_1_UP,
1665 {/*first pointer */ centerPoint + Point(5, 5),
1666 /*second pointer*/ secondPoint});
1667
1668 // Next, the MOVE event for the first pointer
1669 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1670}
1671
Arthur Hungaab25622020-01-16 11:22:11 +08001672TEST_F(TouchIntegrationTest, InputEvent_ProcessPalm) {
1673 NotifyMotionArgs args;
1674 const Point centerPoint = mDevice->getCenterPoint();
1675
1676 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001677 mDevice->sendSlot(FIRST_SLOT);
1678 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001679 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001680 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001681 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1682 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1683
arthurhungcc7f9802020-04-30 17:55:40 +08001684 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001685 const Point secondPoint = centerPoint + Point(100, 100);
1686 mDevice->sendSlot(SECOND_SLOT);
1687 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1688 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001689 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001690 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001691 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001692
arthurhungcc7f9802020-04-30 17:55:40 +08001693 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001694 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001695 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001696 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1698
arthurhungcc7f9802020-04-30 17:55:40 +08001699 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1700 // a palm event.
1701 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001702 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001703 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001704 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001705 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001706 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001707
arthurhungcc7f9802020-04-30 17:55:40 +08001708 // Send up to second slot, expect first slot send moving.
1709 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001710 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001711 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001713
arthurhungcc7f9802020-04-30 17:55:40 +08001714 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001715 mDevice->sendSlot(FIRST_SLOT);
1716 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001718
arthurhungcc7f9802020-04-30 17:55:40 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1720 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001721}
1722
Prabir Pradhanda20b172022-09-26 17:01:18 +00001723TEST_F(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
1724 const Point centerPoint = mDevice->getCenterPoint();
1725
1726 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1727 mDevice->sendSlot(FIRST_SLOT);
1728 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1729 mDevice->sendToolType(MT_TOOL_PEN);
1730 mDevice->sendDown(centerPoint);
1731 mDevice->sendSync();
1732 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1733 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1734 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1735
1736 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1737
1738 // Release the stylus touch.
1739 mDevice->sendUp();
1740 mDevice->sendSync();
1741 ASSERT_NO_FATAL_FAILURE(
1742 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1743
1744 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1745
1746 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1747 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1748 mDevice->sendToolType(MT_TOOL_FINGER);
1749 mDevice->sendDown(centerPoint);
1750 mDevice->sendSync();
1751 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1753 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
1754
1755 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1756
1757 mDevice->sendUp();
1758 mDevice->sendSync();
1759 ASSERT_NO_FATAL_FAILURE(
1760 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1761
1762 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1763 // The policy should be notified of the stylus presence.
1764 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1765 mDevice->sendToolType(MT_TOOL_PEN);
1766 mDevice->sendMove(centerPoint);
1767 mDevice->sendSync();
1768 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1770 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
1771
1772 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1773}
1774
Prabir Pradhan124ea442022-10-28 20:27:44 +00001775// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001776
Prabir Pradhan124ea442022-10-28 20:27:44 +00001777// Verify the behavior of button presses reported by various kinds of styluses, including buttons
1778// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
1779// stylus.
1780template <typename UinputStylusDevice>
1781class StylusButtonIntegrationTest : public TouchIntegrationTest {
1782protected:
1783 void SetUp() override {
1784#if !defined(__ANDROID__)
1785 GTEST_SKIP();
1786#endif
1787 TouchIntegrationTest::SetUp();
1788 mTouchscreen = mDevice.get();
1789 mTouchscreenInfo = mDeviceInfo;
1790
1791 setUpStylusDevice();
1792 }
1793
1794 UinputStylusDevice* mStylus{nullptr};
1795 InputDeviceInfo mStylusInfo{};
1796
1797 UinputTouchScreen* mTouchscreen{nullptr};
1798 InputDeviceInfo mTouchscreenInfo{};
1799
1800private:
1801 // When we are attempting to test stylus button events that are sent from the touchscreen,
1802 // use the same Uinput device for the touchscreen and the stylus.
1803 template <typename T = UinputStylusDevice>
1804 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1805 mStylus = mDevice.get();
1806 mStylusInfo = mDeviceInfo;
1807 }
1808
1809 // When we are attempting to stylus buttons from an external stylus being merged with touches
1810 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
1811 template <typename T = UinputStylusDevice>
1812 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
1813 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
1814 mStylus = mStylusDeviceLifecycleTracker.get();
1815 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1816 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1817 const auto info = findDeviceByName(mStylus->getName());
1818 ASSERT_TRUE(info);
1819 mStylusInfo = *info;
1820 }
1821
1822 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
1823
1824 // Hide the base class's device to expose it with a different name for readability.
1825 using TouchIntegrationTest::mDevice;
1826 using TouchIntegrationTest::mDeviceInfo;
1827};
1828
1829using StylusButtonIntegrationTestTypes =
1830 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
1831TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
1832
1833TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
1834 const auto stylusId = TestFixture::mStylusInfo.getId();
1835
1836 TestFixture::mStylus->pressKey(BTN_STYLUS);
1837 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1838 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1839 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1840
1841 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1842 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001843 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001844 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001845}
1846
Prabir Pradhan124ea442022-10-28 20:27:44 +00001847TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
1848 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1849 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1850 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001851
1852 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001853 TestFixture::mStylus->pressKey(BTN_STYLUS);
1854 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001855 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001856 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001857
1858 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001859 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1860 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1861 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1862 TestFixture::mTouchscreen->sendDown(centerPoint);
1863 TestFixture::mTouchscreen->sendSync();
1864 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001865 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1866 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001867 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1868 WithDeviceId(touchscreenId))));
1869 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001870 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1871 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001872 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1873 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001874
Prabir Pradhan124ea442022-10-28 20:27:44 +00001875 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1876 TestFixture::mTouchscreen->sendSync();
1877 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001878 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001879 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1880 WithDeviceId(touchscreenId))));
1881 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001882 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001883 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1884 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001885
1886 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001887 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1888 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001889 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001890 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001891}
1892
Prabir Pradhan9a561c22022-11-07 16:11:23 +00001893TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
1894 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1895 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1896 const auto stylusId = TestFixture::mStylusInfo.getId();
1897 auto toolTypeDevice =
1898 AllOf(WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithDeviceId(touchscreenId));
1899
1900 // Press the stylus button.
1901 TestFixture::mStylus->pressKey(BTN_STYLUS);
1902 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1903 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
1904 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1905
1906 // Start hovering with the stylus.
1907 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1908 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1909 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1910 TestFixture::mTouchscreen->sendMove(centerPoint);
1911 TestFixture::mTouchscreen->sendSync();
1912 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1913 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1914 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1915 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1916 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1917 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1918 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1919 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1920 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1921
1922 // Touch down with the stylus.
1923 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1924 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1925 TestFixture::mTouchscreen->sendDown(centerPoint);
1926 TestFixture::mTouchscreen->sendSync();
1927 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1928 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1929 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1930
1931 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1932 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
1933 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1934
1935 // Stop touching with the stylus, and start hovering.
1936 TestFixture::mTouchscreen->sendUp();
1937 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1938 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1939 TestFixture::mTouchscreen->sendMove(centerPoint);
1940 TestFixture::mTouchscreen->sendSync();
1941 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1942 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
1943 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1944 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1945 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
1946 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1947 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1948 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
1949 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1950
1951 // Stop hovering.
1952 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
1953 TestFixture::mTouchscreen->sendSync();
1954 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1955 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
1956 WithButtonState(0))));
1957 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
1958 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
1959 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
1960 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
1961
1962 // Release the stylus button.
1963 TestFixture::mStylus->releaseKey(BTN_STYLUS);
1964 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
1965 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
1966 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1967}
1968
Prabir Pradhan124ea442022-10-28 20:27:44 +00001969TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
1970 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
1971 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
1972 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001973
1974 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001975 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
1976 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
1977 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
1978 TestFixture::mTouchscreen->sendDown(centerPoint);
1979 TestFixture::mTouchscreen->sendSync();
1980 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001981 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001982 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
1983 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001984
1985 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00001986 TestFixture::mStylus->pressKey(BTN_STYLUS);
1987 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001988 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001989 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
1990 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001991 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
1992 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001993 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1994 WithDeviceId(touchscreenId))));
1995 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00001996 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
1997 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00001998 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
1999 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002000
Prabir Pradhan124ea442022-10-28 20:27:44 +00002001 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2002 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002003 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002004 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2005 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002006 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002007 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2008 WithDeviceId(touchscreenId))));
2009 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002010 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002011 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2012 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002013
2014 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002015 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2016 TestFixture::mTouchscreen->sendSync();
2017 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002019 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2020 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002021}
2022
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002023TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
2024 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2025 TestFixture::mReader->requestRefreshConfiguration(
2026 InputReaderConfiguration::CHANGE_STYLUS_BUTTON_REPORTING);
2027
2028 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2029 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2030 const auto stylusId = TestFixture::mStylusInfo.getId();
2031
2032 // Start a stylus gesture. By the time this event is processed, the configuration change that
2033 // was requested is guaranteed to be completed.
2034 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2035 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2036 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2037 TestFixture::mTouchscreen->sendDown(centerPoint);
2038 TestFixture::mTouchscreen->sendSync();
2039 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2040 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2041 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2042 WithDeviceId(touchscreenId))));
2043
2044 // Press and release a stylus button. Each change only generates a MOVE motion event.
2045 // Key events are unaffected.
2046 TestFixture::mStylus->pressKey(BTN_STYLUS);
2047 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2048 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2049 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2050 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2052 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2053 WithDeviceId(touchscreenId))));
2054
2055 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2056 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2057 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2058 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2059 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
2061 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2062 WithDeviceId(touchscreenId))));
2063
2064 // Finish the stylus gesture.
2065 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2066 TestFixture::mTouchscreen->sendSync();
2067 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2068 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
2069 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2070 WithDeviceId(touchscreenId))));
2071}
2072
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002073// --- ExternalStylusIntegrationTest ---
2074
2075// Verify the behavior of an external stylus. An external stylus can report pressure or button
2076// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2077// ongoing stylus gesture that is being emitted by the touchscreen.
2078using ExternalStylusIntegrationTest = TouchIntegrationTest;
2079
2080TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
2081 const Point centerPoint = mDevice->getCenterPoint();
2082
2083 // Create an external stylus capable of reporting pressure data that
2084 // should be fused with a touch pointer.
2085 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2086 createUinputDevice<UinputExternalStylusWithPressure>();
2087 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2088 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2089 const auto stylusInfo = findDeviceByName(stylus->getName());
2090 ASSERT_TRUE(stylusInfo);
2091
2092 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2093
2094 const auto touchscreenId = mDeviceInfo.getId();
2095
2096 // Set a pressure value on the stylus. It doesn't generate any events.
2097 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2098 stylus->setPressure(100);
2099 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2100
2101 // Start a finger gesture, and ensure it shows up as stylus gesture
2102 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002103 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002104 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002105 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002106 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002107 mDevice->sendSync();
2108 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2109 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002110 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2111 WithDeviceId(touchscreenId), WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002112
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002113 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2114 // event with the updated pressure.
2115 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002116 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002118 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2119 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002120
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002121 // The external stylus did not generate any events.
2122 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2123 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2124}
2125
2126TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
2127 const Point centerPoint = mDevice->getCenterPoint();
2128
2129 // Create an external stylus capable of reporting pressure data that
2130 // should be fused with a touch pointer.
2131 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2132 createUinputDevice<UinputExternalStylusWithPressure>();
2133 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2134 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2135 const auto stylusInfo = findDeviceByName(stylus->getName());
2136 ASSERT_TRUE(stylusInfo);
2137
2138 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2139
2140 const auto touchscreenId = mDeviceInfo.getId();
2141
2142 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2143 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002144 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2145 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002146 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002147 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002148
2149 // Start a finger gesture. The touch device will withhold generating any touches for
2150 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2151 mDevice->sendSlot(FIRST_SLOT);
2152 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2153 mDevice->sendToolType(MT_TOOL_FINGER);
2154 mDevice->sendDown(centerPoint);
2155 auto waitUntil = std::chrono::system_clock::now() +
2156 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002157 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002158 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002159
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002160 // Since the external stylus did not report a pressure value within the timeout,
2161 // it shows up as a finger pointer.
2162 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2164 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER), WithDeviceId(touchscreenId),
2165 WithPressure(1.f))));
2166
2167 // Change the pressure on the external stylus. Since the pressure was not present at the start
2168 // of the gesture, it is ignored for now.
2169 stylus->setPressure(200);
2170 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2171
2172 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002173 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2174 mDevice->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002177 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
2178
2179 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2180 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2181 mDevice->sendToolType(MT_TOOL_FINGER);
2182 mDevice->sendDown(centerPoint);
2183 mDevice->sendSync();
2184 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2185 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2186 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS), WithButtonState(0),
2187 WithDeviceId(touchscreenId), WithPressure(200.f / RAW_PRESSURE_MAX))));
2188
2189 // The external stylus did not generate any events.
2190 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2191 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002192}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002193
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002194TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
2195 const Point centerPoint = mDevice->getCenterPoint();
2196
2197 // Create an external stylus device that does not support pressure. It should not affect any
2198 // touch pointers.
2199 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2200 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2201 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2202 const auto stylusInfo = findDeviceByName(stylus->getName());
2203 ASSERT_TRUE(stylusInfo);
2204
2205 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2206
2207 const auto touchscreenId = mDeviceInfo.getId();
2208
2209 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2210 // pressure data from the external stylus.
2211 mDevice->sendSlot(FIRST_SLOT);
2212 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2213 mDevice->sendToolType(MT_TOOL_FINGER);
2214 mDevice->sendDown(centerPoint);
2215 auto waitUntil = std::chrono::system_clock::now() +
2216 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2217 mDevice->sendSync();
2218 ASSERT_NO_FATAL_FAILURE(
2219 mTestListener
2220 ->assertNotifyMotionWasCalled(AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2221 WithToolType(
2222 AMOTION_EVENT_TOOL_TYPE_FINGER),
2223 WithButtonState(0),
2224 WithDeviceId(touchscreenId),
2225 WithPressure(1.f)),
2226 waitUntil));
2227
2228 // The external stylus did not generate any events.
2229 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2230 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2231}
2232
Michael Wrightd02c5b62014-02-10 15:10:22 -08002233// --- InputDeviceTest ---
2234class InputDeviceTest : public testing::Test {
2235protected:
2236 static const char* DEVICE_NAME;
2237 static const char* DEVICE_LOCATION;
2238 static const int32_t DEVICE_ID;
2239 static const int32_t DEVICE_GENERATION;
2240 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002241 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002242 static const int32_t EVENTHUB_ID;
2243 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2244
2245 std::shared_ptr<FakeEventHub> mFakeEventHub;
2246 sp<FakeInputReaderPolicy> mFakePolicy;
2247 std::unique_ptr<TestInputListener> mFakeListener;
2248 std::unique_ptr<InstrumentedInputReader> mReader;
2249 std::shared_ptr<InputDevice> mDevice;
2250
2251 void SetUp() override {
2252 mFakeEventHub = std::make_unique<FakeEventHub>();
2253 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2254 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002255 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002256 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002257 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002258 identifier.name = DEVICE_NAME;
2259 identifier.location = DEVICE_LOCATION;
2260 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2261 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2262 identifier);
2263 mReader->pushNextDevice(mDevice);
2264 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002265 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002266 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002267
2268 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002269 mFakeListener.reset();
2270 mFakePolicy.clear();
2271 }
2272};
2273
2274const char* InputDeviceTest::DEVICE_NAME = "device";
2275const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2276const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2277const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002278const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002279const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2280 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002281const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002282const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2283
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002284TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002285 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002286 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2287 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002288}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002289
Michael Wrightd02c5b62014-02-10 15:10:22 -08002290TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2291 ASSERT_EQ(mDevice->isEnabled(), false);
2292}
2293
2294TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2295 // Configuration.
2296 InputReaderConfiguration config;
2297 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2298
2299 // Reset.
2300 unused += mDevice->reset(ARBITRARY_TIME);
2301
2302 NotifyDeviceResetArgs resetArgs;
2303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2304 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2305 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2306
2307 // Metadata.
2308 ASSERT_TRUE(mDevice->isIgnored());
2309 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2310
2311 InputDeviceInfo info = mDevice->getDeviceInfo();
2312 ASSERT_EQ(DEVICE_ID, info.getId());
2313 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2314 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2315 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2316
2317 // State queries.
2318 ASSERT_EQ(0, mDevice->getMetaState());
2319
2320 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2321 << "Ignored device should return unknown key code state.";
2322 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2323 << "Ignored device should return unknown scan code state.";
2324 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2325 << "Ignored device should return unknown switch state.";
2326
2327 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2328 uint8_t flags[2] = { 0, 1 };
2329 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2330 << "Ignored device should never mark any key codes.";
2331 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2332 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2333}
2334
2335TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2336 // Configuration.
2337 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002338
2339 FakeInputMapper& mapper1 =
2340 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
2341 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002342 mapper1.setMetaState(AMETA_ALT_ON);
2343 mapper1.addSupportedKeyCode(AKEYCODE_A);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002344 mapper1.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002345 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2346 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2347 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2348 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2349 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2350
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08002351 FakeInputMapper& mapper2 =
Michael Wrightd02c5b62014-02-10 15:10:22 -08002352 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
2353 mapper2.setMetaState(AMETA_SHIFT_ON);
2354
2355 InputReaderConfiguration config;
2356 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
2357
2358 std::string propertyValue;
2359 ASSERT_TRUE(mDevice->getConfiguration().tryGetProperty("key", propertyValue))
2360 << "Device should have read configuration during configuration phase.";
2361 ASSERT_EQ("value", propertyValue);
2362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002363 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2364 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002365
2366 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002367 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002368 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2369 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002370
2371 NotifyDeviceResetArgs resetArgs;
2372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2373 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2374 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2375
2376 // Metadata.
2377 ASSERT_FALSE(mDevice->isIgnored());
2378 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2379
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002380 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002381 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002382 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002383 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2384 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2385
2386 // State queries.
2387 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2388 << "Should query mappers and combine meta states.";
2389
2390 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2391 << "Should return unknown key code state when source not supported.";
2392 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2393 << "Should return unknown scan code state when source not supported.";
2394 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2395 << "Should return unknown switch state when source not supported.";
2396
2397 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2398 << "Should query mapper when source is supported.";
2399 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2400 << "Should query mapper when source is supported.";
2401 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2402 << "Should query mapper when source is supported.";
2403
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002404 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002405 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002406 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002407 << "Should do nothing when source is unsupported.";
2408 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2409 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2410 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2411 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2412
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002413 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002414 << "Should query mapper when source is supported.";
2415 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2416 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2417 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2418 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2419
2420 // Event handling.
2421 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002422 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002423 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002424
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002425 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2426 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427}
2428
Arthur Hung2c9a3342019-07-23 14:18:59 +08002429// A single input device is associated with a specific display. Check that:
2430// 1. Device is disabled if the viewport corresponding to the associated display is not found
2431// 2. Device is disabled when setEnabled API is called
2432TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002433 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002434
2435 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002436 std::list<NotifyArgs> unused =
2437 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002438
2439 // Device should be enabled by default.
2440 ASSERT_TRUE(mDevice->isEnabled());
2441
2442 // Prepare associated info.
2443 constexpr uint8_t hdmi = 1;
2444 const std::string UNIQUE_ID = "local:1";
2445
2446 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002447 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2448 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002449 // Device should be disabled because it is associated with a specific display via
2450 // input port <-> display port association, but the corresponding display is not found
2451 ASSERT_FALSE(mDevice->isEnabled());
2452
2453 // Prepare displays.
2454 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002455 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002456 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002457 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2458 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002459 ASSERT_TRUE(mDevice->isEnabled());
2460
2461 // Device should be disabled after set disable.
2462 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002463 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2464 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002465 ASSERT_FALSE(mDevice->isEnabled());
2466
2467 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002468 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2469 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002470 ASSERT_FALSE(mDevice->isEnabled());
2471}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002472
Christine Franks1ba71cc2021-04-07 14:37:42 -07002473TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2474 // Device should be enabled by default.
2475 mFakePolicy->clearViewports();
2476 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002477 std::list<NotifyArgs> unused =
2478 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002479 ASSERT_TRUE(mDevice->isEnabled());
2480
2481 // Device should be disabled because it is associated with a specific display, but the
2482 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002483 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002484 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2485 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002486 ASSERT_FALSE(mDevice->isEnabled());
2487
2488 // Device should be enabled when a display is found.
2489 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002490 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002491 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002492 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2493 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002494 ASSERT_TRUE(mDevice->isEnabled());
2495
2496 // Device should be disabled after set disable.
2497 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002498 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2499 InputReaderConfiguration::CHANGE_ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002500 ASSERT_FALSE(mDevice->isEnabled());
2501
2502 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002503 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2504 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002505 ASSERT_FALSE(mDevice->isEnabled());
2506}
2507
Christine Franks2a2293c2022-01-18 11:51:16 -08002508TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2509 mFakePolicy->clearViewports();
2510 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002511 std::list<NotifyArgs> unused =
2512 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
Christine Franks2a2293c2022-01-18 11:51:16 -08002513
Christine Franks2a2293c2022-01-18 11:51:16 -08002514 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2515 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002516 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002517 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002518 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2519 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002520 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2521}
2522
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002523/**
2524 * This test reproduces a crash caused by a dangling reference that remains after device is added
2525 * and removed. The reference is accessed in InputDevice::dump(..);
2526 */
2527TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2528 constexpr int32_t TEST_EVENTHUB_ID = 10;
2529 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2530
Harry Cutts33476232023-01-30 19:57:29 +00002531 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
2532 device.addEventHubDevice(TEST_EVENTHUB_ID, /*populateMappers=*/true);
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002533 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2534 std::string dumpStr, eventHubDevStr;
2535 device.dump(dumpStr, eventHubDevStr);
2536}
2537
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002538TEST_F(InputDeviceTest, GetBluetoothAddress) {
2539 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2540 ASSERT_TRUE(address);
2541 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2542}
2543
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544// --- SwitchInputMapperTest ---
2545
2546class SwitchInputMapperTest : public InputMapperTest {
2547protected:
2548};
2549
2550TEST_F(SwitchInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002553 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002554}
2555
2556TEST_F(SwitchInputMapperTest, GetSwitchState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002557 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002559 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002560 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002562 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564}
2565
2566TEST_F(SwitchInputMapperTest, Process) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002567 SwitchInputMapper& mapper = addMapperAndConfigure<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002568 std::list<NotifyArgs> out;
2569 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2570 ASSERT_TRUE(out.empty());
2571 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2572 ASSERT_TRUE(out.empty());
2573 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2574 ASSERT_TRUE(out.empty());
2575 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002577 ASSERT_EQ(1u, out.size());
2578 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002579 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002580 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2581 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002582 args.switchMask);
2583 ASSERT_EQ(uint32_t(0), args.policyFlags);
2584}
2585
Chris Ye87143712020-11-10 05:05:58 +00002586// --- VibratorInputMapperTest ---
2587class VibratorInputMapperTest : public InputMapperTest {
2588protected:
2589 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2590};
2591
2592TEST_F(VibratorInputMapperTest, GetSources) {
2593 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2594
2595 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2596}
2597
2598TEST_F(VibratorInputMapperTest, GetVibratorIds) {
2599 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2600
2601 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2602}
2603
2604TEST_F(VibratorInputMapperTest, Vibrate) {
2605 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002606 constexpr int32_t VIBRATION_TOKEN = 100;
Chris Ye87143712020-11-10 05:05:58 +00002607 VibratorInputMapper& mapper = addMapperAndConfigure<VibratorInputMapper>();
2608
2609 VibrationElement pattern(2);
2610 VibrationSequence sequence(2);
2611 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002612 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2613 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002614 sequence.addElement(pattern);
2615 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002616 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2617 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002618 sequence.addElement(pattern);
2619
2620 std::vector<int64_t> timings = {0, 1};
2621 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2622
2623 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002624 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002625 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002626 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002627 // Verify vibrator state listener was notified.
2628 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002629 ASSERT_EQ(1u, out.size());
2630 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2631 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2632 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002633 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002634 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002635 ASSERT_FALSE(mapper.isVibrating());
2636 // Verify vibrator state listener was notified.
2637 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002638 ASSERT_EQ(1u, out.size());
2639 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2640 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2641 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002642}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002643
Chris Yef59a2f42020-10-16 12:55:26 -07002644// --- SensorInputMapperTest ---
2645
2646class SensorInputMapperTest : public InputMapperTest {
2647protected:
2648 static const int32_t ACCEL_RAW_MIN;
2649 static const int32_t ACCEL_RAW_MAX;
2650 static const int32_t ACCEL_RAW_FUZZ;
2651 static const int32_t ACCEL_RAW_FLAT;
2652 static const int32_t ACCEL_RAW_RESOLUTION;
2653
2654 static const int32_t GYRO_RAW_MIN;
2655 static const int32_t GYRO_RAW_MAX;
2656 static const int32_t GYRO_RAW_FUZZ;
2657 static const int32_t GYRO_RAW_FLAT;
2658 static const int32_t GYRO_RAW_RESOLUTION;
2659
2660 static const float GRAVITY_MS2_UNIT;
2661 static const float DEGREE_RADIAN_UNIT;
2662
2663 void prepareAccelAxes();
2664 void prepareGyroAxes();
2665 void setAccelProperties();
2666 void setGyroProperties();
2667 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2668};
2669
2670const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
2671const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
2672const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
2673const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
2674const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
2675
2676const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
2677const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
2678const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
2679const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
2680const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
2681
2682const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
2683const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
2684
2685void SensorInputMapperTest::prepareAccelAxes() {
2686 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2687 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2688 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2689 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2690 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
2691 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
2692}
2693
2694void SensorInputMapperTest::prepareGyroAxes() {
2695 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2696 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2697 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2698 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2699 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
2700 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
2701}
2702
2703void SensorInputMapperTest::setAccelProperties() {
2704 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
2705 /* sensorDataIndex */ 0);
2706 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
2707 /* sensorDataIndex */ 1);
2708 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
2709 /* sensorDataIndex */ 2);
2710 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2711 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
2712 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
2713 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
2714 addConfigurationProperty("sensor.accelerometer.power", "1.5");
2715}
2716
2717void SensorInputMapperTest::setGyroProperties() {
2718 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
2719 /* sensorDataIndex */ 0);
2720 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
2721 /* sensorDataIndex */ 1);
2722 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
2723 /* sensorDataIndex */ 2);
2724 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
2725 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
2726 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
2727 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
2728 addConfigurationProperty("sensor.gyroscope.power", "0.8");
2729}
2730
2731TEST_F(SensorInputMapperTest, GetSources) {
2732 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2733
2734 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
2735}
2736
2737TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
2738 setAccelProperties();
2739 prepareAccelAxes();
2740 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2741
2742 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
2743 std::chrono::microseconds(10000),
2744 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
2747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
2748 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
2749 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002751
2752 NotifySensorArgs args;
2753 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2754 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
2755 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
2756
2757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2758 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2759 ASSERT_EQ(args.deviceId, DEVICE_ID);
2760 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
2761 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2762 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2763 ASSERT_EQ(args.values, values);
2764 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
2765}
2766
2767TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
2768 setGyroProperties();
2769 prepareGyroAxes();
2770 SensorInputMapper& mapper = addMapperAndConfigure<SensorInputMapper>();
2771
2772 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
2773 std::chrono::microseconds(10000),
2774 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08002775 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002776 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
2777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
2778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
2779 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
2780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07002781
2782 NotifySensorArgs args;
2783 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2784 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
2785 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
2786
2787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
2788 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
2789 ASSERT_EQ(args.deviceId, DEVICE_ID);
2790 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
2791 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
2792 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
2793 ASSERT_EQ(args.values, values);
2794 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
2795}
2796
Michael Wrightd02c5b62014-02-10 15:10:22 -08002797// --- KeyboardInputMapperTest ---
2798
2799class KeyboardInputMapperTest : public InputMapperTest {
2800protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002801 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00002802 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00002803 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002804
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002805 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002806 int32_t originalKeyCode, int32_t rotatedKeyCode,
2807 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002808};
2809
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002810/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
2811 * orientation.
2812 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00002813void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01002814 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
2815 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07002816}
2817
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002818void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08002819 int32_t originalScanCode, int32_t originalKeyCode,
2820 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002821 NotifyKeyArgs args;
2822
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2825 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2826 ASSERT_EQ(originalScanCode, args.scanCode);
2827 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002828 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002829
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2832 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2833 ASSERT_EQ(originalScanCode, args.scanCode);
2834 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002835 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002836}
2837
Michael Wrightd02c5b62014-02-10 15:10:22 -08002838TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002839 KeyboardInputMapper& mapper =
2840 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2841 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002843 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002844}
2845
2846TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
2847 const int32_t USAGE_A = 0x070004;
2848 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002849 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2850 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07002851 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
2852 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
2853 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002854
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002855 KeyboardInputMapper& mapper =
2856 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2857 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08002858 // Initial metastate is AMETA_NONE.
2859 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002860
2861 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002863 NotifyKeyArgs args;
2864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2865 ASSERT_EQ(DEVICE_ID, args.deviceId);
2866 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2867 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2868 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2869 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2870 ASSERT_EQ(KEY_HOME, args.scanCode);
2871 ASSERT_EQ(AMETA_NONE, args.metaState);
2872 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2873 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2874 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2875
2876 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002877 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2879 ASSERT_EQ(DEVICE_ID, args.deviceId);
2880 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2881 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2882 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2883 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
2884 ASSERT_EQ(KEY_HOME, args.scanCode);
2885 ASSERT_EQ(AMETA_NONE, args.metaState);
2886 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2887 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2888 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2889
2890 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2894 ASSERT_EQ(DEVICE_ID, args.deviceId);
2895 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2896 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2897 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2898 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2899 ASSERT_EQ(0, args.scanCode);
2900 ASSERT_EQ(AMETA_NONE, args.metaState);
2901 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2902 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2903 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2904
2905 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
2907 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2909 ASSERT_EQ(DEVICE_ID, args.deviceId);
2910 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2911 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2912 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2913 ASSERT_EQ(AKEYCODE_A, args.keyCode);
2914 ASSERT_EQ(0, args.scanCode);
2915 ASSERT_EQ(AMETA_NONE, args.metaState);
2916 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2917 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
2918 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2919
2920 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2924 ASSERT_EQ(DEVICE_ID, args.deviceId);
2925 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2926 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
2927 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
2928 ASSERT_EQ(0, args.keyCode);
2929 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2930 ASSERT_EQ(AMETA_NONE, args.metaState);
2931 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2932 ASSERT_EQ(0U, args.policyFlags);
2933 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2934
2935 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002936 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
2937 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2939 ASSERT_EQ(DEVICE_ID, args.deviceId);
2940 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
2941 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
2942 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
2943 ASSERT_EQ(0, args.keyCode);
2944 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
2945 ASSERT_EQ(AMETA_NONE, args.metaState);
2946 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
2947 ASSERT_EQ(0U, args.policyFlags);
2948 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
2949}
2950
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00002951TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
2952 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
2953 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
2954 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
2955
2956 KeyboardInputMapper& mapper =
2957 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2958 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2959
2960 // Key down by scan code.
2961 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
2962 NotifyKeyArgs args;
2963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2964 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2965
2966 // Key up by scan code.
2967 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
2968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2969 ASSERT_EQ(AKEYCODE_B, args.keyCode);
2970}
2971
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002972/**
2973 * Ensure that the readTime is set to the time when the EV_KEY is received.
2974 */
2975TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
2976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
2977
2978 KeyboardInputMapper& mapper =
2979 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
2980 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2981 NotifyKeyArgs args;
2982
2983 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00002984 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2986 ASSERT_EQ(12, args.readTime);
2987
2988 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00002989 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00002990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
2991 ASSERT_EQ(15, args.readTime);
2992}
2993
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
2996 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07002997 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
2998 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
2999 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003001 KeyboardInputMapper& mapper =
3002 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3003 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004
Arthur Hung95f68612022-04-07 14:08:22 +08003005 // Initial metastate is AMETA_NONE.
3006 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003007
3008 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003009 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003010 NotifyKeyArgs args;
3011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3012 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003013 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003014 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015
3016 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003017 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003020 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003021
3022 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003023 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3025 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003026 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003027
3028 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003029 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3031 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003032 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003033 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034}
3035
3036TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003037 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3038 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3039 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003041
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003042 KeyboardInputMapper& mapper =
3043 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3044 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003045
Michael Wrighta9cf4192022-12-01 23:46:39 +00003046 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003047 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3048 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3049 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3050 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3051 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3052 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3053 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3054 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3055}
3056
3057TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003058 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3059 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3060 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3061 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003062
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003064 KeyboardInputMapper& mapper =
3065 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3066 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003067
Michael Wrighta9cf4192022-12-01 23:46:39 +00003068 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003069 ASSERT_NO_FATAL_FAILURE(
3070 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3071 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3072 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3073 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3074 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3075 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3076 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003077
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003079 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003080 ASSERT_NO_FATAL_FAILURE(
3081 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3082 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3083 AKEYCODE_DPAD_UP, DISPLAY_ID));
3084 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3085 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3086 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3087 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003088
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003089 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003090 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003091 ASSERT_NO_FATAL_FAILURE(
3092 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3093 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3094 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3095 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3096 AKEYCODE_DPAD_UP, DISPLAY_ID));
3097 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3098 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003099
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003100 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003101 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003102 ASSERT_NO_FATAL_FAILURE(
3103 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3104 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3105 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3106 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3107 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3108 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3109 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003110
3111 // Special case: if orientation changes while key is down, we still emit the same keycode
3112 // in the key up as we did in the key down.
3113 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003114 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003115 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3119 ASSERT_EQ(KEY_UP, args.scanCode);
3120 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3121
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003122 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003123 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3126 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3127 ASSERT_EQ(KEY_UP, args.scanCode);
3128 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3129}
3130
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003131TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3132 // If the keyboard is not orientation aware,
3133 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003134 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003136 KeyboardInputMapper& mapper =
3137 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3138 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003139 NotifyKeyArgs args;
3140
3141 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003142 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3146 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3147
Michael Wrighta9cf4192022-12-01 23:46:39 +00003148 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003151 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3153 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3154}
3155
3156TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3157 // If the keyboard is orientation aware,
3158 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003159 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003160
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003161 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003162 KeyboardInputMapper& mapper =
3163 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3164 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003165 NotifyKeyArgs args;
3166
3167 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3168 // ^--- already checked by the previous test
3169
Michael Wrighta9cf4192022-12-01 23:46:39 +00003170 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003171 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3176 ASSERT_EQ(DISPLAY_ID, args.displayId);
3177
3178 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003179 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003180 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003181 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3186 ASSERT_EQ(newDisplayId, args.displayId);
3187}
3188
Michael Wrightd02c5b62014-02-10 15:10:22 -08003189TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003190 KeyboardInputMapper& mapper =
3191 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3192 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003193
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003194 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003195 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003196
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003197 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003198 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003199}
3200
Philip Junker4af3b3d2021-12-14 10:36:55 +01003201TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3202 KeyboardInputMapper& mapper =
3203 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3204 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3205
3206 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3207 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3208 << "If a mapping is available, the result is equal to the mapping";
3209
3210 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3211 << "If no mapping is available, the result is the key location";
3212}
3213
Michael Wrightd02c5b62014-02-10 15:10:22 -08003214TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 KeyboardInputMapper& mapper =
3216 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3217 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003219 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003220 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003221
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003222 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003223 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224}
3225
3226TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003227 KeyboardInputMapper& mapper =
3228 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3229 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003230
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003231 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003234 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003235 ASSERT_TRUE(flags[0]);
3236 ASSERT_FALSE(flags[1]);
3237}
3238
3239TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003240 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3241 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3242 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3243 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3244 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3245 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003247 KeyboardInputMapper& mapper =
3248 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3249 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003250 // Initial metastate is AMETA_NONE.
3251 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003254 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3255 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3256 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257
3258 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3260 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003261 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3262 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3263 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003264 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
3266 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3268 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003269 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3270 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3271 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003272 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273
3274 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3276 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003277 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3278 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3279 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
3282 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3286 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3287 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003288 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003289
3290 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003291 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003293 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3294 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3295 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003297
3298 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003299 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3300 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003301 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3302 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3303 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003304 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003305}
3306
Chris Yea52ade12020-08-27 16:49:20 -07003307TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3308 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3309 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3310 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3311 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3312
3313 KeyboardInputMapper& mapper =
3314 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3315 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3316
Chris Yea52ade12020-08-27 16:49:20 -07003317 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003318 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003319 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3320 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3321 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3322 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3323
3324 NotifyKeyArgs args;
3325 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3328 ASSERT_EQ(AMETA_NONE, args.metaState);
3329 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3330 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3331 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3332
3333 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003334 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3336 ASSERT_EQ(AMETA_NONE, args.metaState);
3337 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3338 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3339 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3340}
3341
Arthur Hung2c9a3342019-07-23 14:18:59 +08003342TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3343 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003344 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3345 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3346 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3347 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003348
3349 // keyboard 2.
3350 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003351 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003352 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003353 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003354 std::shared_ptr<InputDevice> device2 =
3355 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003356 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003357
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003358 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3359 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3360 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3361 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003362
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 KeyboardInputMapper& mapper =
3364 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3365 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003366
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003367 KeyboardInputMapper& mapper2 =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003368 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003369 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003370 std::list<NotifyArgs> unused =
3371 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003372 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003373 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003374
3375 // Prepared displays and associated info.
3376 constexpr uint8_t hdmi1 = 0;
3377 constexpr uint8_t hdmi2 = 1;
3378 const std::string SECONDARY_UNIQUE_ID = "local:1";
3379
3380 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3381 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3382
3383 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003384 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3385 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003386 ASSERT_FALSE(device2->isEnabled());
3387
3388 // Prepare second display.
3389 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003390 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003391 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003392 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003393 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003394 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003395 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3396 InputReaderConfiguration::CHANGE_DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003397
3398 // Device should be enabled after the associated display is found.
3399 ASSERT_TRUE(mDevice->isEnabled());
3400 ASSERT_TRUE(device2->isEnabled());
3401
3402 // Test pad key events
3403 ASSERT_NO_FATAL_FAILURE(
3404 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3405 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3406 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3407 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3408 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3409 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3410 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3411
3412 ASSERT_NO_FATAL_FAILURE(
3413 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3414 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3415 AKEYCODE_DPAD_RIGHT, newDisplayId));
3416 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3417 AKEYCODE_DPAD_DOWN, newDisplayId));
3418 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3419 AKEYCODE_DPAD_LEFT, newDisplayId));
3420}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003421
arthurhungc903df12020-08-11 15:08:42 +08003422TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3423 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3424 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3425 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3426 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3427 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3428 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3429
3430 KeyboardInputMapper& mapper =
3431 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3432 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003433 // Initial metastate is AMETA_NONE.
3434 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003435
3436 // Initialization should have turned all of the lights off.
3437 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3438 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3439 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3440
3441 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003444 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3445 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3446
3447 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003448 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003450 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3451 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3452
3453 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003454 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003456 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3457 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3458
3459 mFakeEventHub->removeDevice(EVENTHUB_ID);
3460 mReader->loopOnce();
3461
3462 // keyboard 2 should default toggle keys.
3463 const std::string USB2 = "USB2";
3464 const std::string DEVICE_NAME2 = "KEYBOARD2";
3465 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3466 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3467 std::shared_ptr<InputDevice> device2 =
3468 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003469 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003470 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3471 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3472 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3473 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3474 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3475 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3476
arthurhung6fe95782020-10-05 22:41:16 +08003477 KeyboardInputMapper& mapper2 =
3478 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3479 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003480 std::list<NotifyArgs> unused =
3481 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003482 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003483 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003484
3485 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3486 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3487 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003488 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3489 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003490}
3491
Arthur Hungcb40a002021-08-03 14:31:01 +00003492TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3493 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3496
3497 // Suppose we have two mappers. (DPAD + KEYBOARD)
3498 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
3499 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3500 KeyboardInputMapper& mapper =
3501 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003503 // Initial metastate is AMETA_NONE.
3504 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003505
3506 mReader->toggleCapsLockState(DEVICE_ID);
3507 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3508}
3509
Arthur Hungfb3cc112022-04-13 07:39:50 +00003510TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3511 // keyboard 1.
3512 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3513 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3514 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3515 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3516 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3517 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3518
3519 KeyboardInputMapper& mapper1 =
3520 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3521 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3522
3523 // keyboard 2.
3524 const std::string USB2 = "USB2";
3525 const std::string DEVICE_NAME2 = "KEYBOARD2";
3526 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3527 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3528 std::shared_ptr<InputDevice> device2 =
3529 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3530 ftl::Flags<InputDeviceClass>(0));
3531 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3532 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3533 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3534 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3535 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3536 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3537
3538 KeyboardInputMapper& mapper2 =
3539 device2->addMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3540 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003541 std::list<NotifyArgs> unused =
3542 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00003543 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003544 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003545
Arthur Hung95f68612022-04-07 14:08:22 +08003546 // Initial metastate is AMETA_NONE.
3547 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3548 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3549
3550 // Toggle num lock on and off.
3551 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3552 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003553 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3554 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3555 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3556
3557 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3558 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3559 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3560 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3561 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3562
3563 // Toggle caps lock on and off.
3564 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3565 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3566 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3567 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3568 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3569
3570 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3571 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3572 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3573 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3574 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3575
3576 // Toggle scroll lock on and off.
3577 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3578 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3579 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3580 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3581 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3582
3583 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3584 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3586 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3587 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3588}
3589
Arthur Hung2141d542022-08-23 07:45:21 +00003590TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3591 const int32_t USAGE_A = 0x070004;
3592 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3593 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3594
3595 KeyboardInputMapper& mapper =
3596 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3597 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3598 // Key down by scan code.
3599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3600 NotifyKeyArgs args;
3601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3602 ASSERT_EQ(DEVICE_ID, args.deviceId);
3603 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3604 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3606 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3607 ASSERT_EQ(KEY_HOME, args.scanCode);
3608 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3609
3610 // Disable device, it should synthesize cancellation events for down events.
3611 mFakePolicy->addDisabledDevice(DEVICE_ID);
3612 configureDevice(InputReaderConfiguration::CHANGE_ENABLED_STATE);
3613
3614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3615 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3616 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3617 ASSERT_EQ(KEY_HOME, args.scanCode);
3618 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3619}
3620
Zixuan Qufecb6062022-11-12 04:44:31 +00003621TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
3622 mDevice->addMapper<KeyboardInputMapper>(EVENTHUB_ID, AINPUT_SOURCE_KEYBOARD,
3623 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3624 std::list<NotifyArgs> unused =
3625 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), 0);
3626
3627 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3628
3629 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3630 InputReaderConfiguration::CHANGE_KEYBOARD_LAYOUT_ASSOCIATION);
3631
3632 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3633 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3634 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3635 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3636 deviceInfo.getKeyboardLayoutInfo()->layoutType);
3637}
3638
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003639TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3640 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3641 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3642
3643 // Configuration
3644 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3646 InputReaderConfiguration config;
3647 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, &config, 0);
3648
3649 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3650 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3651}
3652
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003653// --- KeyboardInputMapperTest_ExternalDevice ---
3654
3655class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
3656protected:
Chris Yea52ade12020-08-27 16:49:20 -07003657 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658};
3659
3660TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003661 // For external devices, non-media keys will trigger wake on key down. Media keys need to be
3662 // marked as WAKE in the keylayout file to trigger wake.
Powei Fengd041c5d2019-05-03 17:11:33 -07003663
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3665 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3666 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3667 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003668
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 KeyboardInputMapper& mapper =
3670 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3671 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003672
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003674 NotifyKeyArgs args;
3675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3676 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3677
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003678 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3680 ASSERT_EQ(uint32_t(0), args.policyFlags);
3681
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003682 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3684 ASSERT_EQ(uint32_t(0), args.policyFlags);
3685
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003686 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3688 ASSERT_EQ(uint32_t(0), args.policyFlags);
3689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3692 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3693
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003694 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3696 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3697}
3698
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003699TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07003700 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07003701
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3703 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3704 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003705
Powei Fengd041c5d2019-05-03 17:11:33 -07003706 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003707 KeyboardInputMapper& mapper =
3708 addMapperAndConfigure<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3709 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07003710
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003712 NotifyKeyArgs args;
3713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3714 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3715
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3718 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3719
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3722 ASSERT_EQ(uint32_t(0), args.policyFlags);
3723
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3726 ASSERT_EQ(uint32_t(0), args.policyFlags);
3727
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3730 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3731
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3734 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3735}
3736
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737// --- CursorInputMapperTest ---
3738
3739class CursorInputMapperTest : public InputMapperTest {
3740protected:
3741 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
3742
Michael Wright17db18e2020-06-26 20:51:44 +01003743 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08003744
Chris Yea52ade12020-08-27 16:49:20 -07003745 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746 InputMapperTest::SetUp();
3747
Michael Wright17db18e2020-06-26 20:51:44 +01003748 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00003749 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003750 }
3751
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003752 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
3753 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003754
Michael Wrighta9cf4192022-12-01 23:46:39 +00003755 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003756 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
3757 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
3758 }
3759
3760 void prepareSecondaryDisplay() {
3761 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00003762 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00003763 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07003764 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003765
3766 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
3767 float pressure) {
3768 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
3769 0.0f, 0.0f, 0.0f, EPSILON));
3770 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08003771};
3772
3773const int32_t CursorInputMapperTest::TRACKBALL_MOVEMENT_THRESHOLD = 6;
3774
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775void CursorInputMapperTest::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
3776 int32_t originalY, int32_t rotatedX,
3777 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003778 NotifyMotionArgs args;
3779
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
3781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
3782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3784 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003785 ASSERT_NO_FATAL_FAILURE(
3786 assertCursorPointerCoords(args.pointerCoords[0],
3787 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
3788 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003789}
3790
3791TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003792 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003793 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003794
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003795 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003796}
3797
3798TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003799 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003800 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003801
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003802 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003803}
3804
3805TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003806 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003807 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003808
3809 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003810 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003811
3812 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07003813 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
3814 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003815 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3816 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
3817
3818 // When the bounds are set, then there should be a valid motion range.
3819 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
3820
3821 InputDeviceInfo info2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003822 mapper.populateDeviceInfo(&info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003823
3824 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3825 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
3826 1, 800 - 1, 0.0f, 0.0f));
3827 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3828 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
3829 2, 480 - 1, 0.0f, 0.0f));
3830 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
3831 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
3832 0.0f, 1.0f, 0.0f, 0.0f));
3833}
3834
3835TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003836 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003837 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003838
3839 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003840 mapper.populateDeviceInfo(&info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003841
3842 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3843 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
3844 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3845 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3846 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
3847 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
3848 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
3849 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
3850 0.0f, 1.0f, 0.0f, 0.0f));
3851}
3852
3853TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003854 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003855 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003856
arthurhungdcef2dc2020-08-11 14:47:50 +08003857 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003858
3859 NotifyMotionArgs args;
3860
3861 // Button press.
3862 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3866 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3867 ASSERT_EQ(DEVICE_ID, args.deviceId);
3868 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3869 ASSERT_EQ(uint32_t(0), args.policyFlags);
3870 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
3871 ASSERT_EQ(0, args.flags);
3872 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3873 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3874 ASSERT_EQ(0, args.edgeFlags);
3875 ASSERT_EQ(uint32_t(1), args.pointerCount);
3876 ASSERT_EQ(0, args.pointerProperties[0].id);
3877 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003878 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003879 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3880 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3881 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3882
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3884 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3885 ASSERT_EQ(DEVICE_ID, args.deviceId);
3886 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3887 ASSERT_EQ(uint32_t(0), args.policyFlags);
3888 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
3889 ASSERT_EQ(0, args.flags);
3890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
3892 ASSERT_EQ(0, args.edgeFlags);
3893 ASSERT_EQ(uint32_t(1), args.pointerCount);
3894 ASSERT_EQ(0, args.pointerProperties[0].id);
3895 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003896 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003897 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3898 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3899 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3900
Michael Wrightd02c5b62014-02-10 15:10:22 -08003901 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3903 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3905 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3906 ASSERT_EQ(DEVICE_ID, args.deviceId);
3907 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3908 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
3910 ASSERT_EQ(0, args.flags);
3911 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3912 ASSERT_EQ(0, args.buttonState);
3913 ASSERT_EQ(0, args.edgeFlags);
3914 ASSERT_EQ(uint32_t(1), args.pointerCount);
3915 ASSERT_EQ(0, args.pointerProperties[0].id);
3916 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003917 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003918 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3919 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3920 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3921
3922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3923 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3924 ASSERT_EQ(DEVICE_ID, args.deviceId);
3925 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
3926 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003927 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
3928 ASSERT_EQ(0, args.flags);
3929 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
3930 ASSERT_EQ(0, args.buttonState);
3931 ASSERT_EQ(0, args.edgeFlags);
3932 ASSERT_EQ(uint32_t(1), args.pointerCount);
3933 ASSERT_EQ(0, args.pointerProperties[0].id);
3934 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003935 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003936 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
3937 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
3938 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3939}
3940
3941TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003942 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003943 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003944
3945 NotifyMotionArgs args;
3946
3947 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
3949 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3951 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003952 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
3953 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
3954 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003955
3956 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003957 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
3958 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3960 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003961 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
3962 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003963}
3964
3965TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003966 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003967 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003968
3969 NotifyMotionArgs args;
3970
3971 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
3973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3975 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003976 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
3979 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003980 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003981
Michael Wrightd02c5b62014-02-10 15:10:22 -08003982 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003983 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
3984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003987 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08003988
3989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003990 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07003991 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003992}
3993
3994TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003995 addConfigurationProperty("cursor.mode", "navigation");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003996 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08003997
3998 NotifyMotionArgs args;
3999
4000 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4002 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4006 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004007 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4008 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4009 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004010
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4012 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004013 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4014 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4015 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004016
Michael Wrightd02c5b62014-02-10 15:10:22 -08004017 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004018 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4019 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4022 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004023 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4024 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4025 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004026
4027 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004028 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004031 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004032 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004033
4034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004035 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004036 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004037}
4038
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004039TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004040 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004041 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004042 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4043 // need to be rotated.
4044 addConfigurationProperty("cursor.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004045 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004046
Michael Wrighta9cf4192022-12-01 23:46:39 +00004047 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004048 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4049 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4050 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4051 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4052 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4053 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4054 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4055 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4056}
4057
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004058TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004059 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004060 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004061 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4062 // orientation-aware are affected by display rotation.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004063 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004064
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004065 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004066 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004067 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4068 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4069 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4070 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4071 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4072 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4073 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4074 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4075
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004076 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004077 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004078 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4079 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4080 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4081 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4082 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4083 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4084 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4085 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004086
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004087 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004088 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004089 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4090 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4091 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4092 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4093 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4094 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4095 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4096 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4097
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004098 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004099 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004100 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4101 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4102 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4103 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4104 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4105 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4106 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4107 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004108}
4109
4110TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004111 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004112 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113
4114 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4115 mFakePointerController->setPosition(100, 200);
4116 mFakePointerController->setButtonState(0);
4117
4118 NotifyMotionArgs motionArgs;
4119 NotifyKeyArgs keyArgs;
4120
4121 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004122 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4126 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4127 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004128 ASSERT_NO_FATAL_FAILURE(
4129 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4133 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4134 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004135 ASSERT_NO_FATAL_FAILURE(
4136 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004137
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004141 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004142 ASSERT_EQ(0, motionArgs.buttonState);
4143 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004144 ASSERT_NO_FATAL_FAILURE(
4145 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004146
4147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004148 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 ASSERT_EQ(0, motionArgs.buttonState);
4150 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(
4152 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004153
4154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004156 ASSERT_EQ(0, motionArgs.buttonState);
4157 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004158 ASSERT_NO_FATAL_FAILURE(
4159 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004160
4161 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4163 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4164 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4166 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4167 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4168 motionArgs.buttonState);
4169 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4170 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004171 ASSERT_NO_FATAL_FAILURE(
4172 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4176 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4177 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4178 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004179 ASSERT_NO_FATAL_FAILURE(
4180 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004181
4182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4183 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4184 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4185 motionArgs.buttonState);
4186 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
4187 mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004188 ASSERT_NO_FATAL_FAILURE(
4189 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004190
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004194 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4196 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004197 ASSERT_NO_FATAL_FAILURE(
4198 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004199
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004202 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
4203 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004204 ASSERT_NO_FATAL_FAILURE(
4205 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004206
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004207 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4208 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004210 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4211 ASSERT_EQ(0, motionArgs.buttonState);
4212 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004213 ASSERT_NO_FATAL_FAILURE(
4214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004217
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219 ASSERT_EQ(0, motionArgs.buttonState);
4220 ASSERT_EQ(0, mFakePointerController->getButtonState());
4221 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004222 ASSERT_NO_FATAL_FAILURE(
4223 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004224
Michael Wrightd02c5b62014-02-10 15:10:22 -08004225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4226 ASSERT_EQ(0, motionArgs.buttonState);
4227 ASSERT_EQ(0, mFakePointerController->getButtonState());
4228 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004229 ASSERT_NO_FATAL_FAILURE(
4230 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231
4232 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4236 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4237 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004238
Michael Wrightd02c5b62014-02-10 15:10:22 -08004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004241 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4242 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004243 ASSERT_NO_FATAL_FAILURE(
4244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004245
4246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4248 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4249 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004250 ASSERT_NO_FATAL_FAILURE(
4251 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004252
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4254 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004256 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004257 ASSERT_EQ(0, motionArgs.buttonState);
4258 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004259 ASSERT_NO_FATAL_FAILURE(
4260 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004261
4262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004264 ASSERT_EQ(0, motionArgs.buttonState);
4265 ASSERT_EQ(0, mFakePointerController->getButtonState());
4266
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004267 ASSERT_NO_FATAL_FAILURE(
4268 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4270 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4271 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4272
4273 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004274 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4275 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4277 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4278 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004281 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004282 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4283 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004284 ASSERT_NO_FATAL_FAILURE(
4285 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286
4287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4288 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4289 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
4290 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004291 ASSERT_NO_FATAL_FAILURE(
4292 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004293
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004294 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4295 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 ASSERT_EQ(0, motionArgs.buttonState);
4299 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004300 ASSERT_NO_FATAL_FAILURE(
4301 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004302
4303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4304 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4305 ASSERT_EQ(0, motionArgs.buttonState);
4306 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004307 ASSERT_NO_FATAL_FAILURE(
4308 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4311 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4312 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4313
4314 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4318 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4319 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004320
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004322 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004323 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4324 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004325 ASSERT_NO_FATAL_FAILURE(
4326 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004327
4328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4329 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4330 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4331 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004332 ASSERT_NO_FATAL_FAILURE(
4333 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004334
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004338 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339 ASSERT_EQ(0, motionArgs.buttonState);
4340 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004341 ASSERT_NO_FATAL_FAILURE(
4342 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004343
4344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4345 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4346 ASSERT_EQ(0, motionArgs.buttonState);
4347 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004348 ASSERT_NO_FATAL_FAILURE(
4349 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4353 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4354
4355 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4359 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4360 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004361
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4365 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004366 ASSERT_NO_FATAL_FAILURE(
4367 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004368
4369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4371 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
4372 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(
4374 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004379 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004380 ASSERT_EQ(0, motionArgs.buttonState);
4381 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004382 ASSERT_NO_FATAL_FAILURE(
4383 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004384
4385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4386 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4387 ASSERT_EQ(0, motionArgs.buttonState);
4388 ASSERT_EQ(0, mFakePointerController->getButtonState());
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004389 ASSERT_NO_FATAL_FAILURE(
4390 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004391
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4393 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4394 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4395}
4396
4397TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("cursor.mode", "pointer");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004399 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
4401 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4402 mFakePointerController->setPosition(100, 200);
4403 mFakePointerController->setButtonState(0);
4404
4405 NotifyMotionArgs args;
4406
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4408 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4409 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004411 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4412 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4414 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004415 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004416}
4417
4418TEST_F(CursorInputMapperTest, Process_PointerCapture) {
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004419 addConfigurationProperty("cursor.mode", "pointer");
4420 mFakePolicy->setPointerCapture(true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004421 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004422
4423 NotifyDeviceResetArgs resetArgs;
4424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4425 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4426 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4427
4428 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4429 mFakePointerController->setPosition(100, 200);
4430 mFakePointerController->setButtonState(0);
4431
4432 NotifyMotionArgs args;
4433
4434 // Move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004435 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4436 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4437 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4439 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4441 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4442 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004443 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004444
4445 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004446 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4449 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4451 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4452 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4454 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4455 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4457 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4458
4459 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004460 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4461 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4463 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4465 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4466 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4469 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4470 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4471 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
4472
4473 // Another move.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 30);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 40);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4481 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004482 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(100.0f, 200.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004483
4484 // Disable pointer capture and check that the device generation got bumped
4485 // and events are generated the usual way.
arthurhungdcef2dc2020-08-11 14:47:50 +08004486 const uint32_t generation = mReader->getContext()->getGeneration();
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004487 mFakePolicy->setPointerCapture(false);
4488 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
arthurhungdcef2dc2020-08-11 14:47:50 +08004489 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004490
4491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004492 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4493
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4498 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4501 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004502 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503}
4504
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004505/**
4506 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4507 * pointer acceleration or speed processing should not be applied.
4508 */
4509TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4510 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004511 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4512 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004513 mFakePolicy->setVelocityControlParams(testParams);
4514 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4515
4516 NotifyDeviceResetArgs resetArgs;
4517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4518 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4519 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4520
4521 NotifyMotionArgs args;
4522
4523 // Move and verify scale is applied.
4524 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4528 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4529 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4530 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4531 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4532 ASSERT_GT(relX, 10);
4533 ASSERT_GT(relY, 20);
4534
4535 // Enable Pointer Capture
4536 mFakePolicy->setPointerCapture(true);
4537 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4538 NotifyPointerCaptureChangedArgs captureArgs;
4539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4540 ASSERT_TRUE(captureArgs.request.enable);
4541
4542 // Move and verify scale is not applied.
4543 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4545 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4547 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4549 ASSERT_EQ(10, args.pointerCoords[0].getX());
4550 ASSERT_EQ(20, args.pointerCoords[0].getY());
4551}
4552
Prabir Pradhan208360b2022-06-24 18:37:04 +00004553TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4554 addConfigurationProperty("cursor.mode", "pointer");
4555 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4556
4557 NotifyDeviceResetArgs resetArgs;
4558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4559 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4560 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4561
4562 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004563 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004564
4565 NotifyMotionArgs args;
4566
4567 // Verify that the coordinates are rotated.
4568 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4569 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4572 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4573 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4574 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4575 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4576
4577 // Enable Pointer Capture.
4578 mFakePolicy->setPointerCapture(true);
4579 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
4580 NotifyPointerCaptureChangedArgs captureArgs;
4581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4582 ASSERT_TRUE(captureArgs.request.enable);
4583
4584 // Move and verify rotation is not applied.
4585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4586 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4589 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4591 ASSERT_EQ(10, args.pointerCoords[0].getX());
4592 ASSERT_EQ(20, args.pointerCoords[0].getY());
4593}
4594
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004595TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004596 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004597
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004598 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004599 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004600
4601 // Set up the secondary display as the display on which the pointer should be shown.
4602 // The InputDevice is not associated with any display.
4603 prepareSecondaryDisplay();
4604 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Garfield Tan888a6a42020-01-09 11:39:16 -08004605 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4606
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004607 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004608 mFakePointerController->setPosition(100, 200);
4609 mFakePointerController->setButtonState(0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004610
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004611 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004616 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4617 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4618 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004619 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004620}
4621
4622TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
4623 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4624
4625 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004626 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004627
4628 // Set up the secondary display as the display on which the pointer should be shown,
4629 // and associate the InputDevice with the secondary display.
4630 prepareSecondaryDisplay();
4631 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4632 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4633 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4634
4635 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4636 mFakePointerController->setPosition(100, 200);
4637 mFakePointerController->setButtonState(0);
4638
4639 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4640 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4641 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004643 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4644 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4645 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004646 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004647}
4648
4649TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
4650 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4651
4652 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004653 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004654 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4655
4656 // Associate the InputDevice with the secondary display.
4657 prepareSecondaryDisplay();
4658 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
4659 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
4660
4661 // The mapper should not generate any events because it is associated with a display that is
4662 // different from the pointer display.
4663 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4664 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4665 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004667}
4668
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004669// --- BluetoothCursorInputMapperTest ---
4670
4671class BluetoothCursorInputMapperTest : public CursorInputMapperTest {
4672protected:
4673 void SetUp() override {
4674 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
4675
4676 mFakePointerController = std::make_shared<FakePointerController>();
4677 mFakePolicy->setPointerController(mFakePointerController);
4678 }
4679};
4680
4681TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
4682 addConfigurationProperty("cursor.mode", "pointer");
4683 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4684
4685 nsecs_t kernelEventTime = ARBITRARY_TIME;
4686 nsecs_t expectedEventTime = ARBITRARY_TIME;
4687 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4688 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4691 WithEventTime(expectedEventTime))));
4692
4693 // Process several events that come in quick succession, according to their timestamps.
4694 for (int i = 0; i < 3; i++) {
4695 constexpr static nsecs_t delta = ms2ns(1);
4696 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
4697 kernelEventTime += delta;
4698 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4699
4700 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4701 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4703 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4704 WithEventTime(expectedEventTime))));
4705 }
4706}
4707
4708TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
4709 addConfigurationProperty("cursor.mode", "pointer");
4710 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4711
4712 nsecs_t expectedEventTime = ARBITRARY_TIME;
4713 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4714 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4716 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4717 WithEventTime(expectedEventTime))));
4718
4719 // Process several events with the same timestamp from the kernel.
4720 // Ensure that we do not generate events too far into the future.
4721 constexpr static int32_t numEvents =
4722 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
4723 for (int i = 0; i < numEvents; i++) {
4724 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
4725
4726 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4729 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4730 WithEventTime(expectedEventTime))));
4731 }
4732
4733 // By processing more events with the same timestamp, we should not generate events with a
4734 // timestamp that is more than the specified max time delta from the timestamp at its injection.
4735 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
4736 for (int i = 0; i < 3; i++) {
4737 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4738 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4740 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4741 WithEventTime(cappedEventTime))));
4742 }
4743}
4744
4745TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
4746 addConfigurationProperty("cursor.mode", "pointer");
4747 CursorInputMapper& mapper = addMapperAndConfigure<CursorInputMapper>();
4748
4749 nsecs_t kernelEventTime = ARBITRARY_TIME;
4750 nsecs_t expectedEventTime = ARBITRARY_TIME;
4751 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4752 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4754 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4755 WithEventTime(expectedEventTime))));
4756
4757 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
4758 // smoothening is not needed, its timestamp is not affected.
4759 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
4760 expectedEventTime = kernelEventTime;
4761
4762 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
4763 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
4765 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4766 WithEventTime(expectedEventTime))));
4767}
4768
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769// --- TouchInputMapperTest ---
4770
4771class TouchInputMapperTest : public InputMapperTest {
4772protected:
4773 static const int32_t RAW_X_MIN;
4774 static const int32_t RAW_X_MAX;
4775 static const int32_t RAW_Y_MIN;
4776 static const int32_t RAW_Y_MAX;
4777 static const int32_t RAW_TOUCH_MIN;
4778 static const int32_t RAW_TOUCH_MAX;
4779 static const int32_t RAW_TOOL_MIN;
4780 static const int32_t RAW_TOOL_MAX;
4781 static const int32_t RAW_PRESSURE_MIN;
4782 static const int32_t RAW_PRESSURE_MAX;
4783 static const int32_t RAW_ORIENTATION_MIN;
4784 static const int32_t RAW_ORIENTATION_MAX;
4785 static const int32_t RAW_DISTANCE_MIN;
4786 static const int32_t RAW_DISTANCE_MAX;
4787 static const int32_t RAW_TILT_MIN;
4788 static const int32_t RAW_TILT_MAX;
4789 static const int32_t RAW_ID_MIN;
4790 static const int32_t RAW_ID_MAX;
4791 static const int32_t RAW_SLOT_MIN;
4792 static const int32_t RAW_SLOT_MAX;
4793 static const float X_PRECISION;
4794 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004795 static const float X_PRECISION_VIRTUAL;
4796 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004797
4798 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004799 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
4801 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4802
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004803 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004804 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004805
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 enum Axes {
4807 POSITION = 1 << 0,
4808 TOUCH = 1 << 1,
4809 TOOL = 1 << 2,
4810 PRESSURE = 1 << 3,
4811 ORIENTATION = 1 << 4,
4812 MINOR = 1 << 5,
4813 ID = 1 << 6,
4814 DISTANCE = 1 << 7,
4815 TILT = 1 << 8,
4816 SLOT = 1 << 9,
4817 TOOL_TYPE = 1 << 10,
4818 };
4819
Michael Wrighta9cf4192022-12-01 23:46:39 +00004820 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004821 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004822 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004824 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004825 int32_t toRawX(float displayX);
4826 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004827 int32_t toRotatedRawX(float displayX);
4828 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004829 float toCookedX(float rawX, float rawY);
4830 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004831 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004832 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004833 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004834 float toDisplayY(int32_t rawY, int32_t displayHeight);
4835
Michael Wrightd02c5b62014-02-10 15:10:22 -08004836};
4837
4838const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4839const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4840const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4841const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4842const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4843const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4844const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4845const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004846const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4847const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4849const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4850const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4851const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4852const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4853const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4854const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4855const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4856const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4857const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4858const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4859const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004860const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4861 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4862const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4863 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004864const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4865 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004866
4867const float TouchInputMapperTest::GEOMETRIC_SCALE =
4868 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4869 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4870
4871const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4872 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4873 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4874};
4875
Michael Wrighta9cf4192022-12-01 23:46:39 +00004876void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004877 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4878 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004879}
4880
4881void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4882 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004883 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004884}
4885
Michael Wrighta9cf4192022-12-01 23:46:39 +00004886void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004887 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4888 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4889 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004890}
4891
Michael Wrightd02c5b62014-02-10 15:10:22 -08004892void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004893 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4894 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4895 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4896 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004897}
4898
Jason Gerecke489fda82012-09-07 17:19:40 -07004899void TouchInputMapperTest::prepareLocationCalibration() {
4900 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4901}
4902
Michael Wrightd02c5b62014-02-10 15:10:22 -08004903int32_t TouchInputMapperTest::toRawX(float displayX) {
4904 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4905}
4906
4907int32_t TouchInputMapperTest::toRawY(float displayY) {
4908 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4909}
4910
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004911int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4912 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4913}
4914
4915int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4916 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4917}
4918
Jason Gerecke489fda82012-09-07 17:19:40 -07004919float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4920 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4921 return rawX;
4922}
4923
4924float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4925 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4926 return rawY;
4927}
4928
Michael Wrightd02c5b62014-02-10 15:10:22 -08004929float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004930 return toDisplayX(rawX, DISPLAY_WIDTH);
4931}
4932
4933float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4934 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004935}
4936
4937float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004938 return toDisplayY(rawY, DISPLAY_HEIGHT);
4939}
4940
4941float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4942 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004943}
4944
4945
4946// --- SingleTouchInputMapperTest ---
4947
4948class SingleTouchInputMapperTest : public TouchInputMapperTest {
4949protected:
4950 void prepareButtons();
4951 void prepareAxes(int axes);
4952
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004953 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4954 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4955 void processUp(SingleTouchInputMapper& mappery);
4956 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4957 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4958 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4959 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4960 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4961 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962};
4963
4964void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004965 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966}
4967
4968void SingleTouchInputMapperTest::prepareAxes(int axes) {
4969 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4971 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004972 }
4973 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4975 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004976 }
4977 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004978 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4979 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004980 }
4981 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004982 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4983 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004984 }
4985 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004986 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4987 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 }
4989}
4990
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004991void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004995}
4996
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004997void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005000}
5001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005002void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005003 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005004}
5005
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005006void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005007 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005008}
5009
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005010void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5011 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005013}
5014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005015void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017}
5018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005019void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5020 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5022 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005023}
5024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005025void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5026 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005027 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028}
5029
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005030void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005031 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005032}
5033
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 prepareButtons();
5036 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005037 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005038
Josep del Río2d8c79a2023-01-23 19:33:50 +00005039 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040}
5041
Michael Wrightd02c5b62014-02-10 15:10:22 -08005042TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005043 prepareButtons();
5044 prepareAxes(POSITION);
5045 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005046 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005047
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005048 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005049}
5050
5051TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005053 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005054 prepareButtons();
5055 prepareAxes(POSITION);
5056 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005057 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005058
5059 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005060 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061
5062 // Virtual key is down.
5063 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5064 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5065 processDown(mapper, x, y);
5066 processSync(mapper);
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5068
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005069 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005070
5071 // Virtual key is up.
5072 processUp(mapper);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5075
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005076 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005077}
5078
5079TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005081 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005082 prepareButtons();
5083 prepareAxes(POSITION);
5084 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005085 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005086
5087 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005088 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089
5090 // Virtual key is down.
5091 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5092 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5093 processDown(mapper, x, y);
5094 processSync(mapper);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005097 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005098
5099 // Virtual key is up.
5100 processUp(mapper);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5103
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005104 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005105}
5106
5107TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005109 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005110 prepareButtons();
5111 prepareAxes(POSITION);
5112 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005113 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005114
Michael Wrightd02c5b62014-02-10 15:10:22 -08005115 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005116 ASSERT_TRUE(
5117 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005118 ASSERT_TRUE(flags[0]);
5119 ASSERT_FALSE(flags[1]);
5120}
5121
5122TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005123 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005124 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005125 prepareButtons();
5126 prepareAxes(POSITION);
5127 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005128 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005129
arthurhungdcef2dc2020-08-11 14:47:50 +08005130 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005131
5132 NotifyKeyArgs args;
5133
5134 // Press virtual key.
5135 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5136 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5137 processDown(mapper, x, y);
5138 processSync(mapper);
5139
5140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5141 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5142 ASSERT_EQ(DEVICE_ID, args.deviceId);
5143 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5144 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5145 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5146 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5147 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5148 ASSERT_EQ(KEY_HOME, args.scanCode);
5149 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5150 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5151
5152 // Release virtual key.
5153 processUp(mapper);
5154 processSync(mapper);
5155
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5157 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5158 ASSERT_EQ(DEVICE_ID, args.deviceId);
5159 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5160 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5161 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5162 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5163 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5164 ASSERT_EQ(KEY_HOME, args.scanCode);
5165 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5166 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5167
5168 // Should not have sent any motions.
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5170}
5171
5172TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005173 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005174 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005175 prepareButtons();
5176 prepareAxes(POSITION);
5177 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005178 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005179
arthurhungdcef2dc2020-08-11 14:47:50 +08005180 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005181
5182 NotifyKeyArgs keyArgs;
5183
5184 // Press virtual key.
5185 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5186 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5187 processDown(mapper, x, y);
5188 processSync(mapper);
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5191 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5192 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5193 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5194 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5195 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5196 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
5197 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5198 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5199 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5200 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5201
5202 // Move out of bounds. This should generate a cancel and a pointer down since we moved
5203 // into the display area.
5204 y -= 100;
5205 processMove(mapper, x, y);
5206 processSync(mapper);
5207
5208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5209 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
5210 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
5211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
5212 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
5213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5214 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
5215 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
5216 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
5217 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
5218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
5219 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
5220
5221 NotifyMotionArgs motionArgs;
5222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5224 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5225 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5226 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5227 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5228 ASSERT_EQ(0, motionArgs.flags);
5229 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5230 ASSERT_EQ(0, motionArgs.buttonState);
5231 ASSERT_EQ(0, motionArgs.edgeFlags);
5232 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5233 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5234 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5236 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5237 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5238 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5239 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5240
5241 // Keep moving out of bounds. Should generate a pointer move.
5242 y -= 50;
5243 processMove(mapper, x, y);
5244 processSync(mapper);
5245
5246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5247 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5248 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5249 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5250 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5251 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5252 ASSERT_EQ(0, motionArgs.flags);
5253 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5254 ASSERT_EQ(0, motionArgs.buttonState);
5255 ASSERT_EQ(0, motionArgs.edgeFlags);
5256 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5257 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5258 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5260 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5261 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5262 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5263 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5264
5265 // Release out of bounds. Should generate a pointer up.
5266 processUp(mapper);
5267 processSync(mapper);
5268
5269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5271 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5272 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5273 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5274 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5275 ASSERT_EQ(0, motionArgs.flags);
5276 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5277 ASSERT_EQ(0, motionArgs.buttonState);
5278 ASSERT_EQ(0, motionArgs.edgeFlags);
5279 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5280 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5281 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5282 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5283 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5284 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5285 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5287
5288 // Should not have sent any more keys or motions.
5289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5291}
5292
5293TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005295 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005296 prepareButtons();
5297 prepareAxes(POSITION);
5298 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005299 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005300
arthurhungdcef2dc2020-08-11 14:47:50 +08005301 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005302
5303 NotifyMotionArgs motionArgs;
5304
5305 // Initially go down out of bounds.
5306 int32_t x = -10;
5307 int32_t y = -10;
5308 processDown(mapper, x, y);
5309 processSync(mapper);
5310
5311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5312
5313 // Move into the display area. Should generate a pointer down.
5314 x = 50;
5315 y = 75;
5316 processMove(mapper, x, y);
5317 processSync(mapper);
5318
5319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5325 ASSERT_EQ(0, motionArgs.flags);
5326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5327 ASSERT_EQ(0, motionArgs.buttonState);
5328 ASSERT_EQ(0, motionArgs.edgeFlags);
5329 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5331 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5333 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5334 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5335 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5336 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5337
5338 // Release. Should generate a pointer up.
5339 processUp(mapper);
5340 processSync(mapper);
5341
5342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5344 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5345 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5346 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5347 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5348 ASSERT_EQ(0, motionArgs.flags);
5349 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5350 ASSERT_EQ(0, motionArgs.buttonState);
5351 ASSERT_EQ(0, motionArgs.edgeFlags);
5352 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5353 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5354 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5355 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5356 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5357 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5358 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5359 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5360
5361 // Should not have sent any more keys or motions.
5362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5364}
5365
Santos Cordonfa5cf462017-04-05 10:37:00 -07005366TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005367 addConfigurationProperty("touch.deviceType", "touchScreen");
5368 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
5369
Michael Wrighta9cf4192022-12-01 23:46:39 +00005370 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005371 prepareButtons();
5372 prepareAxes(POSITION);
5373 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005374 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07005375
arthurhungdcef2dc2020-08-11 14:47:50 +08005376 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005377
5378 NotifyMotionArgs motionArgs;
5379
5380 // Down.
5381 int32_t x = 100;
5382 int32_t y = 125;
5383 processDown(mapper, x, y);
5384 processSync(mapper);
5385
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5387 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5388 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5389 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5390 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5391 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5393 ASSERT_EQ(0, motionArgs.flags);
5394 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5395 ASSERT_EQ(0, motionArgs.buttonState);
5396 ASSERT_EQ(0, motionArgs.edgeFlags);
5397 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5398 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5399 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5400 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5401 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5402 1, 0, 0, 0, 0, 0, 0, 0));
5403 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5404 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5405 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5406
5407 // Move.
5408 x += 50;
5409 y += 75;
5410 processMove(mapper, x, y);
5411 processSync(mapper);
5412
5413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5414 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5415 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5416 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5417 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5418 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5419 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5420 ASSERT_EQ(0, motionArgs.flags);
5421 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5422 ASSERT_EQ(0, motionArgs.buttonState);
5423 ASSERT_EQ(0, motionArgs.edgeFlags);
5424 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5425 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5426 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5427 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5428 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5429 1, 0, 0, 0, 0, 0, 0, 0));
5430 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5431 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5432 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5433
5434 // Up.
5435 processUp(mapper);
5436 processSync(mapper);
5437
5438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5439 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5440 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5441 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
5442 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5443 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5444 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5445 ASSERT_EQ(0, motionArgs.flags);
5446 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5447 ASSERT_EQ(0, motionArgs.buttonState);
5448 ASSERT_EQ(0, motionArgs.edgeFlags);
5449 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5450 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5451 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5452 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5453 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
5454 1, 0, 0, 0, 0, 0, 0, 0));
5455 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
5456 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
5457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5458
5459 // Should not have sent any more keys or motions.
5460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5462}
5463
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005466 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 prepareButtons();
5468 prepareAxes(POSITION);
5469 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005470 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471
arthurhungdcef2dc2020-08-11 14:47:50 +08005472 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 NotifyMotionArgs motionArgs;
5475
5476 // Down.
5477 int32_t x = 100;
5478 int32_t y = 125;
5479 processDown(mapper, x, y);
5480 processSync(mapper);
5481
5482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5483 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5484 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5485 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5486 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5488 ASSERT_EQ(0, motionArgs.flags);
5489 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5490 ASSERT_EQ(0, motionArgs.buttonState);
5491 ASSERT_EQ(0, motionArgs.edgeFlags);
5492 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5494 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5496 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5497 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5498 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5499 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5500
5501 // Move.
5502 x += 50;
5503 y += 75;
5504 processMove(mapper, x, y);
5505 processSync(mapper);
5506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5508 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5509 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5510 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5511 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5512 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5513 ASSERT_EQ(0, motionArgs.flags);
5514 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5515 ASSERT_EQ(0, motionArgs.buttonState);
5516 ASSERT_EQ(0, motionArgs.edgeFlags);
5517 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5518 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5519 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5521 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5522 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5523 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5524 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5525
5526 // Up.
5527 processUp(mapper);
5528 processSync(mapper);
5529
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5531 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5532 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5533 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5534 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5535 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5536 ASSERT_EQ(0, motionArgs.flags);
5537 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5538 ASSERT_EQ(0, motionArgs.buttonState);
5539 ASSERT_EQ(0, motionArgs.edgeFlags);
5540 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
5541 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
5542 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
5543 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5544 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5545 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5546 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5547 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5548
5549 // Should not have sent any more keys or motions.
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5552}
5553
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005554TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555 addConfigurationProperty("touch.deviceType", "touchScreen");
5556 prepareButtons();
5557 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005558 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5559 // need to be rotated. Touchscreens are orientation-aware by default.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005560 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005561
5562 NotifyMotionArgs args;
5563
5564 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005565 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 processDown(mapper, toRawX(50), toRawY(75));
5567 processSync(mapper);
5568
5569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5570 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5571 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5572
5573 processUp(mapper);
5574 processSync(mapper);
5575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5576}
5577
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005578TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005579 addConfigurationProperty("touch.deviceType", "touchScreen");
5580 prepareButtons();
5581 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005582 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5583 // orientation-aware are affected by display rotation.
5584 addConfigurationProperty("touch.orientationAware", "0");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005585 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005586
5587 NotifyMotionArgs args;
5588
5589 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005590 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005591 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 processDown(mapper, toRawX(50), toRawY(75));
5593 processSync(mapper);
5594
5595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5596 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5597 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5598
5599 processUp(mapper);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5602
5603 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005604 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005605 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005606 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 processSync(mapper);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5610 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5611 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5612
5613 processUp(mapper);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5616
5617 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005618 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005619 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5621 processSync(mapper);
5622
5623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5624 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5625 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5626
5627 processUp(mapper);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5630
5631 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005632 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005633 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005634 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005635 processSync(mapper);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5638 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5639 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5640
5641 processUp(mapper);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5644}
5645
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005646TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5647 addConfigurationProperty("touch.deviceType", "touchScreen");
5648 prepareButtons();
5649 prepareAxes(POSITION);
5650 addConfigurationProperty("touch.orientationAware", "1");
5651 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5652 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005653 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005654 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5655 NotifyMotionArgs args;
5656
5657 // Orientation 0.
5658 processDown(mapper, toRawX(50), toRawY(75));
5659 processSync(mapper);
5660
5661 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5662 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5663 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5664
5665 processUp(mapper);
5666 processSync(mapper);
5667 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5668}
5669
5670TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5671 addConfigurationProperty("touch.deviceType", "touchScreen");
5672 prepareButtons();
5673 prepareAxes(POSITION);
5674 addConfigurationProperty("touch.orientationAware", "1");
5675 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5676 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005677 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005678 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5679 NotifyMotionArgs args;
5680
5681 // Orientation 90.
5682 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5683 processSync(mapper);
5684
5685 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5686 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5687 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5688
5689 processUp(mapper);
5690 processSync(mapper);
5691 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5692}
5693
5694TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5695 addConfigurationProperty("touch.deviceType", "touchScreen");
5696 prepareButtons();
5697 prepareAxes(POSITION);
5698 addConfigurationProperty("touch.orientationAware", "1");
5699 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5700 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005701 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005702 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5703 NotifyMotionArgs args;
5704
5705 // Orientation 180.
5706 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5707 processSync(mapper);
5708
5709 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5710 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5711 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5712
5713 processUp(mapper);
5714 processSync(mapper);
5715 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5716}
5717
5718TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5719 addConfigurationProperty("touch.deviceType", "touchScreen");
5720 prepareButtons();
5721 prepareAxes(POSITION);
5722 addConfigurationProperty("touch.orientationAware", "1");
5723 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5724 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005725 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005726 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5727 NotifyMotionArgs args;
5728
5729 // Orientation 270.
5730 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5731 processSync(mapper);
5732
5733 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5734 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5735 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5736
5737 processUp(mapper);
5738 processSync(mapper);
5739 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5740}
5741
5742TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5743 addConfigurationProperty("touch.deviceType", "touchScreen");
5744 prepareButtons();
5745 prepareAxes(POSITION);
5746 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5747 // orientation-aware are affected by display rotation.
5748 addConfigurationProperty("touch.orientationAware", "0");
5749 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5750 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5751
5752 NotifyMotionArgs args;
5753
5754 // Orientation 90, Rotation 0.
5755 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005756 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005757 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5758 processSync(mapper);
5759
5760 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5761 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5762 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5763
5764 processUp(mapper);
5765 processSync(mapper);
5766 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5767
5768 // Orientation 90, Rotation 90.
5769 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005770 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005771 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005772 processSync(mapper);
5773
5774 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5775 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5776 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5777
5778 processUp(mapper);
5779 processSync(mapper);
5780 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5781
5782 // Orientation 90, Rotation 180.
5783 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005784 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005785 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5786 processSync(mapper);
5787
5788 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5789 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5790 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5791
5792 processUp(mapper);
5793 processSync(mapper);
5794 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5795
5796 // Orientation 90, Rotation 270.
5797 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005798 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005799 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005800 processSync(mapper);
5801
5802 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5803 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5804 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5805
5806 processUp(mapper);
5807 processSync(mapper);
5808 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5809}
5810
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005811TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5812 addConfigurationProperty("touch.deviceType", "touchScreen");
5813 prepareButtons();
5814 prepareAxes(POSITION);
5815 addConfigurationProperty("touch.orientationAware", "1");
5816 prepareDisplay(ui::ROTATION_0);
5817 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
5818
5819 // Set a physical frame in the display viewport.
5820 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5821 viewport->physicalLeft = 20;
5822 viewport->physicalTop = 600;
5823 viewport->physicalRight = 30;
5824 viewport->physicalBottom = 610;
5825 mFakePolicy->updateViewport(*viewport);
5826 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
5827
5828 // Start the touch.
5829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5830 processSync(mapper);
5831
5832 // Expect all input starting outside the physical frame to be ignored.
5833 const std::array<Point, 6> outsidePoints = {
5834 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5835 for (const auto& p : outsidePoints) {
5836 processMove(mapper, toRawX(p.x), toRawY(p.y));
5837 processSync(mapper);
5838 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5839 }
5840
5841 // Move the touch into the physical frame.
5842 processMove(mapper, toRawX(25), toRawY(605));
5843 processSync(mapper);
5844 NotifyMotionArgs args;
5845 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5846 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5847 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5848 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5849
5850 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5851 for (const auto& p : outsidePoints) {
5852 processMove(mapper, toRawX(p.x), toRawY(p.y));
5853 processSync(mapper);
5854 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5855 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5856 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5857 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5858 }
5859
5860 processUp(mapper);
5861 processSync(mapper);
5862 EXPECT_NO_FATAL_FAILURE(
5863 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5864}
5865
Michael Wrightd02c5b62014-02-10 15:10:22 -08005866TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005868 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869 prepareButtons();
5870 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005871 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872
5873 // These calculations are based on the input device calibration documentation.
5874 int32_t rawX = 100;
5875 int32_t rawY = 200;
5876 int32_t rawPressure = 10;
5877 int32_t rawToolMajor = 12;
5878 int32_t rawDistance = 2;
5879 int32_t rawTiltX = 30;
5880 int32_t rawTiltY = 110;
5881
5882 float x = toDisplayX(rawX);
5883 float y = toDisplayY(rawY);
5884 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5885 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5886 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5887 float distance = float(rawDistance);
5888
5889 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5890 float tiltScale = M_PI / 180;
5891 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5892 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5893 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5894 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5895
5896 processDown(mapper, rawX, rawY);
5897 processPressure(mapper, rawPressure);
5898 processToolMajor(mapper, rawToolMajor);
5899 processDistance(mapper, rawDistance);
5900 processTilt(mapper, rawTiltX, rawTiltY);
5901 processSync(mapper);
5902
5903 NotifyMotionArgs args;
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5905 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5906 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5907 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5908}
5909
Jason Gerecke489fda82012-09-07 17:19:40 -07005910TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005912 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005913 prepareLocationCalibration();
5914 prepareButtons();
5915 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005916 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005917
5918 int32_t rawX = 100;
5919 int32_t rawY = 200;
5920
5921 float x = toDisplayX(toCookedX(rawX, rawY));
5922 float y = toDisplayY(toCookedY(rawX, rawY));
5923
5924 processDown(mapper, rawX, rawY);
5925 processSync(mapper);
5926
5927 NotifyMotionArgs args;
5928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5930 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5931}
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005935 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 prepareButtons();
5937 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005938 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939
5940 NotifyMotionArgs motionArgs;
5941 NotifyKeyArgs keyArgs;
5942
5943 processDown(mapper, 100, 200);
5944 processSync(mapper);
5945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5946 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5947 ASSERT_EQ(0, motionArgs.buttonState);
5948
5949 // press BTN_LEFT, release BTN_LEFT
5950 processKey(mapper, BTN_LEFT, 1);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5954 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5955
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5957 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5958 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5959
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960 processKey(mapper, BTN_LEFT, 0);
5961 processSync(mapper);
5962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005964 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005965
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005967 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005968 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5971 processKey(mapper, BTN_RIGHT, 1);
5972 processKey(mapper, BTN_MIDDLE, 1);
5973 processSync(mapper);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5975 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5976 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5977 motionArgs.buttonState);
5978
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5981 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5982
5983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5984 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5985 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5986 motionArgs.buttonState);
5987
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988 processKey(mapper, BTN_RIGHT, 0);
5989 processSync(mapper);
5990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005992 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005993
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005996 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 processKey(mapper, BTN_MIDDLE, 0);
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006001 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006002 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006006 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007
6008 // press BTN_BACK, release BTN_BACK
6009 processKey(mapper, BTN_BACK, 1);
6010 processSync(mapper);
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6012 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6013 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006014
Michael Wrightd02c5b62014-02-10 15:10:22 -08006015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006016 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006017 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6018
6019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6020 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6021 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022
6023 processKey(mapper, BTN_BACK, 0);
6024 processSync(mapper);
6025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006027 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006028
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006031 ASSERT_EQ(0, motionArgs.buttonState);
6032
Michael Wrightd02c5b62014-02-10 15:10:22 -08006033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6034 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6035 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6036
6037 // press BTN_SIDE, release BTN_SIDE
6038 processKey(mapper, BTN_SIDE, 1);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6041 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6042 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006043
Michael Wrightd02c5b62014-02-10 15:10:22 -08006044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006046 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6047
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6050 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051
6052 processKey(mapper, BTN_SIDE, 0);
6053 processSync(mapper);
6054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006056 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006057
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006059 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006060 ASSERT_EQ(0, motionArgs.buttonState);
6061
Michael Wrightd02c5b62014-02-10 15:10:22 -08006062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6064 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6065
6066 // press BTN_FORWARD, release BTN_FORWARD
6067 processKey(mapper, BTN_FORWARD, 1);
6068 processSync(mapper);
6069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6070 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6071 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006072
Michael Wrightd02c5b62014-02-10 15:10:22 -08006073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006075 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6076
6077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6079 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 processKey(mapper, BTN_FORWARD, 0);
6082 processSync(mapper);
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006084 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006085 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006089 ASSERT_EQ(0, motionArgs.buttonState);
6090
Michael Wrightd02c5b62014-02-10 15:10:22 -08006091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6092 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6093 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6094
6095 // press BTN_EXTRA, release BTN_EXTRA
6096 processKey(mapper, BTN_EXTRA, 1);
6097 processSync(mapper);
6098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6099 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6100 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006101
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006103 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006104 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6105
6106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6108 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006109
6110 processKey(mapper, BTN_EXTRA, 0);
6111 processSync(mapper);
6112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006115
6116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006117 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006118 ASSERT_EQ(0, motionArgs.buttonState);
6119
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6121 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6122 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6123
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6125
Michael Wrightd02c5b62014-02-10 15:10:22 -08006126 // press BTN_STYLUS, release BTN_STYLUS
6127 processKey(mapper, BTN_STYLUS, 1);
6128 processSync(mapper);
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006131 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6132
6133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6135 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006136
6137 processKey(mapper, BTN_STYLUS, 0);
6138 processSync(mapper);
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006142
6143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006146
6147 // press BTN_STYLUS2, release BTN_STYLUS2
6148 processKey(mapper, BTN_STYLUS2, 1);
6149 processSync(mapper);
6150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006152 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
6153
6154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6156 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006157
6158 processKey(mapper, BTN_STYLUS2, 0);
6159 processSync(mapper);
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006163
6164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006166 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006167
6168 // release touch
6169 processUp(mapper);
6170 processSync(mapper);
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6172 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6173 ASSERT_EQ(0, motionArgs.buttonState);
6174}
6175
6176TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006177 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006178 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 prepareButtons();
6180 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006181 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006182
6183 NotifyMotionArgs motionArgs;
6184
6185 // default tool type is finger
6186 processDown(mapper, 100, 200);
6187 processSync(mapper);
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6189 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6190 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6191
6192 // eraser
6193 processKey(mapper, BTN_TOOL_RUBBER, 1);
6194 processSync(mapper);
6195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6196 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6197 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6198
6199 // stylus
6200 processKey(mapper, BTN_TOOL_RUBBER, 0);
6201 processKey(mapper, BTN_TOOL_PEN, 1);
6202 processSync(mapper);
6203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6204 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6205 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6206
6207 // brush
6208 processKey(mapper, BTN_TOOL_PEN, 0);
6209 processKey(mapper, BTN_TOOL_BRUSH, 1);
6210 processSync(mapper);
6211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6213 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6214
6215 // pencil
6216 processKey(mapper, BTN_TOOL_BRUSH, 0);
6217 processKey(mapper, BTN_TOOL_PENCIL, 1);
6218 processSync(mapper);
6219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6221 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6222
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08006223 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08006224 processKey(mapper, BTN_TOOL_PENCIL, 0);
6225 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
6226 processSync(mapper);
6227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6229 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6230
6231 // mouse
6232 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
6233 processKey(mapper, BTN_TOOL_MOUSE, 1);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6236 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6237 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6238
6239 // lens
6240 processKey(mapper, BTN_TOOL_MOUSE, 0);
6241 processKey(mapper, BTN_TOOL_LENS, 1);
6242 processSync(mapper);
6243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6245 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6246
6247 // double-tap
6248 processKey(mapper, BTN_TOOL_LENS, 0);
6249 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
6250 processSync(mapper);
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6252 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6253 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6254
6255 // triple-tap
6256 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
6257 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
6258 processSync(mapper);
6259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6261 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6262
6263 // quad-tap
6264 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
6265 processKey(mapper, BTN_TOOL_QUADTAP, 1);
6266 processSync(mapper);
6267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6269 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6270
6271 // finger
6272 processKey(mapper, BTN_TOOL_QUADTAP, 0);
6273 processKey(mapper, BTN_TOOL_FINGER, 1);
6274 processSync(mapper);
6275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6276 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6277 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6278
6279 // stylus trumps finger
6280 processKey(mapper, BTN_TOOL_PEN, 1);
6281 processSync(mapper);
6282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6283 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6284 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
6285
6286 // eraser trumps stylus
6287 processKey(mapper, BTN_TOOL_RUBBER, 1);
6288 processSync(mapper);
6289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6290 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6291 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
6292
6293 // mouse trumps eraser
6294 processKey(mapper, BTN_TOOL_MOUSE, 1);
6295 processSync(mapper);
6296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6298 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
6299
6300 // back to default tool type
6301 processKey(mapper, BTN_TOOL_MOUSE, 0);
6302 processKey(mapper, BTN_TOOL_RUBBER, 0);
6303 processKey(mapper, BTN_TOOL_PEN, 0);
6304 processKey(mapper, BTN_TOOL_FINGER, 0);
6305 processSync(mapper);
6306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6308 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
6309}
6310
6311TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006312 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006313 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006314 prepareButtons();
6315 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006316 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006317 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006318
6319 NotifyMotionArgs motionArgs;
6320
6321 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
6322 processKey(mapper, BTN_TOOL_FINGER, 1);
6323 processMove(mapper, 100, 200);
6324 processSync(mapper);
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6327 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6328 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6329
6330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6331 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6333 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6334
6335 // move a little
6336 processMove(mapper, 150, 250);
6337 processSync(mapper);
6338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6339 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6342
6343 // down when BTN_TOUCH is pressed, pressure defaults to 1
6344 processKey(mapper, BTN_TOUCH, 1);
6345 processSync(mapper);
6346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6348 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6349 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6350
6351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6353 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6354 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6355
6356 // up when BTN_TOUCH is released, hover restored
6357 processKey(mapper, BTN_TOUCH, 0);
6358 processSync(mapper);
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6360 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6363
6364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6365 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6367 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6368
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6370 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6371 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6372 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6373
6374 // exit hover when pointer goes away
6375 processKey(mapper, BTN_TOOL_FINGER, 0);
6376 processSync(mapper);
6377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6378 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6379 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6380 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6381}
6382
6383TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006385 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 prepareButtons();
6387 prepareAxes(POSITION | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006388 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006389
6390 NotifyMotionArgs motionArgs;
6391
6392 // initially hovering because pressure is 0
6393 processDown(mapper, 100, 200);
6394 processPressure(mapper, 0);
6395 processSync(mapper);
6396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6399 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6400
6401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6402 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6404 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
6405
6406 // move a little
6407 processMove(mapper, 150, 250);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6412 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6413
6414 // down when pressure is non-zero
6415 processPressure(mapper, RAW_PRESSURE_MAX);
6416 processSync(mapper);
6417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6418 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6421
6422 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6423 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6424 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6425 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6426
6427 // up when pressure becomes 0, hover restored
6428 processPressure(mapper, 0);
6429 processSync(mapper);
6430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6431 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6433 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
6434
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6436 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
6437 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6438 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6439
6440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6441 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
6442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6443 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6444
6445 // exit hover when pointer goes away
6446 processUp(mapper);
6447 processSync(mapper);
6448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6449 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
6450 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6451 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
6452}
6453
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006454TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
6455 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006456 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006457 prepareButtons();
6458 prepareAxes(POSITION | PRESSURE);
6459 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6460
6461 // Touch down.
6462 processDown(mapper, 100, 200);
6463 processPressure(mapper, 1);
6464 processSync(mapper);
6465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6466 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
6467
6468 // Reset the mapper. This should cancel the ongoing gesture.
6469 resetMapper(mapper, ARBITRARY_TIME);
6470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6471 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
6472
6473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6474}
6475
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006476TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
6477 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006478 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006479 prepareButtons();
6480 prepareAxes(POSITION | PRESSURE);
6481 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6482
6483 // Set the initial state for the touch pointer.
6484 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
6485 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
6486 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
6487 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6488
6489 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00006490 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
6491 // does not generate any events.
6492 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006493
6494 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
6495 // the recreated touch state to generate a down event.
6496 processSync(mapper);
6497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6499
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6501}
6502
lilinnan687e58f2022-07-19 16:00:50 +08006503TEST_F(SingleTouchInputMapperTest,
6504 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6505 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006506 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006507 prepareButtons();
6508 prepareAxes(POSITION);
6509 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6510 NotifyMotionArgs motionArgs;
6511
6512 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006513 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006514 processSync(mapper);
6515
6516 // We should receive a down event
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6518 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6519
6520 // Change display id
6521 clearViewports();
6522 prepareSecondaryDisplay(ViewportType::INTERNAL);
6523
6524 // We should receive a cancel event
6525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6526 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6527 // Then receive reset called
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6529}
6530
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006531TEST_F(SingleTouchInputMapperTest,
6532 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006534 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006535 prepareButtons();
6536 prepareAxes(POSITION);
6537 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6539 NotifyMotionArgs motionArgs;
6540
6541 // Start a new gesture.
6542 processDown(mapper, 100, 200);
6543 processSync(mapper);
6544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6546
6547 // Make the viewport inactive. This will put the device in disabled mode.
6548 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6549 viewport->isActive = false;
6550 mFakePolicy->updateViewport(*viewport);
6551 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6552
6553 // We should receive a cancel event for the ongoing gesture.
6554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6555 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6556 // Then we should be notified that the device was reset.
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6558
6559 // No events are generated while the viewport is inactive.
6560 processMove(mapper, 101, 201);
6561 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006562 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6565
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006566 // Start a new gesture while the viewport is still inactive.
6567 processDown(mapper, 300, 400);
6568 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6569 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6570 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6571 processSync(mapper);
6572
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006573 // Make the viewport active again. The device should resume processing events.
6574 viewport->isActive = true;
6575 mFakePolicy->updateViewport(*viewport);
6576 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6577
6578 // The device is reset because it changes back to direct mode, without generating any events.
6579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6581
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006582 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006583 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6585 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006586
6587 // No more events.
6588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6590}
6591
Prabir Pradhan211ba622022-10-31 21:09:21 +00006592TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6593 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006594 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006595 prepareButtons();
6596 prepareAxes(POSITION);
6597 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6599
6600 // Press a stylus button.
6601 processKey(mapper, BTN_STYLUS, 1);
6602 processSync(mapper);
6603
6604 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6605 processDown(mapper, 100, 200);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6609 WithCoords(toDisplayX(100), toDisplayY(200)),
6610 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6613 WithCoords(toDisplayX(100), toDisplayY(200)),
6614 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6615
6616 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6617 // the button has not actually been released, since there will be no pointers through which the
6618 // button state can be reported. The event is generated at the location of the pointer before
6619 // it went up.
6620 processUp(mapper);
6621 processSync(mapper);
6622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6624 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6626 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6627 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6628}
6629
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006630TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6631 addConfigurationProperty("touch.deviceType", "touchScreen");
6632 prepareDisplay(ui::ROTATION_0);
6633 prepareButtons();
6634 prepareAxes(POSITION);
6635
6636 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6637
6638 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6640
6641 // Press a stylus button.
6642 processKey(mapper, BTN_STYLUS, 1);
6643 processSync(mapper);
6644
6645 // Start a touch gesture and ensure that the stylus button is not reported.
6646 processDown(mapper, 100, 200);
6647 processSync(mapper);
6648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6649 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6650
6651 // Release and press the stylus button again.
6652 processKey(mapper, BTN_STYLUS, 0);
6653 processSync(mapper);
6654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6655 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6656 processKey(mapper, BTN_STYLUS, 1);
6657 processSync(mapper);
6658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6659 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6660
6661 // Release the touch gesture.
6662 processUp(mapper);
6663 processSync(mapper);
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6665 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6666
6667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6668}
6669
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006670TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6671 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6672 prepareDisplay(ui::ROTATION_0);
6673 prepareButtons();
6674 prepareAxes(POSITION);
6675 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6677
6678 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6679}
6680
Seunghwan Choi356026c2023-02-01 14:37:25 +09006681TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6682 std::shared_ptr<FakePointerController> fakePointerController =
6683 std::make_shared<FakePointerController>();
6684 addConfigurationProperty("touch.deviceType", "touchScreen");
6685 prepareDisplay(ui::ROTATION_0);
6686 prepareButtons();
6687 prepareAxes(POSITION);
6688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6689 mFakePolicy->setPointerController(fakePointerController);
6690 mFakePolicy->setStylusPointerIconEnabled(true);
6691 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6692
6693 processKey(mapper, BTN_TOOL_PEN, 1);
6694 processMove(mapper, 100, 200);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6697 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6698 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6699 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6700 ASSERT_TRUE(fakePointerController->isPointerShown());
6701 ASSERT_NO_FATAL_FAILURE(
6702 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6703}
6704
6705TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6706 std::shared_ptr<FakePointerController> fakePointerController =
6707 std::make_shared<FakePointerController>();
6708 addConfigurationProperty("touch.deviceType", "touchScreen");
6709 prepareDisplay(ui::ROTATION_0);
6710 prepareButtons();
6711 prepareAxes(POSITION);
6712 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6713 mFakePolicy->setPointerController(fakePointerController);
6714 mFakePolicy->setStylusPointerIconEnabled(false);
6715 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6716
6717 processKey(mapper, BTN_TOOL_PEN, 1);
6718 processMove(mapper, 100, 200);
6719 processSync(mapper);
6720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6721 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
6722 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
6723 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6724 ASSERT_FALSE(fakePointerController->isPointerShown());
6725}
6726
Prabir Pradhan5632d622021-09-06 07:57:20 -07006727// --- TouchDisplayProjectionTest ---
6728
6729class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6730public:
6731 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6732 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6733 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006734 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6735 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6736 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006737 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006738 auto rotatedWidth = naturalDisplayWidth;
6739 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006740 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006741 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006742 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006743 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006744 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006745 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006746 inverseRotationFlags = ui::Transform::ROT_180;
6747 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006748 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006749 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006750 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006751 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006752 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006753 inverseRotationFlags = ui::Transform::ROT_0;
6754 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006755 }
6756
Prabir Pradhana9df3162022-12-05 23:57:27 +00006757 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006758 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6759
6760 std::optional<DisplayViewport> internalViewport =
6761 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6762 DisplayViewport& v = *internalViewport;
6763 v.displayId = DISPLAY_ID;
6764 v.orientation = orientation;
6765
6766 v.logicalLeft = 0;
6767 v.logicalTop = 0;
6768 v.logicalRight = 100;
6769 v.logicalBottom = 100;
6770
6771 v.physicalLeft = rotatedPhysicalDisplay.left;
6772 v.physicalTop = rotatedPhysicalDisplay.top;
6773 v.physicalRight = rotatedPhysicalDisplay.right;
6774 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6775
Prabir Pradhana9df3162022-12-05 23:57:27 +00006776 v.deviceWidth = rotatedWidth;
6777 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006778
6779 v.isActive = true;
6780 v.uniqueId = UNIQUE_ID;
6781 v.type = ViewportType::INTERNAL;
6782 mFakePolicy->updateViewport(v);
6783 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
6784 }
6785
6786 void assertReceivedMove(const Point& point) {
6787 NotifyMotionArgs motionArgs;
6788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6790 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6791 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6792 1, 0, 0, 0, 0, 0, 0, 0));
6793 }
6794};
6795
6796TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6797 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006798 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006799
6800 prepareButtons();
6801 prepareAxes(POSITION);
6802 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6803
6804 NotifyMotionArgs motionArgs;
6805
6806 // Configure the DisplayViewport such that the logical display maps to a subsection of
6807 // the display panel called the physical display. Here, the physical display is bounded by the
6808 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6809 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6810 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6811 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6812
Michael Wrighta9cf4192022-12-01 23:46:39 +00006813 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006814 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6815
6816 // Touches outside the physical display should be ignored, and should not generate any
6817 // events. Ensure touches at the following points that lie outside of the physical display
6818 // area do not generate any events.
6819 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6820 processDown(mapper, toRawX(point.x), toRawY(point.y));
6821 processSync(mapper);
6822 processUp(mapper);
6823 processSync(mapper);
6824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6825 << "Unexpected event generated for touch outside physical display at point: "
6826 << point.x << ", " << point.y;
6827 }
6828 }
6829}
6830
6831TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6832 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006833 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006834
6835 prepareButtons();
6836 prepareAxes(POSITION);
6837 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6838
6839 NotifyMotionArgs motionArgs;
6840
6841 // Configure the DisplayViewport such that the logical display maps to a subsection of
6842 // the display panel called the physical display. Here, the physical display is bounded by the
6843 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6844 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6845
Michael Wrighta9cf4192022-12-01 23:46:39 +00006846 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006847 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6848
6849 // Touches that start outside the physical display should be ignored until it enters the
6850 // physical display bounds, at which point it should generate a down event. Start a touch at
6851 // the point (5, 100), which is outside the physical display bounds.
6852 static const Point kOutsidePoint{5, 100};
6853 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6854 processSync(mapper);
6855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6856
6857 // Move the touch into the physical display area. This should generate a pointer down.
6858 processMove(mapper, toRawX(11), toRawY(21));
6859 processSync(mapper);
6860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6862 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
6863 ASSERT_NO_FATAL_FAILURE(
6864 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6865
6866 // Move the touch inside the physical display area. This should generate a pointer move.
6867 processMove(mapper, toRawX(69), toRawY(159));
6868 processSync(mapper);
6869 assertReceivedMove({69, 159});
6870
6871 // Move outside the physical display area. Since the pointer is already down, this should
6872 // now continue generating events.
6873 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6874 processSync(mapper);
6875 assertReceivedMove(kOutsidePoint);
6876
6877 // Release. This should generate a pointer up.
6878 processUp(mapper);
6879 processSync(mapper);
6880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6881 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6883 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6884
6885 // Ensure no more events were generated.
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6888 }
6889}
6890
Prabir Pradhana9df3162022-12-05 23:57:27 +00006891// --- TouchscreenPrecisionTests ---
6892
6893// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6894// in various orientations and with different display rotations. We configure the touchscreen to
6895// have a higher resolution than that of the display by an integer scale factor in each axis so that
6896// we can enforce that coordinates match precisely as expected.
6897class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6898 public ::testing::WithParamInterface<ui::Rotation> {
6899public:
6900 void SetUp() override {
6901 SingleTouchInputMapperTest::SetUp();
6902
6903 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6904 // four times the resolution of the display in the Y axis.
6905 prepareButtons();
6906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
6907 0, 0);
6908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
6909 0, 0);
6910 }
6911
6912 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6913 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6914 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6915 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6916
6917 static const std::array<Point, 4> kRawCorners;
6918};
6919
6920const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6921 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6922 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6923 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6924 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6925}};
6926
6927// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6928// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6929// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6930TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6931 enum class Orientation {
6932 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6933 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6934 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6935 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6936 ftl_last = ORIENTATION_270,
6937 };
6938 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6939 Orientation::ORIENTATION_270;
6940 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6941 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6942 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6943 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6944 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6945 };
6946
6947 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6948
6949 // Configure the touchscreen as being installed in the one of the four different orientations
6950 // relative to the display.
6951 addConfigurationProperty("touch.deviceType", "touchScreen");
6952 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6953 prepareDisplay(ui::ROTATION_0);
6954
6955 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
6956
6957 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6958 // orientations of either 90 or 270) this means the display's natural resolution will be
6959 // flipped.
6960 const bool displayRotated =
6961 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6962 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6963 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6964 const Rect physicalFrame{0, 0, width, height};
6965 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6966
6967 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6968 const float expectedPrecisionX = displayRotated ? 4 : 2;
6969 const float expectedPrecisionY = displayRotated ? 2 : 4;
6970
6971 // Test all four corners.
6972 for (int i = 0; i < 4; i++) {
6973 const auto& raw = kRawCorners[i];
6974 processDown(mapper, raw.x, raw.y);
6975 processSync(mapper);
6976 const auto& expected = expectedPoints[i];
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6978 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6979 WithCoords(expected.x, expected.y),
6980 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6981 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6982 << "with touchscreen orientation "
6983 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6984 << expected.x << ", " << expected.y << ").";
6985 processUp(mapper);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6988 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6989 WithCoords(expected.x, expected.y))));
6990 }
6991}
6992
Prabir Pradhan82687402022-12-06 01:32:53 +00006993TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6994 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6995 kMappedCorners = {
6996 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6997 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6998 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6999 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7000 };
7001
7002 const ui::Rotation displayRotation = GetParam();
7003
7004 addConfigurationProperty("touch.deviceType", "touchScreen");
7005 prepareDisplay(displayRotation);
7006
7007 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7008
7009 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7010
7011 // Test all four corners.
7012 for (int i = 0; i < 4; i++) {
7013 const auto& expected = expectedPoints[i];
7014 const auto& raw = kRawCorners[i];
7015 processDown(mapper, raw.x, raw.y);
7016 processSync(mapper);
7017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7019 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7020 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7021 << "with display rotation " << ui::toCString(displayRotation)
7022 << ", expected point (" << expected.x << ", " << expected.y << ").";
7023 processUp(mapper);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7026 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7027 WithCoords(expected.x, expected.y))));
7028 }
7029}
7030
Prabir Pradhan3e798762022-12-02 21:02:11 +00007031TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7032 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7033 kMappedCorners = {
7034 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7035 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7036 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7037 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7038 };
7039
7040 const ui::Rotation displayRotation = GetParam();
7041
7042 addConfigurationProperty("touch.deviceType", "touchScreen");
7043 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7044
7045 SingleTouchInputMapper& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7046
7047 // Ori 270, so width and height swapped
7048 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7049 prepareDisplay(displayRotation);
7050 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7051
7052 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7053
7054 // Test all four corners.
7055 for (int i = 0; i < 4; i++) {
7056 const auto& expected = expectedPoints[i];
7057 const auto& raw = kRawCorners[i];
7058 processDown(mapper, raw.x, raw.y);
7059 processSync(mapper);
7060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7061 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7062 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7063 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7064 << "with display rotation " << ui::toCString(displayRotation)
7065 << ", expected point (" << expected.x << ", " << expected.y << ").";
7066 processUp(mapper);
7067 processSync(mapper);
7068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7069 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7070 WithCoords(expected.x, expected.y))));
7071 }
7072}
7073
Prabir Pradhana9df3162022-12-05 23:57:27 +00007074// Run the precision tests for all rotations.
7075INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
7076 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
7077 ui::ROTATION_270),
7078 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
7079 return ftl::enum_string(testParamInfo.param);
7080 });
7081
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007082// --- ExternalStylusFusionTest ---
7083
7084class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
7085public:
7086 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
7087 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007088 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007089 prepareButtons();
7090 prepareAxes(POSITION);
7091 auto& mapper = addMapperAndConfigure<SingleTouchInputMapper>();
7092
7093 mStylusState.when = ARBITRARY_TIME;
7094 mStylusState.pressure = 0.f;
7095 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7096 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
7097 configureDevice(InputReaderConfiguration::CHANGE_EXTERNAL_STYLUS_PRESENCE);
7098 processExternalStylusState(mapper);
7099 return mapper;
7100 }
7101
7102 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
7103 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
7104 for (const NotifyArgs& args : generatedArgs) {
7105 mFakeListener->notify(args);
7106 }
7107 // Loop the reader to flush the input listener queue.
7108 mReader->loopOnce();
7109 return generatedArgs;
7110 }
7111
7112protected:
7113 StylusState mStylusState{};
7114 static constexpr uint32_t EXPECTED_SOURCE =
7115 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
7116
7117 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
7118 auto toolTypeSource =
7119 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7120
7121 // The first pointer is withheld.
7122 processDown(mapper, 100, 200);
7123 processSync(mapper);
7124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7125 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7126 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7127
7128 // The external stylus reports pressure. The withheld finger pointer is released as a
7129 // stylus.
7130 mStylusState.pressure = 1.f;
7131 processExternalStylusState(mapper);
7132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7133 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7134 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7135
7136 // Subsequent pointer events are not withheld.
7137 processMove(mapper, 101, 201);
7138 processSync(mapper);
7139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7140 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7141
7142 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7144 }
7145
7146 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7147 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7148
7149 // Releasing the touch pointer ends the gesture.
7150 processUp(mapper);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7154 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7155
7156 mStylusState.pressure = 0.f;
7157 processExternalStylusState(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7160 }
7161
7162 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
7163 auto toolTypeSource =
7164 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER));
7165
7166 // The first pointer is withheld when an external stylus is connected,
7167 // and a timeout is requested.
7168 processDown(mapper, 100, 200);
7169 processSync(mapper);
7170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7171 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7172 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7173
7174 // If the timeout expires early, it is requested again.
7175 handleTimeout(mapper, ARBITRARY_TIME + 1);
7176 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
7177 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
7178
7179 // When the timeout expires, the withheld touch is released as a finger pointer.
7180 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
7181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7182 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7183
7184 // Subsequent pointer events are not withheld.
7185 processMove(mapper, 101, 201);
7186 processSync(mapper);
7187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7188 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7189 processUp(mapper);
7190 processSync(mapper);
7191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7192 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7193
7194 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7196 }
7197
7198private:
7199 InputDeviceInfo mExternalStylusDeviceInfo{};
7200};
7201
7202TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
7203 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7204 ASSERT_EQ(EXPECTED_SOURCE, mapper.getSources());
7205}
7206
7207TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
7208 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7209 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7210}
7211
7212TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
7213 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7214 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7215}
7216
7217// Test a successful stylus fusion gesture where the pressure is reported by the external
7218// before the touch is reported by the touchscreen.
7219TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
7220 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7221 auto toolTypeSource =
7222 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7223
7224 // The external stylus reports pressure first. It is ignored for now.
7225 mStylusState.pressure = 1.f;
7226 processExternalStylusState(mapper);
7227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7228 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7229
7230 // When the touch goes down afterwards, it is reported as a stylus pointer.
7231 processDown(mapper, 100, 200);
7232 processSync(mapper);
7233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7234 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
7235 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7236
7237 processMove(mapper, 101, 201);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7240 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
7241 processUp(mapper);
7242 processSync(mapper);
7243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7244 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
7245
7246 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7248}
7249
7250TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
7251 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7252
7253 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7254 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7255
7256 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7257 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
7258 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7259 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
7260}
7261
7262TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
7263 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7264 auto toolTypeSource =
7265 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7266
7267 mStylusState.pressure = 0.8f;
7268 processExternalStylusState(mapper);
7269 processDown(mapper, 100, 200);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7272 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7273 WithPressure(0.8f))));
7274 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7275
7276 // The external stylus reports a pressure change. We wait for some time for a touch event.
7277 mStylusState.pressure = 0.6f;
7278 processExternalStylusState(mapper);
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7280 ASSERT_NO_FATAL_FAILURE(
7281 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7282
7283 // If a touch is reported within the timeout, it reports the updated pressure.
7284 processMove(mapper, 101, 201);
7285 processSync(mapper);
7286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7287 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7288 WithPressure(0.6f))));
7289 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7290
7291 // There is another pressure change.
7292 mStylusState.pressure = 0.5f;
7293 processExternalStylusState(mapper);
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7295 ASSERT_NO_FATAL_FAILURE(
7296 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7297
7298 // If a touch is not reported within the timeout, a move event is generated to report
7299 // the new pressure.
7300 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7302 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7303 WithPressure(0.5f))));
7304
7305 // If a zero pressure is reported before the touch goes up, the previous pressure value is
7306 // repeated indefinitely.
7307 mStylusState.pressure = 0.0f;
7308 processExternalStylusState(mapper);
7309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7310 ASSERT_NO_FATAL_FAILURE(
7311 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7312 processMove(mapper, 102, 202);
7313 processSync(mapper);
7314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7315 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7316 WithPressure(0.5f))));
7317 processMove(mapper, 103, 203);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7320 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7321 WithPressure(0.5f))));
7322
7323 processUp(mapper);
7324 processSync(mapper);
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7326 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(EXPECTED_SOURCE),
7327 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7328
7329 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7331}
7332
7333TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
7334 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7335 auto source = WithSource(EXPECTED_SOURCE);
7336
7337 mStylusState.pressure = 1.f;
7338 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_ERASER;
7339 processExternalStylusState(mapper);
7340 processDown(mapper, 100, 200);
7341 processSync(mapper);
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7343 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7344 WithToolType(AMOTION_EVENT_TOOL_TYPE_ERASER))));
7345 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7346
7347 // The external stylus reports a tool change. We wait for some time for a touch event.
7348 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_STYLUS;
7349 processExternalStylusState(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7351 ASSERT_NO_FATAL_FAILURE(
7352 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7353
7354 // If a touch is reported within the timeout, it reports the updated pressure.
7355 processMove(mapper, 101, 201);
7356 processSync(mapper);
7357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7358 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7359 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
7360 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7361
7362 // There is another tool type change.
7363 mStylusState.toolType = AMOTION_EVENT_TOOL_TYPE_FINGER;
7364 processExternalStylusState(mapper);
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7366 ASSERT_NO_FATAL_FAILURE(
7367 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7368
7369 // If a touch is not reported within the timeout, a move event is generated to report
7370 // the new tool type.
7371 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7373 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7374 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7375
7376 processUp(mapper);
7377 processSync(mapper);
7378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7379 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
7380 WithToolType(AMOTION_EVENT_TOOL_TYPE_FINGER))));
7381
7382 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7384}
7385
7386TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7387 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
7388 auto toolTypeSource =
7389 AllOf(WithSource(EXPECTED_SOURCE), WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS));
7390
7391 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7392
7393 // The external stylus reports a button change. We wait for some time for a touch event.
7394 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7395 processExternalStylusState(mapper);
7396 ASSERT_NO_FATAL_FAILURE(
7397 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7398
7399 // If a touch is reported within the timeout, it reports the updated button state.
7400 processMove(mapper, 101, 201);
7401 processSync(mapper);
7402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7403 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7404 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7406 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7407 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7408 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7409
7410 // The button is now released.
7411 mStylusState.buttons = 0;
7412 processExternalStylusState(mapper);
7413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7414 ASSERT_NO_FATAL_FAILURE(
7415 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7416
7417 // If a touch is not reported within the timeout, a move event is generated to report
7418 // the new button state.
7419 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7421 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7422 WithButtonState(0))));
7423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007424 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7425 WithButtonState(0))));
7426
7427 processUp(mapper);
7428 processSync(mapper);
7429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007430 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7431
7432 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7434}
7435
Michael Wrightd02c5b62014-02-10 15:10:22 -08007436// --- MultiTouchInputMapperTest ---
7437
7438class MultiTouchInputMapperTest : public TouchInputMapperTest {
7439protected:
7440 void prepareAxes(int axes);
7441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007442 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7443 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7444 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7445 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7446 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7447 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7448 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7449 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7450 void processId(MultiTouchInputMapper& mapper, int32_t id);
7451 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7452 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7453 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007454 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007455 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007456 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7457 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007458};
7459
7460void MultiTouchInputMapperTest::prepareAxes(int axes) {
7461 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7463 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007464 }
7465 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007466 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7467 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007468 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007469 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7470 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 }
7472 }
7473 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7475 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007477 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007478 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007479 }
7480 }
7481 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007482 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7483 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007484 }
7485 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007486 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7487 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 }
7489 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007490 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7491 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007492 }
7493 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007494 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7495 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 }
7497 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7499 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007500 }
7501 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007502 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503 }
7504}
7505
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007506void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7507 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007510}
7511
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007512void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7513 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515}
7516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007517void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7518 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007520}
7521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007522void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524}
7525
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007526void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528}
7529
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007530void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7531 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007532 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533}
7534
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007535void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007536 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007537}
7538
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007539void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541}
7542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007543void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007544 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545}
7546
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007547void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007548 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549}
7550
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007551void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007553}
7554
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007555void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7556 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007558}
7559
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007560void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7561 int32_t value) {
7562 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7563 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7564}
7565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007566void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007567 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007568}
7569
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007570void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7571 nsecs_t readTime) {
7572 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573}
7574
Michael Wrightd02c5b62014-02-10 15:10:22 -08007575TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007577 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007578 prepareAxes(POSITION);
7579 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007580 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007581
arthurhungdcef2dc2020-08-11 14:47:50 +08007582 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007583
7584 NotifyMotionArgs motionArgs;
7585
7586 // Two fingers down at once.
7587 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7588 processPosition(mapper, x1, y1);
7589 processMTSync(mapper);
7590 processPosition(mapper, x2, y2);
7591 processMTSync(mapper);
7592 processSync(mapper);
7593
7594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7595 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7596 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7597 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7598 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7599 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7600 ASSERT_EQ(0, motionArgs.flags);
7601 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7602 ASSERT_EQ(0, motionArgs.buttonState);
7603 ASSERT_EQ(0, motionArgs.edgeFlags);
7604 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7605 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7606 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7607 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7608 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7609 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7610 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7612
7613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7614 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7615 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7616 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7617 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007618 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 ASSERT_EQ(0, motionArgs.flags);
7620 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7621 ASSERT_EQ(0, motionArgs.buttonState);
7622 ASSERT_EQ(0, motionArgs.edgeFlags);
7623 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 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7633 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7634 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7635
7636 // Move.
7637 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7638 processPosition(mapper, x1, y1);
7639 processMTSync(mapper);
7640 processPosition(mapper, x2, y2);
7641 processMTSync(mapper);
7642 processSync(mapper);
7643
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7645 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7646 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7647 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7648 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7649 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7650 ASSERT_EQ(0, motionArgs.flags);
7651 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7652 ASSERT_EQ(0, motionArgs.buttonState);
7653 ASSERT_EQ(0, motionArgs.edgeFlags);
7654 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7655 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7656 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7657 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7658 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7660 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7661 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7662 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7663 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7664 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7666
7667 // First finger up.
7668 x2 += 15; y2 -= 20;
7669 processPosition(mapper, x2, y2);
7670 processMTSync(mapper);
7671 processSync(mapper);
7672
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7675 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7676 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7677 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007678 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007679 ASSERT_EQ(0, motionArgs.flags);
7680 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7681 ASSERT_EQ(0, motionArgs.buttonState);
7682 ASSERT_EQ(0, motionArgs.edgeFlags);
7683 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7684 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7685 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7686 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7687 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7688 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7689 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7690 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7691 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7692 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7693 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7694 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7695
7696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7698 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7699 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7700 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7701 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7702 ASSERT_EQ(0, motionArgs.flags);
7703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7704 ASSERT_EQ(0, motionArgs.buttonState);
7705 ASSERT_EQ(0, motionArgs.edgeFlags);
7706 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7707 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7708 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7710 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7711 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7712 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7714
7715 // Move.
7716 x2 += 20; y2 -= 25;
7717 processPosition(mapper, x2, y2);
7718 processMTSync(mapper);
7719 processSync(mapper);
7720
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7722 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7723 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7724 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7725 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7727 ASSERT_EQ(0, motionArgs.flags);
7728 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7729 ASSERT_EQ(0, motionArgs.buttonState);
7730 ASSERT_EQ(0, motionArgs.edgeFlags);
7731 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7732 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7733 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7735 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7736 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7737 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7738 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7739
7740 // New finger down.
7741 int32_t x3 = 700, y3 = 300;
7742 processPosition(mapper, x2, y2);
7743 processMTSync(mapper);
7744 processPosition(mapper, x3, y3);
7745 processMTSync(mapper);
7746 processSync(mapper);
7747
7748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7749 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7750 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7751 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7752 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007753 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(0, motionArgs.flags);
7755 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7756 ASSERT_EQ(0, motionArgs.buttonState);
7757 ASSERT_EQ(0, motionArgs.edgeFlags);
7758 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7759 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7761 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7762 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7764 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7766 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7767 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7768 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7770
7771 // Second finger up.
7772 x3 += 30; y3 -= 20;
7773 processPosition(mapper, x3, y3);
7774 processMTSync(mapper);
7775 processSync(mapper);
7776
7777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7778 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7779 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7780 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7781 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007782 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007783 ASSERT_EQ(0, motionArgs.flags);
7784 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7785 ASSERT_EQ(0, motionArgs.buttonState);
7786 ASSERT_EQ(0, motionArgs.edgeFlags);
7787 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7788 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7789 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7790 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7791 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7792 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7793 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7795 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7796 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7797 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7798 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7799
7800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7801 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7802 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7803 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7804 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7806 ASSERT_EQ(0, motionArgs.flags);
7807 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7808 ASSERT_EQ(0, motionArgs.buttonState);
7809 ASSERT_EQ(0, motionArgs.edgeFlags);
7810 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7811 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7812 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7813 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7814 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7815 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7816 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7818
7819 // Last finger up.
7820 processMTSync(mapper);
7821 processSync(mapper);
7822
7823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7824 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7825 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7826 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7827 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7829 ASSERT_EQ(0, motionArgs.flags);
7830 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7831 ASSERT_EQ(0, motionArgs.buttonState);
7832 ASSERT_EQ(0, motionArgs.edgeFlags);
7833 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7834 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7835 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7837 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7838 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7839 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7840 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7841
7842 // Should not have sent any more keys or motions.
7843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7845}
7846
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007847TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7848 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007849 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007850
7851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7852 /*fuzz*/ 0, /*resolution*/ 10);
7853 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7854 /*fuzz*/ 0, /*resolution*/ 11);
7855 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7856 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7857 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7858 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7859 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7860 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7861 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7862 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7863
7864 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7865
7866 // X and Y axes
7867 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7868 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7869 // Touch major and minor
7870 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7871 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7872 // Tool major and minor
7873 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7874 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7875}
7876
7877TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7878 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007879 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007880
7881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7882 /*fuzz*/ 0, /*resolution*/ 10);
7883 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7884 /*fuzz*/ 0, /*resolution*/ 11);
7885
7886 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7887
7888 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
7889
7890 // Touch major and minor
7891 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7892 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7893 // Tool major and minor
7894 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7895 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7896}
7897
Michael Wrightd02c5b62014-02-10 15:10:22 -08007898TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007899 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007900 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901 prepareAxes(POSITION | ID);
7902 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007903 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904
arthurhungdcef2dc2020-08-11 14:47:50 +08007905 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007906
7907 NotifyMotionArgs motionArgs;
7908
7909 // Two fingers down at once.
7910 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7911 processPosition(mapper, x1, y1);
7912 processId(mapper, 1);
7913 processMTSync(mapper);
7914 processPosition(mapper, x2, y2);
7915 processId(mapper, 2);
7916 processMTSync(mapper);
7917 processSync(mapper);
7918
7919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7920 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7921 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7922 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7923 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7925 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7926
7927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007928 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007929 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7930 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7931 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7932 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7933 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7935 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7937 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7938
7939 // Move.
7940 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7941 processPosition(mapper, x1, y1);
7942 processId(mapper, 1);
7943 processMTSync(mapper);
7944 processPosition(mapper, x2, y2);
7945 processId(mapper, 2);
7946 processMTSync(mapper);
7947 processSync(mapper);
7948
7949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7951 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7952 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7953 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7954 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7955 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7957 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7958 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7959 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7960
7961 // First finger up.
7962 x2 += 15; y2 -= 20;
7963 processPosition(mapper, x2, y2);
7964 processId(mapper, 2);
7965 processMTSync(mapper);
7966 processSync(mapper);
7967
7968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007969 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007970 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
7971 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
7972 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7973 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
7974 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
7975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7976 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7978 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7982 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7983 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7984 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
7985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7986 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7987
7988 // Move.
7989 x2 += 20; y2 -= 25;
7990 processPosition(mapper, x2, y2);
7991 processId(mapper, 2);
7992 processMTSync(mapper);
7993 processSync(mapper);
7994
7995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7997 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
7998 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
7999 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8000 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8001 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8002
8003 // New finger down.
8004 int32_t x3 = 700, y3 = 300;
8005 processPosition(mapper, x2, y2);
8006 processId(mapper, 2);
8007 processMTSync(mapper);
8008 processPosition(mapper, x3, y3);
8009 processId(mapper, 3);
8010 processMTSync(mapper);
8011 processSync(mapper);
8012
8013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008014 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8016 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8017 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8018 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8019 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8020 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8021 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8023 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8024
8025 // Second finger up.
8026 x3 += 30; y3 -= 20;
8027 processPosition(mapper, x3, y3);
8028 processId(mapper, 3);
8029 processMTSync(mapper);
8030 processSync(mapper);
8031
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008033 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008034 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8035 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8036 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8037 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8038 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8039 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8040 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8041 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8042 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8046 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8047 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8048 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8049 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8050 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8051
8052 // Last finger up.
8053 processMTSync(mapper);
8054 processSync(mapper);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8058 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8059 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8060 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8062 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8063
8064 // Should not have sent any more keys or motions.
8065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8067}
8068
8069TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008071 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 prepareAxes(POSITION | ID | SLOT);
8073 prepareVirtualKeys();
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008074 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075
arthurhungdcef2dc2020-08-11 14:47:50 +08008076 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008077
8078 NotifyMotionArgs motionArgs;
8079
8080 // Two fingers down at once.
8081 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8082 processPosition(mapper, x1, y1);
8083 processId(mapper, 1);
8084 processSlot(mapper, 1);
8085 processPosition(mapper, x2, y2);
8086 processId(mapper, 2);
8087 processSync(mapper);
8088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8090 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8091 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8092 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8093 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8095 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008098 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8101 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8102 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8103 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8104 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8105 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8106 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8107 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8108
8109 // Move.
8110 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8111 processSlot(mapper, 0);
8112 processPosition(mapper, x1, y1);
8113 processSlot(mapper, 1);
8114 processPosition(mapper, x2, y2);
8115 processSync(mapper);
8116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8119 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8120 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8121 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8122 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8123 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8124 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8125 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8126 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8127 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8128
8129 // First finger up.
8130 x2 += 15; y2 -= 20;
8131 processSlot(mapper, 0);
8132 processId(mapper, -1);
8133 processSlot(mapper, 1);
8134 processPosition(mapper, x2, y2);
8135 processSync(mapper);
8136
8137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008138 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8140 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8141 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8142 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8143 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8144 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8145 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8146 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8147 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8148
8149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8151 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8152 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8153 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8154 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8155 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8156
8157 // Move.
8158 x2 += 20; y2 -= 25;
8159 processPosition(mapper, x2, y2);
8160 processSync(mapper);
8161
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8164 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8165 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
8166 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8167 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8168 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8169
8170 // New finger down.
8171 int32_t x3 = 700, y3 = 300;
8172 processPosition(mapper, x2, y2);
8173 processSlot(mapper, 0);
8174 processId(mapper, 3);
8175 processPosition(mapper, x3, y3);
8176 processSync(mapper);
8177
8178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008179 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8182 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8183 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8184 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8185 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8186 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8187 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8188 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8189
8190 // Second finger up.
8191 x3 += 30; y3 -= 20;
8192 processSlot(mapper, 1);
8193 processId(mapper, -1);
8194 processSlot(mapper, 0);
8195 processPosition(mapper, x3, y3);
8196 processSync(mapper);
8197
8198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008199 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008200 ASSERT_EQ(size_t(2), motionArgs.pointerCount);
8201 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8202 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8203 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
8204 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
8205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8206 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8208 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8212 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8213 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8214 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8216 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8217
8218 // Last finger up.
8219 processId(mapper, -1);
8220 processSync(mapper);
8221
8222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8223 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8224 ASSERT_EQ(size_t(1), motionArgs.pointerCount);
8225 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
8226 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8227 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8228 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8229
8230 // Should not have sent any more keys or motions.
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8233}
8234
8235TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008236 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008237 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008238 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008239 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240
8241 // These calculations are based on the input device calibration documentation.
8242 int32_t rawX = 100;
8243 int32_t rawY = 200;
8244 int32_t rawTouchMajor = 7;
8245 int32_t rawTouchMinor = 6;
8246 int32_t rawToolMajor = 9;
8247 int32_t rawToolMinor = 8;
8248 int32_t rawPressure = 11;
8249 int32_t rawDistance = 0;
8250 int32_t rawOrientation = 3;
8251 int32_t id = 5;
8252
8253 float x = toDisplayX(rawX);
8254 float y = toDisplayY(rawY);
8255 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
8256 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8257 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8258 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8259 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8260 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8261 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
8262 float distance = float(rawDistance);
8263
8264 processPosition(mapper, rawX, rawY);
8265 processTouchMajor(mapper, rawTouchMajor);
8266 processTouchMinor(mapper, rawTouchMinor);
8267 processToolMajor(mapper, rawToolMajor);
8268 processToolMinor(mapper, rawToolMinor);
8269 processPressure(mapper, rawPressure);
8270 processOrientation(mapper, rawOrientation);
8271 processDistance(mapper, rawDistance);
8272 processId(mapper, id);
8273 processMTSync(mapper);
8274 processSync(mapper);
8275
8276 NotifyMotionArgs args;
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8278 ASSERT_EQ(0, args.pointerProperties[0].id);
8279 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8280 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
8281 orientation, distance));
8282}
8283
8284TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008286 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008287 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
8288 addConfigurationProperty("touch.size.calibration", "geometric");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008289 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 // These calculations are based on the input device calibration documentation.
8292 int32_t rawX = 100;
8293 int32_t rawY = 200;
8294 int32_t rawTouchMajor = 140;
8295 int32_t rawTouchMinor = 120;
8296 int32_t rawToolMajor = 180;
8297 int32_t rawToolMinor = 160;
8298
8299 float x = toDisplayX(rawX);
8300 float y = toDisplayY(rawY);
8301 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
8302 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
8303 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
8304 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
8305 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
8306
8307 processPosition(mapper, rawX, rawY);
8308 processTouchMajor(mapper, rawTouchMajor);
8309 processTouchMinor(mapper, rawTouchMinor);
8310 processToolMajor(mapper, rawToolMajor);
8311 processToolMinor(mapper, rawToolMinor);
8312 processMTSync(mapper);
8313 processSync(mapper);
8314
8315 NotifyMotionArgs args;
8316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8318 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
8319}
8320
8321TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008322 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008323 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008324 prepareAxes(POSITION | TOUCH | TOOL);
8325 addConfigurationProperty("touch.size.calibration", "diameter");
8326 addConfigurationProperty("touch.size.scale", "10");
8327 addConfigurationProperty("touch.size.bias", "160");
8328 addConfigurationProperty("touch.size.isSummed", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008329 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008330
8331 // These calculations are based on the input device calibration documentation.
8332 // Note: We only provide a single common touch/tool value because the device is assumed
8333 // not to emit separate values for each pointer (isSummed = 1).
8334 int32_t rawX = 100;
8335 int32_t rawY = 200;
8336 int32_t rawX2 = 150;
8337 int32_t rawY2 = 250;
8338 int32_t rawTouchMajor = 5;
8339 int32_t rawToolMajor = 8;
8340
8341 float x = toDisplayX(rawX);
8342 float y = toDisplayY(rawY);
8343 float x2 = toDisplayX(rawX2);
8344 float y2 = toDisplayY(rawY2);
8345 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
8346 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
8347 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
8348
8349 processPosition(mapper, rawX, rawY);
8350 processTouchMajor(mapper, rawTouchMajor);
8351 processToolMajor(mapper, rawToolMajor);
8352 processMTSync(mapper);
8353 processPosition(mapper, rawX2, rawY2);
8354 processTouchMajor(mapper, rawTouchMajor);
8355 processToolMajor(mapper, rawToolMajor);
8356 processMTSync(mapper);
8357 processSync(mapper);
8358
8359 NotifyMotionArgs args;
8360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8361 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
8362
8363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008364 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 ASSERT_EQ(size_t(2), args.pointerCount);
8366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8367 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8368 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
8369 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
8370}
8371
8372TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008374 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008375 prepareAxes(POSITION | TOUCH | TOOL);
8376 addConfigurationProperty("touch.size.calibration", "area");
8377 addConfigurationProperty("touch.size.scale", "43");
8378 addConfigurationProperty("touch.size.bias", "3");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008380
8381 // These calculations are based on the input device calibration documentation.
8382 int32_t rawX = 100;
8383 int32_t rawY = 200;
8384 int32_t rawTouchMajor = 5;
8385 int32_t rawToolMajor = 8;
8386
8387 float x = toDisplayX(rawX);
8388 float y = toDisplayY(rawY);
8389 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8390 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8391 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8392
8393 processPosition(mapper, rawX, rawY);
8394 processTouchMajor(mapper, rawTouchMajor);
8395 processToolMajor(mapper, rawToolMajor);
8396 processMTSync(mapper);
8397 processSync(mapper);
8398
8399 NotifyMotionArgs args;
8400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8402 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8403}
8404
8405TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008408 prepareAxes(POSITION | PRESSURE);
8409 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8410 addConfigurationProperty("touch.pressure.scale", "0.01");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008411 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412
Michael Wrightaa449c92017-12-13 21:21:43 +00008413 InputDeviceInfo info;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008414 mapper.populateDeviceInfo(&info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008415 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8416 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8417 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8418
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419 // These calculations are based on the input device calibration documentation.
8420 int32_t rawX = 100;
8421 int32_t rawY = 200;
8422 int32_t rawPressure = 60;
8423
8424 float x = toDisplayX(rawX);
8425 float y = toDisplayY(rawY);
8426 float pressure = float(rawPressure) * 0.01f;
8427
8428 processPosition(mapper, rawX, rawY);
8429 processPressure(mapper, rawPressure);
8430 processMTSync(mapper);
8431 processSync(mapper);
8432
8433 NotifyMotionArgs args;
8434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8435 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8436 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8437}
8438
8439TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008441 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008443 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444
8445 NotifyMotionArgs motionArgs;
8446 NotifyKeyArgs keyArgs;
8447
8448 processId(mapper, 1);
8449 processPosition(mapper, 100, 200);
8450 processSync(mapper);
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8453 ASSERT_EQ(0, motionArgs.buttonState);
8454
8455 // press BTN_LEFT, release BTN_LEFT
8456 processKey(mapper, BTN_LEFT, 1);
8457 processSync(mapper);
8458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8459 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8460 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8461
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8464 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8465
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466 processKey(mapper, BTN_LEFT, 0);
8467 processSync(mapper);
8468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008469 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008470 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008471
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008473 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008474 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008475
8476 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8477 processKey(mapper, BTN_RIGHT, 1);
8478 processKey(mapper, BTN_MIDDLE, 1);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8482 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8483 motionArgs.buttonState);
8484
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8487 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8488
8489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8490 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8491 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8492 motionArgs.buttonState);
8493
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494 processKey(mapper, BTN_RIGHT, 0);
8495 processSync(mapper);
8496 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008497 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008498 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008502 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503
8504 processKey(mapper, BTN_MIDDLE, 0);
8505 processSync(mapper);
8506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008508 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008509
8510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008511 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008512 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513
8514 // press BTN_BACK, release BTN_BACK
8515 processKey(mapper, BTN_BACK, 1);
8516 processSync(mapper);
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8518 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8519 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008520
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008523 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8524
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8527 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008528
8529 processKey(mapper, BTN_BACK, 0);
8530 processSync(mapper);
8531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008532 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008534
8535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008537 ASSERT_EQ(0, motionArgs.buttonState);
8538
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8540 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8541 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8542
8543 // press BTN_SIDE, release BTN_SIDE
8544 processKey(mapper, BTN_SIDE, 1);
8545 processSync(mapper);
8546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8547 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8548 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008549
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008552 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8553
8554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8555 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8556 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557
8558 processKey(mapper, BTN_SIDE, 0);
8559 processSync(mapper);
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008561 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008562 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008563
8564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008566 ASSERT_EQ(0, motionArgs.buttonState);
8567
Michael Wrightd02c5b62014-02-10 15:10:22 -08008568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8569 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8570 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8571
8572 // press BTN_FORWARD, release BTN_FORWARD
8573 processKey(mapper, BTN_FORWARD, 1);
8574 processSync(mapper);
8575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8576 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8577 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008578
Michael Wrightd02c5b62014-02-10 15:10:22 -08008579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008581 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8582
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8585 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008586
8587 processKey(mapper, BTN_FORWARD, 0);
8588 processSync(mapper);
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008590 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008591 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008592
8593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008595 ASSERT_EQ(0, motionArgs.buttonState);
8596
Michael Wrightd02c5b62014-02-10 15:10:22 -08008597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8598 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8599 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8600
8601 // press BTN_EXTRA, release BTN_EXTRA
8602 processKey(mapper, BTN_EXTRA, 1);
8603 processSync(mapper);
8604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8605 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8606 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008607
Michael Wrightd02c5b62014-02-10 15:10:22 -08008608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008609 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008610 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8613 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8614 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615
8616 processKey(mapper, BTN_EXTRA, 0);
8617 processSync(mapper);
8618 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008619 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008621
8622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008624 ASSERT_EQ(0, motionArgs.buttonState);
8625
Michael Wrightd02c5b62014-02-10 15:10:22 -08008626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8627 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8628 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8629
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8631
Michael Wrightd02c5b62014-02-10 15:10:22 -08008632 // press BTN_STYLUS, release BTN_STYLUS
8633 processKey(mapper, BTN_STYLUS, 1);
8634 processSync(mapper);
8635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8636 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008637 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8638
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8641 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642
8643 processKey(mapper, BTN_STYLUS, 0);
8644 processSync(mapper);
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008646 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008648
8649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008651 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652
8653 // press BTN_STYLUS2, release BTN_STYLUS2
8654 processKey(mapper, BTN_STYLUS2, 1);
8655 processSync(mapper);
8656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008658 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8659
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8662 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008663
8664 processKey(mapper, BTN_STYLUS2, 0);
8665 processSync(mapper);
8666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008667 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008668 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008669
8670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008672 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673
8674 // release touch
8675 processId(mapper, -1);
8676 processSync(mapper);
8677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8678 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8679 ASSERT_EQ(0, motionArgs.buttonState);
8680}
8681
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008682TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8683 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008684 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008685 prepareAxes(POSITION | ID | SLOT);
8686 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
8687
8688 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8689 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8690
8691 // Touch down.
8692 processId(mapper, 1);
8693 processPosition(mapper, 100, 200);
8694 processSync(mapper);
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8696 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8697
8698 // Press and release button mapped to the primary stylus button.
8699 processKey(mapper, BTN_A, 1);
8700 processSync(mapper);
8701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8702 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8703 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8705 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8706 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8707
8708 processKey(mapper, BTN_A, 0);
8709 processSync(mapper);
8710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8711 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8714
8715 // Press and release the HID usage mapped to the secondary stylus button.
8716 processHidUsage(mapper, 0xabcd, 1);
8717 processSync(mapper);
8718 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8719 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8720 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8722 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8723 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8724
8725 processHidUsage(mapper, 0xabcd, 0);
8726 processSync(mapper);
8727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8728 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8731
8732 // Release touch.
8733 processId(mapper, -1);
8734 processSync(mapper);
8735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8736 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8737}
8738
Michael Wrightd02c5b62014-02-10 15:10:22 -08008739TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008740 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008741 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008742 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008743 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008744
8745 NotifyMotionArgs motionArgs;
8746
8747 // default tool type is finger
8748 processId(mapper, 1);
8749 processPosition(mapper, 100, 200);
8750 processSync(mapper);
8751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8752 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8753 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8754
8755 // eraser
8756 processKey(mapper, BTN_TOOL_RUBBER, 1);
8757 processSync(mapper);
8758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8760 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8761
8762 // stylus
8763 processKey(mapper, BTN_TOOL_RUBBER, 0);
8764 processKey(mapper, BTN_TOOL_PEN, 1);
8765 processSync(mapper);
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8769
8770 // brush
8771 processKey(mapper, BTN_TOOL_PEN, 0);
8772 processKey(mapper, BTN_TOOL_BRUSH, 1);
8773 processSync(mapper);
8774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8776 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8777
8778 // pencil
8779 processKey(mapper, BTN_TOOL_BRUSH, 0);
8780 processKey(mapper, BTN_TOOL_PENCIL, 1);
8781 processSync(mapper);
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8784 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8785
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008786 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008787 processKey(mapper, BTN_TOOL_PENCIL, 0);
8788 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8789 processSync(mapper);
8790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8792 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8793
8794 // mouse
8795 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8796 processKey(mapper, BTN_TOOL_MOUSE, 1);
8797 processSync(mapper);
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8800 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8801
8802 // lens
8803 processKey(mapper, BTN_TOOL_MOUSE, 0);
8804 processKey(mapper, BTN_TOOL_LENS, 1);
8805 processSync(mapper);
8806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8808 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8809
8810 // double-tap
8811 processKey(mapper, BTN_TOOL_LENS, 0);
8812 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8813 processSync(mapper);
8814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8815 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8816 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8817
8818 // triple-tap
8819 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8820 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8821 processSync(mapper);
8822 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8823 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8824 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8825
8826 // quad-tap
8827 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8828 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8829 processSync(mapper);
8830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8831 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8832 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8833
8834 // finger
8835 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8836 processKey(mapper, BTN_TOOL_FINGER, 1);
8837 processSync(mapper);
8838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8839 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8840 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8841
8842 // stylus trumps finger
8843 processKey(mapper, BTN_TOOL_PEN, 1);
8844 processSync(mapper);
8845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8847 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8848
8849 // eraser trumps stylus
8850 processKey(mapper, BTN_TOOL_RUBBER, 1);
8851 processSync(mapper);
8852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8854 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_ERASER, motionArgs.pointerProperties[0].toolType);
8855
8856 // mouse trumps eraser
8857 processKey(mapper, BTN_TOOL_MOUSE, 1);
8858 processSync(mapper);
8859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8861 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_MOUSE, motionArgs.pointerProperties[0].toolType);
8862
8863 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8864 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8865 processSync(mapper);
8866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8868 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8869
8870 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8871 processToolType(mapper, MT_TOOL_PEN);
8872 processSync(mapper);
8873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8874 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8875 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_STYLUS, motionArgs.pointerProperties[0].toolType);
8876
8877 // back to default tool type
8878 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8879 processKey(mapper, BTN_TOOL_MOUSE, 0);
8880 processKey(mapper, BTN_TOOL_RUBBER, 0);
8881 processKey(mapper, BTN_TOOL_PEN, 0);
8882 processKey(mapper, BTN_TOOL_FINGER, 0);
8883 processSync(mapper);
8884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8886 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
8887}
8888
8889TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008891 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008892 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008893 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008894 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008895
8896 NotifyMotionArgs motionArgs;
8897
8898 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8899 processId(mapper, 1);
8900 processPosition(mapper, 100, 200);
8901 processSync(mapper);
8902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8903 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8905 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8906
8907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8910 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8911
8912 // move a little
8913 processPosition(mapper, 150, 250);
8914 processSync(mapper);
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8917 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8918 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8919
8920 // down when BTN_TOUCH is pressed, pressure defaults to 1
8921 processKey(mapper, BTN_TOUCH, 1);
8922 processSync(mapper);
8923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8924 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8926 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8927
8928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8929 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8930 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8931 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8932
8933 // up when BTN_TOUCH is released, hover restored
8934 processKey(mapper, BTN_TOUCH, 0);
8935 processSync(mapper);
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8937 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8939 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8940
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8944 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8945
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8949 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8950
8951 // exit hover when pointer goes away
8952 processId(mapper, -1);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8958}
8959
8960TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008961 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008962 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008963 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008964 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008965
8966 NotifyMotionArgs motionArgs;
8967
8968 // initially hovering because pressure is 0
8969 processId(mapper, 1);
8970 processPosition(mapper, 100, 200);
8971 processPressure(mapper, 0);
8972 processSync(mapper);
8973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8974 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8976 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8977
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8981 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8982
8983 // move a little
8984 processPosition(mapper, 150, 250);
8985 processSync(mapper);
8986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8987 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8989 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8990
8991 // down when pressure becomes non-zero
8992 processPressure(mapper, RAW_PRESSURE_MAX);
8993 processSync(mapper);
8994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8995 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8996 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8997 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8998
8999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9000 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9001 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9002 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9003
9004 // up when pressure becomes 0, hover restored
9005 processPressure(mapper, 0);
9006 processSync(mapper);
9007 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9008 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9010 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9011
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9014 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9015 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9016
9017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9018 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9019 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9020 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9021
9022 // exit hover when pointer goes away
9023 processId(mapper, -1);
9024 processSync(mapper);
9025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9028 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9029}
9030
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009031/**
9032 * Set the input device port <--> display port associations, and check that the
9033 * events are routed to the display that matches the display port.
9034 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
9035 */
9036TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009037 const std::string usb2 = "USB2";
9038 const uint8_t hdmi1 = 0;
9039 const uint8_t hdmi2 = 1;
9040 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009041 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009042
9043 addConfigurationProperty("touch.deviceType", "touchScreen");
9044 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009045 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009046
9047 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9048 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
9049
9050 // We are intentionally not adding the viewport for display 1 yet. Since the port association
9051 // for this input device is specified, and the matching viewport is not present,
9052 // the input device should be disabled (at the mapper level).
9053
9054 // Add viewport for display 2 on hdmi2
9055 prepareSecondaryDisplay(type, hdmi2);
9056 // Send a touch event
9057 processPosition(mapper, 100, 100);
9058 processSync(mapper);
9059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9060
9061 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00009062 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009063 // Send a touch event again
9064 processPosition(mapper, 100, 100);
9065 processSync(mapper);
9066
9067 NotifyMotionArgs args;
9068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9069 ASSERT_EQ(DISPLAY_ID, args.displayId);
9070}
Michael Wrightd02c5b62014-02-10 15:10:22 -08009071
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009072TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
9073 addConfigurationProperty("touch.deviceType", "touchScreen");
9074 prepareAxes(POSITION);
9075 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9076
9077 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
9078
Michael Wrighta9cf4192022-12-01 23:46:39 +00009079 prepareDisplay(ui::ROTATION_0);
9080 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00009081
9082 // Send a touch event
9083 processPosition(mapper, 100, 100);
9084 processSync(mapper);
9085
9086 NotifyMotionArgs args;
9087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9088 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
9089}
9090
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009091TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08009092 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01009093 std::shared_ptr<FakePointerController> fakePointerController =
9094 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08009095 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009096 fakePointerController->setPosition(100, 200);
9097 fakePointerController->setButtonState(0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009098 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009099
Garfield Tan888a6a42020-01-09 11:39:16 -08009100 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009101 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08009102
Michael Wrighta9cf4192022-12-01 23:46:39 +00009103 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009104 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009105 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009106
Josep del Río2d8c79a2023-01-23 19:33:50 +00009107 // Check source is mouse that would obtain the PointerController.
9108 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08009109
9110 NotifyMotionArgs motionArgs;
9111 processPosition(mapper, 100, 100);
9112 processSync(mapper);
9113
9114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9115 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9116 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9117}
9118
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009119/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009120 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
9121 */
9122TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
9123 addConfigurationProperty("touch.deviceType", "touchScreen");
9124 prepareAxes(POSITION);
9125 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9126
Michael Wrighta9cf4192022-12-01 23:46:39 +00009127 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00009128 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
9129 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
9130 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
9131 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009132
9133 NotifyMotionArgs args;
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9135 ASSERT_EQ(26, args.readTime);
9136
Harry Cutts33476232023-01-30 19:57:29 +00009137 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
9138 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
9139 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00009140
9141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9142 ASSERT_EQ(33, args.readTime);
9143}
9144
9145/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009146 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
9147 * events should not be delivered to the listener.
9148 */
9149TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
9150 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009151 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009152 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009153 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00009154 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9155 prepareAxes(POSITION);
9156 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9157
9158 NotifyMotionArgs motionArgs;
9159 processPosition(mapper, 100, 100);
9160 processSync(mapper);
9161
9162 mFakeListener->assertNotifyMotionWasNotCalled();
9163}
9164
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009165/**
9166 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
9167 * the touch mapper can process the events and the events can be delivered to the listener.
9168 */
9169TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
9170 addConfigurationProperty("touch.deviceType", "touchScreen");
9171 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009172 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009173 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009174 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9175 prepareAxes(POSITION);
9176 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9177
9178 NotifyMotionArgs motionArgs;
9179 processPosition(mapper, 100, 100);
9180 processSync(mapper);
9181
9182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9183 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9184}
9185
Garfield Tanc734e4f2021-01-15 20:01:39 -08009186TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
9187 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07009188 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009189 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00009190 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009191 std::optional<DisplayViewport> optionalDisplayViewport =
9192 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
9193 ASSERT_TRUE(optionalDisplayViewport.has_value());
9194 DisplayViewport displayViewport = *optionalDisplayViewport;
9195
9196 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9197 prepareAxes(POSITION);
9198 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9199
9200 // Finger down
9201 int32_t x = 100, y = 100;
9202 processPosition(mapper, x, y);
9203 processSync(mapper);
9204
9205 NotifyMotionArgs motionArgs;
9206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9207 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9208
9209 // Deactivate display viewport
9210 displayViewport.isActive = false;
9211 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9212 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9213
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009214 // The ongoing touch should be canceled immediately
9215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9216 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9217
9218 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08009219 x += 10, y += 10;
9220 processPosition(mapper, x, y);
9221 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08009223
9224 // Reactivate display viewport
9225 displayViewport.isActive = true;
9226 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
9227 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
9228
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009229 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08009230 x += 10, y += 10;
9231 processPosition(mapper, x, y);
9232 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00009233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9234 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08009235}
9236
Arthur Hung7c645402019-01-25 17:45:42 +08009237TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
9238 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08009239 prepareAxes(POSITION | ID | SLOT);
9240 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009241 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08009242
9243 // Create the second touch screen device, and enable multi fingers.
9244 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08009245 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08009246 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009247 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08009248 std::shared_ptr<InputDevice> device2 =
9249 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009250 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08009251
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009252 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009253 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009254 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009255 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009256 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009257 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009258 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00009259 /*flat=*/0, /*fuzz=*/0);
9260 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009261 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
9262 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08009263
9264 // Setup the second touch screen device.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009265 MultiTouchInputMapper& mapper2 = device2->addMapper<MultiTouchInputMapper>(SECOND_EVENTHUB_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009266 std::list<NotifyArgs> unused =
9267 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Harry Cutts33476232023-01-30 19:57:29 +00009268 /*changes=*/0);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009269 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08009270
9271 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01009272 std::shared_ptr<FakePointerController> fakePointerController =
9273 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009274 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08009275
9276 // Setup policy for associated displays and show touches.
9277 const uint8_t hdmi1 = 0;
9278 const uint8_t hdmi2 = 1;
9279 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
9280 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
9281 mFakePolicy->setShowTouches(true);
9282
9283 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009284 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009285 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08009286
9287 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009288 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9289 InputReaderConfiguration::CHANGE_DISPLAY_INFO |
9290 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08009291
9292 // Two fingers down at default display.
9293 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9294 processPosition(mapper, x1, y1);
9295 processId(mapper, 1);
9296 processSlot(mapper, 1);
9297 processPosition(mapper, x2, y2);
9298 processId(mapper, 2);
9299 processSync(mapper);
9300
9301 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
9302 fakePointerController->getSpots().find(DISPLAY_ID);
9303 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9304 ASSERT_EQ(size_t(2), iter->second.size());
9305
9306 // Two fingers down at second display.
9307 processPosition(mapper2, x1, y1);
9308 processId(mapper2, 1);
9309 processSlot(mapper2, 1);
9310 processPosition(mapper2, x2, y2);
9311 processId(mapper2, 2);
9312 processSync(mapper2);
9313
9314 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
9315 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
9316 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00009317
9318 // Disable the show touches configuration and ensure the spots are cleared.
9319 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07009320 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
9321 InputReaderConfiguration::CHANGE_SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00009322
9323 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009324}
9325
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009326TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009327 prepareAxes(POSITION);
9328 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009329 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009330 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009331
9332 NotifyMotionArgs motionArgs;
9333 // Unrotated video frame
9334 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9335 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009336 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009337 processPosition(mapper, 100, 200);
9338 processSync(mapper);
9339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9340 ASSERT_EQ(frames, motionArgs.videoFrames);
9341
9342 // Subsequent touch events should not have any videoframes
9343 // This is implemented separately in FakeEventHub,
9344 // but that should match the behaviour of TouchVideoDevice.
9345 processPosition(mapper, 200, 200);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9349}
9350
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009351TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009352 prepareAxes(POSITION);
9353 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009354 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009355 // Unrotated video frame
9356 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9357 NotifyMotionArgs motionArgs;
9358
9359 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009360 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009361 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9362 clearViewports();
9363 prepareDisplay(orientation);
9364 std::vector<TouchVideoFrame> frames{frame};
9365 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9366 processPosition(mapper, 100, 200);
9367 processSync(mapper);
9368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9369 ASSERT_EQ(frames, motionArgs.videoFrames);
9370 }
9371}
9372
9373TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9374 prepareAxes(POSITION);
9375 addConfigurationProperty("touch.deviceType", "touchScreen");
9376 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9377 // orientation-aware are affected by display rotation.
9378 addConfigurationProperty("touch.orientationAware", "0");
9379 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9380 // Unrotated video frame
9381 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9382 NotifyMotionArgs motionArgs;
9383
9384 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009385 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009386 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9387 clearViewports();
9388 prepareDisplay(orientation);
9389 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009390 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009391 processPosition(mapper, 100, 200);
9392 processSync(mapper);
9393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009394 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9395 // compared to the display. This is so that when the window transform (which contains the
9396 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9397 // window's coordinate space.
9398 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009399 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009400
9401 // Release finger.
9402 processSync(mapper);
9403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009404 }
9405}
9406
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009407TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009408 prepareAxes(POSITION);
9409 addConfigurationProperty("touch.deviceType", "touchScreen");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009410 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009411 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9412 // so mix these.
9413 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9414 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9415 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9416 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9417 NotifyMotionArgs motionArgs;
9418
Michael Wrighta9cf4192022-12-01 23:46:39 +00009419 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009420 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009421 processPosition(mapper, 100, 200);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009424 ASSERT_EQ(frames, motionArgs.videoFrames);
9425}
9426
9427TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9428 prepareAxes(POSITION);
9429 addConfigurationProperty("touch.deviceType", "touchScreen");
9430 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9431 // orientation-aware are affected by display rotation.
9432 addConfigurationProperty("touch.orientationAware", "0");
9433 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9434 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9435 // so mix these.
9436 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9437 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9438 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9439 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9440 NotifyMotionArgs motionArgs;
9441
Michael Wrighta9cf4192022-12-01 23:46:39 +00009442 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009443 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9444 processPosition(mapper, 100, 200);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9448 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9449 // compared to the display. This is so that when the window transform (which contains the
9450 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9451 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009452 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009453 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009454 ASSERT_EQ(frames, motionArgs.videoFrames);
9455}
9456
Arthur Hung9da14732019-09-02 16:16:58 +08009457/**
9458 * If we had defined port associations, but the viewport is not ready, the touch device would be
9459 * expected to be disabled, and it should be enabled after the viewport has found.
9460 */
9461TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009462 constexpr uint8_t hdmi2 = 1;
9463 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009464 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009465
9466 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9467
9468 addConfigurationProperty("touch.deviceType", "touchScreen");
9469 prepareAxes(POSITION);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009470 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009471
9472 ASSERT_EQ(mDevice->isEnabled(), false);
9473
9474 // Add display on hdmi2, the device should be enabled and can receive touch event.
9475 prepareSecondaryDisplay(type, hdmi2);
9476 ASSERT_EQ(mDevice->isEnabled(), true);
9477
9478 // Send a touch event.
9479 processPosition(mapper, 100, 100);
9480 processSync(mapper);
9481
9482 NotifyMotionArgs args;
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9484 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9485}
9486
Arthur Hung421eb1c2020-01-16 00:09:42 +08009487TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009488 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009489 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009490 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009491 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009492
9493 NotifyMotionArgs motionArgs;
9494
9495 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9496 // finger down
9497 processId(mapper, 1);
9498 processPosition(mapper, x1, y1);
9499 processSync(mapper);
9500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9503
9504 // finger move
9505 processId(mapper, 1);
9506 processPosition(mapper, x2, y2);
9507 processSync(mapper);
9508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9510 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9511
9512 // finger up.
9513 processId(mapper, -1);
9514 processSync(mapper);
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9516 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9517 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9518
9519 // new finger down
9520 processId(mapper, 1);
9521 processPosition(mapper, x3, y3);
9522 processSync(mapper);
9523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9525 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9526}
9527
9528/**
arthurhungcc7f9802020-04-30 17:55:40 +08009529 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9530 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009531 */
arthurhungcc7f9802020-04-30 17:55:40 +08009532TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009534 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009535 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08009536 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009537
9538 NotifyMotionArgs motionArgs;
9539
9540 // default tool type is finger
9541 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009542 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009543 processPosition(mapper, x1, y1);
9544 processSync(mapper);
9545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9547 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9548
9549 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9550 processToolType(mapper, MT_TOOL_PALM);
9551 processSync(mapper);
9552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9553 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9554
9555 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009556 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009557 processPosition(mapper, x2, y2);
9558 processSync(mapper);
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9560
9561 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009562 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9565
9566 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009567 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009568 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009569 processPosition(mapper, x3, y3);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9573 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9574}
9575
arthurhungbf89a482020-04-17 17:37:55 +08009576/**
arthurhungcc7f9802020-04-30 17:55:40 +08009577 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9578 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009579 */
arthurhungcc7f9802020-04-30 17:55:40 +08009580TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009581 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009582 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009583 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9584 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9585
9586 NotifyMotionArgs motionArgs;
9587
9588 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009589 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9590 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009591 processPosition(mapper, x1, y1);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9594 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9595 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9596
9597 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009598 processSlot(mapper, SECOND_SLOT);
9599 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009600 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009601 processSync(mapper);
9602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009603 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009604 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[1].toolType);
9605
9606 // If the tool type of the first finger changes to MT_TOOL_PALM,
9607 // we expect to receive ACTION_POINTER_UP with cancel flag.
9608 processSlot(mapper, FIRST_SLOT);
9609 processId(mapper, FIRST_TRACKING_ID);
9610 processToolType(mapper, MT_TOOL_PALM);
9611 processSync(mapper);
9612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009613 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009614 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9615
9616 // The following MOVE events of second finger should be processed.
9617 processSlot(mapper, SECOND_SLOT);
9618 processId(mapper, SECOND_TRACKING_ID);
9619 processPosition(mapper, x2 + 1, y2 + 1);
9620 processSync(mapper);
9621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9623 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9624
9625 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9626 // it. Second finger receive move.
9627 processSlot(mapper, FIRST_SLOT);
9628 processId(mapper, INVALID_TRACKING_ID);
9629 processSync(mapper);
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9631 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9632 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9633
9634 // Second finger keeps moving.
9635 processSlot(mapper, SECOND_SLOT);
9636 processId(mapper, SECOND_TRACKING_ID);
9637 processPosition(mapper, x2 + 2, y2 + 2);
9638 processSync(mapper);
9639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9640 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9641 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9642
9643 // Second finger up.
9644 processId(mapper, INVALID_TRACKING_ID);
9645 processSync(mapper);
9646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9647 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9648 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9649}
9650
9651/**
9652 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9653 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9654 */
9655TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9656 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009657 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009658 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9659 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9660
9661 NotifyMotionArgs motionArgs;
9662
9663 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9664 // First finger down.
9665 processId(mapper, FIRST_TRACKING_ID);
9666 processPosition(mapper, x1, y1);
9667 processSync(mapper);
9668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9669 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9670 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9671
9672 // Second finger down.
9673 processSlot(mapper, SECOND_SLOT);
9674 processId(mapper, SECOND_TRACKING_ID);
9675 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009676 processSync(mapper);
9677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009678 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungbf89a482020-04-17 17:37:55 +08009679 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9680
arthurhungcc7f9802020-04-30 17:55:40 +08009681 // If the tool type of the first finger changes to MT_TOOL_PALM,
9682 // we expect to receive ACTION_POINTER_UP with cancel flag.
9683 processSlot(mapper, FIRST_SLOT);
9684 processId(mapper, FIRST_TRACKING_ID);
9685 processToolType(mapper, MT_TOOL_PALM);
9686 processSync(mapper);
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009688 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009689 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9690
9691 // Second finger keeps moving.
9692 processSlot(mapper, SECOND_SLOT);
9693 processId(mapper, SECOND_TRACKING_ID);
9694 processPosition(mapper, x2 + 1, y2 + 1);
9695 processSync(mapper);
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9698
9699 // second finger becomes palm, receive cancel due to only 1 finger is active.
9700 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009701 processToolType(mapper, MT_TOOL_PALM);
9702 processSync(mapper);
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9704 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9705
arthurhungcc7f9802020-04-30 17:55:40 +08009706 // third finger down.
9707 processSlot(mapper, THIRD_SLOT);
9708 processId(mapper, THIRD_TRACKING_ID);
9709 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009710 processPosition(mapper, x3, y3);
9711 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9714 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009715 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9716
9717 // third finger move
9718 processId(mapper, THIRD_TRACKING_ID);
9719 processPosition(mapper, x3 + 1, y3 + 1);
9720 processSync(mapper);
9721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9723
9724 // first finger up, third finger receive move.
9725 processSlot(mapper, FIRST_SLOT);
9726 processId(mapper, INVALID_TRACKING_ID);
9727 processSync(mapper);
9728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9730 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9731
9732 // second finger up, third finger receive move.
9733 processSlot(mapper, SECOND_SLOT);
9734 processId(mapper, INVALID_TRACKING_ID);
9735 processSync(mapper);
9736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9737 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9738 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9739
9740 // third finger up.
9741 processSlot(mapper, THIRD_SLOT);
9742 processId(mapper, INVALID_TRACKING_ID);
9743 processSync(mapper);
9744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9745 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9746 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9747}
9748
9749/**
9750 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9751 * and the active finger could still be allowed to receive the events
9752 */
9753TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9754 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009755 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009756 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
9757 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9758
9759 NotifyMotionArgs motionArgs;
9760
9761 // default tool type is finger
9762 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9763 processId(mapper, FIRST_TRACKING_ID);
9764 processPosition(mapper, x1, y1);
9765 processSync(mapper);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9768 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9769
9770 // Second finger down.
9771 processSlot(mapper, SECOND_SLOT);
9772 processId(mapper, SECOND_TRACKING_ID);
9773 processPosition(mapper, x2, y2);
9774 processSync(mapper);
9775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009776 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009777 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
9778
9779 // If the tool type of the second finger changes to MT_TOOL_PALM,
9780 // we expect to receive ACTION_POINTER_UP with cancel flag.
9781 processId(mapper, SECOND_TRACKING_ID);
9782 processToolType(mapper, MT_TOOL_PALM);
9783 processSync(mapper);
9784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009785 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009786 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9787
9788 // The following MOVE event should be processed.
9789 processSlot(mapper, FIRST_SLOT);
9790 processId(mapper, FIRST_TRACKING_ID);
9791 processPosition(mapper, x1 + 1, y1 + 1);
9792 processSync(mapper);
9793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9795 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9796
9797 // second finger up.
9798 processSlot(mapper, SECOND_SLOT);
9799 processId(mapper, INVALID_TRACKING_ID);
9800 processSync(mapper);
9801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9802 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9803
9804 // first finger keep moving
9805 processSlot(mapper, FIRST_SLOT);
9806 processId(mapper, FIRST_TRACKING_ID);
9807 processPosition(mapper, x1 + 2, y1 + 2);
9808 processSync(mapper);
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9811
9812 // first finger up.
9813 processId(mapper, INVALID_TRACKING_ID);
9814 processSync(mapper);
9815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9816 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9817 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009818}
9819
Arthur Hung9ad18942021-06-19 02:04:46 +00009820/**
9821 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9822 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9823 * cause slot be valid again.
9824 */
9825TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9826 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009827 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009828 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9829 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9830
9831 NotifyMotionArgs motionArgs;
9832
9833 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9834 // First finger down.
9835 processId(mapper, FIRST_TRACKING_ID);
9836 processPosition(mapper, x1, y1);
9837 processPressure(mapper, RAW_PRESSURE_MAX);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9841 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9842
9843 // First finger move.
9844 processId(mapper, FIRST_TRACKING_ID);
9845 processPosition(mapper, x1 + 1, y1 + 1);
9846 processPressure(mapper, RAW_PRESSURE_MAX);
9847 processSync(mapper);
9848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9850 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9851
9852 // Second finger down.
9853 processSlot(mapper, SECOND_SLOT);
9854 processId(mapper, SECOND_TRACKING_ID);
9855 processPosition(mapper, x2, y2);
9856 processPressure(mapper, RAW_PRESSURE_MAX);
9857 processSync(mapper);
9858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009859 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009860 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9861
9862 // second finger up with some unexpected data.
9863 processSlot(mapper, SECOND_SLOT);
9864 processId(mapper, INVALID_TRACKING_ID);
9865 processPosition(mapper, x2, y2);
9866 processSync(mapper);
9867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009868 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Arthur Hung9ad18942021-06-19 02:04:46 +00009869 ASSERT_EQ(uint32_t(2), motionArgs.pointerCount);
9870
9871 // first finger up with some unexpected data.
9872 processSlot(mapper, FIRST_SLOT);
9873 processId(mapper, INVALID_TRACKING_ID);
9874 processPosition(mapper, x2, y2);
9875 processPressure(mapper, RAW_PRESSURE_MAX);
9876 processSync(mapper);
9877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9878 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9879 ASSERT_EQ(uint32_t(1), motionArgs.pointerCount);
9880}
9881
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009882TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
9883 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009884 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009885 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9886 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9887
9888 // First finger down.
9889 processId(mapper, FIRST_TRACKING_ID);
9890 processPosition(mapper, 100, 200);
9891 processPressure(mapper, RAW_PRESSURE_MAX);
9892 processSync(mapper);
9893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9894 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9895
9896 // Second finger down.
9897 processSlot(mapper, SECOND_SLOT);
9898 processId(mapper, SECOND_TRACKING_ID);
9899 processPosition(mapper, 300, 400);
9900 processPressure(mapper, RAW_PRESSURE_MAX);
9901 processSync(mapper);
9902 ASSERT_NO_FATAL_FAILURE(
9903 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9904
9905 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009906 // preserved. Resetting should cancel the ongoing gesture.
9907 resetMapper(mapper, ARBITRARY_TIME);
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9909 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009910
9911 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9912 // the existing touch state to generate a down event.
9913 processPosition(mapper, 301, 302);
9914 processSync(mapper);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9916 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
9917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9918 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
9919
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9921}
9922
9923TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
9924 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009925 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009926 prepareAxes(POSITION | ID | SLOT | PRESSURE);
9927 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
9928
9929 // First finger touches down and releases.
9930 processId(mapper, FIRST_TRACKING_ID);
9931 processPosition(mapper, 100, 200);
9932 processPressure(mapper, RAW_PRESSURE_MAX);
9933 processSync(mapper);
9934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9935 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9936 processId(mapper, INVALID_TRACKING_ID);
9937 processSync(mapper);
9938 ASSERT_NO_FATAL_FAILURE(
9939 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9940
9941 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9942 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009943 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9945
9946 // Send an empty sync frame. Since there are no pointers, no events are generated.
9947 processSync(mapper);
9948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9949}
9950
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009951TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009952 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009953 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009954 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
9955 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009957
9958 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9959 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9960 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9961 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9962 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9963
9964 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009965 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009966 processId(mapper, FIRST_TRACKING_ID);
9967 processToolType(mapper, MT_TOOL_PEN);
9968 processPosition(mapper, 100, 200);
9969 processPressure(mapper, RAW_PRESSURE_MAX);
9970 processSync(mapper);
9971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9972 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9973 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9974 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
9975
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009976 // Now that we know the device supports styluses, ensure that the device is re-configured with
9977 // the stylus source.
9978 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9979 {
9980 const auto& devices = mReader->getInputDevices();
9981 auto deviceInfo =
9982 std::find_if(devices.begin(), devices.end(),
9983 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9984 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9985 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9986 }
9987
9988 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9990
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009991 processId(mapper, INVALID_TRACKING_ID);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9995 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
9996 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009997}
9998
Seunghwan Choi356026c2023-02-01 14:37:25 +09009999TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
10000 addConfigurationProperty("touch.deviceType", "touchScreen");
10001 prepareDisplay(ui::ROTATION_0);
10002 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10003 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10004 // indicate stylus presence dynamically.
10005 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10006 std::shared_ptr<FakePointerController> fakePointerController =
10007 std::make_shared<FakePointerController>();
10008 mFakePolicy->setPointerController(fakePointerController);
10009 mFakePolicy->setStylusPointerIconEnabled(true);
10010 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10011
10012 processId(mapper, FIRST_TRACKING_ID);
10013 processPressure(mapper, RAW_PRESSURE_MIN);
10014 processPosition(mapper, 100, 200);
10015 processToolType(mapper, MT_TOOL_PEN);
10016 processSync(mapper);
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10019 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10020 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10021 ASSERT_TRUE(fakePointerController->isPointerShown());
10022 ASSERT_NO_FATAL_FAILURE(
10023 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
10024}
10025
10026TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
10027 addConfigurationProperty("touch.deviceType", "touchScreen");
10028 prepareDisplay(ui::ROTATION_0);
10029 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
10030 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
10031 // indicate stylus presence dynamically.
10032 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10033 std::shared_ptr<FakePointerController> fakePointerController =
10034 std::make_shared<FakePointerController>();
10035 mFakePolicy->setPointerController(fakePointerController);
10036 mFakePolicy->setStylusPointerIconEnabled(false);
10037 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10038
10039 processId(mapper, FIRST_TRACKING_ID);
10040 processPressure(mapper, RAW_PRESSURE_MIN);
10041 processPosition(mapper, 100, 200);
10042 processToolType(mapper, MT_TOOL_PEN);
10043 processSync(mapper);
10044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10045 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
10046 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS),
10047 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
10048 ASSERT_FALSE(fakePointerController->isPointerShown());
10049}
10050
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010051// --- MultiTouchInputMapperTest_ExternalDevice ---
10052
10053class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
10054protected:
Chris Yea52ade12020-08-27 16:49:20 -070010055 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010056};
10057
10058/**
10059 * Expect fallback to internal viewport if device is external and external viewport is not present.
10060 */
10061TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
10062 prepareAxes(POSITION);
10063 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010064 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010065 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10066
10067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10068
10069 NotifyMotionArgs motionArgs;
10070
10071 // Expect the event to be sent to the internal viewport,
10072 // because an external viewport is not present.
10073 processPosition(mapper, 100, 100);
10074 processSync(mapper);
10075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10076 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
10077
10078 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010079 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010080 processPosition(mapper, 100, 100);
10081 processSync(mapper);
10082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10083 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10084}
Arthur Hung4197f6b2020-03-16 15:39:59 +080010085
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010086TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
10087 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
10088 std::shared_ptr<FakePointerController> fakePointerController =
10089 std::make_shared<FakePointerController>();
10090 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10091 fakePointerController->setPosition(0, 0);
10092 fakePointerController->setButtonState(0);
10093
10094 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010095 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010096 prepareAxes(POSITION | ID | SLOT);
10097 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10098 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
10099 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010100 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010101 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10102
10103 // captured touchpad should be a touchpad source
10104 NotifyDeviceResetArgs resetArgs;
10105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
10106 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10107
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010108 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070010109
10110 const InputDeviceInfo::MotionRange* relRangeX =
10111 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
10112 ASSERT_NE(relRangeX, nullptr);
10113 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
10114 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
10115 const InputDeviceInfo::MotionRange* relRangeY =
10116 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
10117 ASSERT_NE(relRangeY, nullptr);
10118 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
10119 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
10120
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010121 // run captured pointer tests - note that this is unscaled, so input listener events should be
10122 // identical to what the hardware sends (accounting for any
10123 // calibration).
10124 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070010125 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010126 processId(mapper, 1);
10127 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
10128 processKey(mapper, BTN_TOUCH, 1);
10129 processSync(mapper);
10130
10131 // expect coord[0] to contain initial location of touch 0
10132 NotifyMotionArgs args;
10133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10135 ASSERT_EQ(1U, args.pointerCount);
10136 ASSERT_EQ(0, args.pointerProperties[0].id);
10137 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
10138 ASSERT_NO_FATAL_FAILURE(
10139 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10140
10141 // FINGER 1 DOWN
10142 processSlot(mapper, 1);
10143 processId(mapper, 2);
10144 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
10145 processSync(mapper);
10146
10147 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010149 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010150 ASSERT_EQ(2U, args.pointerCount);
10151 ASSERT_EQ(0, args.pointerProperties[0].id);
10152 ASSERT_EQ(1, args.pointerProperties[1].id);
10153 ASSERT_NO_FATAL_FAILURE(
10154 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10155 ASSERT_NO_FATAL_FAILURE(
10156 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
10157
10158 // FINGER 1 MOVE
10159 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
10160 processSync(mapper);
10161
10162 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
10163 // from move
10164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10166 ASSERT_NO_FATAL_FAILURE(
10167 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
10168 ASSERT_NO_FATAL_FAILURE(
10169 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10170
10171 // FINGER 0 MOVE
10172 processSlot(mapper, 0);
10173 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
10174 processSync(mapper);
10175
10176 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
10177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10178 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10179 ASSERT_NO_FATAL_FAILURE(
10180 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
10181 ASSERT_NO_FATAL_FAILURE(
10182 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
10183
10184 // BUTTON DOWN
10185 processKey(mapper, BTN_LEFT, 1);
10186 processSync(mapper);
10187
10188 // touchinputmapper design sends a move before button press
10189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10192 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10193
10194 // BUTTON UP
10195 processKey(mapper, BTN_LEFT, 0);
10196 processSync(mapper);
10197
10198 // touchinputmapper design sends a move after button release
10199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10200 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10202 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10203
10204 // FINGER 0 UP
10205 processId(mapper, -1);
10206 processSync(mapper);
10207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10208 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
10209
10210 // FINGER 1 MOVE
10211 processSlot(mapper, 1);
10212 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
10213 processSync(mapper);
10214
10215 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
10216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
10218 ASSERT_EQ(1U, args.pointerCount);
10219 ASSERT_EQ(1, args.pointerProperties[0].id);
10220 ASSERT_NO_FATAL_FAILURE(
10221 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
10222
10223 // FINGER 1 UP
10224 processId(mapper, -1);
10225 processKey(mapper, BTN_TOUCH, 0);
10226 processSync(mapper);
10227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10228 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
10229
Josep del Río2d8c79a2023-01-23 19:33:50 +000010230 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010231 mFakePolicy->setPointerCapture(false);
10232 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000010234 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010235}
10236
10237TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
10238 std::shared_ptr<FakePointerController> fakePointerController =
10239 std::make_shared<FakePointerController>();
10240 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10241 fakePointerController->setPosition(0, 0);
10242 fakePointerController->setButtonState(0);
10243
10244 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000010245 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010246 prepareAxes(POSITION | ID | SLOT);
10247 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
10248 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010249 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010250 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10251 // run uncaptured pointer tests - pushes out generic events
10252 // FINGER 0 DOWN
10253 processId(mapper, 3);
10254 processPosition(mapper, 100, 100);
10255 processKey(mapper, BTN_TOUCH, 1);
10256 processSync(mapper);
10257
10258 // start at (100,100), cursor should be at (0,0) * scale
10259 NotifyMotionArgs args;
10260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10261 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10262 ASSERT_NO_FATAL_FAILURE(
10263 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
10264
10265 // FINGER 0 MOVE
10266 processPosition(mapper, 200, 200);
10267 processSync(mapper);
10268
10269 // compute scaling to help with touch position checking
10270 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10271 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10272 float scale =
10273 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10274
10275 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10277 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
10278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
10279 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080010280
10281 // BUTTON DOWN
10282 processKey(mapper, BTN_LEFT, 1);
10283 processSync(mapper);
10284
10285 // touchinputmapper design sends a move before button press
10286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10287 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
10288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10289 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
10290
10291 // BUTTON UP
10292 processKey(mapper, BTN_LEFT, 0);
10293 processSync(mapper);
10294
10295 // touchinputmapper design sends a move after button release
10296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10297 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
10298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10299 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010300}
10301
10302TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
10303 std::shared_ptr<FakePointerController> fakePointerController =
10304 std::make_shared<FakePointerController>();
10305
Michael Wrighta9cf4192022-12-01 23:46:39 +000010306 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010307 prepareAxes(POSITION | ID | SLOT);
10308 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010309 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010310 mFakePolicy->setPointerCapture(false);
10311 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10312
Josep del Río2d8c79a2023-01-23 19:33:50 +000010313 // uncaptured touchpad should be a pointer device
10314 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010315
Josep del Río2d8c79a2023-01-23 19:33:50 +000010316 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010317 mFakePolicy->setPointerCapture(true);
10318 configureDevice(InputReaderConfiguration::CHANGE_POINTER_CAPTURE);
10319 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10320}
10321
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010322// --- BluetoothMultiTouchInputMapperTest ---
10323
10324class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10325protected:
10326 void SetUp() override {
10327 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10328 }
10329};
10330
10331TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010333 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010334 prepareAxes(POSITION | ID | SLOT | PRESSURE);
10335 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10336
10337 nsecs_t kernelEventTime = ARBITRARY_TIME;
10338 nsecs_t expectedEventTime = ARBITRARY_TIME;
10339 // Touch down.
10340 processId(mapper, FIRST_TRACKING_ID);
10341 processPosition(mapper, 100, 200);
10342 processPressure(mapper, RAW_PRESSURE_MAX);
10343 processSync(mapper, ARBITRARY_TIME);
10344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10345 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10346
10347 // Process several events that come in quick succession, according to their timestamps.
10348 for (int i = 0; i < 3; i++) {
10349 constexpr static nsecs_t delta = ms2ns(1);
10350 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10351 kernelEventTime += delta;
10352 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10353
10354 processPosition(mapper, 101 + i, 201 + i);
10355 processSync(mapper, kernelEventTime);
10356 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10357 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10358 WithEventTime(expectedEventTime))));
10359 }
10360
10361 // Release the touch.
10362 processId(mapper, INVALID_TRACKING_ID);
10363 processPressure(mapper, RAW_PRESSURE_MIN);
10364 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10367 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10368}
10369
10370// --- MultiTouchPointerModeTest ---
10371
HQ Liue6983c72022-04-19 22:14:56 +000010372class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10373protected:
10374 float mPointerMovementScale;
10375 float mPointerXZoomScale;
10376 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10377 addConfigurationProperty("touch.deviceType", "pointer");
10378 std::shared_ptr<FakePointerController> fakePointerController =
10379 std::make_shared<FakePointerController>();
10380 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10381 fakePointerController->setPosition(0, 0);
10382 fakePointerController->setButtonState(0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010383 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010384
10385 prepareAxes(POSITION);
10386 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10387 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10388 // needs to be disabled, and the pointer gesture needs to be enabled.
10389 mFakePolicy->setPointerCapture(false);
10390 mFakePolicy->setPointerGestureEnabled(true);
10391 mFakePolicy->setPointerController(fakePointerController);
10392
10393 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10394 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10395 mPointerMovementScale =
10396 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10397 mPointerXZoomScale =
10398 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10399 }
10400
10401 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10402 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10403 /*flat*/ 0,
10404 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10405 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10406 /*flat*/ 0,
10407 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10408 }
10409};
10410
10411/**
10412 * Two fingers down on a pointer mode touch pad. The width
10413 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10414 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10415 * be greater than the both value to be freeform gesture, so that after two
10416 * fingers start to move downwards, the gesture should be swipe.
10417 */
10418TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10419 // The min freeform gesture width is 25units/mm x 30mm = 750
10420 // which is greater than fraction of the diagnal length of the touchpad (349).
10421 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010422 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010423 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10424 NotifyMotionArgs motionArgs;
10425
10426 // Two fingers down at once.
10427 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10428 // Pointer's initial position is used the [0,0] coordinate.
10429 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10430
10431 processId(mapper, FIRST_TRACKING_ID);
10432 processPosition(mapper, x1, y1);
10433 processMTSync(mapper);
10434 processId(mapper, SECOND_TRACKING_ID);
10435 processPosition(mapper, x2, y2);
10436 processMTSync(mapper);
10437 processSync(mapper);
10438
10439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10440 ASSERT_EQ(1U, motionArgs.pointerCount);
10441 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10442 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010443 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010444 ASSERT_NO_FATAL_FAILURE(
10445 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10446
10447 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10448 // that there should be 1 pointer.
10449 int32_t movingDistance = 200;
10450 y1 += movingDistance;
10451 y2 += movingDistance;
10452
10453 processId(mapper, FIRST_TRACKING_ID);
10454 processPosition(mapper, x1, y1);
10455 processMTSync(mapper);
10456 processId(mapper, SECOND_TRACKING_ID);
10457 processPosition(mapper, x2, y2);
10458 processMTSync(mapper);
10459 processSync(mapper);
10460
10461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10462 ASSERT_EQ(1U, motionArgs.pointerCount);
10463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10464 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010465 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010466 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10467 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10468 0, 0, 0, 0));
10469}
10470
10471/**
10472 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10473 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10474 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10475 * value to be freeform gesture, so that after two fingers start to move downwards,
10476 * the gesture should be swipe.
10477 */
10478TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10479 // The min freeform gesture width is 5units/mm x 30mm = 150
10480 // which is greater than fraction of the diagnal length of the touchpad (349).
10481 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010482 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
HQ Liue6983c72022-04-19 22:14:56 +000010483 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10484 NotifyMotionArgs motionArgs;
10485
10486 // Two fingers down at once.
10487 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10488 // Pointer's initial position is used the [0,0] coordinate.
10489 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10490
10491 processId(mapper, FIRST_TRACKING_ID);
10492 processPosition(mapper, x1, y1);
10493 processMTSync(mapper);
10494 processId(mapper, SECOND_TRACKING_ID);
10495 processPosition(mapper, x2, y2);
10496 processMTSync(mapper);
10497 processSync(mapper);
10498
10499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10500 ASSERT_EQ(1U, motionArgs.pointerCount);
10501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10502 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010503 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010504 ASSERT_NO_FATAL_FAILURE(
10505 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10506
10507 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10508 // and there should be 1 pointer.
10509 int32_t movingDistance = 200;
10510 y1 += movingDistance;
10511 y2 += movingDistance;
10512
10513 processId(mapper, FIRST_TRACKING_ID);
10514 processPosition(mapper, x1, y1);
10515 processMTSync(mapper);
10516 processId(mapper, SECOND_TRACKING_ID);
10517 processPosition(mapper, x2, y2);
10518 processMTSync(mapper);
10519 processSync(mapper);
10520
10521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10522 ASSERT_EQ(1U, motionArgs.pointerCount);
10523 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10524 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010525 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010526 // New coordinate is the scaled relative coordinate from the initial coordinate.
10527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10528 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10529 0, 0, 0, 0));
10530}
10531
10532/**
10533 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10534 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10535 * freeform gestures after two fingers start to move downwards.
10536 */
10537TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010538 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
HQ Liue6983c72022-04-19 22:14:56 +000010539 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10540
10541 NotifyMotionArgs motionArgs;
10542
10543 // Two fingers down at once. Wider than the max swipe width.
10544 // The gesture is expected to be PRESS, then transformed to FREEFORM
10545 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10546
10547 processId(mapper, FIRST_TRACKING_ID);
10548 processPosition(mapper, x1, y1);
10549 processMTSync(mapper);
10550 processId(mapper, SECOND_TRACKING_ID);
10551 processPosition(mapper, x2, y2);
10552 processMTSync(mapper);
10553 processSync(mapper);
10554
10555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10556 ASSERT_EQ(1U, motionArgs.pointerCount);
10557 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10558 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010559 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010560 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10561 ASSERT_NO_FATAL_FAILURE(
10562 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10563
10564 int32_t movingDistance = 200;
10565
10566 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10567 // then two down events for two pointers.
10568 y1 += movingDistance;
10569 y2 += movingDistance;
10570
10571 processId(mapper, FIRST_TRACKING_ID);
10572 processPosition(mapper, x1, y1);
10573 processMTSync(mapper);
10574 processId(mapper, SECOND_TRACKING_ID);
10575 processPosition(mapper, x2, y2);
10576 processMTSync(mapper);
10577 processSync(mapper);
10578
10579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10580 // The previous PRESS gesture is cancelled, because it is transformed to freeform
10581 ASSERT_EQ(1U, motionArgs.pointerCount);
10582 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10584 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
10585 ASSERT_EQ(1U, motionArgs.pointerCount);
10586 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10588 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010589 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010590 ASSERT_EQ(2U, motionArgs.pointerCount);
10591 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
10592 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010593 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010594 // Two pointers' scaled relative coordinates from their initial centroid.
10595 // Initial y coordinates are 0 as y1 and y2 have the same value.
10596 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10597 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10598 // When pointers move, the new coordinates equal to the initial coordinates plus
10599 // scaled moving distance.
10600 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10601 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10602 0, 0, 0, 0));
10603 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10604 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10605 0, 0, 0, 0));
10606
10607 // Move two fingers down again, expect one MOVE motion event.
10608 y1 += movingDistance;
10609 y2 += movingDistance;
10610
10611 processId(mapper, FIRST_TRACKING_ID);
10612 processPosition(mapper, x1, y1);
10613 processMTSync(mapper);
10614 processId(mapper, SECOND_TRACKING_ID);
10615 processPosition(mapper, x2, y2);
10616 processMTSync(mapper);
10617 processSync(mapper);
10618
10619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10620 ASSERT_EQ(2U, motionArgs.pointerCount);
10621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10622 ASSERT_EQ(AMOTION_EVENT_TOOL_TYPE_FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010623 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010624 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10625 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10626 0, 0, 0, 0, 0));
10627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10628 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10629 0, 0, 0, 0, 0));
10630}
10631
Harry Cutts39b7ca22022-10-05 15:55:48 +000010632TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010633 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Harry Cutts39b7ca22022-10-05 15:55:48 +000010634 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10635 NotifyMotionArgs motionArgs;
10636
10637 // Place two fingers down.
10638 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10639
10640 processId(mapper, FIRST_TRACKING_ID);
10641 processPosition(mapper, x1, y1);
10642 processMTSync(mapper);
10643 processId(mapper, SECOND_TRACKING_ID);
10644 processPosition(mapper, x2, y2);
10645 processMTSync(mapper);
10646 processSync(mapper);
10647
10648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10649 ASSERT_EQ(1U, motionArgs.pointerCount);
10650 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10651 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10652 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10653 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10654
10655 // Move the two fingers down and to the left.
10656 int32_t movingDistance = 200;
10657 x1 -= movingDistance;
10658 y1 += movingDistance;
10659 x2 -= movingDistance;
10660 y2 += movingDistance;
10661
10662 processId(mapper, FIRST_TRACKING_ID);
10663 processPosition(mapper, x1, y1);
10664 processMTSync(mapper);
10665 processId(mapper, SECOND_TRACKING_ID);
10666 processPosition(mapper, x2, y2);
10667 processMTSync(mapper);
10668 processSync(mapper);
10669
10670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10671 ASSERT_EQ(1U, motionArgs.pointerCount);
10672 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10673 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10674 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10675 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10676}
10677
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010678TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010679 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010680 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
10681 MultiTouchInputMapper& mapper = addMapperAndConfigure<MultiTouchInputMapper>();
10682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10683
10684 // Start a stylus gesture.
10685 processKey(mapper, BTN_TOOL_PEN, 1);
10686 processId(mapper, FIRST_TRACKING_ID);
10687 processPosition(mapper, 100, 200);
10688 processSync(mapper);
10689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10691 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10692 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10693 // TODO(b/257078296): Pointer mode generates extra event.
10694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10695 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10696 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10697 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10699
10700 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10701 // gesture should be disabled.
10702 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10703 viewport->isActive = false;
10704 mFakePolicy->updateViewport(*viewport);
10705 configureDevice(InputReaderConfiguration::CHANGE_DISPLAY_INFO);
10706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10707 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10708 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10709 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10710 // TODO(b/257078296): Pointer mode generates extra event.
10711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10712 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10713 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
10714 WithToolType(AMOTION_EVENT_TOOL_TYPE_STYLUS))));
10715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10716}
10717
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010718// --- JoystickInputMapperTest ---
10719
10720class JoystickInputMapperTest : public InputMapperTest {
10721protected:
10722 static const int32_t RAW_X_MIN;
10723 static const int32_t RAW_X_MAX;
10724 static const int32_t RAW_Y_MIN;
10725 static const int32_t RAW_Y_MAX;
10726
10727 void SetUp() override {
10728 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10729 }
10730 void prepareAxes() {
10731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10732 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10733 }
10734
10735 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10737 }
10738
10739 void processSync(JoystickInputMapper& mapper) {
10740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10741 }
10742
Michael Wrighta9cf4192022-12-01 23:46:39 +000010743 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010744 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10745 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10746 NO_PORT, ViewportType::VIRTUAL);
10747 }
10748};
10749
10750const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10751const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10752const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10753const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10754
10755TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10756 prepareAxes();
10757 JoystickInputMapper& mapper = addMapperAndConfigure<JoystickInputMapper>();
10758
10759 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10760
Michael Wrighta9cf4192022-12-01 23:46:39 +000010761 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010762
10763 // Send an axis event
10764 processAxis(mapper, ABS_X, 100);
10765 processSync(mapper);
10766
10767 NotifyMotionArgs args;
10768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10769 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10770
10771 // Send another axis event
10772 processAxis(mapper, ABS_Y, 100);
10773 processSync(mapper);
10774
10775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10776 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10777}
10778
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010779// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010780
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010781class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010782protected:
10783 static const char* DEVICE_NAME;
10784 static const char* DEVICE_LOCATION;
10785 static const int32_t DEVICE_ID;
10786 static const int32_t DEVICE_GENERATION;
10787 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010788 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010789 static const int32_t EVENTHUB_ID;
10790
10791 std::shared_ptr<FakeEventHub> mFakeEventHub;
10792 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010793 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010794 std::unique_ptr<InstrumentedInputReader> mReader;
10795 std::shared_ptr<InputDevice> mDevice;
10796
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010797 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010798 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010799 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010800 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010801 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010802 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010803 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10804 }
10805
10806 void SetUp() override { SetUp(DEVICE_CLASSES); }
10807
10808 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010809 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010810 mFakePolicy.clear();
10811 }
10812
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010813 std::list<NotifyArgs> configureDevice(uint32_t changes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010814 if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
10815 mReader->requestRefreshConfiguration(changes);
10816 mReader->loopOnce();
10817 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010818 return mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010819 }
10820
10821 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10822 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010823 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010824 InputDeviceIdentifier identifier;
10825 identifier.name = name;
10826 identifier.location = location;
10827 std::shared_ptr<InputDevice> device =
10828 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10829 identifier);
10830 mReader->pushNextDevice(device);
10831 mFakeEventHub->addDevice(eventHubId, name, classes);
10832 mReader->loopOnce();
10833 return device;
10834 }
10835
10836 template <class T, typename... Args>
10837 T& addControllerAndConfigure(Args... args) {
10838 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10839
10840 return controller;
10841 }
10842};
10843
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010844const char* PeripheralControllerTest::DEVICE_NAME = "device";
10845const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10846const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10847const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10848const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010849const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10850 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010851const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010852
10853// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010854class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010855protected:
10856 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010857 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010858 }
10859};
10860
10861TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010862 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010863
Harry Cuttsa5b71292022-11-28 12:56:17 +000010864 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10865 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10866 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010867}
10868
10869TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010870 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010871
Harry Cuttsa5b71292022-11-28 12:56:17 +000010872 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10873 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10874 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010875}
10876
10877// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010878class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010879protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010880 void SetUp() override {
10881 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10882 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010883};
10884
Chris Ye85758332021-05-16 23:05:17 -070010885TEST_F(LightControllerTest, MonoLight) {
10886 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010887 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010888 .maxBrightness = 255,
10889 .flags = InputLightClass::BRIGHTNESS,
10890 .path = ""};
10891 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010892
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010893 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010894 InputDeviceInfo info;
10895 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010896 std::vector<InputDeviceLightInfo> lights = info.getLights();
10897 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010898 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10899 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10900
10901 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10902 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10903}
10904
10905TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10906 RawLightInfo infoMono = {.id = 1,
10907 .name = "mono_keyboard_backlight",
10908 .maxBrightness = 255,
10909 .flags = InputLightClass::BRIGHTNESS |
10910 InputLightClass::KEYBOARD_BACKLIGHT,
10911 .path = ""};
10912 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10913
10914 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10915 InputDeviceInfo info;
10916 controller.populateDeviceInfo(&info);
10917 std::vector<InputDeviceLightInfo> lights = info.getLights();
10918 ASSERT_EQ(1U, lights.size());
10919 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10920 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010921
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010922 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10923 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010924}
10925
10926TEST_F(LightControllerTest, RGBLight) {
10927 RawLightInfo infoRed = {.id = 1,
10928 .name = "red",
10929 .maxBrightness = 255,
10930 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10931 .path = ""};
10932 RawLightInfo infoGreen = {.id = 2,
10933 .name = "green",
10934 .maxBrightness = 255,
10935 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10936 .path = ""};
10937 RawLightInfo infoBlue = {.id = 3,
10938 .name = "blue",
10939 .maxBrightness = 255,
10940 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10941 .path = ""};
10942 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10943 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10944 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10945
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010946 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010947 InputDeviceInfo info;
10948 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010949 std::vector<InputDeviceLightInfo> lights = info.getLights();
10950 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010951 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10952 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10953 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10954
10955 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10956 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10957}
10958
10959TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10960 RawLightInfo infoRed = {.id = 1,
10961 .name = "red_keyboard_backlight",
10962 .maxBrightness = 255,
10963 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10964 InputLightClass::KEYBOARD_BACKLIGHT,
10965 .path = ""};
10966 RawLightInfo infoGreen = {.id = 2,
10967 .name = "green_keyboard_backlight",
10968 .maxBrightness = 255,
10969 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10970 InputLightClass::KEYBOARD_BACKLIGHT,
10971 .path = ""};
10972 RawLightInfo infoBlue = {.id = 3,
10973 .name = "blue_keyboard_backlight",
10974 .maxBrightness = 255,
10975 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10976 InputLightClass::KEYBOARD_BACKLIGHT,
10977 .path = ""};
10978 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10979 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10980 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10981
10982 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10983 InputDeviceInfo info;
10984 controller.populateDeviceInfo(&info);
10985 std::vector<InputDeviceLightInfo> lights = info.getLights();
10986 ASSERT_EQ(1U, lights.size());
10987 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10988 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10989 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10990
10991 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10992 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10993}
10994
10995TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10996 RawLightInfo infoRed = {.id = 1,
10997 .name = "red",
10998 .maxBrightness = 255,
10999 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
11000 .path = ""};
11001 RawLightInfo infoGreen = {.id = 2,
11002 .name = "green",
11003 .maxBrightness = 255,
11004 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
11005 .path = ""};
11006 RawLightInfo infoBlue = {.id = 3,
11007 .name = "blue",
11008 .maxBrightness = 255,
11009 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
11010 .path = ""};
11011 RawLightInfo infoGlobal = {.id = 3,
11012 .name = "global_keyboard_backlight",
11013 .maxBrightness = 255,
11014 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
11015 InputLightClass::KEYBOARD_BACKLIGHT,
11016 .path = ""};
11017 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
11018 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
11019 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
11020 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
11021
11022 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11023 InputDeviceInfo info;
11024 controller.populateDeviceInfo(&info);
11025 std::vector<InputDeviceLightInfo> lights = info.getLights();
11026 ASSERT_EQ(1U, lights.size());
11027 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11028 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11029 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011030
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011031 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11032 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011033}
11034
11035TEST_F(LightControllerTest, MultiColorRGBLight) {
11036 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011037 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080011038 .maxBrightness = 255,
11039 .flags = InputLightClass::BRIGHTNESS |
11040 InputLightClass::MULTI_INTENSITY |
11041 InputLightClass::MULTI_INDEX,
11042 .path = ""};
11043
11044 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11045
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011046 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011047 InputDeviceInfo info;
11048 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011049 std::vector<InputDeviceLightInfo> lights = info.getLights();
11050 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011051 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11052 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11053 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
11054
11055 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11056 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
11057}
11058
11059TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
11060 RawLightInfo infoColor = {.id = 1,
11061 .name = "multi_color_keyboard_backlight",
11062 .maxBrightness = 255,
11063 .flags = InputLightClass::BRIGHTNESS |
11064 InputLightClass::MULTI_INTENSITY |
11065 InputLightClass::MULTI_INDEX |
11066 InputLightClass::KEYBOARD_BACKLIGHT,
11067 .path = ""};
11068
11069 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
11070
11071 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11072 InputDeviceInfo info;
11073 controller.populateDeviceInfo(&info);
11074 std::vector<InputDeviceLightInfo> lights = info.getLights();
11075 ASSERT_EQ(1U, lights.size());
11076 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11077 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11078 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011079
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011080 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11081 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011082}
11083
11084TEST_F(LightControllerTest, PlayerIdLight) {
11085 RawLightInfo info1 = {.id = 1,
11086 .name = "player1",
11087 .maxBrightness = 255,
11088 .flags = InputLightClass::BRIGHTNESS,
11089 .path = ""};
11090 RawLightInfo info2 = {.id = 2,
11091 .name = "player2",
11092 .maxBrightness = 255,
11093 .flags = InputLightClass::BRIGHTNESS,
11094 .path = ""};
11095 RawLightInfo info3 = {.id = 3,
11096 .name = "player3",
11097 .maxBrightness = 255,
11098 .flags = InputLightClass::BRIGHTNESS,
11099 .path = ""};
11100 RawLightInfo info4 = {.id = 4,
11101 .name = "player4",
11102 .maxBrightness = 255,
11103 .flags = InputLightClass::BRIGHTNESS,
11104 .path = ""};
11105 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
11106 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
11107 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
11108 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
11109
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011110 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011111 InputDeviceInfo info;
11112 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011113 std::vector<InputDeviceLightInfo> lights = info.getLights();
11114 ASSERT_EQ(1U, lights.size());
11115 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011116 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11117 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011118
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011119 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
11120 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
11121 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011122}
11123
Michael Wrightd02c5b62014-02-10 15:10:22 -080011124} // namespace android