blob: 5044b3e7773e7159c9d39ec44034f4765635a9ba [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>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
100// Maximum smoothing time delta so that we don't generate events too far into the future.
101constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
102
Byoungho Jungda10dd32023-10-06 17:03:45 +0900103namespace input_flags = com::android::input::flags;
104
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105template<typename T>
106static inline T min(T a, T b) {
107 return a < b ? a : b;
108}
109
110static inline float avg(float x, float y) {
111 return (x + y) / 2;
112}
113
Chris Ye3fdbfef2021-01-06 18:45:18 -0800114// Mapping for light color name and the light color
115const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
116 {"green", LightColor::GREEN},
117 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700120 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000121 case ui::ROTATION_90:
122 return ui::ROTATION_270;
123 case ui::ROTATION_270:
124 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700125 default:
126 return orientation;
127 }
128}
129
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800130static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
131 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000132 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800133
134 const InputDeviceInfo::MotionRange* motionRange =
135 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
136 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
137}
138
139static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
140 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000141 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_EQ(nullptr, motionRange);
146}
147
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700148[[maybe_unused]] static void dumpReader(InputReader& reader) {
149 std::string dump;
150 reader.dump(dump);
151 std::istringstream iss(dump);
152 for (std::string line; std::getline(iss, line);) {
153 ALOGE("%s", line.c_str());
154 std::this_thread::sleep_for(std::chrono::milliseconds(1));
155 }
156}
157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158// --- FakeInputMapper ---
159
160class FakeInputMapper : public InputMapper {
161 uint32_t mSources;
162 int32_t mKeyboardType;
163 int32_t mMetaState;
164 KeyedVector<int32_t, int32_t> mKeyCodeStates;
165 KeyedVector<int32_t, int32_t> mScanCodeStates;
166 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100167 // fake mapping which would normally come from keyCharacterMap
168 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700170 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700172 std::mutex mLock;
173 std::condition_variable mStateChangedCondition;
174 bool mConfigureWasCalled GUARDED_BY(mLock);
175 bool mResetWasCalled GUARDED_BY(mLock);
176 bool mProcessWasCalled GUARDED_BY(mLock);
177 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Arthur Hungc23540e2018-11-29 20:42:11 +0800179 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000181 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
182 uint32_t sources)
183 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800184 mSources(sources),
185 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800187 mConfigureWasCalled(false),
188 mResetWasCalled(false),
189 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
193 void setKeyboardType(int32_t keyboardType) {
194 mKeyboardType = keyboardType;
195 }
196
197 void setMetaState(int32_t metaState) {
198 mMetaState = metaState;
199 }
200
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700201 // Sets the return value for the `process` call.
202 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
203 mProcessResult.clear();
204 for (auto notifyArg : notifyArgs) {
205 mProcessResult.push_back(notifyArg);
206 }
207 }
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700210 std::unique_lock<std::mutex> lock(mLock);
211 base::ScopedLockAssertion assumeLocked(mLock);
212 const bool configureCalled =
213 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
214 return mConfigureWasCalled;
215 });
216 if (!configureCalled) {
217 FAIL() << "Expected configure() to have been called.";
218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 mConfigureWasCalled = false;
220 }
221
222 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700223 std::unique_lock<std::mutex> lock(mLock);
224 base::ScopedLockAssertion assumeLocked(mLock);
225 const bool resetCalled =
226 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
227 return mResetWasCalled;
228 });
229 if (!resetCalled) {
230 FAIL() << "Expected reset() to have been called.";
231 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 mResetWasCalled = false;
233 }
234
Yi Kong9b14ac62018-07-17 13:48:38 -0700235 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700236 std::unique_lock<std::mutex> lock(mLock);
237 base::ScopedLockAssertion assumeLocked(mLock);
238 const bool processCalled =
239 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
240 return mProcessWasCalled;
241 });
242 if (!processCalled) {
243 FAIL() << "Expected process() to have been called.";
244 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800245 if (outLastEvent) {
246 *outLastEvent = mLastEvent;
247 }
248 mProcessWasCalled = false;
249 }
250
251 void setKeyCodeState(int32_t keyCode, int32_t state) {
252 mKeyCodeStates.replaceValueFor(keyCode, state);
253 }
254
255 void setScanCodeState(int32_t scanCode, int32_t state) {
256 mScanCodeStates.replaceValueFor(scanCode, state);
257 }
258
259 void setSwitchState(int32_t switchCode, int32_t state) {
260 mSwitchStates.replaceValueFor(switchCode, state);
261 }
262
263 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800264 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800265 }
266
Philip Junker4af3b3d2021-12-14 10:36:55 +0100267 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
268 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
269 }
270
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100272 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273
Harry Cuttsd02ea102023-03-17 18:21:30 +0000274 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 InputMapper::populateDeviceInfo(deviceInfo);
276
277 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000278 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279 }
280 }
281
Arpit Singhed6c3de2023-04-05 19:24:37 +0000282 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000283 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700284 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800286
287 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800288 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000289 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800291 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292
293 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700294 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 }
296
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700297 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700298 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800299 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700301 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800302 }
303
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700304 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700305 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800306 mLastEvent = *rawEvent;
307 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700309 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Chris Yea52ade12020-08-27 16:49:20 -0700312 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
314 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
315 }
316
Philip Junker4af3b3d2021-12-14 10:36:55 +0100317 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
318 auto it = mKeyCodeMapping.find(locationKeyCode);
319 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
320 }
321
Chris Yea52ade12020-08-27 16:49:20 -0700322 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
324 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
325 }
326
Chris Yea52ade12020-08-27 16:49:20 -0700327 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800328 ssize_t index = mSwitchStates.indexOfKey(switchCode);
329 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
330 }
331
Chris Yea52ade12020-08-27 16:49:20 -0700332 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700333 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700334 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700335 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
337 if (keyCodes[i] == mSupportedKeyCodes[j]) {
338 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800339 }
340 }
341 }
Chris Yea52ade12020-08-27 16:49:20 -0700342 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800343 return result;
344 }
345
346 virtual int32_t getMetaState() {
347 return mMetaState;
348 }
349
350 virtual void fadePointer() {
351 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800352
353 virtual std::optional<int32_t> getAssociatedDisplay() {
354 if (mViewport) {
355 return std::make_optional(mViewport->displayId);
356 }
357 return std::nullopt;
358 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800359};
360
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700361// --- InputReaderPolicyTest ---
362class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700363protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700364 sp<FakeInputReaderPolicy> mFakePolicy;
365
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700366 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700367 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700368};
369
370/**
371 * Check that empty set of viewports is an acceptable configuration.
372 * Also try to get internal viewport two different ways - by type and by uniqueId.
373 *
374 * There will be confusion if two viewports with empty uniqueId and identical type are present.
375 * Such configuration is not currently allowed.
376 */
377TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700378 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700379
380 // We didn't add any viewports yet, so there shouldn't be any.
381 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100382 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700383 ASSERT_FALSE(internalViewport);
384
385 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000386 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000387 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700388
389 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700390 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100392 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700393
394 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100395 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700397 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700398
399 mFakePolicy->clearViewports();
400 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700401 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_FALSE(internalViewport);
405}
406
407TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
408 const std::string internalUniqueId = "local:0";
409 const std::string externalUniqueId = "local:1";
410 const std::string virtualUniqueId1 = "virtual:2";
411 const std::string virtualUniqueId2 = "virtual:3";
412 constexpr int32_t virtualDisplayId1 = 2;
413 constexpr int32_t virtualDisplayId2 = 3;
414
415 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000416 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000417 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000418 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700419 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000420 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000421 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000422 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700423 // Add an virtual viewport
424 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000425 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add another virtual viewport
428 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000429 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431
432 // Check matching by type for internal
433 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100434 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 ASSERT_TRUE(internalViewport);
436 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
437
438 // Check matching by type for external
439 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100440 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(externalViewport);
442 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
443
444 // Check matching by uniqueId for virtual viewport #1
445 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700446 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700447 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
450 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
451
452 // Check matching by uniqueId for virtual viewport #2
453 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
458 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
459}
460
461
462/**
463 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
464 * that lookup works by checking display id.
465 * Check that 2 viewports of each kind is possible, for all existing viewport types.
466 */
467TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
468 const std::string uniqueId1 = "uniqueId1";
469 const std::string uniqueId2 = "uniqueId2";
470 constexpr int32_t displayId1 = 2;
471 constexpr int32_t displayId2 = 3;
472
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100473 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
474 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700475 for (const ViewportType& type : types) {
476 mFakePolicy->clearViewports();
477 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000478 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000479 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700480 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000481 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000482 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483
484 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700485 std::optional<DisplayViewport> viewport1 =
486 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700487 ASSERT_TRUE(viewport1);
488 ASSERT_EQ(displayId1, viewport1->displayId);
489 ASSERT_EQ(type, viewport1->type);
490
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700491 std::optional<DisplayViewport> viewport2 =
492 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700493 ASSERT_TRUE(viewport2);
494 ASSERT_EQ(displayId2, viewport2->displayId);
495 ASSERT_EQ(type, viewport2->type);
496
497 // When there are multiple viewports of the same kind, and uniqueId is not specified
498 // in the call to getDisplayViewport, then that situation is not supported.
499 // The viewports can be stored in any order, so we cannot rely on the order, since that
500 // is just implementation detail.
501 // However, we can check that it still returns *a* viewport, we just cannot assert
502 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700503 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700504 ASSERT_TRUE(someViewport);
505 }
506}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800507
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700508/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000509 * When we have multiple internal displays make sure we always return the default display when
510 * querying by type.
511 */
512TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
513 const std::string uniqueId1 = "uniqueId1";
514 const std::string uniqueId2 = "uniqueId2";
515 constexpr int32_t nonDefaultDisplayId = 2;
516 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
517 "Test display ID should not be ADISPLAY_ID_DEFAULT");
518
519 // Add the default display first and ensure it gets returned.
520 mFakePolicy->clearViewports();
521 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000522 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000523 ViewportType::INTERNAL);
524 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000525 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000526 ViewportType::INTERNAL);
527
528 std::optional<DisplayViewport> viewport =
529 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
530 ASSERT_TRUE(viewport);
531 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
532 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
533
534 // Add the default display second to make sure order doesn't matter.
535 mFakePolicy->clearViewports();
536 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000537 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000538 ViewportType::INTERNAL);
539 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000540 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000541 ViewportType::INTERNAL);
542
543 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
544 ASSERT_TRUE(viewport);
545 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
546 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
547}
548
549/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700550 * Check getDisplayViewportByPort
551 */
552TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100553 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700554 const std::string uniqueId1 = "uniqueId1";
555 const std::string uniqueId2 = "uniqueId2";
556 constexpr int32_t displayId1 = 1;
557 constexpr int32_t displayId2 = 2;
558 const uint8_t hdmi1 = 0;
559 const uint8_t hdmi2 = 1;
560 const uint8_t hdmi3 = 2;
561
562 mFakePolicy->clearViewports();
563 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000564 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000565 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700566 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000567 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000568 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700569
570 // Check that correct display viewport was returned by comparing the display ports.
571 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
572 ASSERT_TRUE(hdmi1Viewport);
573 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
574 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
575
576 // Check that we can still get the same viewport using the uniqueId
577 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
578 ASSERT_TRUE(hdmi1Viewport);
579 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
580 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
581 ASSERT_EQ(type, hdmi1Viewport->type);
582
583 // Check that we cannot find a port with "HDMI2", because we never added one
584 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
585 ASSERT_FALSE(hdmi2Viewport);
586}
587
Michael Wrightd02c5b62014-02-10 15:10:22 -0800588// --- InputReaderTest ---
589
590class InputReaderTest : public testing::Test {
591protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700592 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800593 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700594 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000595 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596
Chris Yea52ade12020-08-27 16:49:20 -0700597 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700598 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700599 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601
Prabir Pradhan28efc192019-11-05 01:10:04 +0000602 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700603 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604 }
605
Chris Yea52ade12020-08-27 16:49:20 -0700606 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700607 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800608 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609 }
610
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700611 void addDevice(int32_t eventHubId, const std::string& name,
612 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800613 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614
615 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800616 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000619 mReader->loopOnce();
620 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700621 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700623 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624 }
625
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800626 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700627 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000628 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700629 }
630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800631 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700632 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000633 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700634 }
635
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800636 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700637 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700638 ftl::Flags<InputDeviceClass> classes,
639 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800640 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800641 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000642 FakeInputMapper& mapper =
643 device->addMapper<FakeInputMapper>(eventHubId,
644 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800645 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800646 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800647 return mapper;
648 }
649};
650
Chris Ye98d3f532020-10-01 21:48:59 -0700651TEST_F(InputReaderTest, PolicyGetInputDevices) {
652 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700653 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700654 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655
656 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700657 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800658 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800659 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100660 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800661 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
662 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000663 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800664}
665
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000666TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
667 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
668 mFakeEventHub->setSysfsRootPath(1, "xyz");
669
670 // Should also have received a notification describing the new input device.
671 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
672 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
673 ASSERT_EQ(0U, inputDevice.getLights().size());
674
675 RawLightInfo infoMonolight = {.id = 123,
676 .name = "mono_keyboard_backlight",
677 .maxBrightness = 255,
678 .flags = InputLightClass::BRIGHTNESS,
679 .path = ""};
680 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
681 mReader->sysfsNodeChanged("xyz");
682 mReader->loopOnce();
683
684 // Should also have received a notification describing the new recreated input device.
685 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
686 inputDevice = mFakePolicy->getInputDevices()[0];
687 ASSERT_EQ(1U, inputDevice.getLights().size());
688}
689
Chris Yee7310032020-09-22 15:36:28 -0700690TEST_F(InputReaderTest, GetMergedInputDevices) {
691 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
692 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
693 // Add two subdevices to device
694 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
695 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000696 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
697 AINPUT_SOURCE_KEYBOARD);
698 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
699 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700700
701 // Push same device instance for next device to be added, so they'll have same identifier.
702 mReader->pushNextDevice(device);
703 mReader->pushNextDevice(device);
704 ASSERT_NO_FATAL_FAILURE(
705 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
706 ASSERT_NO_FATAL_FAILURE(
707 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
708
709 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000710 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700711}
712
Chris Yee14523a2020-12-19 13:46:00 -0800713TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
714 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
715 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
716 // Add two subdevices to device
717 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
718 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000719 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
720 AINPUT_SOURCE_KEYBOARD);
721 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
722 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800723
724 // Push same device instance for next device to be added, so they'll have same identifier.
725 mReader->pushNextDevice(device);
726 mReader->pushNextDevice(device);
727 // Sensor device is initially disabled
728 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
729 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
730 nullptr));
731 // Device is disabled because the only sub device is a sensor device and disabled initially.
732 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
733 ASSERT_FALSE(device->isEnabled());
734 ASSERT_NO_FATAL_FAILURE(
735 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
736 // The merged device is enabled if any sub device is enabled
737 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
738 ASSERT_TRUE(device->isEnabled());
739}
740
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700741TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800742 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700743 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800744 constexpr int32_t eventHubId = 1;
745 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000747 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
748 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800749 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700751
Yi Kong9b14ac62018-07-17 13:48:38 -0700752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700753
754 NotifyDeviceResetArgs resetArgs;
755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700756 ASSERT_EQ(deviceId, resetArgs.deviceId);
757
758 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800759 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000760 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700763 ASSERT_EQ(deviceId, resetArgs.deviceId);
764 ASSERT_EQ(device->isEnabled(), false);
765
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800766 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000767 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700770 ASSERT_EQ(device->isEnabled(), false);
771
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800772 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000773 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700775 ASSERT_EQ(deviceId, resetArgs.deviceId);
776 ASSERT_EQ(device->isEnabled(), true);
777}
778
Michael Wrightd02c5b62014-02-10 15:10:22 -0800779TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800780 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700781 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800783 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800784 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800785 AINPUT_SOURCE_KEYBOARD, nullptr);
786 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787
788 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
789 AINPUT_SOURCE_ANY, AKEYCODE_A))
790 << "Should return unknown when the device id is >= 0 but unknown.";
791
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 ASSERT_EQ(AKEY_STATE_UNKNOWN,
793 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
794 << "Should return unknown when the device id is valid but the sources are not "
795 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800796
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800797 ASSERT_EQ(AKEY_STATE_DOWN,
798 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
799 AKEYCODE_A))
800 << "Should return value provided by mapper when device id is valid and the device "
801 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800802
803 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
804 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
805 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
806
807 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
808 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
809 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
810}
811
Philip Junker4af3b3d2021-12-14 10:36:55 +0100812TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
813 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
814 constexpr int32_t eventHubId = 1;
815 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
816 InputDeviceClass::KEYBOARD,
817 AINPUT_SOURCE_KEYBOARD, nullptr);
818 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
819
820 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
821 << "Should return unknown when the device with the specified id is not found.";
822
823 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
824 << "Should return correct mapping when device id is valid and mapping exists.";
825
826 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
827 << "Should return the location key code when device id is valid and there's no "
828 "mapping.";
829}
830
831TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
832 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
833 constexpr int32_t eventHubId = 1;
834 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
835 InputDeviceClass::JOYSTICK,
836 AINPUT_SOURCE_GAMEPAD, nullptr);
837 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
838
839 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
840 << "Should return unknown when the device id is valid but there is no keyboard mapper";
841}
842
Michael Wrightd02c5b62014-02-10 15:10:22 -0800843TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800844 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700845 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800847 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800849 AINPUT_SOURCE_KEYBOARD, nullptr);
850 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851
852 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
853 AINPUT_SOURCE_ANY, KEY_A))
854 << "Should return unknown when the device id is >= 0 but unknown.";
855
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 ASSERT_EQ(AKEY_STATE_UNKNOWN,
857 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
858 << "Should return unknown when the device id is valid but the sources are not "
859 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800860
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800861 ASSERT_EQ(AKEY_STATE_DOWN,
862 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
863 KEY_A))
864 << "Should return value provided by mapper when device id is valid and the device "
865 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800866
867 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
868 AINPUT_SOURCE_TRACKBALL, KEY_A))
869 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
870
871 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
872 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
873 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
874}
875
876TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800877 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700878 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800880 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800881 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800882 AINPUT_SOURCE_KEYBOARD, nullptr);
883 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800884
885 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
886 AINPUT_SOURCE_ANY, SW_LID))
887 << "Should return unknown when the device id is >= 0 but unknown.";
888
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 ASSERT_EQ(AKEY_STATE_UNKNOWN,
890 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
891 << "Should return unknown when the device id is valid but the sources are not "
892 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800893
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800894 ASSERT_EQ(AKEY_STATE_DOWN,
895 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
896 SW_LID))
897 << "Should return value provided by mapper when device id is valid and the device "
898 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800899
900 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
901 AINPUT_SOURCE_TRACKBALL, SW_LID))
902 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
903
904 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
905 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
906 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
907}
908
909TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800910 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700911 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800912 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800913 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800915 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800917 mapper.addSupportedKeyCode(AKEYCODE_A);
918 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800919
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700920 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921 uint8_t flags[4] = { 0, 0, 0, 1 };
922
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700923 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800924 << "Should return false when device id is >= 0 but unknown.";
925 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
926
927 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800929 << "Should return false when device id is valid but the sources are not supported by "
930 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
932
933 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700934 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800935 keyCodes, flags))
936 << "Should return value provided by mapper when device id is valid and the device "
937 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800938 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
939
940 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700941 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
942 << "Should return false when the device id is < 0 but the sources are not supported by "
943 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800944 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
945
946 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700947 ASSERT_TRUE(
948 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
949 << "Should return value provided by mapper when device id is < 0 and one of the "
950 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800951 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
952}
953
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000954TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800955 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700956 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800957
958 NotifyConfigurationChangedArgs args;
959
960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
961 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
962}
963
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000964TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700966 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000967 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800968 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000969 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800970 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800971 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800972 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800973
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000974 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000975 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800976 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
977
978 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800979 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000980 ASSERT_EQ(when, event.when);
981 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800982 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983 ASSERT_EQ(EV_KEY, event.type);
984 ASSERT_EQ(KEY_A, event.code);
985 ASSERT_EQ(1, event.value);
986}
987
Garfield Tan1c7bc862020-01-28 13:24:04 -0800988TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800989 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700990 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800991 constexpr int32_t eventHubId = 1;
992 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800993 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000994 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
995 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800996 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800998
999 NotifyDeviceResetArgs resetArgs;
1000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001001 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001003 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001004 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001006 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001007 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001008
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001009 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001010 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001012 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001013 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001015 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001016 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001018 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001019 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001020}
1021
Garfield Tan1c7bc862020-01-28 13:24:04 -08001022TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1023 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001024 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001025 constexpr int32_t eventHubId = 1;
1026 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1027 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001028 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1029 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001030 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001031 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1032
1033 NotifyDeviceResetArgs resetArgs;
1034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1035 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1036}
1037
Arthur Hungc23540e2018-11-29 20:42:11 +08001038TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001039 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001040 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001041 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001042 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001043 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1044 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001045 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1046 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001047 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001048
1049 const uint8_t hdmi1 = 1;
1050
1051 // Associated touch screen with second display.
1052 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1053
1054 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001055 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001056 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001057 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001058 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001059 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001060 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001061 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001062 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063
1064 // Add the device, and make sure all of the callbacks are triggered.
1065 // The device is added after the input port associations are processed since
1066 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001067 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001070 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001071
Arthur Hung2c9a3342019-07-23 14:18:59 +08001072 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001073 ASSERT_EQ(deviceId, device->getId());
1074 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1075 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001076
1077 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001079 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001081}
1082
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001083TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1084 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001085 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001086 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1087 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1088 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001089 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1090 AINPUT_SOURCE_KEYBOARD);
1091 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1092 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001093 mReader->pushNextDevice(device);
1094 mReader->pushNextDevice(device);
1095 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1096 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1097
1098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1099
1100 NotifyDeviceResetArgs resetArgs;
1101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1102 ASSERT_EQ(deviceId, resetArgs.deviceId);
1103 ASSERT_TRUE(device->isEnabled());
1104 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1105 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1106
1107 disableDevice(deviceId);
1108 mReader->loopOnce();
1109
1110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1111 ASSERT_EQ(deviceId, resetArgs.deviceId);
1112 ASSERT_FALSE(device->isEnabled());
1113 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1114 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1115
1116 enableDevice(deviceId);
1117 mReader->loopOnce();
1118
1119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1120 ASSERT_EQ(deviceId, resetArgs.deviceId);
1121 ASSERT_TRUE(device->isEnabled());
1122 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1123 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1124}
1125
1126TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1127 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001128 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001129 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1130 // Add two subdevices to device
1131 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1132 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001133 device->addMapper<FakeInputMapper>(eventHubIds[0],
1134 mFakePolicy->getReaderConfiguration(),
1135 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001136 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001137 device->addMapper<FakeInputMapper>(eventHubIds[1],
1138 mFakePolicy->getReaderConfiguration(),
1139 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001140 mReader->pushNextDevice(device);
1141 mReader->pushNextDevice(device);
1142 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1143 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1144
1145 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1146 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1147
1148 ASSERT_EQ(AKEY_STATE_DOWN,
1149 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1150 ASSERT_EQ(AKEY_STATE_DOWN,
1151 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1152 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1153 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1154}
1155
Prabir Pradhan7e186182020-11-10 13:56:45 -08001156TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1157 NotifyPointerCaptureChangedArgs args;
1158
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001159 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001160 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001161 mReader->loopOnce();
1162 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001163 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1164 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001165
1166 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001167 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001168 mReader->loopOnce();
1169 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001170 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001171
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001174 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001175 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001176 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001177}
1178
Chris Ye87143712020-11-10 05:05:58 +00001179class FakeVibratorInputMapper : public FakeInputMapper {
1180public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001181 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1182 const InputReaderConfiguration& readerConfig, uint32_t sources)
1183 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001184
1185 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1186};
1187
1188TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1189 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001190 ftl::Flags<InputDeviceClass> deviceClass =
1191 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001192 constexpr int32_t eventHubId = 1;
1193 const char* DEVICE_LOCATION = "BLUETOOTH";
1194 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1195 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001196 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1197 mFakePolicy->getReaderConfiguration(),
1198 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001199 mReader->pushNextDevice(device);
1200
1201 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1202 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1203
1204 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1205 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1206}
1207
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001208// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001209
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001210class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001211public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001212 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001213
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001214 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001215
Andy Chenf9f1a022022-08-29 20:07:10 -04001216 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1217
Chris Yee2b1e5c2021-03-10 22:45:12 -08001218 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1219
1220 void dump(std::string& dump) override {}
1221
1222 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1223 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001224 }
1225
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1227 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001228 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001229
1230 bool setLightColor(int32_t lightId, int32_t color) override {
1231 getDeviceContext().setLightBrightness(lightId, color >> 24);
1232 return true;
1233 }
1234
1235 std::optional<int32_t> getLightColor(int32_t lightId) override {
1236 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1237 if (!result.has_value()) {
1238 return std::nullopt;
1239 }
1240 return result.value() << 24;
1241 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001242
1243 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1244
1245 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1246
1247private:
1248 InputDeviceContext& mDeviceContext;
1249 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1250 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001251 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001252};
1253
Chris Yee2b1e5c2021-03-10 22:45:12 -08001254TEST_F(InputReaderTest, BatteryGetCapacity) {
1255 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001256 ftl::Flags<InputDeviceClass> deviceClass =
1257 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001258 constexpr int32_t eventHubId = 1;
1259 const char* DEVICE_LOCATION = "BLUETOOTH";
1260 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001261 FakePeripheralController& controller =
1262 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001263 mReader->pushNextDevice(device);
1264
1265 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1266
Harry Cuttsa5b71292022-11-28 12:56:17 +00001267 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1268 FakeEventHub::BATTERY_CAPACITY);
1269 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001270}
1271
1272TEST_F(InputReaderTest, BatteryGetStatus) {
1273 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001274 ftl::Flags<InputDeviceClass> deviceClass =
1275 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001276 constexpr int32_t eventHubId = 1;
1277 const char* DEVICE_LOCATION = "BLUETOOTH";
1278 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001279 FakePeripheralController& controller =
1280 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001281 mReader->pushNextDevice(device);
1282
1283 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1284
Harry Cuttsa5b71292022-11-28 12:56:17 +00001285 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1286 FakeEventHub::BATTERY_STATUS);
1287 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001288}
1289
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001290TEST_F(InputReaderTest, BatteryGetDevicePath) {
1291 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1292 ftl::Flags<InputDeviceClass> deviceClass =
1293 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1294 constexpr int32_t eventHubId = 1;
1295 const char* DEVICE_LOCATION = "BLUETOOTH";
1296 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1297 device->addController<FakePeripheralController>(eventHubId);
1298 mReader->pushNextDevice(device);
1299
1300 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1301
Harry Cuttsa5b71292022-11-28 12:56:17 +00001302 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001303}
1304
Chris Ye3fdbfef2021-01-06 18:45:18 -08001305TEST_F(InputReaderTest, LightGetColor) {
1306 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001307 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001308 constexpr int32_t eventHubId = 1;
1309 const char* DEVICE_LOCATION = "BLUETOOTH";
1310 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001311 FakePeripheralController& controller =
1312 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313 mReader->pushNextDevice(device);
1314 RawLightInfo info = {.id = 1,
1315 .name = "Mono",
1316 .maxBrightness = 255,
1317 .flags = InputLightClass::BRIGHTNESS,
1318 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001319 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1320 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001321
1322 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001323
Harry Cutts33476232023-01-30 19:57:29 +00001324 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1325 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1326 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1327 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001328}
1329
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001330// --- InputReaderIntegrationTest ---
1331
1332// These tests create and interact with the InputReader only through its interface.
1333// The InputReader is started during SetUp(), which starts its processing in its own
1334// thread. The tests use linux uinput to emulate input devices.
1335// NOTE: Interacting with the physical device while these tests are running may cause
1336// the tests to fail.
1337class InputReaderIntegrationTest : public testing::Test {
1338protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001339 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001340 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001341 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001342
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001343 std::shared_ptr<FakePointerController> mFakePointerController;
1344
Chris Yea52ade12020-08-27 16:49:20 -07001345 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001346#if !defined(__ANDROID__)
1347 GTEST_SKIP();
1348#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001349 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001350 mFakePointerController = std::make_shared<FakePointerController>();
1351 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001352
Arpit Singh440bf652023-08-09 09:23:43 +00001353 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001354 }
1355
Chris Yea52ade12020-08-27 16:49:20 -07001356 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001357#if !defined(__ANDROID__)
1358 return;
1359#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001360 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001361 mReader.reset();
1362 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001363 mFakePolicy.clear();
1364 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001365
1366 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1367 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1368 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1369 [&name](const InputDeviceInfo& info) {
1370 return info.getIdentifier().name == name;
1371 });
1372 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1373 }
Arpit Singh440bf652023-08-09 09:23:43 +00001374
1375 void setupInputReader() {
1376 mTestListener = std::make_unique<TestInputListener>(/*eventHappenedTimeout=*/2000ms,
1377 /*eventDidNotHappenTimeout=*/30ms);
1378
1379 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1380 *mTestListener);
1381 ASSERT_EQ(mReader->start(), OK);
1382
1383 // Since this test is run on a real device, all the input devices connected
1384 // to the test device will show up in mReader. We wait for those input devices to
1385 // show up before beginning the tests.
1386 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1389 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001390};
1391
1392TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1393 // An invalid input device that is only used for this test.
1394 class InvalidUinputDevice : public UinputDevice {
1395 public:
Harry Cutts33476232023-01-30 19:57:29 +00001396 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001397
1398 private:
1399 void configureDevice(int fd, uinput_user_dev* device) override {}
1400 };
1401
1402 const size_t numDevices = mFakePolicy->getInputDevices().size();
1403
1404 // UinputDevice does not set any event or key bits, so InputReader should not
1405 // consider it as a valid device.
1406 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1407 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1408 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1409 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1410
1411 invalidDevice.reset();
1412 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1414 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1415}
1416
1417TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1418 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1419
1420 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1421 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1422 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1423 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1424
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001425 const auto device = findDeviceByName(keyboard->getName());
1426 ASSERT_TRUE(device.has_value());
1427 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1428 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1429 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001430
1431 keyboard.reset();
1432 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1434 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1435}
1436
1437TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1438 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1439 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1440
1441 NotifyConfigurationChangedArgs configChangedArgs;
1442 ASSERT_NO_FATAL_FAILURE(
1443 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001444 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001445 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1446
1447 NotifyKeyArgs keyArgs;
1448 keyboard->pressAndReleaseHomeKey();
1449 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1450 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001451 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001452 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001453 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001454 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001455 prevTimestamp = keyArgs.eventTime;
1456
1457 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1458 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001459 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001460 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001461 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001462}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001463
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001464TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1465 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1466 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1467
1468 const auto device = findDeviceByName(stylus->getName());
1469 ASSERT_TRUE(device.has_value());
1470
Prabir Pradhana3621852022-10-14 18:57:23 +00001471 // An external stylus with buttons should also be recognized as a keyboard.
1472 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001473 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1474 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1475
1476 const auto DOWN =
1477 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1478 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1479
1480 stylus->pressAndReleaseKey(BTN_STYLUS);
1481 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1482 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1483 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1484 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1485
1486 stylus->pressAndReleaseKey(BTN_STYLUS2);
1487 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1488 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1489 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1490 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1491
1492 stylus->pressAndReleaseKey(BTN_STYLUS3);
1493 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1494 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1495 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1496 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1497}
1498
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001499TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1500 std::unique_ptr<UinputKeyboard> keyboard =
1501 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1502 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1503 KEY_R, KEY_T, KEY_Y,
1504 BTN_STYLUS, BTN_STYLUS2,
1505 BTN_STYLUS3});
1506 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1507
1508 const auto device = findDeviceByName(keyboard->getName());
1509 ASSERT_TRUE(device.has_value());
1510
1511 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1512 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1513 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1514 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1515}
1516
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001517TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1518 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1519 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1520 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1521 createUinputDevice<UinputKeyboardWithHidUsage>(
1522 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1523 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1524
1525 const auto device = findDeviceByName(keyboard->getName());
1526 ASSERT_TRUE(device.has_value());
1527
1528 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1529 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1530
1531 // If a device supports reporting HID usage codes, it shouldn't automatically support
1532 // stylus keys.
1533 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1534 uint8_t outFlags[] = {0};
1535 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1536 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1537}
1538
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001539/**
1540 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1541 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1542 * are passed to the listener.
1543 */
1544static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1545TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1546 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1547 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1548 NotifyKeyArgs keyArgs;
1549
1550 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1551 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1552 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1553 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1554
1555 controller->pressAndReleaseKey(BTN_GEAR_UP);
1556 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1558 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1559}
1560
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001561// --- TouchIntegrationTest ---
1562
Arpit Singh440bf652023-08-09 09:23:43 +00001563class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001564protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001565 const std::string UNIQUE_ID = "local:0";
1566
Chris Yea52ade12020-08-27 16:49:20 -07001567 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001568#if !defined(__ANDROID__)
1569 GTEST_SKIP();
1570#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001571 InputReaderIntegrationTest::SetUp();
1572 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001573 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1574 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001575
1576 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1577 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1578 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001579 const auto info = findDeviceByName(mDevice->getName());
1580 ASSERT_TRUE(info);
1581 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001582 }
1583
1584 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001585 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001586 std::optional<uint8_t> physicalPort,
1587 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001588 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001589 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001590 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001591 }
1592
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001593 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1594 NotifyMotionArgs args;
1595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1596 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001597 ASSERT_EQ(points.size(), args.getPointerCount());
1598 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001599 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1600 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1601 }
1602 }
1603
Arthur Hungaab25622020-01-16 11:22:11 +08001604 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001605 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001606};
1607
Arpit Singh440bf652023-08-09 09:23:43 +00001608enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1609
1610class TouchIntegrationTest : public BaseTouchIntegrationTest,
1611 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1612protected:
1613 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1614 const std::string INPUT_PORT = "uinput_touch/input0";
1615
1616 void SetUp() override {
1617#if !defined(__ANDROID__)
1618 GTEST_SKIP();
1619#endif
1620 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1621 BaseTouchIntegrationTest::SetUp();
1622 return;
1623 }
1624
1625 // setup policy with a input-port or UniqueId association to the display
1626 bool isInputPortAssociation =
1627 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1628
1629 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1630 if (isInputPortAssociation) {
1631 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1632 } else {
1633 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1634 }
1635 mFakePointerController = std::make_shared<FakePointerController>();
1636 mFakePolicy->setPointerController(mFakePointerController);
1637
1638 InputReaderIntegrationTest::setupInputReader();
1639
1640 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1641 INPUT_PORT);
1642 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1643
1644 // Add a display linked to a physical port or UniqueId.
1645 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1646 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1647 ViewportType::INTERNAL);
1648 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1649 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1650 const auto info = findDeviceByName(mDevice->getName());
1651 ASSERT_TRUE(info);
1652 mDeviceInfo = *info;
1653 }
1654};
1655
1656TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001657 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1658 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1659 // presses).
1660 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1661 mDeviceInfo.getSources());
1662}
1663
Arpit Singh440bf652023-08-09 09:23:43 +00001664TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001665 NotifyMotionArgs args;
1666 const Point centerPoint = mDevice->getCenterPoint();
1667
1668 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001669 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001670 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001671 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001672 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1673 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1674
1675 // ACTION_MOVE
1676 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001677 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001678 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1679 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1680
1681 // ACTION_UP
1682 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001683 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001684 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1685 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1686}
1687
Arpit Singh440bf652023-08-09 09:23:43 +00001688TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001689 NotifyMotionArgs args;
1690 const Point centerPoint = mDevice->getCenterPoint();
1691
1692 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001693 mDevice->sendSlot(FIRST_SLOT);
1694 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001695 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1699
1700 // ACTION_POINTER_DOWN (Second slot)
1701 const Point secondPoint = centerPoint + Point(100, 100);
1702 mDevice->sendSlot(SECOND_SLOT);
1703 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001704 mDevice->sendDown(secondPoint);
1705 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001706 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001707 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001708
1709 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001710 mDevice->sendMove(secondPoint + Point(1, 1));
1711 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1713 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1714
1715 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001716 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001718 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001719 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001720
1721 // ACTION_UP
1722 mDevice->sendSlot(FIRST_SLOT);
1723 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001724 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001725 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1726 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1727}
1728
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001729/**
1730 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1731 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1732 * data?
1733 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1734 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1735 * for Pointer 0 only is generated after.
1736 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1737 * events, we will not miss any information.
1738 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1739 * event generated afterwards that contains the newest movement of pointer 0.
1740 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1741 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1742 * losing information about non-palm pointers.
1743 */
Arpit Singh440bf652023-08-09 09:23:43 +00001744TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001745 NotifyMotionArgs args;
1746 const Point centerPoint = mDevice->getCenterPoint();
1747
1748 // ACTION_DOWN
1749 mDevice->sendSlot(FIRST_SLOT);
1750 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1751 mDevice->sendDown(centerPoint);
1752 mDevice->sendSync();
1753 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1754
1755 // ACTION_POINTER_DOWN (Second slot)
1756 const Point secondPoint = centerPoint + Point(100, 100);
1757 mDevice->sendSlot(SECOND_SLOT);
1758 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1759 mDevice->sendDown(secondPoint);
1760 mDevice->sendSync();
1761 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1762
1763 // ACTION_MOVE (First slot)
1764 mDevice->sendSlot(FIRST_SLOT);
1765 mDevice->sendMove(centerPoint + Point(5, 5));
1766 // ACTION_POINTER_UP (Second slot)
1767 mDevice->sendSlot(SECOND_SLOT);
1768 mDevice->sendPointerUp();
1769 // Send a single sync for the above 2 pointer updates
1770 mDevice->sendSync();
1771
1772 // First, we should get POINTER_UP for the second pointer
1773 assertReceivedMotion(ACTION_POINTER_1_UP,
1774 {/*first pointer */ centerPoint + Point(5, 5),
1775 /*second pointer*/ secondPoint});
1776
1777 // Next, the MOVE event for the first pointer
1778 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1779}
1780
1781/**
1782 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1783 * move, and then it will go up, all in the same frame.
1784 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1785 * gets sent to the listener.
1786 */
Arpit Singh440bf652023-08-09 09:23:43 +00001787TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001788 NotifyMotionArgs args;
1789 const Point centerPoint = mDevice->getCenterPoint();
1790
1791 // ACTION_DOWN
1792 mDevice->sendSlot(FIRST_SLOT);
1793 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1794 mDevice->sendDown(centerPoint);
1795 mDevice->sendSync();
1796 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1797
1798 // ACTION_POINTER_DOWN (Second slot)
1799 const Point secondPoint = centerPoint + Point(100, 100);
1800 mDevice->sendSlot(SECOND_SLOT);
1801 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1802 mDevice->sendDown(secondPoint);
1803 mDevice->sendSync();
1804 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1805
1806 // ACTION_MOVE (First slot)
1807 mDevice->sendSlot(FIRST_SLOT);
1808 mDevice->sendMove(centerPoint + Point(5, 5));
1809 // ACTION_POINTER_UP (Second slot)
1810 mDevice->sendSlot(SECOND_SLOT);
1811 mDevice->sendMove(secondPoint + Point(6, 6));
1812 mDevice->sendPointerUp();
1813 // Send a single sync for the above 2 pointer updates
1814 mDevice->sendSync();
1815
1816 // First, we should get POINTER_UP for the second pointer
1817 // The movement of the second pointer during the liftoff frame is ignored.
1818 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1819 assertReceivedMotion(ACTION_POINTER_1_UP,
1820 {/*first pointer */ centerPoint + Point(5, 5),
1821 /*second pointer*/ secondPoint});
1822
1823 // Next, the MOVE event for the first pointer
1824 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1825}
1826
Arpit Singh440bf652023-08-09 09:23:43 +00001827TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001828 NotifyMotionArgs args;
1829 const Point centerPoint = mDevice->getCenterPoint();
1830
1831 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001832 mDevice->sendSlot(FIRST_SLOT);
1833 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001834 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001835 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001836 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1837 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1838
arthurhungcc7f9802020-04-30 17:55:40 +08001839 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001840 const Point secondPoint = centerPoint + Point(100, 100);
1841 mDevice->sendSlot(SECOND_SLOT);
1842 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1843 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001844 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001845 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001846 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001847
arthurhungcc7f9802020-04-30 17:55:40 +08001848 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001849 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001850 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1853
arthurhungcc7f9802020-04-30 17:55:40 +08001854 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1855 // a palm event.
1856 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001857 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001858 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001859 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001860 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001861 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001862
arthurhungcc7f9802020-04-30 17:55:40 +08001863 // Send up to second slot, expect first slot send moving.
1864 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001865 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001866 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001868
arthurhungcc7f9802020-04-30 17:55:40 +08001869 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001870 mDevice->sendSlot(FIRST_SLOT);
1871 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001872 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001873
arthurhungcc7f9802020-04-30 17:55:40 +08001874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1875 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001876}
1877
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001878/**
1879 * Some drivers historically have reported axis values outside of the range specified in the
1880 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1881 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1882 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1883 * and no units for pressure (resolution) is specified by the evdev documentation.
1884 */
1885TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1886 const Point centerPoint = mDevice->getCenterPoint();
1887
1888 // Down with pressure outside the reported range
1889 mDevice->sendSlot(FIRST_SLOT);
1890 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1891 mDevice->sendDown(centerPoint);
1892 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1893 mDevice->sendSync();
1894 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1895 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1896
1897 // Move to a point outside the reported range
1898 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1899 mDevice->sendSync();
1900 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1901 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1902
1903 // Up
1904 mDevice->sendUp();
1905 mDevice->sendSync();
1906 ASSERT_NO_FATAL_FAILURE(
1907 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1908}
1909
Arpit Singh440bf652023-08-09 09:23:43 +00001910TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001911 const Point centerPoint = mDevice->getCenterPoint();
1912
1913 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1914 mDevice->sendSlot(FIRST_SLOT);
1915 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1916 mDevice->sendToolType(MT_TOOL_PEN);
1917 mDevice->sendDown(centerPoint);
1918 mDevice->sendSync();
1919 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001921 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001922
1923 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1924
1925 // Release the stylus touch.
1926 mDevice->sendUp();
1927 mDevice->sendSync();
1928 ASSERT_NO_FATAL_FAILURE(
1929 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1930
1931 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1932
1933 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1934 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1935 mDevice->sendToolType(MT_TOOL_FINGER);
1936 mDevice->sendDown(centerPoint);
1937 mDevice->sendSync();
1938 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1939 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001940 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001941
1942 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1943
1944 mDevice->sendUp();
1945 mDevice->sendSync();
1946 ASSERT_NO_FATAL_FAILURE(
1947 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1948
1949 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1950 // The policy should be notified of the stylus presence.
1951 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1952 mDevice->sendToolType(MT_TOOL_PEN);
1953 mDevice->sendMove(centerPoint);
1954 mDevice->sendSync();
1955 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1956 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001957 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001958
1959 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1960}
1961
Arpit Singh440bf652023-08-09 09:23:43 +00001962TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001963 const Point centerPoint = mDevice->getCenterPoint();
1964
1965 // Down
1966 mDevice->sendSlot(FIRST_SLOT);
1967 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1968 mDevice->sendDown(centerPoint);
1969 mDevice->sendSync();
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1971 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1972
1973 // Move
1974 mDevice->sendMove(centerPoint + Point(1, 1));
1975 mDevice->sendSync();
1976 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1977 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1978
1979 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1980 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1981 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1982 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1983 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1984 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001985
1986 // Move
1987 mDevice->sendMove(centerPoint + Point(2, 2));
1988 mDevice->sendSync();
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1990 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1991
1992 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1993 externalStylus.reset();
1994 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1996 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
1997
1998 // Up
1999 mDevice->sendUp();
2000 mDevice->sendSync();
2001 ASSERT_NO_FATAL_FAILURE(
2002 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2003
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2005}
2006
Arpit Singh440bf652023-08-09 09:23:43 +00002007INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2008 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2009 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2010 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2011
Prabir Pradhan124ea442022-10-28 20:27:44 +00002012// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002013
Prabir Pradhan124ea442022-10-28 20:27:44 +00002014// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2015// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2016// stylus.
2017template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002018class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002019protected:
2020 void SetUp() override {
2021#if !defined(__ANDROID__)
2022 GTEST_SKIP();
2023#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002024 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025 mTouchscreen = mDevice.get();
2026 mTouchscreenInfo = mDeviceInfo;
2027
2028 setUpStylusDevice();
2029 }
2030
2031 UinputStylusDevice* mStylus{nullptr};
2032 InputDeviceInfo mStylusInfo{};
2033
2034 UinputTouchScreen* mTouchscreen{nullptr};
2035 InputDeviceInfo mTouchscreenInfo{};
2036
2037private:
2038 // When we are attempting to test stylus button events that are sent from the touchscreen,
2039 // use the same Uinput device for the touchscreen and the stylus.
2040 template <typename T = UinputStylusDevice>
2041 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2042 mStylus = mDevice.get();
2043 mStylusInfo = mDeviceInfo;
2044 }
2045
2046 // When we are attempting to stylus buttons from an external stylus being merged with touches
2047 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2048 template <typename T = UinputStylusDevice>
2049 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2050 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2051 mStylus = mStylusDeviceLifecycleTracker.get();
2052 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2053 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2054 const auto info = findDeviceByName(mStylus->getName());
2055 ASSERT_TRUE(info);
2056 mStylusInfo = *info;
2057 }
2058
2059 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2060
2061 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002062 using BaseTouchIntegrationTest::mDevice;
2063 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002064};
2065
2066using StylusButtonIntegrationTestTypes =
2067 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2068TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2069
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002070TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002071 const auto stylusId = TestFixture::mStylusInfo.getId();
2072
2073 TestFixture::mStylus->pressKey(BTN_STYLUS);
2074 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2075 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2076 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2077
2078 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2079 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002080 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002081 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002082}
2083
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002084TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002085 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2086 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2087 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002088
2089 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002090 TestFixture::mStylus->pressKey(BTN_STYLUS);
2091 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002092 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002093 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002094
2095 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2097 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2098 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2099 TestFixture::mTouchscreen->sendDown(centerPoint);
2100 TestFixture::mTouchscreen->sendSync();
2101 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002102 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002103 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002104 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2105 WithDeviceId(touchscreenId))));
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002107 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002108 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002109 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2110 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002111
Prabir Pradhan124ea442022-10-28 20:27:44 +00002112 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002117 WithDeviceId(touchscreenId))));
2118 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002119 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002120 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002121 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122
2123 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002127 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002128}
2129
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002130TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002131 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2132 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2133 const auto stylusId = TestFixture::mStylusInfo.getId();
2134 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002135 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002136
2137 // Press the stylus button.
2138 TestFixture::mStylus->pressKey(BTN_STYLUS);
2139 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2140 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2141 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2142
2143 // Start hovering with the stylus.
2144 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2145 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2146 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2147 TestFixture::mTouchscreen->sendMove(centerPoint);
2148 TestFixture::mTouchscreen->sendSync();
2149 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2150 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2151 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2152 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2153 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2154 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2155 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2156 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2157 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2158
2159 // Touch down with the stylus.
2160 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2161 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2162 TestFixture::mTouchscreen->sendDown(centerPoint);
2163 TestFixture::mTouchscreen->sendSync();
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2165 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2166 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2167
2168 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2169 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2170 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2171
2172 // Stop touching with the stylus, and start hovering.
2173 TestFixture::mTouchscreen->sendUp();
2174 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2175 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2176 TestFixture::mTouchscreen->sendMove(centerPoint);
2177 TestFixture::mTouchscreen->sendSync();
2178 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2179 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2180 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2181 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2182 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2183 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2184 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2185 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2186 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2187
2188 // Stop hovering.
2189 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2190 TestFixture::mTouchscreen->sendSync();
2191 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2192 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2193 WithButtonState(0))));
2194 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2196 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2197 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2198
2199 // Release the stylus button.
2200 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2201 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2202 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2203 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2204}
2205
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002206TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002207 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2208 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2209 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002210
2211 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002212 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2213 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2214 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2215 TestFixture::mTouchscreen->sendDown(centerPoint);
2216 TestFixture::mTouchscreen->sendSync();
2217 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002218 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002219 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002220 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002221
2222 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002223 TestFixture::mStylus->pressKey(BTN_STYLUS);
2224 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002225 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002226 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2227 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002228 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002229 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002230 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2231 WithDeviceId(touchscreenId))));
2232 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002234 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002235 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2236 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002237
Prabir Pradhan124ea442022-10-28 20:27:44 +00002238 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002241 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2242 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002244 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002245 WithDeviceId(touchscreenId))));
2246 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002247 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002248 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002249 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002250
2251 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002252 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2253 TestFixture::mTouchscreen->sendSync();
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002256 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002257 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258}
2259
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002260TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002261 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2262 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002263 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002264
2265 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2266 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2267 const auto stylusId = TestFixture::mStylusInfo.getId();
2268
2269 // Start a stylus gesture. By the time this event is processed, the configuration change that
2270 // was requested is guaranteed to be completed.
2271 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2272 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2273 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2274 TestFixture::mTouchscreen->sendDown(centerPoint);
2275 TestFixture::mTouchscreen->sendSync();
2276 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002278 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002279 WithDeviceId(touchscreenId))));
2280
2281 // Press and release a stylus button. Each change only generates a MOVE motion event.
2282 // Key events are unaffected.
2283 TestFixture::mStylus->pressKey(BTN_STYLUS);
2284 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2285 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2286 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2287 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002289 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002290 WithDeviceId(touchscreenId))));
2291
2292 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2293 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2294 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2295 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2296 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2297 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002298 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002299 WithDeviceId(touchscreenId))));
2300
2301 // Finish the stylus gesture.
2302 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2303 TestFixture::mTouchscreen->sendSync();
2304 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2305 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002306 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002307 WithDeviceId(touchscreenId))));
2308}
2309
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002310// --- ExternalStylusIntegrationTest ---
2311
2312// Verify the behavior of an external stylus. An external stylus can report pressure or button
2313// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2314// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002315using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002316
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002317TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2318 // Create an external stylus capable of reporting pressure data that
2319 // should be fused with a touch pointer.
2320 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2321 createUinputDevice<UinputExternalStylusWithPressure>();
2322 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2323 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2324 const auto stylusInfo = findDeviceByName(stylus->getName());
2325 ASSERT_TRUE(stylusInfo);
2326
2327 // Connecting an external stylus changes the source of the touchscreen.
2328 const auto deviceInfo = findDeviceByName(mDevice->getName());
2329 ASSERT_TRUE(deviceInfo);
2330 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2331}
2332
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002333TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002334 const Point centerPoint = mDevice->getCenterPoint();
2335
2336 // Create an external stylus capable of reporting pressure data that
2337 // should be fused with a touch pointer.
2338 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2339 createUinputDevice<UinputExternalStylusWithPressure>();
2340 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2341 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2342 const auto stylusInfo = findDeviceByName(stylus->getName());
2343 ASSERT_TRUE(stylusInfo);
2344
2345 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2346
2347 const auto touchscreenId = mDeviceInfo.getId();
2348
2349 // Set a pressure value on the stylus. It doesn't generate any events.
2350 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2351 stylus->setPressure(100);
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2353
2354 // Start a finger gesture, and ensure it shows up as stylus gesture
2355 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002356 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002357 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002358 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002359 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002360 mDevice->sendSync();
2361 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002362 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2363 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2364 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002365
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002366 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2367 // event with the updated pressure.
2368 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002369 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002370 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2371 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2372 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002373
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002374 // The external stylus did not generate any events.
2375 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2377}
2378
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002379TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002380 const Point centerPoint = mDevice->getCenterPoint();
2381
2382 // Create an external stylus capable of reporting pressure data that
2383 // should be fused with a touch pointer.
2384 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2385 createUinputDevice<UinputExternalStylusWithPressure>();
2386 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2388 const auto stylusInfo = findDeviceByName(stylus->getName());
2389 ASSERT_TRUE(stylusInfo);
2390
2391 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2392
2393 const auto touchscreenId = mDeviceInfo.getId();
2394
2395 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2396 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002397 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2398 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002399 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002401
2402 // Start a finger gesture. The touch device will withhold generating any touches for
2403 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2404 mDevice->sendSlot(FIRST_SLOT);
2405 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2406 mDevice->sendToolType(MT_TOOL_FINGER);
2407 mDevice->sendDown(centerPoint);
2408 auto waitUntil = std::chrono::system_clock::now() +
2409 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002410 mDevice->sendSync();
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntil));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002412
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002413 // Since the external stylus did not report a pressure value within the timeout,
2414 // it shows up as a finger pointer.
2415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2416 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002417 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
2418 WithToolType(ToolType::FINGER), WithDeviceId(touchscreenId), WithPressure(1.f))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002419
2420 // Change the pressure on the external stylus. Since the pressure was not present at the start
2421 // of the gesture, it is ignored for now.
2422 stylus->setPressure(200);
2423 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2424
2425 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002426 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2427 mDevice->sendSync();
2428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2429 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002430 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002431 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002432
2433 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2434 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2435 mDevice->sendToolType(MT_TOOL_FINGER);
2436 mDevice->sendDown(centerPoint);
2437 mDevice->sendSync();
2438 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002439 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2440 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2441 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002442
2443 // The external stylus did not generate any events.
2444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2445 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002446}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002447
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002448TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002449 const Point centerPoint = mDevice->getCenterPoint();
2450
2451 // Create an external stylus device that does not support pressure. It should not affect any
2452 // touch pointers.
2453 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2454 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2456 const auto stylusInfo = findDeviceByName(stylus->getName());
2457 ASSERT_TRUE(stylusInfo);
2458
2459 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2460
2461 const auto touchscreenId = mDeviceInfo.getId();
2462
2463 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2464 // pressure data from the external stylus.
2465 mDevice->sendSlot(FIRST_SLOT);
2466 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2467 mDevice->sendToolType(MT_TOOL_FINGER);
2468 mDevice->sendDown(centerPoint);
2469 auto waitUntil = std::chrono::system_clock::now() +
2470 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2471 mDevice->sendSync();
2472 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002473 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2474 AMOTION_EVENT_ACTION_DOWN),
2475 WithToolType(ToolType::FINGER),
2476 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2477 AINPUT_SOURCE_STYLUS),
2478 WithButtonState(0),
2479 WithDeviceId(touchscreenId),
2480 WithPressure(1.f)),
2481 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002482
2483 // The external stylus did not generate any events.
2484 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2485 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2486}
2487
Michael Wrightd02c5b62014-02-10 15:10:22 -08002488// --- InputDeviceTest ---
2489class InputDeviceTest : public testing::Test {
2490protected:
2491 static const char* DEVICE_NAME;
2492 static const char* DEVICE_LOCATION;
2493 static const int32_t DEVICE_ID;
2494 static const int32_t DEVICE_GENERATION;
2495 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002496 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002497 static const int32_t EVENTHUB_ID;
2498 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2499
2500 std::shared_ptr<FakeEventHub> mFakeEventHub;
2501 sp<FakeInputReaderPolicy> mFakePolicy;
2502 std::unique_ptr<TestInputListener> mFakeListener;
2503 std::unique_ptr<InstrumentedInputReader> mReader;
2504 std::shared_ptr<InputDevice> mDevice;
2505
2506 void SetUp() override {
2507 mFakeEventHub = std::make_unique<FakeEventHub>();
2508 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2509 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002510 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002512 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513 identifier.name = DEVICE_NAME;
2514 identifier.location = DEVICE_LOCATION;
2515 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2516 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2517 identifier);
2518 mReader->pushNextDevice(mDevice);
2519 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002520 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002521 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002522
2523 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002524 mFakeListener.reset();
2525 mFakePolicy.clear();
2526 }
2527};
2528
2529const char* InputDeviceTest::DEVICE_NAME = "device";
2530const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2531const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2532const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002533const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002534const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2535 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002536const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002537const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2538
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002539TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002541 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2542 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002543}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002544
Michael Wrightd02c5b62014-02-10 15:10:22 -08002545TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2546 ASSERT_EQ(mDevice->isEnabled(), false);
2547}
2548
2549TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2550 // Configuration.
2551 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002552 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002553
2554 // Reset.
2555 unused += mDevice->reset(ARBITRARY_TIME);
2556
2557 NotifyDeviceResetArgs resetArgs;
2558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2559 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2560 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2561
2562 // Metadata.
2563 ASSERT_TRUE(mDevice->isIgnored());
2564 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2565
2566 InputDeviceInfo info = mDevice->getDeviceInfo();
2567 ASSERT_EQ(DEVICE_ID, info.getId());
2568 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2569 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2570 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2571
2572 // State queries.
2573 ASSERT_EQ(0, mDevice->getMetaState());
2574
2575 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2576 << "Ignored device should return unknown key code state.";
2577 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2578 << "Ignored device should return unknown scan code state.";
2579 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2580 << "Ignored device should return unknown switch state.";
2581
2582 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2583 uint8_t flags[2] = { 0, 1 };
2584 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2585 << "Ignored device should never mark any key codes.";
2586 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2587 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2588}
2589
2590TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2591 // Configuration.
2592 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2593
2594 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002595 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2596 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002597 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2598 mapper1.setMetaState(AMETA_ALT_ON);
2599 mapper1.addSupportedKeyCode(AKEYCODE_A);
2600 mapper1.addSupportedKeyCode(AKEYCODE_B);
2601 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2602 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2603 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2604 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2605 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2606
2607 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002608 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2609 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610 mapper2.setMetaState(AMETA_SHIFT_ON);
2611
2612 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002613 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614
Harry Cuttsf13161a2023-03-08 14:15:49 +00002615 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2616 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002618 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002619
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002620 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2621 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622
2623 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002624 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002625 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2626 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002627
2628 NotifyDeviceResetArgs resetArgs;
2629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2630 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2631 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2632
2633 // Metadata.
2634 ASSERT_FALSE(mDevice->isIgnored());
2635 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2636
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002637 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002638 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002639 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2641 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2642
2643 // State queries.
2644 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2645 << "Should query mappers and combine meta states.";
2646
2647 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2648 << "Should return unknown key code state when source not supported.";
2649 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2650 << "Should return unknown scan code state when source not supported.";
2651 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2652 << "Should return unknown switch state when source not supported.";
2653
2654 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2655 << "Should query mapper when source is supported.";
2656 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2657 << "Should query mapper when source is supported.";
2658 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2659 << "Should query mapper when source is supported.";
2660
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002661 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002662 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002663 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002664 << "Should do nothing when source is unsupported.";
2665 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2666 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2667 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2668 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2669
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002670 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002671 << "Should query mapper when source is supported.";
2672 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2673 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2674 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2675 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2676
2677 // Event handling.
2678 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002679 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002680 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002681
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002682 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2683 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002684}
2685
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002686TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2687 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2688 FakeInputMapper& mapper =
2689 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2690 AINPUT_SOURCE_KEYBOARD);
2691 NotifyMotionArgs args1;
2692 NotifySwitchArgs args2;
2693 NotifyKeyArgs args3;
2694 mapper.setProcessResult({args1, args2, args3});
2695
2696 InputReaderConfiguration config;
2697 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2698
2699 RawEvent event;
2700 event.deviceId = EVENTHUB_ID;
2701 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2702
2703 for (auto& arg : notifyArgs) {
2704 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2705 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2706 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2707 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2708 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2709 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2710 }
2711 }
2712}
2713
2714TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2715 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2716 FakeInputMapper& mapper =
2717 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2718 AINPUT_SOURCE_KEYBOARD);
2719 NotifyMotionArgs args;
2720 mapper.setProcessResult({args});
2721
2722 InputReaderConfiguration config;
2723 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2724
2725 RawEvent event;
2726 event.deviceId = EVENTHUB_ID;
2727 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2728
2729 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2730 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2731}
2732
2733TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2734 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2735 FakeInputMapper& mapper =
2736 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2737 AINPUT_SOURCE_KEYBOARD);
2738 NotifyMotionArgs args;
2739 args.policyFlags = POLICY_FLAG_WAKE;
2740 mapper.setProcessResult({args});
2741
2742 InputReaderConfiguration config;
2743 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2744
2745 RawEvent event;
2746 event.deviceId = EVENTHUB_ID;
2747 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2748
2749 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2750 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2751}
2752
Arthur Hung2c9a3342019-07-23 14:18:59 +08002753// A single input device is associated with a specific display. Check that:
2754// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002755// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002756TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002757 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2758 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002759
2760 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002761 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002762 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2763 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002764
2765 // Device should be enabled by default.
2766 ASSERT_TRUE(mDevice->isEnabled());
2767
2768 // Prepare associated info.
2769 constexpr uint8_t hdmi = 1;
2770 const std::string UNIQUE_ID = "local:1";
2771
2772 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002773 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002774 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002775 // Device should be disabled because it is associated with a specific display via
2776 // input port <-> display port association, but the corresponding display is not found
2777 ASSERT_FALSE(mDevice->isEnabled());
2778
2779 // Prepare displays.
2780 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002781 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002782 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002783 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002784 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002785 ASSERT_TRUE(mDevice->isEnabled());
2786
2787 // Device should be disabled after set disable.
2788 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002789 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002790 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002791 ASSERT_FALSE(mDevice->isEnabled());
2792
2793 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002794 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002795 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002796 ASSERT_FALSE(mDevice->isEnabled());
2797}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002798
Christine Franks1ba71cc2021-04-07 14:37:42 -07002799TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2800 // Device should be enabled by default.
2801 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002802 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2803 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002804 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002805 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2806 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002807 ASSERT_TRUE(mDevice->isEnabled());
2808
2809 // Device should be disabled because it is associated with a specific display, but the
2810 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002811 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002812 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002813 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002814 ASSERT_FALSE(mDevice->isEnabled());
2815
2816 // Device should be enabled when a display is found.
2817 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002818 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002819 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002820 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002821 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002822 ASSERT_TRUE(mDevice->isEnabled());
2823
2824 // Device should be disabled after set disable.
2825 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002826 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002827 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002828 ASSERT_FALSE(mDevice->isEnabled());
2829
2830 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002831 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002832 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002833 ASSERT_FALSE(mDevice->isEnabled());
2834}
2835
Christine Franks2a2293c2022-01-18 11:51:16 -08002836TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2837 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002838 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2839 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002840 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002841 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2842 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002843
Christine Franks2a2293c2022-01-18 11:51:16 -08002844 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2845 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002846 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002847 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002848 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002849 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002850 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2851}
2852
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002853/**
2854 * This test reproduces a crash caused by a dangling reference that remains after device is added
2855 * and removed. The reference is accessed in InputDevice::dump(..);
2856 */
2857TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2858 constexpr int32_t TEST_EVENTHUB_ID = 10;
2859 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2860
Harry Cutts33476232023-01-30 19:57:29 +00002861 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002862 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2863 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002864 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2865 std::string dumpStr, eventHubDevStr;
2866 device.dump(dumpStr, eventHubDevStr);
2867}
2868
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002869TEST_F(InputDeviceTest, GetBluetoothAddress) {
2870 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2871 ASSERT_TRUE(address);
2872 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2873}
2874
Michael Wrightd02c5b62014-02-10 15:10:22 -08002875// --- SwitchInputMapperTest ---
2876
2877class SwitchInputMapperTest : public InputMapperTest {
2878protected:
2879};
2880
2881TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002882 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002883
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002884 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002885}
2886
2887TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002888 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002889
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002890 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002891 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002892
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002893 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002894 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002895}
2896
2897TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002898 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002899 std::list<NotifyArgs> out;
2900 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2901 ASSERT_TRUE(out.empty());
2902 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2903 ASSERT_TRUE(out.empty());
2904 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2905 ASSERT_TRUE(out.empty());
2906 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002907
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002908 ASSERT_EQ(1u, out.size());
2909 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002910 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002911 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2912 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002913 args.switchMask);
2914 ASSERT_EQ(uint32_t(0), args.policyFlags);
2915}
2916
Chris Ye87143712020-11-10 05:05:58 +00002917// --- VibratorInputMapperTest ---
2918class VibratorInputMapperTest : public InputMapperTest {
2919protected:
2920 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
2921};
2922
2923TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002924 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002925
2926 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
2927}
2928
2929TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00002930 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002931
2932 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
2933}
2934
2935TEST_F(VibratorInputMapperTest, Vibrate) {
2936 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08002937 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00002938 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00002939
2940 VibrationElement pattern(2);
2941 VibrationSequence sequence(2);
2942 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00002943 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
2944 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002945 sequence.addElement(pattern);
2946 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00002947 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
2948 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00002949 sequence.addElement(pattern);
2950
2951 std::vector<int64_t> timings = {0, 1};
2952 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
2953
2954 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002955 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00002956 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00002957 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08002958 // Verify vibrator state listener was notified.
2959 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002960 ASSERT_EQ(1u, out.size());
2961 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2962 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
2963 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08002964 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002965 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08002966 ASSERT_FALSE(mapper.isVibrating());
2967 // Verify vibrator state listener was notified.
2968 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002969 ASSERT_EQ(1u, out.size());
2970 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
2971 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
2972 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00002973}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974
Chris Yef59a2f42020-10-16 12:55:26 -07002975// --- SensorInputMapperTest ---
2976
2977class SensorInputMapperTest : public InputMapperTest {
2978protected:
2979 static const int32_t ACCEL_RAW_MIN;
2980 static const int32_t ACCEL_RAW_MAX;
2981 static const int32_t ACCEL_RAW_FUZZ;
2982 static const int32_t ACCEL_RAW_FLAT;
2983 static const int32_t ACCEL_RAW_RESOLUTION;
2984
2985 static const int32_t GYRO_RAW_MIN;
2986 static const int32_t GYRO_RAW_MAX;
2987 static const int32_t GYRO_RAW_FUZZ;
2988 static const int32_t GYRO_RAW_FLAT;
2989 static const int32_t GYRO_RAW_RESOLUTION;
2990
2991 static const float GRAVITY_MS2_UNIT;
2992 static const float DEGREE_RADIAN_UNIT;
2993
2994 void prepareAccelAxes();
2995 void prepareGyroAxes();
2996 void setAccelProperties();
2997 void setGyroProperties();
2998 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
2999};
3000
3001const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3002const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3003const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3004const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3005const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3006
3007const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3008const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3009const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3010const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3011const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3012
3013const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3014const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3015
3016void SensorInputMapperTest::prepareAccelAxes() {
3017 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3018 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3019 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3020 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3021 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3022 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3023}
3024
3025void SensorInputMapperTest::prepareGyroAxes() {
3026 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3027 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3028 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3029 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3030 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3031 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3032}
3033
3034void SensorInputMapperTest::setAccelProperties() {
3035 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3036 /* sensorDataIndex */ 0);
3037 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3038 /* sensorDataIndex */ 1);
3039 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3040 /* sensorDataIndex */ 2);
3041 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3042 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3043 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3044 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3045 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3046}
3047
3048void SensorInputMapperTest::setGyroProperties() {
3049 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3050 /* sensorDataIndex */ 0);
3051 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3052 /* sensorDataIndex */ 1);
3053 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3054 /* sensorDataIndex */ 2);
3055 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3056 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3057 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3058 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3059 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3060}
3061
3062TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003063 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003064
3065 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3066}
3067
3068TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3069 setAccelProperties();
3070 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003071 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003072
3073 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3074 std::chrono::microseconds(10000),
3075 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003076 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3078 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003082
3083 NotifySensorArgs args;
3084 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3085 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3086 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3087
3088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3089 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3090 ASSERT_EQ(args.deviceId, DEVICE_ID);
3091 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3092 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3093 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3094 ASSERT_EQ(args.values, values);
3095 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3096}
3097
3098TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3099 setGyroProperties();
3100 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003101 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003102
3103 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3104 std::chrono::microseconds(10000),
3105 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003106 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3109 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003112
3113 NotifySensorArgs args;
3114 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3115 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3116 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3117
3118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3119 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3120 ASSERT_EQ(args.deviceId, DEVICE_ID);
3121 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3122 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3123 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3124 ASSERT_EQ(args.values, values);
3125 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3126}
3127
Michael Wrightd02c5b62014-02-10 15:10:22 -08003128// --- KeyboardInputMapperTest ---
3129
3130class KeyboardInputMapperTest : public InputMapperTest {
3131protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003132 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003133 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003134 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003135
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003136 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003137 int32_t originalKeyCode, int32_t rotatedKeyCode,
3138 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003139};
3140
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003141/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3142 * orientation.
3143 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003144void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003145 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3146 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003147}
3148
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003149void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003150 int32_t originalScanCode, int32_t originalKeyCode,
3151 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003152 NotifyKeyArgs args;
3153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3156 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3157 ASSERT_EQ(originalScanCode, args.scanCode);
3158 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003159 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3163 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3164 ASSERT_EQ(originalScanCode, args.scanCode);
3165 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003166 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003167}
3168
Michael Wrightd02c5b62014-02-10 15:10:22 -08003169TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003170 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003171 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003172 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003173
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003174 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003175}
3176
3177TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3178 const int32_t USAGE_A = 0x070004;
3179 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003180 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3181 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003182 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3183 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3184 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003185
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003186 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003187 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003188 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003189 // Initial metastate is AMETA_NONE.
3190 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003191
3192 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003193 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003194 NotifyKeyArgs args;
3195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3196 ASSERT_EQ(DEVICE_ID, args.deviceId);
3197 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3198 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3199 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3200 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3201 ASSERT_EQ(KEY_HOME, args.scanCode);
3202 ASSERT_EQ(AMETA_NONE, args.metaState);
3203 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3204 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3205 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3206
3207 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003208 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3210 ASSERT_EQ(DEVICE_ID, args.deviceId);
3211 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3212 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3213 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3214 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3215 ASSERT_EQ(KEY_HOME, args.scanCode);
3216 ASSERT_EQ(AMETA_NONE, args.metaState);
3217 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3218 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3219 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3220
3221 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003222 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3223 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3225 ASSERT_EQ(DEVICE_ID, args.deviceId);
3226 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3227 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3228 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3229 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3230 ASSERT_EQ(0, args.scanCode);
3231 ASSERT_EQ(AMETA_NONE, args.metaState);
3232 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3233 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3234 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3235
3236 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3238 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3240 ASSERT_EQ(DEVICE_ID, args.deviceId);
3241 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3242 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3243 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3244 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3245 ASSERT_EQ(0, args.scanCode);
3246 ASSERT_EQ(AMETA_NONE, args.metaState);
3247 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3248 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3249 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3250
3251 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003252 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3253 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3255 ASSERT_EQ(DEVICE_ID, args.deviceId);
3256 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3257 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3258 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3259 ASSERT_EQ(0, args.keyCode);
3260 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3261 ASSERT_EQ(AMETA_NONE, args.metaState);
3262 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3263 ASSERT_EQ(0U, args.policyFlags);
3264 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3265
3266 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003267 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3268 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3270 ASSERT_EQ(DEVICE_ID, args.deviceId);
3271 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3272 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3274 ASSERT_EQ(0, args.keyCode);
3275 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3276 ASSERT_EQ(AMETA_NONE, args.metaState);
3277 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3278 ASSERT_EQ(0U, args.policyFlags);
3279 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3280}
3281
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003282TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3283 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3284 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3285 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3286
3287 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003288 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003289 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3290
3291 // Key down by scan code.
3292 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3293 NotifyKeyArgs args;
3294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3295 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3296
3297 // Key up by scan code.
3298 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3300 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3301}
3302
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003303/**
3304 * Ensure that the readTime is set to the time when the EV_KEY is received.
3305 */
3306TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3307 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3308
3309 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003310 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003311 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3312 NotifyKeyArgs args;
3313
3314 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003315 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3317 ASSERT_EQ(12, args.readTime);
3318
3319 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003320 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3322 ASSERT_EQ(15, args.readTime);
3323}
3324
Michael Wrightd02c5b62014-02-10 15:10:22 -08003325TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003326 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3327 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003328 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3329 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3330 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003331
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003332 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003333 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003334 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003335
Arthur Hung95f68612022-04-07 14:08:22 +08003336 // Initial metastate is AMETA_NONE.
3337 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003338
3339 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003341 NotifyKeyArgs args;
3342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3343 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003344 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003345 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003346
3347 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003348 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3350 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003351 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352
3353 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003354 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003358
3359 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003360 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3362 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003363 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003364 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003365}
3366
3367TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003368 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3370 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3371 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003372
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003373 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003374 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003375 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003376
Michael Wrighta9cf4192022-12-01 23:46:39 +00003377 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3379 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3380 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3381 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3382 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3383 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3384 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3385 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3386}
3387
3388TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003389 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3390 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3391 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003393
Michael Wrightd02c5b62014-02-10 15:10:22 -08003394 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003395 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003396 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003397 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003398
Michael Wrighta9cf4192022-12-01 23:46:39 +00003399 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003400 ASSERT_NO_FATAL_FAILURE(
3401 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3402 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3403 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3404 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3405 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3406 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3407 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003408
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003409 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003410 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003411 ASSERT_NO_FATAL_FAILURE(
3412 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3413 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3414 AKEYCODE_DPAD_UP, DISPLAY_ID));
3415 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3416 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3417 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3418 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003419
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003420 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003421 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003422 ASSERT_NO_FATAL_FAILURE(
3423 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3424 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3425 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3426 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3427 AKEYCODE_DPAD_UP, DISPLAY_ID));
3428 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3429 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003431 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003432 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003433 ASSERT_NO_FATAL_FAILURE(
3434 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3435 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3436 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3437 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3438 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3439 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3440 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441
3442 // Special case: if orientation changes while key is down, we still emit the same keycode
3443 // in the key up as we did in the key down.
3444 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003445 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003446 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003447 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3449 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3450 ASSERT_EQ(KEY_UP, args.scanCode);
3451 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3452
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003453 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003454 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003455 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3457 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3458 ASSERT_EQ(KEY_UP, args.scanCode);
3459 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3460}
3461
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003462TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3463 // If the keyboard is not orientation aware,
3464 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003465 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003466
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003468 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003469 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003470 NotifyKeyArgs args;
3471
3472 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3477 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3478
Michael Wrighta9cf4192022-12-01 23:46:39 +00003479 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3484 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3485}
3486
3487TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3488 // If the keyboard is orientation aware,
3489 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003490 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003491
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003492 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003493 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003494 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003495 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003496 NotifyKeyArgs args;
3497
3498 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3499 // ^--- already checked by the previous test
3500
Michael Wrighta9cf4192022-12-01 23:46:39 +00003501 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003502 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3507 ASSERT_EQ(DISPLAY_ID, args.displayId);
3508
3509 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003510 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003511 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003512 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003515 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3517 ASSERT_EQ(newDisplayId, args.displayId);
3518}
3519
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003521 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003522 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003523 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003525 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003526 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003528 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003529 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003530}
3531
Philip Junker4af3b3d2021-12-14 10:36:55 +01003532TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3533 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003534 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003535 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3536
3537 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3538 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3539 << "If a mapping is available, the result is equal to the mapping";
3540
3541 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3542 << "If no mapping is available, the result is the key location";
3543}
3544
Michael Wrightd02c5b62014-02-10 15:10:22 -08003545TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003547 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003550 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003551 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003552
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003553 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003554 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003555}
3556
3557TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003558 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003559 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003562 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003563
Michael Wrightd02c5b62014-02-10 15:10:22 -08003564 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003565 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_TRUE(flags[0]);
3567 ASSERT_FALSE(flags[1]);
3568}
3569
3570TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003571 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3572 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3573 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3576 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003579 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003580 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003581 // Initial metastate is AMETA_NONE.
3582 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003583
3584 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003585 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3586 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3587 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003588
3589 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003592 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3593 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3594 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003595 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003596
3597 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003598 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003600 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3601 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3602 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003604
3605 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003606 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003608 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3609 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3610 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003611 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003612
3613 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003616 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3617 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3618 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003619 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003620
3621 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003624 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3625 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3626 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003627 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
3629 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3634 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636}
3637
Chris Yea52ade12020-08-27 16:49:20 -07003638TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3639 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3640 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3641 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3642 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3643
3644 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003645 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003646 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3647
Chris Yea52ade12020-08-27 16:49:20 -07003648 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003649 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003650 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3651 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3652 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3653 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3654
3655 NotifyKeyArgs args;
3656 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3659 ASSERT_EQ(AMETA_NONE, args.metaState);
3660 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3661 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3662 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3663
3664 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003665 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3667 ASSERT_EQ(AMETA_NONE, args.metaState);
3668 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3669 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3670 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3671}
3672
Arthur Hung2c9a3342019-07-23 14:18:59 +08003673TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3674 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003675 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3676 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3677 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3678 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003679
3680 // keyboard 2.
3681 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003682 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003683 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003684 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003685 std::shared_ptr<InputDevice> device2 =
3686 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003687 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003688
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003689 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3690 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3691 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3692 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003693
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003694 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003695 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003696 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003697
Arpit Singh67ca6842023-04-26 14:43:16 +00003698 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003699 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003700 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3701 mFakePolicy
3702 ->getReaderConfiguration(),
3703 AINPUT_SOURCE_KEYBOARD,
3704 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003705 std::list<NotifyArgs> unused =
3706 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003707 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003708 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003709
3710 // Prepared displays and associated info.
3711 constexpr uint8_t hdmi1 = 0;
3712 constexpr uint8_t hdmi2 = 1;
3713 const std::string SECONDARY_UNIQUE_ID = "local:1";
3714
3715 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3716 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3717
3718 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003719 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003720 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003721 ASSERT_FALSE(device2->isEnabled());
3722
3723 // Prepare second display.
3724 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003725 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003726 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003727 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003728 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003729 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003730 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003731 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003732
3733 // Device should be enabled after the associated display is found.
3734 ASSERT_TRUE(mDevice->isEnabled());
3735 ASSERT_TRUE(device2->isEnabled());
3736
3737 // Test pad key events
3738 ASSERT_NO_FATAL_FAILURE(
3739 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3740 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3741 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3742 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3743 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3744 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3745 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3746
3747 ASSERT_NO_FATAL_FAILURE(
3748 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3749 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3750 AKEYCODE_DPAD_RIGHT, newDisplayId));
3751 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3752 AKEYCODE_DPAD_DOWN, newDisplayId));
3753 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3754 AKEYCODE_DPAD_LEFT, newDisplayId));
3755}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003756
arthurhungc903df12020-08-11 15:08:42 +08003757TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3758 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3759 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3760 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3761 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3762 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3763 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3764
3765 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003766 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003767 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003768 // Initial metastate is AMETA_NONE.
3769 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003770
3771 // Initialization should have turned all of the lights off.
3772 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3773 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3774 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3775
3776 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003777 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3778 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003779 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3780 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3781
3782 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003785 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3786 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3787
3788 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003789 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3790 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003791 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3792 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3793
3794 mFakeEventHub->removeDevice(EVENTHUB_ID);
3795 mReader->loopOnce();
3796
3797 // keyboard 2 should default toggle keys.
3798 const std::string USB2 = "USB2";
3799 const std::string DEVICE_NAME2 = "KEYBOARD2";
3800 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3801 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3802 std::shared_ptr<InputDevice> device2 =
3803 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003804 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003805 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3806 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3807 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3808 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3809 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3810 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3811
Arpit Singh67ca6842023-04-26 14:43:16 +00003812 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003813 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003814 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3815 mFakePolicy
3816 ->getReaderConfiguration(),
3817 AINPUT_SOURCE_KEYBOARD,
3818 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003819 std::list<NotifyArgs> unused =
3820 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003821 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003822 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003823
3824 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3825 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3826 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003827 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3828 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003829}
3830
Arthur Hungcb40a002021-08-03 14:31:01 +00003831TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3832 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3833 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3834 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3835
3836 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003837 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003838 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3839 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003840 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003841 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003842 // Initial metastate is AMETA_NONE.
3843 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003844
3845 mReader->toggleCapsLockState(DEVICE_ID);
3846 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3847}
3848
Arthur Hungfb3cc112022-04-13 07:39:50 +00003849TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3850 // keyboard 1.
3851 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3852 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3853 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3854 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3855 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3856 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3857
3858 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003859 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003860 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3861
3862 // keyboard 2.
3863 const std::string USB2 = "USB2";
3864 const std::string DEVICE_NAME2 = "KEYBOARD2";
3865 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3866 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3867 std::shared_ptr<InputDevice> device2 =
3868 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3869 ftl::Flags<InputDeviceClass>(0));
3870 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3871 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3872 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3873 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3874 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3875 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3876
Arpit Singh67ca6842023-04-26 14:43:16 +00003877 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003878 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003879 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3880 mFakePolicy
3881 ->getReaderConfiguration(),
3882 AINPUT_SOURCE_KEYBOARD,
3883 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003884 std::list<NotifyArgs> unused =
3885 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003886 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003887 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003888
Arthur Hung95f68612022-04-07 14:08:22 +08003889 // Initial metastate is AMETA_NONE.
3890 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3891 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3892
3893 // Toggle num lock on and off.
3894 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3895 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003896 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3897 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3898 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3899
3900 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3901 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3902 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3903 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3904 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3905
3906 // Toggle caps lock on and off.
3907 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3908 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3909 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3910 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3911 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3912
3913 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3914 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3915 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3916 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3917 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3918
3919 // Toggle scroll lock on and off.
3920 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3921 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3922 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3923 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3924 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3925
3926 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3927 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3928 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3929 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3930 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3931}
3932
Arthur Hung2141d542022-08-23 07:45:21 +00003933TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3934 const int32_t USAGE_A = 0x070004;
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3936 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3937
3938 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003939 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00003940 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3941 // Key down by scan code.
3942 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3943 NotifyKeyArgs args;
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3945 ASSERT_EQ(DEVICE_ID, args.deviceId);
3946 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3947 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3948 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3949 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3950 ASSERT_EQ(KEY_HOME, args.scanCode);
3951 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3952
3953 // Disable device, it should synthesize cancellation events for down events.
3954 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003955 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003956
3957 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3958 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3959 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3960 ASSERT_EQ(KEY_HOME, args.scanCode);
3961 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3962}
3963
Zixuan Qufecb6062022-11-12 04:44:31 +00003964TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00003965 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
3966 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00003967 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003968 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3969 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003970
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003971 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003972 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3973
3974 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003975 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003976
3977 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3978 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3979 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3980 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3981 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003982 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3983
3984 // Call change layout association with the same values: Generation shouldn't change
3985 generation = mReader->getContext()->getGeneration();
3986 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3987 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3988 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3989 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003990}
3991
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003992TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3993 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3994 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3995
3996 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00003997 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003998 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3999 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004000 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004001
4002 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4003 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4004}
4005
Justin Chung71ddb432023-03-27 04:29:07 +00004006TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4007 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4008 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004009 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004010 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4011 NotifyKeyArgs args;
4012
4013 // Key down
4014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4017}
4018
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004019// --- KeyboardInputMapperTest_ExternalDevice ---
4020
4021class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4022protected:
Chris Yea52ade12020-08-27 16:49:20 -07004023 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004024};
4025
4026TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004027 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4028 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004029
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004030 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4031 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4032 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4033 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004034
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004035 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004036 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004037 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004038
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 NotifyKeyArgs args;
4041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4042 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4043
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004044 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4046 ASSERT_EQ(uint32_t(0), args.policyFlags);
4047
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004048 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004050 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004051
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004052 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4054 ASSERT_EQ(uint32_t(0), args.policyFlags);
4055
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004056 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4058 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4059
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004060 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4062 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4063}
4064
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004065TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004066 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004067
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004068 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4069 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4070 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004071
Powei Fengd041c5d2019-05-03 17:11:33 -07004072 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004073 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004074 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004076
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004077 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004078 NotifyKeyArgs args;
4079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4080 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4081
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004082 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4084 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4085
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4088 ASSERT_EQ(uint32_t(0), args.policyFlags);
4089
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004090 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4092 ASSERT_EQ(uint32_t(0), args.policyFlags);
4093
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4096 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4097
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004098 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4100 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4101}
4102
Byoungho Jungda10dd32023-10-06 17:03:45 +09004103// --- CursorInputMapperTestBase ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
Byoungho Jungda10dd32023-10-06 17:03:45 +09004105class CursorInputMapperTestBase : public InputMapperTest {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004106protected:
4107 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4108
Michael Wright17db18e2020-06-26 20:51:44 +01004109 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004110
Chris Yea52ade12020-08-27 16:49:20 -07004111 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004112 InputMapperTest::SetUp();
4113
Michael Wright17db18e2020-06-26 20:51:44 +01004114 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004115 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004116 }
4117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004118 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4119 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004120
Michael Wrighta9cf4192022-12-01 23:46:39 +00004121 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004122 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4123 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4124 }
4125
4126 void prepareSecondaryDisplay() {
4127 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004128 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004129 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004130 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004131
4132 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4133 float pressure) {
4134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4135 0.0f, 0.0f, 0.0f, EPSILON));
4136 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004137};
4138
Byoungho Jungda10dd32023-10-06 17:03:45 +09004139const int32_t CursorInputMapperTestBase::TRACKBALL_MOVEMENT_THRESHOLD = 6;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140
Byoungho Jungda10dd32023-10-06 17:03:45 +09004141void CursorInputMapperTestBase::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4142 int32_t originalY, int32_t rotatedX,
4143 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004144 NotifyMotionArgs args;
4145
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004151 ASSERT_NO_FATAL_FAILURE(
4152 assertCursorPointerCoords(args.pointerCoords[0],
4153 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4154 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155}
4156
Byoungho Jungda10dd32023-10-06 17:03:45 +09004157// --- CursorInputMapperTest ---
4158
4159class CursorInputMapperTest : public CursorInputMapperTestBase {
4160protected:
4161 void SetUp() override {
4162 input_flags::enable_pointer_choreographer(false);
4163 CursorInputMapperTestBase::SetUp();
4164 }
4165};
4166
Michael Wrightd02c5b62014-02-10 15:10:22 -08004167TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004168 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004169 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004170
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004171 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004172}
4173
4174TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004175 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004176 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004177
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004178 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004179}
4180
4181TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004182 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004183 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004184
4185 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004186 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004187
4188 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004189 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4190 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4192 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4193
4194 // When the bounds are set, then there should be a valid motion range.
4195 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
Byoungho Jungda10dd32023-10-06 17:03:45 +09004196 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004197
4198 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004199 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004200
4201 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4202 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4203 1, 800 - 1, 0.0f, 0.0f));
4204 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4205 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4206 2, 480 - 1, 0.0f, 0.0f));
4207 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4208 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4209 0.0f, 1.0f, 0.0f, 0.0f));
4210}
4211
4212TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004213 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004214 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004215
4216 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004217 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004218
4219 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4220 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4221 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4222 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4223 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4224 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4225 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4226 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4227 0.0f, 1.0f, 0.0f, 0.0f));
4228}
4229
4230TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004231 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004232 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004233
arthurhungdcef2dc2020-08-11 14:47:50 +08004234 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004235
4236 NotifyMotionArgs args;
4237
4238 // Button press.
4239 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4241 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4243 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4244 ASSERT_EQ(DEVICE_ID, args.deviceId);
4245 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4246 ASSERT_EQ(uint32_t(0), args.policyFlags);
4247 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4248 ASSERT_EQ(0, args.flags);
4249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4250 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4251 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004252 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004253 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004254 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004255 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4257 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4258 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4259
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4261 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4262 ASSERT_EQ(DEVICE_ID, args.deviceId);
4263 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4264 ASSERT_EQ(uint32_t(0), args.policyFlags);
4265 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4266 ASSERT_EQ(0, args.flags);
4267 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4268 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4269 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004270 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004271 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004272 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004273 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004274 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4275 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4276 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4277
Michael Wrightd02c5b62014-02-10 15:10:22 -08004278 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004279 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4280 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4282 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4283 ASSERT_EQ(DEVICE_ID, args.deviceId);
4284 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4285 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4287 ASSERT_EQ(0, args.flags);
4288 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4289 ASSERT_EQ(0, args.buttonState);
4290 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004291 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004292 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004293 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004294 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004295 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4296 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4297 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4298
4299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4300 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4301 ASSERT_EQ(DEVICE_ID, args.deviceId);
4302 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4303 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4305 ASSERT_EQ(0, args.flags);
4306 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4307 ASSERT_EQ(0, args.buttonState);
4308 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004309 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004311 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004312 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4314 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4315 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4316}
4317
4318TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004319 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004320 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321
4322 NotifyMotionArgs args;
4323
4324 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004325 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4326 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4328 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004329 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4330 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4331 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332
4333 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004334 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4335 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004336 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4337 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004338 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4339 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340}
4341
4342TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004343 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004344 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345
4346 NotifyMotionArgs args;
4347
4348 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004349 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4350 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4352 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004353 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004354
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4356 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004357 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004358
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004360 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004363 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004364 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365
4366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004367 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004368 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369}
4370
4371TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004373 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004374
4375 NotifyMotionArgs args;
4376
4377 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4379 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4380 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4381 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4383 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004384 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4385 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4386 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4389 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004390 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4391 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4392 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004393
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004395 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4396 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004400 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4401 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4402 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
4404 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4406 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004408 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004409 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004410
4411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004413 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004414}
4415
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004416TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004417 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004418 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004419 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4420 // need to be rotated.
4421 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004422 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004423
Michael Wrighta9cf4192022-12-01 23:46:39 +00004424 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4426 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4427 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4428 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4429 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4430 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4431 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4432 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4433}
4434
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004435TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004436 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004437 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004438 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4439 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004440 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004442 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004443 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004444 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4445 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4446 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4447 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4448 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4449 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4450 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4451 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4452
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004453 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004454 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004455 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4456 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4457 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4458 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4459 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4460 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4461 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4462 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004463
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004464 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004465 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4467 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4468 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4469 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4470 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4471 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4472 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4473 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4474
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004475 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004476 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004477 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4478 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4479 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4480 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4481 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4482 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4483 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4484 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004485}
4486
4487TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004488 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004489 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490
4491 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4492 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493
4494 NotifyMotionArgs motionArgs;
4495 NotifyKeyArgs keyArgs;
4496
4497 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4499 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4502 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004503 ASSERT_NO_FATAL_FAILURE(
4504 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4507 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4508 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004509 ASSERT_NO_FATAL_FAILURE(
4510 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004511
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4513 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004515 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004517 ASSERT_NO_FATAL_FAILURE(
4518 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004519
4520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004521 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004523 ASSERT_NO_FATAL_FAILURE(
4524 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004525
4526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004528 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004529 ASSERT_NO_FATAL_FAILURE(
4530 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4538 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004539 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004540 ASSERT_NO_FATAL_FAILURE(
4541 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4545 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004546 ASSERT_NO_FATAL_FAILURE(
4547 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004548
4549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4551 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004552 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004553 ASSERT_NO_FATAL_FAILURE(
4554 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004555
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004561 ASSERT_NO_FATAL_FAILURE(
4562 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004563
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004565 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004566 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004567 ASSERT_NO_FATAL_FAILURE(
4568 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4571 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4574 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004575 ASSERT_NO_FATAL_FAILURE(
4576 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004579
4580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004583 ASSERT_NO_FATAL_FAILURE(
4584 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4587 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004589 ASSERT_NO_FATAL_FAILURE(
4590 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591
4592 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004593 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4596 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4597 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004598
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4607 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004611 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004616 ASSERT_NO_FATAL_FAILURE(
4617 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004618
4619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004621 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004623 ASSERT_NO_FATAL_FAILURE(
4624 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4626 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4627 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4628
4629 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004630 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4633 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4634 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004635
Michael Wrightd02c5b62014-02-10 15:10:22 -08004636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004637 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004638 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004639 ASSERT_NO_FATAL_FAILURE(
4640 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004641
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4644 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004645 ASSERT_NO_FATAL_FAILURE(
4646 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004651 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004653 ASSERT_NO_FATAL_FAILURE(
4654 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004655
4656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4657 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4658 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004659 ASSERT_NO_FATAL_FAILURE(
4660 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004661
Michael Wrightd02c5b62014-02-10 15:10:22 -08004662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4663 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4664 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4665
4666 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004667 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4668 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4670 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4671 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004672
Michael Wrightd02c5b62014-02-10 15:10:22 -08004673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004674 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004675 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004676 ASSERT_NO_FATAL_FAILURE(
4677 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004678
4679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4680 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4681 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004682 ASSERT_NO_FATAL_FAILURE(
4683 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004684
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004688 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004690 ASSERT_NO_FATAL_FAILURE(
4691 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004692
4693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4694 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4695 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004696 ASSERT_NO_FATAL_FAILURE(
4697 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004698
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4700 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4701 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4702
4703 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4705 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4707 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4708 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004709
Michael Wrightd02c5b62014-02-10 15:10:22 -08004710 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004711 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004712 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004713 ASSERT_NO_FATAL_FAILURE(
4714 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004715
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4717 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4718 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004719 ASSERT_NO_FATAL_FAILURE(
4720 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004721
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004722 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004725 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004727 ASSERT_NO_FATAL_FAILURE(
4728 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004729
4730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4732 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004733 ASSERT_NO_FATAL_FAILURE(
4734 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004735
Michael Wrightd02c5b62014-02-10 15:10:22 -08004736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4737 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4738 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4739}
4740
4741TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004742 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004743 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004744
4745 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4746 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004747
4748 NotifyMotionArgs args;
4749
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004750 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4751 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4752 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004754 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4755 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4757 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 +00004758 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004759}
4760
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004761/**
4762 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4763 * pointer acceleration or speed processing should not be applied.
4764 */
4765TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4766 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004767 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4768 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004769 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004770 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004771
4772 NotifyDeviceResetArgs resetArgs;
4773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4774 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4775 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4776
4777 NotifyMotionArgs args;
4778
4779 // Move and verify scale is applied.
4780 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4781 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4782 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4784 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4785 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4786 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4787 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4788 ASSERT_GT(relX, 10);
4789 ASSERT_GT(relY, 20);
4790
4791 // Enable Pointer Capture
4792 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004793 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004794 NotifyPointerCaptureChangedArgs captureArgs;
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4796 ASSERT_TRUE(captureArgs.request.enable);
4797
4798 // Move and verify scale is not applied.
4799 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4800 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4803 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4805 ASSERT_EQ(10, args.pointerCoords[0].getX());
4806 ASSERT_EQ(20, args.pointerCoords[0].getY());
4807}
4808
Prabir Pradhan208360b2022-06-24 18:37:04 +00004809TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4810 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004811 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004812
4813 NotifyDeviceResetArgs resetArgs;
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4815 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4816 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4817
4818 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004819 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004820
4821 NotifyMotionArgs args;
4822
4823 // Verify that the coordinates are rotated.
4824 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4826 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4828 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4830 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4831 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4832
4833 // Enable Pointer Capture.
4834 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004835 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004836 NotifyPointerCaptureChangedArgs captureArgs;
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4838 ASSERT_TRUE(captureArgs.request.enable);
4839
4840 // Move and verify rotation is not applied.
4841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4843 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4845 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4847 ASSERT_EQ(10, args.pointerCoords[0].getX());
4848 ASSERT_EQ(20, args.pointerCoords[0].getY());
4849}
4850
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004851TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004852 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004853
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004854 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004855 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004856
4857 // Set up the secondary display as the display on which the pointer should be shown.
4858 // The InputDevice is not associated with any display.
4859 prepareSecondaryDisplay();
4860 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004861 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004862
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004863 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004864 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004865
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004866 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004867 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004871 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4872 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4873 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004874 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004875}
4876
4877TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004878 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004879
4880 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004881 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004882
4883 // Set up the secondary display as the display on which the pointer should be shown,
4884 // and associate the InputDevice with the secondary display.
4885 prepareSecondaryDisplay();
4886 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4887 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004888 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004889
4890 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4891 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004892
4893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004897 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4898 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4899 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004900 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004901}
4902
4903TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004904 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004905
4906 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004907 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004908 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4909
4910 // Associate the InputDevice with the secondary display.
4911 prepareSecondaryDisplay();
4912 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004913 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004914
4915 // The mapper should not generate any events because it is associated with a display that is
4916 // different from the pointer display.
4917 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4918 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4919 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004921}
4922
Byoungho Jungda10dd32023-10-06 17:03:45 +09004923// --- CursorInputMapperTestWithChoreographer ---
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004924
Byoungho Jungda10dd32023-10-06 17:03:45 +09004925class CursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004926protected:
4927 void SetUp() override {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004928 input_flags::enable_pointer_choreographer(true);
4929 CursorInputMapperTestBase::SetUp();
4930 }
4931};
4932
Prabir Pradhan19767602023-11-03 16:53:31 +00004933TEST_F(CursorInputMapperTestWithChoreographer, PopulateDeviceInfoReturnsRangeFromPolicy) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004934 addConfigurationProperty("cursor.mode", "pointer");
4935 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4936
4937 InputDeviceInfo info;
4938 mapper.populateDeviceInfo(info);
4939
4940 // Initially there may not be a valid motion range.
4941 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4942 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
4943 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, AINPUT_MOTION_RANGE_PRESSURE,
4944 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4945
4946 // When the viewport and the default pointer display ID is set, then there should be a valid
4947 // motion range.
4948 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4949 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
4950 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
4951 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
4952
4953 InputDeviceInfo info2;
4954 mapper.populateDeviceInfo(info2);
4955
4956 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE, 0,
4957 DISPLAY_WIDTH - 1, 0.0f, 0.0f));
4958 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE, 0,
4959 DISPLAY_HEIGHT - 1, 0.0f, 0.0f));
4960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_PRESSURE,
4961 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4962}
4963
Prabir Pradhan19767602023-11-03 16:53:31 +00004964TEST_F(CursorInputMapperTestWithChoreographer, ProcessShouldHandleAllButtonsWithZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004965 addConfigurationProperty("cursor.mode", "pointer");
4966 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4967
4968 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4969 prepareDisplay(ui::ROTATION_0);
4970
4971 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4972 mFakePointerController->setPosition(100, 200);
4973
4974 NotifyMotionArgs motionArgs;
4975 NotifyKeyArgs keyArgs;
4976
4977 // press BTN_LEFT, release BTN_LEFT
4978 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4979 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4982 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4983 ASSERT_NO_FATAL_FAILURE(
4984 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
4985
4986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4987 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4988 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4989 ASSERT_NO_FATAL_FAILURE(
4990 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
4991
4992 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4995 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4996 ASSERT_EQ(0, motionArgs.buttonState);
4997 ASSERT_NO_FATAL_FAILURE(
4998 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
4999
5000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5001 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5002 ASSERT_EQ(0, motionArgs.buttonState);
5003 ASSERT_NO_FATAL_FAILURE(
5004 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5005
5006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5007 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5008 ASSERT_EQ(0, motionArgs.buttonState);
5009 ASSERT_NO_FATAL_FAILURE(
5010 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5011
5012 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5013 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5014 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5017 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5018 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5019 motionArgs.buttonState);
5020 ASSERT_NO_FATAL_FAILURE(
5021 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5022
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5024 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5025 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5026 ASSERT_NO_FATAL_FAILURE(
5027 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5028
5029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5030 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5031 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5032 motionArgs.buttonState);
5033 ASSERT_NO_FATAL_FAILURE(
5034 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5035
5036 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5037 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5039 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5040 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5041 ASSERT_NO_FATAL_FAILURE(
5042 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5043
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5046 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5047 ASSERT_NO_FATAL_FAILURE(
5048 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5049
5050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5053 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5054 ASSERT_EQ(0, motionArgs.buttonState);
5055 ASSERT_NO_FATAL_FAILURE(
5056 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5057 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5058 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5059
5060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5061 ASSERT_EQ(0, motionArgs.buttonState);
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5063 ASSERT_NO_FATAL_FAILURE(
5064 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5065
5066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5067 ASSERT_EQ(0, motionArgs.buttonState);
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5069 ASSERT_NO_FATAL_FAILURE(
5070 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5071
5072 // press BTN_BACK, release BTN_BACK
5073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5076 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5077 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5078
5079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5081 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5082 ASSERT_NO_FATAL_FAILURE(
5083 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5084
5085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5087 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5088 ASSERT_NO_FATAL_FAILURE(
5089 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5090
5091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5092 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5095 ASSERT_EQ(0, motionArgs.buttonState);
5096 ASSERT_NO_FATAL_FAILURE(
5097 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5098
5099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5100 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5101 ASSERT_EQ(0, motionArgs.buttonState);
5102
5103 ASSERT_NO_FATAL_FAILURE(
5104 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5106 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5107 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5108
5109 // press BTN_SIDE, release BTN_SIDE
5110 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5113 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5114 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5115
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5117 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5118 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5119 ASSERT_NO_FATAL_FAILURE(
5120 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5121
5122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5123 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5125 ASSERT_NO_FATAL_FAILURE(
5126 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5127
5128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5131 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5132 ASSERT_EQ(0, motionArgs.buttonState);
5133 ASSERT_NO_FATAL_FAILURE(
5134 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5135
5136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5137 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5138 ASSERT_EQ(0, motionArgs.buttonState);
5139 ASSERT_NO_FATAL_FAILURE(
5140 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5141
5142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5143 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5144 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5145
5146 // press BTN_FORWARD, release BTN_FORWARD
5147 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5150 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5151 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5152
5153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5154 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5155 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5156 ASSERT_NO_FATAL_FAILURE(
5157 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5158
5159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5160 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5161 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5162 ASSERT_NO_FATAL_FAILURE(
5163 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5164
5165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5168 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5169 ASSERT_EQ(0, motionArgs.buttonState);
5170 ASSERT_NO_FATAL_FAILURE(
5171 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5172
5173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5174 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5175 ASSERT_EQ(0, motionArgs.buttonState);
5176 ASSERT_NO_FATAL_FAILURE(
5177 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5178
5179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5180 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5181 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5182
5183 // press BTN_EXTRA, release BTN_EXTRA
5184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5187 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5188 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5189
5190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5192 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5193 ASSERT_NO_FATAL_FAILURE(
5194 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5195
5196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5197 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5199 ASSERT_NO_FATAL_FAILURE(
5200 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5201
5202 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5203 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5205 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5206 ASSERT_EQ(0, motionArgs.buttonState);
5207 ASSERT_NO_FATAL_FAILURE(
5208 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5209
5210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5211 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5212 ASSERT_EQ(0, motionArgs.buttonState);
5213 ASSERT_NO_FATAL_FAILURE(
5214 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5215
5216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5217 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5218 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5219}
5220
Prabir Pradhan19767602023-11-03 16:53:31 +00005221TEST_F(CursorInputMapperTestWithChoreographer, ProcessWhenModeIsPointerShouldKeepZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005222 addConfigurationProperty("cursor.mode", "pointer");
5223 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5224
5225 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5226 prepareDisplay(ui::ROTATION_0);
5227
5228 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5229 mFakePointerController->setPosition(100, 200);
5230
5231 NotifyMotionArgs args;
5232
5233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5237 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5238 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5239 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
5240 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5241}
5242
5243TEST_F(CursorInputMapperTestWithChoreographer, PointerCaptureDisablesVelocityProcessing) {
5244 addConfigurationProperty("cursor.mode", "pointer");
Prabir Pradhan19767602023-11-03 16:53:31 +00005245 const VelocityControlParameters testParams(/*scale=*/5.f, /*lowThreshold=*/0.f,
5246 /*highThreshold=*/100.f, /*acceleration=*/10.f);
Byoungho Jungda10dd32023-10-06 17:03:45 +09005247 mFakePolicy->setVelocityControlParams(testParams);
5248 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5249
5250 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5251 prepareDisplay(ui::ROTATION_0);
5252
5253 NotifyDeviceResetArgs resetArgs;
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5255 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5256 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5257
5258 NotifyMotionArgs args;
5259
5260 // Move and verify scale is applied.
5261 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5262 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5265 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5266 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5267 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5268 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5269 ASSERT_GT(relX, 10);
5270 ASSERT_GT(relY, 20);
5271
5272 // Enable Pointer Capture
5273 mFakePolicy->setPointerCapture(true);
5274 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
5275 NotifyPointerCaptureChangedArgs captureArgs;
5276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5277 ASSERT_TRUE(captureArgs.request.enable);
5278
5279 // Move and verify scale is not applied.
5280 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5281 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5284 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5285 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5286 const float relX2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5287 const float relY2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5288 ASSERT_EQ(10, relX2);
5289 ASSERT_EQ(20, relY2);
5290}
5291
Prabir Pradhan19767602023-11-03 16:53:31 +00005292TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdNoAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005293 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5294
5295 // Set up the default display.
5296 prepareDisplay(ui::ROTATION_90);
5297
5298 // Set up the secondary display as the display on which the pointer should be shown.
5299 // The InputDevice is not associated with any display.
5300 prepareSecondaryDisplay();
5301 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5302 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5303
5304 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5305 mFakePointerController->setPosition(100, 200);
5306
5307 // Ensure input events are generated without display ID and coords,
5308 // because they will be decided later by PointerChoreographer.
5309 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5310 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5314 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(ADISPLAY_ID_NONE),
5315 WithCoords(0.0f, 0.0f))));
5316}
5317
Prabir Pradhan19767602023-11-03 16:53:31 +00005318TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdWithAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005319 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5320
5321 // Set up the default display.
5322 prepareDisplay(ui::ROTATION_90);
5323
5324 // Set up the secondary display as the display on which the pointer should be shown,
5325 // and associate the InputDevice with the secondary display.
5326 prepareSecondaryDisplay();
5327 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5328 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5329 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5330
5331 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5332 mFakePointerController->setPosition(100, 200);
5333
5334 // Ensure input events are generated with associated display ID but not with coords,
5335 // because the coords will be decided later by PointerChoreographer.
5336 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5337 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5340 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5341 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5342 WithCoords(0.0f, 0.0f))));
5343}
5344
5345TEST_F(CursorInputMapperTestWithChoreographer,
Prabir Pradhan19767602023-11-03 16:53:31 +00005346 ConfigureDisplayIdShouldGenerateEventWithMismatchedPointerDisplay) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005347 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5348
5349 // Set up the default display as the display on which the pointer should be shown.
5350 prepareDisplay(ui::ROTATION_90);
5351 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5352
5353 // Associate the InputDevice with the secondary display.
5354 prepareSecondaryDisplay();
5355 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5356 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5357
5358 // With PointerChoreographer enabled, there could be a PointerController for the associated
5359 // display even if it is different from the pointer display. So the mapper should generate an
5360 // event.
5361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5365 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5366 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5367 WithCoords(0.0f, 0.0f))));
5368}
5369
5370// --- BluetoothCursorInputMapperTest ---
5371
5372class BluetoothCursorInputMapperTest : public CursorInputMapperTestBase {
5373protected:
5374 void SetUp() override {
5375 input_flags::enable_pointer_choreographer(false);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005376 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5377
5378 mFakePointerController = std::make_shared<FakePointerController>();
5379 mFakePolicy->setPointerController(mFakePointerController);
5380 }
5381};
5382
5383TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5384 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005385 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005386
5387 nsecs_t kernelEventTime = ARBITRARY_TIME;
5388 nsecs_t expectedEventTime = ARBITRARY_TIME;
5389 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5390 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5392 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5393 WithEventTime(expectedEventTime))));
5394
5395 // Process several events that come in quick succession, according to their timestamps.
5396 for (int i = 0; i < 3; i++) {
5397 constexpr static nsecs_t delta = ms2ns(1);
5398 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5399 kernelEventTime += delta;
5400 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5401
5402 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5403 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5405 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5406 WithEventTime(expectedEventTime))));
5407 }
5408}
5409
5410TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5411 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005412 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005413
5414 nsecs_t expectedEventTime = ARBITRARY_TIME;
5415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5416 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5418 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5419 WithEventTime(expectedEventTime))));
5420
5421 // Process several events with the same timestamp from the kernel.
5422 // Ensure that we do not generate events too far into the future.
5423 constexpr static int32_t numEvents =
5424 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5425 for (int i = 0; i < numEvents; i++) {
5426 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5427
5428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5432 WithEventTime(expectedEventTime))));
5433 }
5434
5435 // By processing more events with the same timestamp, we should not generate events with a
5436 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5437 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5438 for (int i = 0; i < 3; i++) {
5439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5442 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5443 WithEventTime(cappedEventTime))));
5444 }
5445}
5446
5447TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5448 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005449 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005450
5451 nsecs_t kernelEventTime = ARBITRARY_TIME;
5452 nsecs_t expectedEventTime = ARBITRARY_TIME;
5453 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5454 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5456 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5457 WithEventTime(expectedEventTime))));
5458
5459 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5460 // smoothening is not needed, its timestamp is not affected.
5461 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5462 expectedEventTime = kernelEventTime;
5463
5464 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5465 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5467 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5468 WithEventTime(expectedEventTime))));
5469}
5470
Byoungho Jungda10dd32023-10-06 17:03:45 +09005471// --- BluetoothCursorInputMapperTestWithChoreographer ---
5472
5473class BluetoothCursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
5474protected:
5475 void SetUp() override {
5476 input_flags::enable_pointer_choreographer(true);
5477 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5478
5479 mFakePointerController = std::make_shared<FakePointerController>();
5480 mFakePolicy->setPointerController(mFakePointerController);
5481 }
5482};
5483
5484TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmoothening) {
5485 addConfigurationProperty("cursor.mode", "pointer");
5486 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5487
5488 // Set up the default display.
5489 prepareDisplay(ui::ROTATION_0);
5490 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5491 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5492
5493 nsecs_t kernelEventTime = ARBITRARY_TIME;
5494 nsecs_t expectedEventTime = ARBITRARY_TIME;
5495 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5496 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5498 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5499 WithEventTime(expectedEventTime))));
5500
5501 // Process several events that come in quick succession, according to their timestamps.
5502 for (int i = 0; i < 3; i++) {
5503 constexpr static nsecs_t delta = ms2ns(1);
5504 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5505 kernelEventTime += delta;
5506 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5507
5508 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5509 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5512 WithEventTime(expectedEventTime))));
5513 }
5514}
5515
5516TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningIsCapped) {
5517 addConfigurationProperty("cursor.mode", "pointer");
5518 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5519
5520 // Set up the default display.
5521 prepareDisplay(ui::ROTATION_0);
5522 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5523 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5524
5525 nsecs_t expectedEventTime = ARBITRARY_TIME;
5526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5529 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5530 WithEventTime(expectedEventTime))));
5531
5532 // Process several events with the same timestamp from the kernel.
5533 // Ensure that we do not generate events too far into the future.
5534 constexpr static int32_t numEvents =
5535 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5536 for (int i = 0; i < numEvents; i++) {
5537 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5538
5539 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5540 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5542 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5543 WithEventTime(expectedEventTime))));
5544 }
5545
5546 // By processing more events with the same timestamp, we should not generate events with a
5547 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5548 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5549 for (int i = 0; i < 3; i++) {
5550 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5551 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5553 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5554 WithEventTime(cappedEventTime))));
5555 }
5556}
5557
5558TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningNotUsed) {
5559 addConfigurationProperty("cursor.mode", "pointer");
5560 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5561
5562 // Set up the default display.
5563 prepareDisplay(ui::ROTATION_0);
5564 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5565 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5566
5567 nsecs_t kernelEventTime = ARBITRARY_TIME;
5568 nsecs_t expectedEventTime = ARBITRARY_TIME;
5569 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5570 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5572 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5573 WithEventTime(expectedEventTime))));
5574
5575 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5576 // smoothening is not needed, its timestamp is not affected.
5577 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5578 expectedEventTime = kernelEventTime;
5579
5580 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5581 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5583 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5584 WithEventTime(expectedEventTime))));
5585}
5586
Michael Wrightd02c5b62014-02-10 15:10:22 -08005587// --- TouchInputMapperTest ---
5588
5589class TouchInputMapperTest : public InputMapperTest {
5590protected:
5591 static const int32_t RAW_X_MIN;
5592 static const int32_t RAW_X_MAX;
5593 static const int32_t RAW_Y_MIN;
5594 static const int32_t RAW_Y_MAX;
5595 static const int32_t RAW_TOUCH_MIN;
5596 static const int32_t RAW_TOUCH_MAX;
5597 static const int32_t RAW_TOOL_MIN;
5598 static const int32_t RAW_TOOL_MAX;
5599 static const int32_t RAW_PRESSURE_MIN;
5600 static const int32_t RAW_PRESSURE_MAX;
5601 static const int32_t RAW_ORIENTATION_MIN;
5602 static const int32_t RAW_ORIENTATION_MAX;
5603 static const int32_t RAW_DISTANCE_MIN;
5604 static const int32_t RAW_DISTANCE_MAX;
5605 static const int32_t RAW_TILT_MIN;
5606 static const int32_t RAW_TILT_MAX;
5607 static const int32_t RAW_ID_MIN;
5608 static const int32_t RAW_ID_MAX;
5609 static const int32_t RAW_SLOT_MIN;
5610 static const int32_t RAW_SLOT_MAX;
5611 static const float X_PRECISION;
5612 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005613 static const float X_PRECISION_VIRTUAL;
5614 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005615
5616 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005617 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618
5619 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5620
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005621 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005622 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005623
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 enum Axes {
5625 POSITION = 1 << 0,
5626 TOUCH = 1 << 1,
5627 TOOL = 1 << 2,
5628 PRESSURE = 1 << 3,
5629 ORIENTATION = 1 << 4,
5630 MINOR = 1 << 5,
5631 ID = 1 << 6,
5632 DISTANCE = 1 << 7,
5633 TILT = 1 << 8,
5634 SLOT = 1 << 9,
5635 TOOL_TYPE = 1 << 10,
5636 };
5637
Michael Wrighta9cf4192022-12-01 23:46:39 +00005638 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005639 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005640 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005641 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005642 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643 int32_t toRawX(float displayX);
5644 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005645 int32_t toRotatedRawX(float displayX);
5646 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005647 float toCookedX(float rawX, float rawY);
5648 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005650 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005652 float toDisplayY(int32_t rawY, int32_t displayHeight);
5653
Michael Wrightd02c5b62014-02-10 15:10:22 -08005654};
5655
5656const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5657const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5658const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5659const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5660const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5661const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5662const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5663const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005664const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5665const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5667const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5668const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5669const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5670const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5671const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5672const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5673const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5674const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5675const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5676const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5677const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005678const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5679 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5680const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5681 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005682const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5683 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005684
5685const float TouchInputMapperTest::GEOMETRIC_SCALE =
5686 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5687 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5688
5689const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5690 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5691 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5692};
5693
Michael Wrighta9cf4192022-12-01 23:46:39 +00005694void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005695 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5696 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005697}
5698
5699void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5700 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005701 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702}
5703
Michael Wrighta9cf4192022-12-01 23:46:39 +00005704void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005705 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5706 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5707 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005708}
5709
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005711 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5712 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5713 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5714 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005715}
5716
Jason Gerecke489fda82012-09-07 17:19:40 -07005717void TouchInputMapperTest::prepareLocationCalibration() {
5718 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5719}
5720
Michael Wrightd02c5b62014-02-10 15:10:22 -08005721int32_t TouchInputMapperTest::toRawX(float displayX) {
5722 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5723}
5724
5725int32_t TouchInputMapperTest::toRawY(float displayY) {
5726 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5727}
5728
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005729int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5730 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5731}
5732
5733int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5734 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5735}
5736
Jason Gerecke489fda82012-09-07 17:19:40 -07005737float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5738 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5739 return rawX;
5740}
5741
5742float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5743 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5744 return rawY;
5745}
5746
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005748 return toDisplayX(rawX, DISPLAY_WIDTH);
5749}
5750
5751float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5752 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005753}
5754
5755float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005756 return toDisplayY(rawY, DISPLAY_HEIGHT);
5757}
5758
5759float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5760 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005761}
5762
5763
5764// --- SingleTouchInputMapperTest ---
5765
5766class SingleTouchInputMapperTest : public TouchInputMapperTest {
5767protected:
5768 void prepareButtons();
5769 void prepareAxes(int axes);
5770
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005771 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5772 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5773 void processUp(SingleTouchInputMapper& mappery);
5774 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5775 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5776 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5777 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5778 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5779 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005780};
5781
5782void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005783 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005784}
5785
5786void SingleTouchInputMapperTest::prepareAxes(int axes) {
5787 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005788 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5789 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005790 }
5791 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005792 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5793 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005794 }
5795 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005796 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5797 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005798 }
5799 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005800 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5801 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802 }
5803 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005804 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5805 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005806 }
5807}
5808
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005809void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005810 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5811 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813}
5814
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005815void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005816 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5817 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818}
5819
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005820void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005821 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822}
5823
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005824void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005825 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005826}
5827
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005828void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5829 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005831}
5832
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005833void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005834 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005835}
5836
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005837void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5838 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005839 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005841}
5842
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005843void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5844 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005845 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005846}
5847
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005848void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005849 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005850}
5851
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005853 prepareButtons();
5854 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005855 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005856
Josep del Río2d8c79a2023-01-23 19:33:50 +00005857 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858}
5859
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861 prepareButtons();
5862 prepareAxes(POSITION);
5863 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005864 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005866 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867}
5868
5869TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005870 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005871 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 prepareButtons();
5873 prepareAxes(POSITION);
5874 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005875 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005876
5877 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005878 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879
5880 // Virtual key is down.
5881 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5882 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5883 processDown(mapper, x, y);
5884 processSync(mapper);
5885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5886
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005887 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888
5889 // Virtual key is up.
5890 processUp(mapper);
5891 processSync(mapper);
5892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5893
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005894 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005895}
5896
5897TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005898 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005899 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 prepareButtons();
5901 prepareAxes(POSITION);
5902 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005903 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005904
5905 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005906 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907
5908 // Virtual key is down.
5909 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5910 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5911 processDown(mapper, x, y);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5914
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005915 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916
5917 // Virtual key is up.
5918 processUp(mapper);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5921
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005922 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005923}
5924
5925TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005926 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005927 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 prepareButtons();
5929 prepareAxes(POSITION);
5930 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005931 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005934 ASSERT_TRUE(
5935 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005936 ASSERT_TRUE(flags[0]);
5937 ASSERT_FALSE(flags[1]);
5938}
5939
5940TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005942 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 prepareButtons();
5944 prepareAxes(POSITION);
5945 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005946 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005947
arthurhungdcef2dc2020-08-11 14:47:50 +08005948 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
5950 NotifyKeyArgs args;
5951
5952 // Press virtual key.
5953 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5954 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5955 processDown(mapper, x, y);
5956 processSync(mapper);
5957
5958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5959 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5960 ASSERT_EQ(DEVICE_ID, args.deviceId);
5961 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5962 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5963 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5964 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5965 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5966 ASSERT_EQ(KEY_HOME, args.scanCode);
5967 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5968 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5969
5970 // Release virtual key.
5971 processUp(mapper);
5972 processSync(mapper);
5973
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5975 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5976 ASSERT_EQ(DEVICE_ID, args.deviceId);
5977 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5978 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5979 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5980 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5981 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5982 ASSERT_EQ(KEY_HOME, args.scanCode);
5983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5984 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5985
5986 // Should not have sent any motions.
5987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5988}
5989
5990TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005991 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005992 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 prepareButtons();
5994 prepareAxes(POSITION);
5995 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005996 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
arthurhungdcef2dc2020-08-11 14:47:50 +08005998 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
6000 NotifyKeyArgs keyArgs;
6001
6002 // Press virtual key.
6003 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6004 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6005 processDown(mapper, x, y);
6006 processSync(mapper);
6007
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6009 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6010 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6011 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6012 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6013 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6014 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6015 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6016 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6017 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6018 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6019
6020 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6021 // into the display area.
6022 y -= 100;
6023 processMove(mapper, x, y);
6024 processSync(mapper);
6025
6026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6027 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6028 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6029 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6030 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6031 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6032 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6033 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6034 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6035 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6036 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6037 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6038
6039 NotifyMotionArgs motionArgs;
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6041 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6042 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6043 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6044 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6045 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6046 ASSERT_EQ(0, motionArgs.flags);
6047 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6048 ASSERT_EQ(0, motionArgs.buttonState);
6049 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006050 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006051 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006052 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6054 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6055 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6056 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6057 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6058
6059 // Keep moving out of bounds. Should generate a pointer move.
6060 y -= 50;
6061 processMove(mapper, x, y);
6062 processSync(mapper);
6063
6064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6065 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6066 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6067 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6068 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6069 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6070 ASSERT_EQ(0, motionArgs.flags);
6071 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6072 ASSERT_EQ(0, motionArgs.buttonState);
6073 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006074 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006075 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006076 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6078 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6079 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6080 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6081 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6082
6083 // Release out of bounds. Should generate a pointer up.
6084 processUp(mapper);
6085 processSync(mapper);
6086
6087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6088 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6089 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6090 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6091 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6092 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6093 ASSERT_EQ(0, motionArgs.flags);
6094 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6095 ASSERT_EQ(0, motionArgs.buttonState);
6096 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006097 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006098 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006099 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6101 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6102 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6103 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6104 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6105
6106 // Should not have sent any more keys or motions.
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6109}
6110
6111TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006112 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006113 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 prepareButtons();
6115 prepareAxes(POSITION);
6116 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006117 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006118
arthurhungdcef2dc2020-08-11 14:47:50 +08006119 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120
6121 NotifyMotionArgs motionArgs;
6122
6123 // Initially go down out of bounds.
6124 int32_t x = -10;
6125 int32_t y = -10;
6126 processDown(mapper, x, y);
6127 processSync(mapper);
6128
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6130
6131 // Move into the display area. Should generate a pointer down.
6132 x = 50;
6133 y = 75;
6134 processMove(mapper, x, y);
6135 processSync(mapper);
6136
6137 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6139 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6140 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6141 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6142 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6143 ASSERT_EQ(0, motionArgs.flags);
6144 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6145 ASSERT_EQ(0, motionArgs.buttonState);
6146 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006147 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006148 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006149 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6151 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6152 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6153 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6154 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6155
6156 // Release. Should generate a pointer up.
6157 processUp(mapper);
6158 processSync(mapper);
6159
6160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6161 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6162 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6163 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6164 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6165 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6166 ASSERT_EQ(0, motionArgs.flags);
6167 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6168 ASSERT_EQ(0, motionArgs.buttonState);
6169 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006170 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006171 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006172 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6174 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6175 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6176 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6177 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6178
6179 // Should not have sent any more keys or motions.
6180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6182}
6183
Santos Cordonfa5cf462017-04-05 10:37:00 -07006184TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006185 addConfigurationProperty("touch.deviceType", "touchScreen");
6186 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6187
Michael Wrighta9cf4192022-12-01 23:46:39 +00006188 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006189 prepareButtons();
6190 prepareAxes(POSITION);
6191 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006192 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006193
arthurhungdcef2dc2020-08-11 14:47:50 +08006194 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006195
6196 NotifyMotionArgs motionArgs;
6197
6198 // Down.
6199 int32_t x = 100;
6200 int32_t y = 125;
6201 processDown(mapper, x, y);
6202 processSync(mapper);
6203
6204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6206 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6207 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6208 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6209 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6210 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6211 ASSERT_EQ(0, motionArgs.flags);
6212 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6213 ASSERT_EQ(0, motionArgs.buttonState);
6214 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006215 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006216 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006217 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6219 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6220 1, 0, 0, 0, 0, 0, 0, 0));
6221 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6222 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6223 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6224
6225 // Move.
6226 x += 50;
6227 y += 75;
6228 processMove(mapper, x, y);
6229 processSync(mapper);
6230
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6232 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6233 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6234 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6235 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6236 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6237 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6238 ASSERT_EQ(0, motionArgs.flags);
6239 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6240 ASSERT_EQ(0, motionArgs.buttonState);
6241 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006242 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006243 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6246 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6247 1, 0, 0, 0, 0, 0, 0, 0));
6248 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6249 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6251
6252 // Up.
6253 processUp(mapper);
6254 processSync(mapper);
6255
6256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6257 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6258 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6259 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6260 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6261 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6262 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6263 ASSERT_EQ(0, motionArgs.flags);
6264 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6265 ASSERT_EQ(0, motionArgs.buttonState);
6266 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006267 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006268 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006269 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006270 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6271 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6272 1, 0, 0, 0, 0, 0, 0, 0));
6273 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6274 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6275 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6276
6277 // Should not have sent any more keys or motions.
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6280}
6281
Michael Wrightd02c5b62014-02-10 15:10:22 -08006282TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006283 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006284 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 prepareButtons();
6286 prepareAxes(POSITION);
6287 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006288 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006289
arthurhungdcef2dc2020-08-11 14:47:50 +08006290 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291
6292 NotifyMotionArgs motionArgs;
6293
6294 // Down.
6295 int32_t x = 100;
6296 int32_t y = 125;
6297 processDown(mapper, x, y);
6298 processSync(mapper);
6299
6300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6301 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6302 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6303 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6304 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6305 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6306 ASSERT_EQ(0, motionArgs.flags);
6307 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6308 ASSERT_EQ(0, motionArgs.buttonState);
6309 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006310 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006311 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006312 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6314 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6315 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6316 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6317 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6318
6319 // Move.
6320 x += 50;
6321 y += 75;
6322 processMove(mapper, x, y);
6323 processSync(mapper);
6324
6325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6326 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6327 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6328 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6329 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6331 ASSERT_EQ(0, motionArgs.flags);
6332 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6333 ASSERT_EQ(0, motionArgs.buttonState);
6334 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006335 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006336 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006337 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6339 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6340 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6341 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6342 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6343
6344 // Up.
6345 processUp(mapper);
6346 processSync(mapper);
6347
6348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6350 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6353 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6354 ASSERT_EQ(0, motionArgs.flags);
6355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6356 ASSERT_EQ(0, motionArgs.buttonState);
6357 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006358 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006360 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6362 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6363 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6364 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6365 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6366
6367 // Should not have sent any more keys or motions.
6368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6370}
6371
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006372TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006373 addConfigurationProperty("touch.deviceType", "touchScreen");
6374 prepareButtons();
6375 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006376 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6377 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00006378 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006379
6380 NotifyMotionArgs args;
6381
6382 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006383 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006384 processDown(mapper, toRawX(50), toRawY(75));
6385 processSync(mapper);
6386
6387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6388 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6389 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6390
6391 processUp(mapper);
6392 processSync(mapper);
6393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6394}
6395
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006396TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006397 addConfigurationProperty("touch.deviceType", "touchScreen");
6398 prepareButtons();
6399 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006400 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6401 // orientation-aware are affected by display rotation.
6402 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00006403 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006404
6405 NotifyMotionArgs args;
6406
6407 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006408 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006409 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006410 processDown(mapper, toRawX(50), toRawY(75));
6411 processSync(mapper);
6412
6413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6414 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6415 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6416
6417 processUp(mapper);
6418 processSync(mapper);
6419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6420
6421 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006422 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006423 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006424 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006425 processSync(mapper);
6426
6427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6428 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6429 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6430
6431 processUp(mapper);
6432 processSync(mapper);
6433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6434
6435 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006436 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006437 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006438 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6439 processSync(mapper);
6440
6441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6442 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6443 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6444
6445 processUp(mapper);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6448
6449 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006450 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006451 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006452 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006453 processSync(mapper);
6454
6455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6456 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6457 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6458
6459 processUp(mapper);
6460 processSync(mapper);
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6462}
6463
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006464TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6465 addConfigurationProperty("touch.deviceType", "touchScreen");
6466 prepareButtons();
6467 prepareAxes(POSITION);
6468 addConfigurationProperty("touch.orientationAware", "1");
6469 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6470 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006471 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006472 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006473 NotifyMotionArgs args;
6474
6475 // Orientation 0.
6476 processDown(mapper, toRawX(50), toRawY(75));
6477 processSync(mapper);
6478
6479 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6480 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6481 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6482
6483 processUp(mapper);
6484 processSync(mapper);
6485 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6486}
6487
6488TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6489 addConfigurationProperty("touch.deviceType", "touchScreen");
6490 prepareButtons();
6491 prepareAxes(POSITION);
6492 addConfigurationProperty("touch.orientationAware", "1");
6493 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6494 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006495 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006496 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006497 NotifyMotionArgs args;
6498
6499 // Orientation 90.
6500 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6501 processSync(mapper);
6502
6503 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6504 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6505 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6506
6507 processUp(mapper);
6508 processSync(mapper);
6509 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6510}
6511
6512TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6513 addConfigurationProperty("touch.deviceType", "touchScreen");
6514 prepareButtons();
6515 prepareAxes(POSITION);
6516 addConfigurationProperty("touch.orientationAware", "1");
6517 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6518 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006519 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006520 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006521 NotifyMotionArgs args;
6522
6523 // Orientation 180.
6524 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6525 processSync(mapper);
6526
6527 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6528 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6529 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6530
6531 processUp(mapper);
6532 processSync(mapper);
6533 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6534}
6535
6536TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6537 addConfigurationProperty("touch.deviceType", "touchScreen");
6538 prepareButtons();
6539 prepareAxes(POSITION);
6540 addConfigurationProperty("touch.orientationAware", "1");
6541 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6542 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006543 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006544 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006545 NotifyMotionArgs args;
6546
6547 // Orientation 270.
6548 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6549 processSync(mapper);
6550
6551 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6552 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6553 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6554
6555 processUp(mapper);
6556 processSync(mapper);
6557 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6558}
6559
6560TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6561 addConfigurationProperty("touch.deviceType", "touchScreen");
6562 prepareButtons();
6563 prepareAxes(POSITION);
6564 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6565 // orientation-aware are affected by display rotation.
6566 addConfigurationProperty("touch.orientationAware", "0");
6567 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006568 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006569
6570 NotifyMotionArgs args;
6571
6572 // Orientation 90, Rotation 0.
6573 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006574 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006575 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6576 processSync(mapper);
6577
6578 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6579 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6580 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6581
6582 processUp(mapper);
6583 processSync(mapper);
6584 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6585
6586 // Orientation 90, Rotation 90.
6587 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006588 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006589 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006590 processSync(mapper);
6591
6592 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6593 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6594 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6595
6596 processUp(mapper);
6597 processSync(mapper);
6598 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6599
6600 // Orientation 90, Rotation 180.
6601 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006602 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006603 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6604 processSync(mapper);
6605
6606 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6607 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6608 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6609
6610 processUp(mapper);
6611 processSync(mapper);
6612 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6613
6614 // Orientation 90, Rotation 270.
6615 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006616 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006617 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 -07006618 processSync(mapper);
6619
6620 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6621 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6622 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6623
6624 processUp(mapper);
6625 processSync(mapper);
6626 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6627}
6628
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006629TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6630 addConfigurationProperty("touch.deviceType", "touchScreen");
6631 prepareButtons();
6632 prepareAxes(POSITION);
6633 addConfigurationProperty("touch.orientationAware", "1");
6634 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006635 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006636
6637 // Set a physical frame in the display viewport.
6638 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6639 viewport->physicalLeft = 20;
6640 viewport->physicalTop = 600;
6641 viewport->physicalRight = 30;
6642 viewport->physicalBottom = 610;
6643 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006644 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006645
6646 // Start the touch.
6647 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6648 processSync(mapper);
6649
6650 // Expect all input starting outside the physical frame to be ignored.
6651 const std::array<Point, 6> outsidePoints = {
6652 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6653 for (const auto& p : outsidePoints) {
6654 processMove(mapper, toRawX(p.x), toRawY(p.y));
6655 processSync(mapper);
6656 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6657 }
6658
6659 // Move the touch into the physical frame.
6660 processMove(mapper, toRawX(25), toRawY(605));
6661 processSync(mapper);
6662 NotifyMotionArgs args;
6663 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6664 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6665 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6666 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6667
6668 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6669 for (const auto& p : outsidePoints) {
6670 processMove(mapper, toRawX(p.x), toRawY(p.y));
6671 processSync(mapper);
6672 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6673 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6674 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6675 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6676 }
6677
6678 processUp(mapper);
6679 processSync(mapper);
6680 EXPECT_NO_FATAL_FAILURE(
6681 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6682}
6683
Harry Cutts1db43992023-06-19 17:05:07 +00006684TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6685 std::shared_ptr<FakePointerController> fakePointerController =
6686 std::make_shared<FakePointerController>();
6687 mFakePolicy->setPointerController(fakePointerController);
6688
6689 addConfigurationProperty("touch.deviceType", "pointer");
6690 prepareAxes(POSITION);
6691 prepareDisplay(ui::ROTATION_0);
6692 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6693
6694 // Set a physical frame in the display viewport.
6695 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6696 viewport->physicalLeft = 20;
6697 viewport->physicalTop = 600;
6698 viewport->physicalRight = 30;
6699 viewport->physicalBottom = 610;
6700 mFakePolicy->updateViewport(*viewport);
6701 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6702
6703 // Start the touch.
6704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6705 processSync(mapper);
6706
6707 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6708 // produced.
6709 const std::array<Point, 6> outsidePoints = {
6710 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6711 for (const auto& p : outsidePoints) {
6712 processMove(mapper, toRawX(p.x), toRawY(p.y));
6713 processSync(mapper);
6714 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6715 }
6716}
6717
Michael Wrightd02c5b62014-02-10 15:10:22 -08006718TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006719 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006720 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006721 prepareButtons();
6722 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006723 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006724
6725 // These calculations are based on the input device calibration documentation.
6726 int32_t rawX = 100;
6727 int32_t rawY = 200;
6728 int32_t rawPressure = 10;
6729 int32_t rawToolMajor = 12;
6730 int32_t rawDistance = 2;
6731 int32_t rawTiltX = 30;
6732 int32_t rawTiltY = 110;
6733
6734 float x = toDisplayX(rawX);
6735 float y = toDisplayY(rawY);
6736 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6737 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6738 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6739 float distance = float(rawDistance);
6740
6741 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6742 float tiltScale = M_PI / 180;
6743 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6744 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6745 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6746 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6747
6748 processDown(mapper, rawX, rawY);
6749 processPressure(mapper, rawPressure);
6750 processToolMajor(mapper, rawToolMajor);
6751 processDistance(mapper, rawDistance);
6752 processTilt(mapper, rawTiltX, rawTiltY);
6753 processSync(mapper);
6754
6755 NotifyMotionArgs args;
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6758 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6759 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6760}
6761
Jason Gerecke489fda82012-09-07 17:19:40 -07006762TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006763 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006764 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006765 prepareLocationCalibration();
6766 prepareButtons();
6767 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006768 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006769
6770 int32_t rawX = 100;
6771 int32_t rawY = 200;
6772
6773 float x = toDisplayX(toCookedX(rawX, rawY));
6774 float y = toDisplayY(toCookedY(rawX, rawY));
6775
6776 processDown(mapper, rawX, rawY);
6777 processSync(mapper);
6778
6779 NotifyMotionArgs args;
6780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6782 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6783}
6784
Michael Wrightd02c5b62014-02-10 15:10:22 -08006785TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006786 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006787 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006788 prepareButtons();
6789 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006790 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006791
6792 NotifyMotionArgs motionArgs;
6793 NotifyKeyArgs keyArgs;
6794
6795 processDown(mapper, 100, 200);
6796 processSync(mapper);
6797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6798 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6799 ASSERT_EQ(0, motionArgs.buttonState);
6800
6801 // press BTN_LEFT, release BTN_LEFT
6802 processKey(mapper, BTN_LEFT, 1);
6803 processSync(mapper);
6804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6806 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6807
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6809 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6810 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6811
Michael Wrightd02c5b62014-02-10 15:10:22 -08006812 processKey(mapper, BTN_LEFT, 0);
6813 processSync(mapper);
6814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006815 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006816 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006817
6818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006820 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821
6822 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6823 processKey(mapper, BTN_RIGHT, 1);
6824 processKey(mapper, BTN_MIDDLE, 1);
6825 processSync(mapper);
6826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6827 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6828 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6829 motionArgs.buttonState);
6830
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6832 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6833 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6834
6835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6836 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6837 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6838 motionArgs.buttonState);
6839
Michael Wrightd02c5b62014-02-10 15:10:22 -08006840 processKey(mapper, BTN_RIGHT, 0);
6841 processSync(mapper);
6842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006843 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006844 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006845
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006847 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006848 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006849
6850 processKey(mapper, BTN_MIDDLE, 0);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006853 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006854 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006855
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006857 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006858 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859
6860 // press BTN_BACK, release BTN_BACK
6861 processKey(mapper, BTN_BACK, 1);
6862 processSync(mapper);
6863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6864 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6865 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006866
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006868 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006869 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6870
6871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6872 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6873 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006874
6875 processKey(mapper, BTN_BACK, 0);
6876 processSync(mapper);
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006878 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006879 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006880
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006883 ASSERT_EQ(0, motionArgs.buttonState);
6884
Michael Wrightd02c5b62014-02-10 15:10:22 -08006885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6886 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6887 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6888
6889 // press BTN_SIDE, release BTN_SIDE
6890 processKey(mapper, BTN_SIDE, 1);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6893 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6894 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006895
Michael Wrightd02c5b62014-02-10 15:10:22 -08006896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006898 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6899
6900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6901 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6902 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006903
6904 processKey(mapper, BTN_SIDE, 0);
6905 processSync(mapper);
6906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006907 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006909
6910 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006911 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006912 ASSERT_EQ(0, motionArgs.buttonState);
6913
Michael Wrightd02c5b62014-02-10 15:10:22 -08006914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6915 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6916 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6917
6918 // press BTN_FORWARD, release BTN_FORWARD
6919 processKey(mapper, BTN_FORWARD, 1);
6920 processSync(mapper);
6921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6922 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6923 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006924
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006926 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006927 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6928
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6930 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6931 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006932
6933 processKey(mapper, BTN_FORWARD, 0);
6934 processSync(mapper);
6935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006937 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006938
6939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006941 ASSERT_EQ(0, motionArgs.buttonState);
6942
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6944 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6945 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6946
6947 // press BTN_EXTRA, release BTN_EXTRA
6948 processKey(mapper, BTN_EXTRA, 1);
6949 processSync(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6951 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6952 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006953
Michael Wrightd02c5b62014-02-10 15:10:22 -08006954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006956 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6957
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6960 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006961
6962 processKey(mapper, BTN_EXTRA, 0);
6963 processSync(mapper);
6964 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006965 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006967
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006969 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006970 ASSERT_EQ(0, motionArgs.buttonState);
6971
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6973 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6974 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6975
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6977
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 // press BTN_STYLUS, release BTN_STYLUS
6979 processKey(mapper, BTN_STYLUS, 1);
6980 processSync(mapper);
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6982 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006983 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6984
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6986 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6987 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006988
6989 processKey(mapper, BTN_STYLUS, 0);
6990 processSync(mapper);
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006992 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006993 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006994
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006997 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998
6999 // press BTN_STYLUS2, release BTN_STYLUS2
7000 processKey(mapper, BTN_STYLUS2, 1);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7003 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007004 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7005
7006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7007 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7008 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009
7010 processKey(mapper, BTN_STYLUS2, 0);
7011 processSync(mapper);
7012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007013 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007014 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007015
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007018 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019
7020 // release touch
7021 processUp(mapper);
7022 processSync(mapper);
7023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7024 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7025 ASSERT_EQ(0, motionArgs.buttonState);
7026}
7027
7028TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007030 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031 prepareButtons();
7032 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007033 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007034
7035 NotifyMotionArgs motionArgs;
7036
7037 // default tool type is finger
7038 processDown(mapper, 100, 200);
7039 processSync(mapper);
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7041 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007042 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043
7044 // eraser
7045 processKey(mapper, BTN_TOOL_RUBBER, 1);
7046 processSync(mapper);
7047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7048 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007049 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050
7051 // stylus
7052 processKey(mapper, BTN_TOOL_RUBBER, 0);
7053 processKey(mapper, BTN_TOOL_PEN, 1);
7054 processSync(mapper);
7055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7056 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007057 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007058
7059 // brush
7060 processKey(mapper, BTN_TOOL_PEN, 0);
7061 processKey(mapper, BTN_TOOL_BRUSH, 1);
7062 processSync(mapper);
7063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007065 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007066
7067 // pencil
7068 processKey(mapper, BTN_TOOL_BRUSH, 0);
7069 processKey(mapper, BTN_TOOL_PENCIL, 1);
7070 processSync(mapper);
7071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007073 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007075 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076 processKey(mapper, BTN_TOOL_PENCIL, 0);
7077 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7078 processSync(mapper);
7079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7080 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007081 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082
7083 // mouse
7084 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7085 processKey(mapper, BTN_TOOL_MOUSE, 1);
7086 processSync(mapper);
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7088 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007089 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
7091 // lens
7092 processKey(mapper, BTN_TOOL_MOUSE, 0);
7093 processKey(mapper, BTN_TOOL_LENS, 1);
7094 processSync(mapper);
7095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007097 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098
7099 // double-tap
7100 processKey(mapper, BTN_TOOL_LENS, 0);
7101 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7102 processSync(mapper);
7103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7104 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007106
7107 // triple-tap
7108 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7109 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7110 processSync(mapper);
7111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7112 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007113 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007114
7115 // quad-tap
7116 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7117 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7118 processSync(mapper);
7119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7120 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007122
7123 // finger
7124 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7125 processKey(mapper, BTN_TOOL_FINGER, 1);
7126 processSync(mapper);
7127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007129 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130
7131 // stylus trumps finger
7132 processKey(mapper, BTN_TOOL_PEN, 1);
7133 processSync(mapper);
7134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7135 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007136 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137
7138 // eraser trumps stylus
7139 processKey(mapper, BTN_TOOL_RUBBER, 1);
7140 processSync(mapper);
7141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7142 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007143 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144
7145 // mouse trumps eraser
7146 processKey(mapper, BTN_TOOL_MOUSE, 1);
7147 processSync(mapper);
7148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7149 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007150 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151
7152 // back to default tool type
7153 processKey(mapper, BTN_TOOL_MOUSE, 0);
7154 processKey(mapper, BTN_TOOL_RUBBER, 0);
7155 processKey(mapper, BTN_TOOL_PEN, 0);
7156 processKey(mapper, BTN_TOOL_FINGER, 0);
7157 processSync(mapper);
7158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7159 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007160 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007161}
7162
7163TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007164 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007165 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166 prepareButtons();
7167 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007168 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00007169 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170
7171 NotifyMotionArgs motionArgs;
7172
7173 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7174 processKey(mapper, BTN_TOOL_FINGER, 1);
7175 processMove(mapper, 100, 200);
7176 processSync(mapper);
7177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7178 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7181
7182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7183 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7184 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7185 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7186
7187 // move a little
7188 processMove(mapper, 150, 250);
7189 processSync(mapper);
7190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7191 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7192 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7193 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7194
7195 // down when BTN_TOUCH is pressed, pressure defaults to 1
7196 processKey(mapper, BTN_TOUCH, 1);
7197 processSync(mapper);
7198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7199 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7201 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7202
7203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7204 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7205 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7206 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7207
7208 // up when BTN_TOUCH is released, hover restored
7209 processKey(mapper, BTN_TOUCH, 0);
7210 processSync(mapper);
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7214 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7218 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7219 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7220
7221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7222 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7223 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7224 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7225
7226 // exit hover when pointer goes away
7227 processKey(mapper, BTN_TOOL_FINGER, 0);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7232 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7233}
7234
7235TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007237 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 prepareButtons();
7239 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007240 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241
7242 NotifyMotionArgs motionArgs;
7243
7244 // initially hovering because pressure is 0
7245 processDown(mapper, 100, 200);
7246 processPressure(mapper, 0);
7247 processSync(mapper);
7248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7249 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7250 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7251 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7252
7253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7254 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7255 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7256 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7257
7258 // move a little
7259 processMove(mapper, 150, 250);
7260 processSync(mapper);
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7263 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7264 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7265
7266 // down when pressure is non-zero
7267 processPressure(mapper, RAW_PRESSURE_MAX);
7268 processSync(mapper);
7269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7270 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7271 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7272 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7273
7274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7275 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7277 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7278
7279 // up when pressure becomes 0, hover restored
7280 processPressure(mapper, 0);
7281 processSync(mapper);
7282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7283 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7284 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7285 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7286
7287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7288 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7289 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7290 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7295 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7296
7297 // exit hover when pointer goes away
7298 processUp(mapper);
7299 processSync(mapper);
7300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7303 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7304}
7305
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007306TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7307 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007308 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007309 prepareButtons();
7310 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007311 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007312
7313 // Touch down.
7314 processDown(mapper, 100, 200);
7315 processPressure(mapper, 1);
7316 processSync(mapper);
7317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7318 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7319
7320 // Reset the mapper. This should cancel the ongoing gesture.
7321 resetMapper(mapper, ARBITRARY_TIME);
7322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7323 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7324
7325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7326}
7327
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007328TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7329 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007330 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007331 prepareButtons();
7332 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007333 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007334
7335 // Set the initial state for the touch pointer.
7336 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7337 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7338 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7339 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7340
7341 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007342 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7343 // does not generate any events.
7344 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007345
7346 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7347 // the recreated touch state to generate a down event.
7348 processSync(mapper);
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7350 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7351
7352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7353}
7354
lilinnan687e58f2022-07-19 16:00:50 +08007355TEST_F(SingleTouchInputMapperTest,
7356 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7357 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007358 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08007359 prepareButtons();
7360 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007361 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08007362 NotifyMotionArgs motionArgs;
7363
7364 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007365 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007366 processSync(mapper);
7367
7368 // We should receive a down event
7369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7370 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7371
7372 // Change display id
7373 clearViewports();
7374 prepareSecondaryDisplay(ViewportType::INTERNAL);
7375
7376 // We should receive a cancel event
7377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7378 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7379 // Then receive reset called
7380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7381}
7382
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007383TEST_F(SingleTouchInputMapperTest,
7384 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7385 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007386 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007387 prepareButtons();
7388 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007389 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7391 NotifyMotionArgs motionArgs;
7392
7393 // Start a new gesture.
7394 processDown(mapper, 100, 200);
7395 processSync(mapper);
7396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7397 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7398
7399 // Make the viewport inactive. This will put the device in disabled mode.
7400 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7401 viewport->isActive = false;
7402 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007403 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007404
7405 // We should receive a cancel event for the ongoing gesture.
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7407 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7408 // Then we should be notified that the device was reset.
7409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7410
7411 // No events are generated while the viewport is inactive.
7412 processMove(mapper, 101, 201);
7413 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007414 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007415 processSync(mapper);
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007418 // Start a new gesture while the viewport is still inactive.
7419 processDown(mapper, 300, 400);
7420 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7421 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7422 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7423 processSync(mapper);
7424
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007425 // Make the viewport active again. The device should resume processing events.
7426 viewport->isActive = true;
7427 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007428 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007429
7430 // The device is reset because it changes back to direct mode, without generating any events.
7431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7433
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007434 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007435 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7437 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007438
7439 // No more events.
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7442}
7443
Prabir Pradhan211ba622022-10-31 21:09:21 +00007444TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7445 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007446 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00007447 prepareButtons();
7448 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007449 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00007450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7451
7452 // Press a stylus button.
7453 processKey(mapper, BTN_STYLUS, 1);
7454 processSync(mapper);
7455
7456 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7457 processDown(mapper, 100, 200);
7458 processSync(mapper);
7459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7460 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7461 WithCoords(toDisplayX(100), toDisplayY(200)),
7462 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7464 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7465 WithCoords(toDisplayX(100), toDisplayY(200)),
7466 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7467
7468 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7469 // the button has not actually been released, since there will be no pointers through which the
7470 // button state can be reported. The event is generated at the location of the pointer before
7471 // it went up.
7472 processUp(mapper);
7473 processSync(mapper);
7474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7475 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7476 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7478 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7479 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7480}
7481
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007482TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
7483 addConfigurationProperty("touch.deviceType", "touchScreen");
7484 prepareDisplay(ui::ROTATION_0);
7485 prepareButtons();
7486 prepareAxes(POSITION);
7487
7488 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
7489
Arpit Singha8c236b2023-04-25 13:56:05 +00007490 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7492
7493 // Press a stylus button.
7494 processKey(mapper, BTN_STYLUS, 1);
7495 processSync(mapper);
7496
7497 // Start a touch gesture and ensure that the stylus button is not reported.
7498 processDown(mapper, 100, 200);
7499 processSync(mapper);
7500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7501 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
7502
7503 // Release and press the stylus button again.
7504 processKey(mapper, BTN_STYLUS, 0);
7505 processSync(mapper);
7506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7507 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7508 processKey(mapper, BTN_STYLUS, 1);
7509 processSync(mapper);
7510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7511 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7512
7513 // Release the touch gesture.
7514 processUp(mapper);
7515 processSync(mapper);
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7518
7519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7520}
7521
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007522TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
7523 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7524 prepareDisplay(ui::ROTATION_0);
7525 prepareButtons();
7526 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007527 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7529
7530 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
7531}
7532
Seunghwan Choi356026c2023-02-01 14:37:25 +09007533TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
7534 std::shared_ptr<FakePointerController> fakePointerController =
7535 std::make_shared<FakePointerController>();
7536 addConfigurationProperty("touch.deviceType", "touchScreen");
7537 prepareDisplay(ui::ROTATION_0);
7538 prepareButtons();
7539 prepareAxes(POSITION);
7540 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7541 mFakePolicy->setPointerController(fakePointerController);
7542 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007543 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007544
7545 processKey(mapper, BTN_TOOL_PEN, 1);
7546 processMove(mapper, 100, 200);
7547 processSync(mapper);
7548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7549 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007551 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7552 ASSERT_TRUE(fakePointerController->isPointerShown());
7553 ASSERT_NO_FATAL_FAILURE(
7554 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7555}
7556
7557TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7558 std::shared_ptr<FakePointerController> fakePointerController =
7559 std::make_shared<FakePointerController>();
7560 addConfigurationProperty("touch.deviceType", "touchScreen");
7561 prepareDisplay(ui::ROTATION_0);
7562 prepareButtons();
7563 prepareAxes(POSITION);
7564 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7565 mFakePolicy->setPointerController(fakePointerController);
7566 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007567 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007568
7569 processKey(mapper, BTN_TOOL_PEN, 1);
7570 processMove(mapper, 100, 200);
7571 processSync(mapper);
7572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007574 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007575 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7576 ASSERT_FALSE(fakePointerController->isPointerShown());
7577}
7578
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007579TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7580 // Initialize the device without setting device source to touch navigation.
7581 addConfigurationProperty("touch.deviceType", "touchScreen");
7582 prepareDisplay(ui::ROTATION_0);
7583 prepareButtons();
7584 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007585 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007586
7587 // Ensure that the device is created as a touchscreen, not touch navigation.
7588 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7589
7590 // Add device type association after the device was created.
7591 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7592
7593 // Send update to the mapper.
7594 std::list<NotifyArgs> unused2 =
7595 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007596 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007597
7598 // Check whether device type update was successful.
7599 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7600}
7601
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007602TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7603 // Initialize the device without setting device source to touch navigation.
7604 addConfigurationProperty("touch.deviceType", "touchScreen");
7605 prepareDisplay(ui::ROTATION_0);
7606 prepareButtons();
7607 prepareAxes(POSITION);
7608 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7609
7610 // Set a physical frame in the display viewport.
7611 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7612 viewport->physicalLeft = 0;
7613 viewport->physicalTop = 0;
7614 viewport->physicalRight = DISPLAY_WIDTH / 2;
7615 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7616 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007617 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007618
Arpit Singha8c236b2023-04-25 13:56:05 +00007619 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007620
7621 // Hovering inside the physical frame produces events.
7622 processKey(mapper, BTN_TOOL_PEN, 1);
7623 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7624 processSync(mapper);
7625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7626 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7628 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7629
7630 // Leaving the physical frame ends the hovering gesture.
7631 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7632 processSync(mapper);
7633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7634 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7635
7636 // Moving outside the physical frame does not produce events.
7637 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7638 processSync(mapper);
7639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7640
7641 // Re-entering the physical frame produces events.
7642 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7643 processSync(mapper);
7644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7645 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7647 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7648}
7649
Prabir Pradhan5632d622021-09-06 07:57:20 -07007650// --- TouchDisplayProjectionTest ---
7651
7652class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7653public:
7654 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7655 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7656 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007657 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7658 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7659 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007660 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007661 auto rotatedWidth = naturalDisplayWidth;
7662 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007663 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007664 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007665 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007666 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007667 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007668 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007669 inverseRotationFlags = ui::Transform::ROT_180;
7670 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007671 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007672 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007673 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007674 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007675 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007676 inverseRotationFlags = ui::Transform::ROT_0;
7677 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007678 }
7679
Prabir Pradhana9df3162022-12-05 23:57:27 +00007680 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007681 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7682
7683 std::optional<DisplayViewport> internalViewport =
7684 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7685 DisplayViewport& v = *internalViewport;
7686 v.displayId = DISPLAY_ID;
7687 v.orientation = orientation;
7688
7689 v.logicalLeft = 0;
7690 v.logicalTop = 0;
7691 v.logicalRight = 100;
7692 v.logicalBottom = 100;
7693
7694 v.physicalLeft = rotatedPhysicalDisplay.left;
7695 v.physicalTop = rotatedPhysicalDisplay.top;
7696 v.physicalRight = rotatedPhysicalDisplay.right;
7697 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7698
Prabir Pradhana9df3162022-12-05 23:57:27 +00007699 v.deviceWidth = rotatedWidth;
7700 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007701
7702 v.isActive = true;
7703 v.uniqueId = UNIQUE_ID;
7704 v.type = ViewportType::INTERNAL;
7705 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007706 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007707 }
7708
7709 void assertReceivedMove(const Point& point) {
7710 NotifyMotionArgs motionArgs;
7711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7712 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007713 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007714 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7715 1, 0, 0, 0, 0, 0, 0, 0));
7716 }
7717};
7718
7719TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7720 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007721 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007722
7723 prepareButtons();
7724 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007725 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007726
7727 NotifyMotionArgs motionArgs;
7728
7729 // Configure the DisplayViewport such that the logical display maps to a subsection of
7730 // the display panel called the physical display. Here, the physical display is bounded by the
7731 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7732 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7733 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7734 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7735
Michael Wrighta9cf4192022-12-01 23:46:39 +00007736 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007737 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7738
7739 // Touches outside the physical display should be ignored, and should not generate any
7740 // events. Ensure touches at the following points that lie outside of the physical display
7741 // area do not generate any events.
7742 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7743 processDown(mapper, toRawX(point.x), toRawY(point.y));
7744 processSync(mapper);
7745 processUp(mapper);
7746 processSync(mapper);
7747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7748 << "Unexpected event generated for touch outside physical display at point: "
7749 << point.x << ", " << point.y;
7750 }
7751 }
7752}
7753
7754TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7755 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007756 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007757
7758 prepareButtons();
7759 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007760 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007761
7762 NotifyMotionArgs motionArgs;
7763
7764 // Configure the DisplayViewport such that the logical display maps to a subsection of
7765 // the display panel called the physical display. Here, the physical display is bounded by the
7766 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7767 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7768
Michael Wrighta9cf4192022-12-01 23:46:39 +00007769 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007770 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7771
7772 // Touches that start outside the physical display should be ignored until it enters the
7773 // physical display bounds, at which point it should generate a down event. Start a touch at
7774 // the point (5, 100), which is outside the physical display bounds.
7775 static const Point kOutsidePoint{5, 100};
7776 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7777 processSync(mapper);
7778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7779
7780 // Move the touch into the physical display area. This should generate a pointer down.
7781 processMove(mapper, toRawX(11), toRawY(21));
7782 processSync(mapper);
7783 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7784 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007785 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007786 ASSERT_NO_FATAL_FAILURE(
7787 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7788
7789 // Move the touch inside the physical display area. This should generate a pointer move.
7790 processMove(mapper, toRawX(69), toRawY(159));
7791 processSync(mapper);
7792 assertReceivedMove({69, 159});
7793
7794 // Move outside the physical display area. Since the pointer is already down, this should
7795 // now continue generating events.
7796 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7797 processSync(mapper);
7798 assertReceivedMove(kOutsidePoint);
7799
7800 // Release. This should generate a pointer up.
7801 processUp(mapper);
7802 processSync(mapper);
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7804 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7806 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7807
7808 // Ensure no more events were generated.
7809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7811 }
7812}
7813
Prabir Pradhana9df3162022-12-05 23:57:27 +00007814// --- TouchscreenPrecisionTests ---
7815
7816// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7817// in various orientations and with different display rotations. We configure the touchscreen to
7818// have a higher resolution than that of the display by an integer scale factor in each axis so that
7819// we can enforce that coordinates match precisely as expected.
7820class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7821 public ::testing::WithParamInterface<ui::Rotation> {
7822public:
7823 void SetUp() override {
7824 SingleTouchInputMapperTest::SetUp();
7825
7826 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7827 // four times the resolution of the display in the Y axis.
7828 prepareButtons();
7829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007830 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7831 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007832 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007833 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7834 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007835 }
7836
7837 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7838 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7839 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7840 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7841
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007842 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7843 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7844
7845 static const int32_t PRECISION_RAW_X_FLAT = 16;
7846 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7847
7848 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7849 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7850
Prabir Pradhana9df3162022-12-05 23:57:27 +00007851 static const std::array<Point, 4> kRawCorners;
7852};
7853
7854const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7855 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7856 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7857 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7858 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7859}};
7860
7861// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7862// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7863// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7864TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7865 enum class Orientation {
7866 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7867 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7868 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7869 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7870 ftl_last = ORIENTATION_270,
7871 };
7872 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7873 Orientation::ORIENTATION_270;
7874 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7875 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7876 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7877 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7878 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7879 };
7880
7881 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7882
7883 // Configure the touchscreen as being installed in the one of the four different orientations
7884 // relative to the display.
7885 addConfigurationProperty("touch.deviceType", "touchScreen");
7886 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7887 prepareDisplay(ui::ROTATION_0);
7888
Arpit Singha8c236b2023-04-25 13:56:05 +00007889 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007890
7891 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7892 // orientations of either 90 or 270) this means the display's natural resolution will be
7893 // flipped.
7894 const bool displayRotated =
7895 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7896 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7897 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7898 const Rect physicalFrame{0, 0, width, height};
7899 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7900
7901 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7902 const float expectedPrecisionX = displayRotated ? 4 : 2;
7903 const float expectedPrecisionY = displayRotated ? 2 : 4;
7904
7905 // Test all four corners.
7906 for (int i = 0; i < 4; i++) {
7907 const auto& raw = kRawCorners[i];
7908 processDown(mapper, raw.x, raw.y);
7909 processSync(mapper);
7910 const auto& expected = expectedPoints[i];
7911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7912 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7913 WithCoords(expected.x, expected.y),
7914 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7915 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7916 << "with touchscreen orientation "
7917 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7918 << expected.x << ", " << expected.y << ").";
7919 processUp(mapper);
7920 processSync(mapper);
7921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7922 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7923 WithCoords(expected.x, expected.y))));
7924 }
7925}
7926
Prabir Pradhan82687402022-12-06 01:32:53 +00007927TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7928 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7929 kMappedCorners = {
7930 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7931 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7932 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7933 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7934 };
7935
7936 const ui::Rotation displayRotation = GetParam();
7937
7938 addConfigurationProperty("touch.deviceType", "touchScreen");
7939 prepareDisplay(displayRotation);
7940
Arpit Singha8c236b2023-04-25 13:56:05 +00007941 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007942
7943 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7944
7945 // Test all four corners.
7946 for (int i = 0; i < 4; i++) {
7947 const auto& expected = expectedPoints[i];
7948 const auto& raw = kRawCorners[i];
7949 processDown(mapper, raw.x, raw.y);
7950 processSync(mapper);
7951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7952 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7953 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7954 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7955 << "with display rotation " << ui::toCString(displayRotation)
7956 << ", expected point (" << expected.x << ", " << expected.y << ").";
7957 processUp(mapper);
7958 processSync(mapper);
7959 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7960 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7961 WithCoords(expected.x, expected.y))));
7962 }
7963}
7964
Prabir Pradhan3e798762022-12-02 21:02:11 +00007965TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7966 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7967 kMappedCorners = {
7968 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7969 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7970 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7971 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7972 };
7973
7974 const ui::Rotation displayRotation = GetParam();
7975
7976 addConfigurationProperty("touch.deviceType", "touchScreen");
7977 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7978
Arpit Singha8c236b2023-04-25 13:56:05 +00007979 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007980
7981 // Ori 270, so width and height swapped
7982 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7983 prepareDisplay(displayRotation);
7984 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7985
7986 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7987
7988 // Test all four corners.
7989 for (int i = 0; i < 4; i++) {
7990 const auto& expected = expectedPoints[i];
7991 const auto& raw = kRawCorners[i];
7992 processDown(mapper, raw.x, raw.y);
7993 processSync(mapper);
7994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7995 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7996 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7997 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7998 << "with display rotation " << ui::toCString(displayRotation)
7999 << ", expected point (" << expected.x << ", " << expected.y << ").";
8000 processUp(mapper);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
8004 WithCoords(expected.x, expected.y))));
8005 }
8006}
8007
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008008TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
8009 const ui::Rotation displayRotation = GetParam();
8010
8011 addConfigurationProperty("touch.deviceType", "touchScreen");
8012 prepareDisplay(displayRotation);
8013
8014 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00008015 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008016
8017 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
8018 // MotionRanges use display pixels as their units
8019 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
8020 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
8021
8022 // The MotionRanges should be oriented in the rotated display's coordinate space
8023 const bool displayRotated =
8024 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
8025
8026 constexpr float MAX_X = 479.5;
8027 constexpr float MAX_Y = 799.75;
8028 EXPECT_EQ(xRange->min, 0.f);
8029 EXPECT_EQ(yRange->min, 0.f);
8030 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
8031 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
8032
8033 EXPECT_EQ(xRange->flat, 8.f);
8034 EXPECT_EQ(yRange->flat, 8.f);
8035
8036 EXPECT_EQ(xRange->fuzz, 2.f);
8037 EXPECT_EQ(yRange->fuzz, 2.f);
8038
8039 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
8040 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
8041}
8042
Prabir Pradhana9df3162022-12-05 23:57:27 +00008043// Run the precision tests for all rotations.
8044INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
8045 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
8046 ui::ROTATION_270),
8047 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
8048 return ftl::enum_string(testParamInfo.param);
8049 });
8050
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008051// --- ExternalStylusFusionTest ---
8052
8053class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
8054public:
8055 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
8056 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008057 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008058 prepareButtons();
8059 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008060 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008061
8062 mStylusState.when = ARBITRARY_TIME;
8063 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008064 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008065 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008066 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008067 processExternalStylusState(mapper);
8068 return mapper;
8069 }
8070
8071 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
8072 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
8073 for (const NotifyArgs& args : generatedArgs) {
8074 mFakeListener->notify(args);
8075 }
8076 // Loop the reader to flush the input listener queue.
8077 mReader->loopOnce();
8078 return generatedArgs;
8079 }
8080
8081protected:
8082 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008083
8084 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
8085 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008086 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008087
8088 // The first pointer is withheld.
8089 processDown(mapper, 100, 200);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8092 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8093 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8094
8095 // The external stylus reports pressure. The withheld finger pointer is released as a
8096 // stylus.
8097 mStylusState.pressure = 1.f;
8098 processExternalStylusState(mapper);
8099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8100 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8101 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8102
8103 // Subsequent pointer events are not withheld.
8104 processMove(mapper, 101, 201);
8105 processSync(mapper);
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8107 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8108
8109 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8111 }
8112
8113 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
8114 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8115
8116 // Releasing the touch pointer ends the gesture.
8117 processUp(mapper);
8118 processSync(mapper);
8119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008121 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008122
8123 mStylusState.pressure = 0.f;
8124 processExternalStylusState(mapper);
8125 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8127 }
8128
8129 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008130 // When stylus fusion is not successful, events should be reported with the original source.
8131 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008132 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008133 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008134
8135 // The first pointer is withheld when an external stylus is connected,
8136 // and a timeout is requested.
8137 processDown(mapper, 100, 200);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8140 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8141 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8142
8143 // If the timeout expires early, it is requested again.
8144 handleTimeout(mapper, ARBITRARY_TIME + 1);
8145 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8146 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8147
8148 // When the timeout expires, the withheld touch is released as a finger pointer.
8149 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8151 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8152
8153 // Subsequent pointer events are not withheld.
8154 processMove(mapper, 101, 201);
8155 processSync(mapper);
8156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8157 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8158 processUp(mapper);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8161 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8162
8163 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8165 }
8166
8167private:
8168 InputDeviceInfo mExternalStylusDeviceInfo{};
8169};
8170
8171TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
8172 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008173 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008174}
8175
8176TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
8177 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8178 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8179}
8180
8181TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
8182 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8183 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8184}
8185
8186// Test a successful stylus fusion gesture where the pressure is reported by the external
8187// before the touch is reported by the touchscreen.
8188TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
8189 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008190 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008191
8192 // The external stylus reports pressure first. It is ignored for now.
8193 mStylusState.pressure = 1.f;
8194 processExternalStylusState(mapper);
8195 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8196 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8197
8198 // When the touch goes down afterwards, it is reported as a stylus pointer.
8199 processDown(mapper, 100, 200);
8200 processSync(mapper);
8201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8202 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8203 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8204
8205 processMove(mapper, 101, 201);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8208 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8209 processUp(mapper);
8210 processSync(mapper);
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8212 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8213
8214 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8216}
8217
8218TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
8219 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8220
8221 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8222 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8223
8224 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8225 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8226 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8227 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8228}
8229
8230TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
8231 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008232 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008233
8234 mStylusState.pressure = 0.8f;
8235 processExternalStylusState(mapper);
8236 processDown(mapper, 100, 200);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8239 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8240 WithPressure(0.8f))));
8241 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8242
8243 // The external stylus reports a pressure change. We wait for some time for a touch event.
8244 mStylusState.pressure = 0.6f;
8245 processExternalStylusState(mapper);
8246 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8247 ASSERT_NO_FATAL_FAILURE(
8248 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8249
8250 // If a touch is reported within the timeout, it reports the updated pressure.
8251 processMove(mapper, 101, 201);
8252 processSync(mapper);
8253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8254 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8255 WithPressure(0.6f))));
8256 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8257
8258 // There is another pressure change.
8259 mStylusState.pressure = 0.5f;
8260 processExternalStylusState(mapper);
8261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8262 ASSERT_NO_FATAL_FAILURE(
8263 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8264
8265 // If a touch is not reported within the timeout, a move event is generated to report
8266 // the new pressure.
8267 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8269 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8270 WithPressure(0.5f))));
8271
8272 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8273 // repeated indefinitely.
8274 mStylusState.pressure = 0.0f;
8275 processExternalStylusState(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8277 ASSERT_NO_FATAL_FAILURE(
8278 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8279 processMove(mapper, 102, 202);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8282 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8283 WithPressure(0.5f))));
8284 processMove(mapper, 103, 203);
8285 processSync(mapper);
8286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8287 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8288 WithPressure(0.5f))));
8289
8290 processUp(mapper);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008294 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008295
8296 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8298}
8299
8300TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8301 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008302 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008303
8304 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008305 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008306 processExternalStylusState(mapper);
8307 processDown(mapper, 100, 200);
8308 processSync(mapper);
8309 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8310 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008311 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008312 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8313
8314 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008315 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008316 processExternalStylusState(mapper);
8317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8318 ASSERT_NO_FATAL_FAILURE(
8319 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8320
8321 // If a touch is reported within the timeout, it reports the updated pressure.
8322 processMove(mapper, 101, 201);
8323 processSync(mapper);
8324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8325 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008326 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008327 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8328
8329 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008330 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008331 processExternalStylusState(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8333 ASSERT_NO_FATAL_FAILURE(
8334 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8335
8336 // If a touch is not reported within the timeout, a move event is generated to report
8337 // the new tool type.
8338 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8340 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008342
8343 processUp(mapper);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8346 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008347 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008348
8349 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8351}
8352
8353TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8354 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008355 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008356
8357 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8358
8359 // The external stylus reports a button change. We wait for some time for a touch event.
8360 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8361 processExternalStylusState(mapper);
8362 ASSERT_NO_FATAL_FAILURE(
8363 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8364
8365 // If a touch is reported within the timeout, it reports the updated button state.
8366 processMove(mapper, 101, 201);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8369 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8370 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8372 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8373 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8374 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8375
8376 // The button is now released.
8377 mStylusState.buttons = 0;
8378 processExternalStylusState(mapper);
8379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8380 ASSERT_NO_FATAL_FAILURE(
8381 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8382
8383 // If a touch is not reported within the timeout, a move event is generated to report
8384 // the new button state.
8385 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8387 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8388 WithButtonState(0))));
8389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008390 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8391 WithButtonState(0))));
8392
8393 processUp(mapper);
8394 processSync(mapper);
8395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008396 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8397
8398 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8400}
8401
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402// --- MultiTouchInputMapperTest ---
8403
8404class MultiTouchInputMapperTest : public TouchInputMapperTest {
8405protected:
8406 void prepareAxes(int axes);
8407
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008408 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8409 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8410 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8411 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8412 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8413 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8414 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8415 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8416 void processId(MultiTouchInputMapper& mapper, int32_t id);
8417 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8418 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8419 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008420 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008421 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008422 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
8423 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008424};
8425
8426void MultiTouchInputMapperTest::prepareAxes(int axes) {
8427 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008428 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008430 }
8431 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008432 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8433 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8436 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008437 }
8438 }
8439 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8441 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008444 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008445 }
8446 }
8447 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008448 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8449 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450 }
8451 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8453 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008454 }
8455 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008456 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8457 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458 }
8459 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008460 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8461 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 }
8463 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008464 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8465 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466 }
8467 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008468 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008469 }
8470}
8471
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008472void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8473 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008476}
8477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008478void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8479 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008481}
8482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008483void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8484 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486}
8487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008488void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008490}
8491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008492void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008493 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008494}
8495
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008496void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8497 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008498 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008499}
8500
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008501void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008502 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008503}
8504
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008505void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008506 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507}
8508
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008509void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008511}
8512
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008513void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008514 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515}
8516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008517void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008518 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008519}
8520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008521void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8522 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008523 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008524}
8525
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008526void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8527 int32_t value) {
8528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8530}
8531
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008532void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008533 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534}
8535
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008536void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8537 nsecs_t readTime) {
8538 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539}
8540
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008542 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008543 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 prepareAxes(POSITION);
8545 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008546 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547
arthurhungdcef2dc2020-08-11 14:47:50 +08008548 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549
8550 NotifyMotionArgs motionArgs;
8551
8552 // Two fingers down at once.
8553 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8554 processPosition(mapper, x1, y1);
8555 processMTSync(mapper);
8556 processPosition(mapper, x2, y2);
8557 processMTSync(mapper);
8558 processSync(mapper);
8559
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8561 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8562 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8563 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8564 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8565 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8566 ASSERT_EQ(0, motionArgs.flags);
8567 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8568 ASSERT_EQ(0, motionArgs.buttonState);
8569 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008570 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008572 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8575 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8576 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8577 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8578
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8580 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8581 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8582 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8583 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008584 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008585 ASSERT_EQ(0, motionArgs.flags);
8586 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8587 ASSERT_EQ(0, motionArgs.buttonState);
8588 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008589 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008590 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008591 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008593 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8595 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8597 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8598 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8599 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8601
8602 // Move.
8603 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8604 processPosition(mapper, x1, y1);
8605 processMTSync(mapper);
8606 processPosition(mapper, x2, y2);
8607 processMTSync(mapper);
8608 processSync(mapper);
8609
8610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8611 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8612 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8613 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8614 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8615 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8616 ASSERT_EQ(0, motionArgs.flags);
8617 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8618 ASSERT_EQ(0, motionArgs.buttonState);
8619 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008620 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008621 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008622 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008624 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8626 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8628 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8629 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8630 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8631 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8632
8633 // First finger up.
8634 x2 += 15; y2 -= 20;
8635 processPosition(mapper, x2, y2);
8636 processMTSync(mapper);
8637 processSync(mapper);
8638
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8640 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8641 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8642 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8643 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008644 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008645 ASSERT_EQ(0, motionArgs.flags);
8646 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8647 ASSERT_EQ(0, motionArgs.buttonState);
8648 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008649 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008650 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008651 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8657 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8658 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8659 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8660 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8661
8662 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8663 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8664 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8665 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8666 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8667 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8668 ASSERT_EQ(0, motionArgs.flags);
8669 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8670 ASSERT_EQ(0, motionArgs.buttonState);
8671 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008672 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8676 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8677 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8678 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8679 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8680
8681 // Move.
8682 x2 += 20; y2 -= 25;
8683 processPosition(mapper, x2, y2);
8684 processMTSync(mapper);
8685 processSync(mapper);
8686
8687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8688 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8689 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8690 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8691 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8693 ASSERT_EQ(0, motionArgs.flags);
8694 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8695 ASSERT_EQ(0, motionArgs.buttonState);
8696 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008697 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008698 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008699 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8701 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8702 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8703 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8704 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8705
8706 // New finger down.
8707 int32_t x3 = 700, y3 = 300;
8708 processPosition(mapper, x2, y2);
8709 processMTSync(mapper);
8710 processPosition(mapper, x3, y3);
8711 processMTSync(mapper);
8712 processSync(mapper);
8713
8714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8715 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8716 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8717 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8718 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008719 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008720 ASSERT_EQ(0, motionArgs.flags);
8721 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8722 ASSERT_EQ(0, motionArgs.buttonState);
8723 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008724 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008725 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008726 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008728 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008729 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8730 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8732 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8733 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8734 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8735 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8736
8737 // Second finger up.
8738 x3 += 30; y3 -= 20;
8739 processPosition(mapper, x3, y3);
8740 processMTSync(mapper);
8741 processSync(mapper);
8742
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008748 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008749 ASSERT_EQ(0, motionArgs.flags);
8750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8751 ASSERT_EQ(0, motionArgs.buttonState);
8752 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008753 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8759 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8761 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8762 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8763 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8764 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8765
8766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8769 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8770 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8772 ASSERT_EQ(0, motionArgs.flags);
8773 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8774 ASSERT_EQ(0, motionArgs.buttonState);
8775 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008776 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008777 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008778 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8780 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8781 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8782 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8783 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8784
8785 // Last finger up.
8786 processMTSync(mapper);
8787 processSync(mapper);
8788
8789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8790 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8791 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8792 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8793 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8794 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8795 ASSERT_EQ(0, motionArgs.flags);
8796 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8797 ASSERT_EQ(0, motionArgs.buttonState);
8798 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008799 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008800 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8803 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8804 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8805 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8806 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8807
8808 // Should not have sent any more keys or motions.
8809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8811}
8812
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008813TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8814 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008815 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008816
8817 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8818 /*fuzz*/ 0, /*resolution*/ 10);
8819 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8820 /*fuzz*/ 0, /*resolution*/ 11);
8821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8822 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8823 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8824 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8825 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8826 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8827 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8828 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8829
Arpit Singha8c236b2023-04-25 13:56:05 +00008830 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008831
8832 // X and Y axes
8833 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8834 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8835 // Touch major and minor
8836 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8837 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8838 // Tool major and minor
8839 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8840 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8841}
8842
8843TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8844 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008845 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008846
8847 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8848 /*fuzz*/ 0, /*resolution*/ 10);
8849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8850 /*fuzz*/ 0, /*resolution*/ 11);
8851
8852 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8853
Arpit Singha8c236b2023-04-25 13:56:05 +00008854 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008855
8856 // Touch major and minor
8857 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8858 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8859 // Tool major and minor
8860 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8861 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8862}
8863
Michael Wrightd02c5b62014-02-10 15:10:22 -08008864TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008865 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008866 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008867 prepareAxes(POSITION | ID);
8868 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008869 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008870
arthurhungdcef2dc2020-08-11 14:47:50 +08008871 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872
8873 NotifyMotionArgs motionArgs;
8874
8875 // Two fingers down at once.
8876 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8877 processPosition(mapper, x1, y1);
8878 processId(mapper, 1);
8879 processMTSync(mapper);
8880 processPosition(mapper, x2, y2);
8881 processId(mapper, 2);
8882 processMTSync(mapper);
8883 processSync(mapper);
8884
8885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008887 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008888 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008889 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8891 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8892
8893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008894 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008895 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008896 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008897 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8901 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8903 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8904
8905 // Move.
8906 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8907 processPosition(mapper, x1, y1);
8908 processId(mapper, 1);
8909 processMTSync(mapper);
8910 processPosition(mapper, x2, y2);
8911 processId(mapper, 2);
8912 processMTSync(mapper);
8913 processSync(mapper);
8914
8915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8916 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008917 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008918 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008919 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008921 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8923 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8925 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8926
8927 // First finger up.
8928 x2 += 15; y2 -= 20;
8929 processPosition(mapper, x2, y2);
8930 processId(mapper, 2);
8931 processMTSync(mapper);
8932 processSync(mapper);
8933
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008935 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008936 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008937 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008938 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008940 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8942 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8944 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8945
8946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8947 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008948 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008949 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8952 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8953
8954 // Move.
8955 x2 += 20; y2 -= 25;
8956 processPosition(mapper, x2, y2);
8957 processId(mapper, 2);
8958 processMTSync(mapper);
8959 processSync(mapper);
8960
8961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8962 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008963 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008964 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008965 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8967 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8968
8969 // New finger down.
8970 int32_t x3 = 700, y3 = 300;
8971 processPosition(mapper, x2, y2);
8972 processId(mapper, 2);
8973 processMTSync(mapper);
8974 processPosition(mapper, x3, y3);
8975 processId(mapper, 3);
8976 processMTSync(mapper);
8977 processSync(mapper);
8978
8979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008980 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008981 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008982 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008983 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008985 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008986 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8987 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8989 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8990
8991 // Second finger up.
8992 x3 += 30; y3 -= 20;
8993 processPosition(mapper, x3, y3);
8994 processId(mapper, 3);
8995 processMTSync(mapper);
8996 processSync(mapper);
8997
8998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008999 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009000 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009002 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009004 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9006 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9008 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9009
9010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9011 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009012 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009013 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009014 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9016 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9017
9018 // Last finger up.
9019 processMTSync(mapper);
9020 processSync(mapper);
9021
9022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9023 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009024 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009025 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009026 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9028 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9029
9030 // Should not have sent any more keys or motions.
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9033}
9034
9035TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009036 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009037 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038 prepareAxes(POSITION | ID | SLOT);
9039 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00009040 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009041
arthurhungdcef2dc2020-08-11 14:47:50 +08009042 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009043
9044 NotifyMotionArgs motionArgs;
9045
9046 // Two fingers down at once.
9047 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9048 processPosition(mapper, x1, y1);
9049 processId(mapper, 1);
9050 processSlot(mapper, 1);
9051 processPosition(mapper, x2, y2);
9052 processId(mapper, 2);
9053 processSync(mapper);
9054
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009057 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009058 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009059 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009060 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9061 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9062
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009064 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009065 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009066 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009067 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9071 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9073 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9074
9075 // Move.
9076 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
9077 processSlot(mapper, 0);
9078 processPosition(mapper, x1, y1);
9079 processSlot(mapper, 1);
9080 processPosition(mapper, x2, y2);
9081 processSync(mapper);
9082
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009085 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009087 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009088 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009090 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9091 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9093 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9094
9095 // First finger up.
9096 x2 += 15; y2 -= 20;
9097 processSlot(mapper, 0);
9098 processId(mapper, -1);
9099 processSlot(mapper, 1);
9100 processPosition(mapper, x2, y2);
9101 processSync(mapper);
9102
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009104 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009105 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009107 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009109 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009110 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9111 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9113 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9114
9115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9116 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009117 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009118 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009119 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009120 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9121 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9122
9123 // Move.
9124 x2 += 20; y2 -= 25;
9125 processPosition(mapper, x2, y2);
9126 processSync(mapper);
9127
9128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009130 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009131 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009132 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9134 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9135
9136 // New finger down.
9137 int32_t x3 = 700, y3 = 300;
9138 processPosition(mapper, x2, y2);
9139 processSlot(mapper, 0);
9140 processId(mapper, 3);
9141 processPosition(mapper, x3, y3);
9142 processSync(mapper);
9143
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009145 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009146 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009150 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9152 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9155
9156 // Second finger up.
9157 x3 += 30; y3 -= 20;
9158 processSlot(mapper, 1);
9159 processId(mapper, -1);
9160 processSlot(mapper, 0);
9161 processPosition(mapper, x3, y3);
9162 processSync(mapper);
9163
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009165 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009166 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009169 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009170 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9172 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9174 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9175
9176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009178 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9182 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9183
9184 // Last finger up.
9185 processId(mapper, -1);
9186 processSync(mapper);
9187
9188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9189 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009190 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009191 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009192 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9194 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9195
9196 // Should not have sent any more keys or motions.
9197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9199}
9200
9201TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009202 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009203 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009204 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009205 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009206
9207 // These calculations are based on the input device calibration documentation.
9208 int32_t rawX = 100;
9209 int32_t rawY = 200;
9210 int32_t rawTouchMajor = 7;
9211 int32_t rawTouchMinor = 6;
9212 int32_t rawToolMajor = 9;
9213 int32_t rawToolMinor = 8;
9214 int32_t rawPressure = 11;
9215 int32_t rawDistance = 0;
9216 int32_t rawOrientation = 3;
9217 int32_t id = 5;
9218
9219 float x = toDisplayX(rawX);
9220 float y = toDisplayY(rawY);
9221 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
9222 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9223 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9224 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9225 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9226 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9227 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
9228 float distance = float(rawDistance);
9229
9230 processPosition(mapper, rawX, rawY);
9231 processTouchMajor(mapper, rawTouchMajor);
9232 processTouchMinor(mapper, rawTouchMinor);
9233 processToolMajor(mapper, rawToolMajor);
9234 processToolMinor(mapper, rawToolMinor);
9235 processPressure(mapper, rawPressure);
9236 processOrientation(mapper, rawOrientation);
9237 processDistance(mapper, rawDistance);
9238 processId(mapper, id);
9239 processMTSync(mapper);
9240 processSync(mapper);
9241
9242 NotifyMotionArgs args;
9243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9244 ASSERT_EQ(0, args.pointerProperties[0].id);
9245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9246 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
9247 orientation, distance));
9248}
9249
9250TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009251 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009252 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009253 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9254 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00009255 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009256
9257 // These calculations are based on the input device calibration documentation.
9258 int32_t rawX = 100;
9259 int32_t rawY = 200;
9260 int32_t rawTouchMajor = 140;
9261 int32_t rawTouchMinor = 120;
9262 int32_t rawToolMajor = 180;
9263 int32_t rawToolMinor = 160;
9264
9265 float x = toDisplayX(rawX);
9266 float y = toDisplayY(rawY);
9267 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9268 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9269 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9270 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9271 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9272
9273 processPosition(mapper, rawX, rawY);
9274 processTouchMajor(mapper, rawTouchMajor);
9275 processTouchMinor(mapper, rawTouchMinor);
9276 processToolMajor(mapper, rawToolMajor);
9277 processToolMinor(mapper, rawToolMinor);
9278 processMTSync(mapper);
9279 processSync(mapper);
9280
9281 NotifyMotionArgs args;
9282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9283 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9284 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9285}
9286
9287TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009288 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009289 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009290 prepareAxes(POSITION | TOUCH | TOOL);
9291 addConfigurationProperty("touch.size.calibration", "diameter");
9292 addConfigurationProperty("touch.size.scale", "10");
9293 addConfigurationProperty("touch.size.bias", "160");
9294 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00009295 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009296
9297 // These calculations are based on the input device calibration documentation.
9298 // Note: We only provide a single common touch/tool value because the device is assumed
9299 // not to emit separate values for each pointer (isSummed = 1).
9300 int32_t rawX = 100;
9301 int32_t rawY = 200;
9302 int32_t rawX2 = 150;
9303 int32_t rawY2 = 250;
9304 int32_t rawTouchMajor = 5;
9305 int32_t rawToolMajor = 8;
9306
9307 float x = toDisplayX(rawX);
9308 float y = toDisplayY(rawY);
9309 float x2 = toDisplayX(rawX2);
9310 float y2 = toDisplayY(rawY2);
9311 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9312 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9313 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9314
9315 processPosition(mapper, rawX, rawY);
9316 processTouchMajor(mapper, rawTouchMajor);
9317 processToolMajor(mapper, rawToolMajor);
9318 processMTSync(mapper);
9319 processPosition(mapper, rawX2, rawY2);
9320 processTouchMajor(mapper, rawTouchMajor);
9321 processToolMajor(mapper, rawToolMajor);
9322 processMTSync(mapper);
9323 processSync(mapper);
9324
9325 NotifyMotionArgs args;
9326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9327 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9328
9329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009330 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009331 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009332 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9333 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9335 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9336}
9337
9338TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009339 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009340 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009341 prepareAxes(POSITION | TOUCH | TOOL);
9342 addConfigurationProperty("touch.size.calibration", "area");
9343 addConfigurationProperty("touch.size.scale", "43");
9344 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00009345 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009346
9347 // These calculations are based on the input device calibration documentation.
9348 int32_t rawX = 100;
9349 int32_t rawY = 200;
9350 int32_t rawTouchMajor = 5;
9351 int32_t rawToolMajor = 8;
9352
9353 float x = toDisplayX(rawX);
9354 float y = toDisplayY(rawY);
9355 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9356 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9357 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9358
9359 processPosition(mapper, rawX, rawY);
9360 processTouchMajor(mapper, rawTouchMajor);
9361 processToolMajor(mapper, rawToolMajor);
9362 processMTSync(mapper);
9363 processSync(mapper);
9364
9365 NotifyMotionArgs args;
9366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9367 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9368 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9369}
9370
9371TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009372 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009373 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009374 prepareAxes(POSITION | PRESSURE);
9375 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9376 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00009377 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009378
Michael Wrightaa449c92017-12-13 21:21:43 +00009379 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00009380 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009381 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9382 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9383 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9384
Michael Wrightd02c5b62014-02-10 15:10:22 -08009385 // These calculations are based on the input device calibration documentation.
9386 int32_t rawX = 100;
9387 int32_t rawY = 200;
9388 int32_t rawPressure = 60;
9389
9390 float x = toDisplayX(rawX);
9391 float y = toDisplayY(rawY);
9392 float pressure = float(rawPressure) * 0.01f;
9393
9394 processPosition(mapper, rawX, rawY);
9395 processPressure(mapper, rawPressure);
9396 processMTSync(mapper);
9397 processSync(mapper);
9398
9399 NotifyMotionArgs args;
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9401 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9402 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9403}
9404
9405TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009407 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009408 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009409 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009410
9411 NotifyMotionArgs motionArgs;
9412 NotifyKeyArgs keyArgs;
9413
9414 processId(mapper, 1);
9415 processPosition(mapper, 100, 200);
9416 processSync(mapper);
9417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9418 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9419 ASSERT_EQ(0, motionArgs.buttonState);
9420
9421 // press BTN_LEFT, release BTN_LEFT
9422 processKey(mapper, BTN_LEFT, 1);
9423 processSync(mapper);
9424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9426 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9427
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9429 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9430 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9431
Michael Wrightd02c5b62014-02-10 15:10:22 -08009432 processKey(mapper, BTN_LEFT, 0);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009436 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009437
9438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009440 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009441
9442 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9443 processKey(mapper, BTN_RIGHT, 1);
9444 processKey(mapper, BTN_MIDDLE, 1);
9445 processSync(mapper);
9446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9447 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9448 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9449 motionArgs.buttonState);
9450
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9452 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9453 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9454
9455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9456 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9457 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9458 motionArgs.buttonState);
9459
Michael Wrightd02c5b62014-02-10 15:10:22 -08009460 processKey(mapper, BTN_RIGHT, 0);
9461 processSync(mapper);
9462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009463 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009464 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009465
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009467 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009468 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009469
9470 processKey(mapper, BTN_MIDDLE, 0);
9471 processSync(mapper);
9472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009473 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009474 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009475
9476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009478 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009479
9480 // press BTN_BACK, release BTN_BACK
9481 processKey(mapper, BTN_BACK, 1);
9482 processSync(mapper);
9483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9484 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9485 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009486
Michael Wrightd02c5b62014-02-10 15:10:22 -08009487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009489 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9490
9491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9492 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9493 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009494
9495 processKey(mapper, BTN_BACK, 0);
9496 processSync(mapper);
9497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009498 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009499 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009500
9501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009503 ASSERT_EQ(0, motionArgs.buttonState);
9504
Michael Wrightd02c5b62014-02-10 15:10:22 -08009505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9506 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9507 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9508
9509 // press BTN_SIDE, release BTN_SIDE
9510 processKey(mapper, BTN_SIDE, 1);
9511 processSync(mapper);
9512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9513 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9514 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009515
Michael Wrightd02c5b62014-02-10 15:10:22 -08009516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009518 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9519
9520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9521 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9522 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009523
9524 processKey(mapper, BTN_SIDE, 0);
9525 processSync(mapper);
9526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009527 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009528 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009529
9530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009531 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009532 ASSERT_EQ(0, motionArgs.buttonState);
9533
Michael Wrightd02c5b62014-02-10 15:10:22 -08009534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9535 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9536 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9537
9538 // press BTN_FORWARD, release BTN_FORWARD
9539 processKey(mapper, BTN_FORWARD, 1);
9540 processSync(mapper);
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9542 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9543 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009544
Michael Wrightd02c5b62014-02-10 15:10:22 -08009545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009547 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9548
9549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9550 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9551 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009552
9553 processKey(mapper, BTN_FORWARD, 0);
9554 processSync(mapper);
9555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009556 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009557 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009558
9559 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009560 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009561 ASSERT_EQ(0, motionArgs.buttonState);
9562
Michael Wrightd02c5b62014-02-10 15:10:22 -08009563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9564 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9565 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9566
9567 // press BTN_EXTRA, release BTN_EXTRA
9568 processKey(mapper, BTN_EXTRA, 1);
9569 processSync(mapper);
9570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9571 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9572 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009573
Michael Wrightd02c5b62014-02-10 15:10:22 -08009574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009575 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009576 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9577
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9580 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009581
9582 processKey(mapper, BTN_EXTRA, 0);
9583 processSync(mapper);
9584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009585 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009586 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009587
9588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009589 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009590 ASSERT_EQ(0, motionArgs.buttonState);
9591
Michael Wrightd02c5b62014-02-10 15:10:22 -08009592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9593 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9594 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9595
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9597
Michael Wrightd02c5b62014-02-10 15:10:22 -08009598 // press BTN_STYLUS, release BTN_STYLUS
9599 processKey(mapper, BTN_STYLUS, 1);
9600 processSync(mapper);
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9602 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009603 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9604
9605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9606 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9607 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009608
9609 processKey(mapper, BTN_STYLUS, 0);
9610 processSync(mapper);
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009612 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009613 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009614
9615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009617 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009618
9619 // press BTN_STYLUS2, release BTN_STYLUS2
9620 processKey(mapper, BTN_STYLUS2, 1);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9623 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009624 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9625
9626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9627 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9628 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009629
9630 processKey(mapper, BTN_STYLUS2, 0);
9631 processSync(mapper);
9632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009634 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009635
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009637 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009638 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009639
9640 // release touch
9641 processId(mapper, -1);
9642 processSync(mapper);
9643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9644 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9645 ASSERT_EQ(0, motionArgs.buttonState);
9646}
9647
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009648TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9649 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009650 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009651 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009652 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009653
9654 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9655 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9656
9657 // Touch down.
9658 processId(mapper, 1);
9659 processPosition(mapper, 100, 200);
9660 processSync(mapper);
9661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9662 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9663
9664 // Press and release button mapped to the primary stylus button.
9665 processKey(mapper, BTN_A, 1);
9666 processSync(mapper);
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9668 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9669 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9671 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9672 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9673
9674 processKey(mapper, BTN_A, 0);
9675 processSync(mapper);
9676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9677 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9680
9681 // Press and release the HID usage mapped to the secondary stylus button.
9682 processHidUsage(mapper, 0xabcd, 1);
9683 processSync(mapper);
9684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9685 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9686 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9688 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9689 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9690
9691 processHidUsage(mapper, 0xabcd, 0);
9692 processSync(mapper);
9693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9694 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9696 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9697
9698 // Release touch.
9699 processId(mapper, -1);
9700 processSync(mapper);
9701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9702 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9703}
9704
Michael Wrightd02c5b62014-02-10 15:10:22 -08009705TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009706 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009707 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009708 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009709 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009710
9711 NotifyMotionArgs motionArgs;
9712
9713 // default tool type is finger
9714 processId(mapper, 1);
9715 processPosition(mapper, 100, 200);
9716 processSync(mapper);
9717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9718 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009719 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009720
9721 // eraser
9722 processKey(mapper, BTN_TOOL_RUBBER, 1);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009726 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009727
9728 // stylus
9729 processKey(mapper, BTN_TOOL_RUBBER, 0);
9730 processKey(mapper, BTN_TOOL_PEN, 1);
9731 processSync(mapper);
9732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9733 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009734 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009735
9736 // brush
9737 processKey(mapper, BTN_TOOL_PEN, 0);
9738 processKey(mapper, BTN_TOOL_BRUSH, 1);
9739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9741 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009742 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009743
9744 // pencil
9745 processKey(mapper, BTN_TOOL_BRUSH, 0);
9746 processKey(mapper, BTN_TOOL_PENCIL, 1);
9747 processSync(mapper);
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9749 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009750 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009751
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009752 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009753 processKey(mapper, BTN_TOOL_PENCIL, 0);
9754 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9755 processSync(mapper);
9756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009758 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009759
9760 // mouse
9761 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9762 processKey(mapper, BTN_TOOL_MOUSE, 1);
9763 processSync(mapper);
9764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009766 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009767
9768 // lens
9769 processKey(mapper, BTN_TOOL_MOUSE, 0);
9770 processKey(mapper, BTN_TOOL_LENS, 1);
9771 processSync(mapper);
9772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009774 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009775
9776 // double-tap
9777 processKey(mapper, BTN_TOOL_LENS, 0);
9778 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9779 processSync(mapper);
9780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9781 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009782 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009783
9784 // triple-tap
9785 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9786 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9787 processSync(mapper);
9788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9789 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009790 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009791
9792 // quad-tap
9793 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9794 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9795 processSync(mapper);
9796 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9797 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009798 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009799
9800 // finger
9801 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9802 processKey(mapper, BTN_TOOL_FINGER, 1);
9803 processSync(mapper);
9804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9805 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009806 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009807
9808 // stylus trumps finger
9809 processKey(mapper, BTN_TOOL_PEN, 1);
9810 processSync(mapper);
9811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9812 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009813 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009814
9815 // eraser trumps stylus
9816 processKey(mapper, BTN_TOOL_RUBBER, 1);
9817 processSync(mapper);
9818 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9819 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009820 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009821
9822 // mouse trumps eraser
9823 processKey(mapper, BTN_TOOL_MOUSE, 1);
9824 processSync(mapper);
9825 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9826 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009827 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009828
9829 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9830 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9831 processSync(mapper);
9832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9833 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009834 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009835
9836 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9837 processToolType(mapper, MT_TOOL_PEN);
9838 processSync(mapper);
9839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9840 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009841 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009842
9843 // back to default tool type
9844 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9845 processKey(mapper, BTN_TOOL_MOUSE, 0);
9846 processKey(mapper, BTN_TOOL_RUBBER, 0);
9847 processKey(mapper, BTN_TOOL_PEN, 0);
9848 processKey(mapper, BTN_TOOL_FINGER, 0);
9849 processSync(mapper);
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009853}
9854
9855TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009857 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009858 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009859 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009860 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009861
9862 NotifyMotionArgs motionArgs;
9863
9864 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9865 processId(mapper, 1);
9866 processPosition(mapper, 100, 200);
9867 processSync(mapper);
9868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9871 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9872
9873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9876 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9877
9878 // move a little
9879 processPosition(mapper, 150, 250);
9880 processSync(mapper);
9881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9882 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9884 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9885
9886 // down when BTN_TOUCH is pressed, pressure defaults to 1
9887 processKey(mapper, BTN_TOUCH, 1);
9888 processSync(mapper);
9889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9890 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9891 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9892 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9893
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9895 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9897 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9898
9899 // up when BTN_TOUCH is released, hover restored
9900 processKey(mapper, BTN_TOUCH, 0);
9901 processSync(mapper);
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9903 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9905 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9906
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9909 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9910 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9911
9912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9915 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9916
9917 // exit hover when pointer goes away
9918 processId(mapper, -1);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9923 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9924}
9925
9926TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009927 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009928 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009929 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009930 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009931
9932 NotifyMotionArgs motionArgs;
9933
9934 // initially hovering because pressure is 0
9935 processId(mapper, 1);
9936 processPosition(mapper, 100, 200);
9937 processPressure(mapper, 0);
9938 processSync(mapper);
9939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9942 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9943
9944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9945 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9947 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9948
9949 // move a little
9950 processPosition(mapper, 150, 250);
9951 processSync(mapper);
9952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9955 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9956
9957 // down when pressure becomes non-zero
9958 processPressure(mapper, RAW_PRESSURE_MAX);
9959 processSync(mapper);
9960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9961 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9962 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9963 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9964
9965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9966 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9967 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9968 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9969
9970 // up when pressure becomes 0, hover restored
9971 processPressure(mapper, 0);
9972 processSync(mapper);
9973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9974 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9975 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9976 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9977
9978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9979 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9980 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9981 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9982
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9984 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9986 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9987
9988 // exit hover when pointer goes away
9989 processId(mapper, -1);
9990 processSync(mapper);
9991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9992 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9994 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9995}
9996
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009997/**
9998 * Set the input device port <--> display port associations, and check that the
9999 * events are routed to the display that matches the display port.
10000 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
10001 */
10002TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010003 const std::string usb2 = "USB2";
10004 const uint8_t hdmi1 = 0;
10005 const uint8_t hdmi2 = 1;
10006 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010007 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010008
10009 addConfigurationProperty("touch.deviceType", "touchScreen");
10010 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010011 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010012
10013 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10014 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
10015
10016 // We are intentionally not adding the viewport for display 1 yet. Since the port association
10017 // for this input device is specified, and the matching viewport is not present,
10018 // the input device should be disabled (at the mapper level).
10019
10020 // Add viewport for display 2 on hdmi2
10021 prepareSecondaryDisplay(type, hdmi2);
10022 // Send a touch event
10023 processPosition(mapper, 100, 100);
10024 processSync(mapper);
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10026
10027 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +000010028 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010029 // Send a touch event again
10030 processPosition(mapper, 100, 100);
10031 processSync(mapper);
10032
10033 NotifyMotionArgs args;
10034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10035 ASSERT_EQ(DISPLAY_ID, args.displayId);
10036}
Michael Wrightd02c5b62014-02-10 15:10:22 -080010037
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010038TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
10039 addConfigurationProperty("touch.deviceType", "touchScreen");
10040 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010041 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010042
10043 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10044
Michael Wrighta9cf4192022-12-01 23:46:39 +000010045 prepareDisplay(ui::ROTATION_0);
10046 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010047
10048 // Send a touch event
10049 processPosition(mapper, 100, 100);
10050 processSync(mapper);
10051
10052 NotifyMotionArgs args;
10053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10054 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10055}
10056
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010057TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -080010058 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +010010059 std::shared_ptr<FakePointerController> fakePointerController =
10060 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -080010061 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010062 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010063 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010064
Garfield Tan888a6a42020-01-09 11:39:16 -080010065 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010066 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -080010067
Michael Wrighta9cf4192022-12-01 23:46:39 +000010068 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010069 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010070 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010071
Josep del Río2d8c79a2023-01-23 19:33:50 +000010072 // Check source is mouse that would obtain the PointerController.
10073 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010074
10075 NotifyMotionArgs motionArgs;
10076 processPosition(mapper, 100, 100);
10077 processSync(mapper);
10078
10079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10080 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10081 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10082}
10083
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010084/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010085 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
10086 */
10087TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
10088 addConfigurationProperty("touch.deviceType", "touchScreen");
10089 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010090 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010091
Michael Wrighta9cf4192022-12-01 23:46:39 +000010092 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +000010093 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
10094 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
10095 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
10096 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010097
10098 NotifyMotionArgs args;
10099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10100 ASSERT_EQ(26, args.readTime);
10101
Harry Cutts33476232023-01-30 19:57:29 +000010102 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
10103 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
10104 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010105
10106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10107 ASSERT_EQ(33, args.readTime);
10108}
10109
10110/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010111 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
10112 * events should not be delivered to the listener.
10113 */
10114TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
10115 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010116 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010117 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010118 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010119 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010120 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010121 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010122
10123 NotifyMotionArgs motionArgs;
10124 processPosition(mapper, 100, 100);
10125 processSync(mapper);
10126
10127 mFakeListener->assertNotifyMotionWasNotCalled();
10128}
10129
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010130/**
10131 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
10132 * the touch mapper can process the events and the events can be delivered to the listener.
10133 */
10134TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
10135 addConfigurationProperty("touch.deviceType", "touchScreen");
10136 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010137 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010138 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010139 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010140 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010141 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010142
10143 NotifyMotionArgs motionArgs;
10144 processPosition(mapper, 100, 100);
10145 processSync(mapper);
10146
10147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10148 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10149}
10150
Josh Thielene986aed2023-06-01 14:17:30 +000010151/**
10152 * When the viewport is deactivated (isActive transitions from true to false),
10153 * and touch.enableForInactiveViewport is false, touches prior to the transition
10154 * should be cancelled.
10155 */
Garfield Tanc734e4f2021-01-15 20:01:39 -080010156TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
10157 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010158 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010159 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010160 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010161 std::optional<DisplayViewport> optionalDisplayViewport =
10162 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10163 ASSERT_TRUE(optionalDisplayViewport.has_value());
10164 DisplayViewport displayViewport = *optionalDisplayViewport;
10165
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010166 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010167 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010168 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -080010169
10170 // Finger down
10171 int32_t x = 100, y = 100;
10172 processPosition(mapper, x, y);
10173 processSync(mapper);
10174
10175 NotifyMotionArgs motionArgs;
10176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10177 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10178
10179 // Deactivate display viewport
10180 displayViewport.isActive = false;
10181 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010182 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010183
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010184 // The ongoing touch should be canceled immediately
10185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10186 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10187
10188 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -080010189 x += 10, y += 10;
10190 processPosition(mapper, x, y);
10191 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -080010193
10194 // Reactivate display viewport
10195 displayViewport.isActive = true;
10196 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010197 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010198
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010199 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -080010200 x += 10, y += 10;
10201 processPosition(mapper, x, y);
10202 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10204 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010205}
10206
Josh Thielene986aed2023-06-01 14:17:30 +000010207/**
10208 * When the viewport is deactivated (isActive transitions from true to false),
10209 * and touch.enableForInactiveViewport is true, touches prior to the transition
10210 * should not be cancelled.
10211 */
10212TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
10213 addConfigurationProperty("touch.deviceType", "touchScreen");
10214 addConfigurationProperty("touch.enableForInactiveViewport", "1");
10215 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
10216 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
10217 std::optional<DisplayViewport> optionalDisplayViewport =
10218 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10219 ASSERT_TRUE(optionalDisplayViewport.has_value());
10220 DisplayViewport displayViewport = *optionalDisplayViewport;
10221
10222 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10223 prepareAxes(POSITION);
10224 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
10225
10226 // Finger down
10227 int32_t x = 100, y = 100;
10228 processPosition(mapper, x, y);
10229 processSync(mapper);
10230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10231 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10232
10233 // Deactivate display viewport
10234 displayViewport.isActive = false;
10235 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10236 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10237
10238 // The ongoing touch should not be canceled
10239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10240
10241 // Finger move is not ignored
10242 x += 10, y += 10;
10243 processPosition(mapper, x, y);
10244 processSync(mapper);
10245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10246 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10247
10248 // Reactivate display viewport
10249 displayViewport.isActive = true;
10250 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10251 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10252
10253 // Finger move continues and does not start new gesture
10254 x += 10, y += 10;
10255 processPosition(mapper, x, y);
10256 processSync(mapper);
10257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10258 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10259}
10260
Arthur Hung7c645402019-01-25 17:45:42 +080010261TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
10262 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +080010263 prepareAxes(POSITION | ID | SLOT);
10264 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010265 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +080010266
10267 // Create the second touch screen device, and enable multi fingers.
10268 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +080010269 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +080010270 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010271 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +080010272 std::shared_ptr<InputDevice> device2 =
10273 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010274 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +080010275
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010276 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010277 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010278 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010279 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010280 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010281 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010282 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010283 /*flat=*/0, /*fuzz=*/0);
10284 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010285 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
10286 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +080010287
10288 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +000010289 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
10290 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
10291 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010292 std::list<NotifyArgs> unused =
10293 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010294 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010295 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +080010296
10297 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +010010298 std::shared_ptr<FakePointerController> fakePointerController =
10299 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010300 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +080010301
10302 // Setup policy for associated displays and show touches.
10303 const uint8_t hdmi1 = 0;
10304 const uint8_t hdmi2 = 1;
10305 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10306 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10307 mFakePolicy->setShowTouches(true);
10308
10309 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010310 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010311 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010312
10313 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010314 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010315 InputReaderConfiguration::Change::DISPLAY_INFO |
10316 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010317
10318 // Two fingers down at default display.
10319 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10320 processPosition(mapper, x1, y1);
10321 processId(mapper, 1);
10322 processSlot(mapper, 1);
10323 processPosition(mapper, x2, y2);
10324 processId(mapper, 2);
10325 processSync(mapper);
10326
10327 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10328 fakePointerController->getSpots().find(DISPLAY_ID);
10329 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10330 ASSERT_EQ(size_t(2), iter->second.size());
10331
10332 // Two fingers down at second display.
10333 processPosition(mapper2, x1, y1);
10334 processId(mapper2, 1);
10335 processSlot(mapper2, 1);
10336 processPosition(mapper2, x2, y2);
10337 processId(mapper2, 2);
10338 processSync(mapper2);
10339
10340 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10341 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10342 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010343
10344 // Disable the show touches configuration and ensure the spots are cleared.
10345 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010346 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010347 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010348
10349 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010350}
10351
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010352TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010353 prepareAxes(POSITION);
10354 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010355 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010356 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010357
10358 NotifyMotionArgs motionArgs;
10359 // Unrotated video frame
10360 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10361 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010362 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010363 processPosition(mapper, 100, 200);
10364 processSync(mapper);
10365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10366 ASSERT_EQ(frames, motionArgs.videoFrames);
10367
10368 // Subsequent touch events should not have any videoframes
10369 // This is implemented separately in FakeEventHub,
10370 // but that should match the behaviour of TouchVideoDevice.
10371 processPosition(mapper, 200, 200);
10372 processSync(mapper);
10373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10374 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10375}
10376
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010377TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010378 prepareAxes(POSITION);
10379 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010380 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010381 // Unrotated video frame
10382 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10383 NotifyMotionArgs motionArgs;
10384
10385 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010386 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010387 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10388 clearViewports();
10389 prepareDisplay(orientation);
10390 std::vector<TouchVideoFrame> frames{frame};
10391 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10392 processPosition(mapper, 100, 200);
10393 processSync(mapper);
10394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10395 ASSERT_EQ(frames, motionArgs.videoFrames);
10396 }
10397}
10398
10399TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10400 prepareAxes(POSITION);
10401 addConfigurationProperty("touch.deviceType", "touchScreen");
10402 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10403 // orientation-aware are affected by display rotation.
10404 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010405 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010406 // Unrotated video frame
10407 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10408 NotifyMotionArgs motionArgs;
10409
10410 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010411 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010412 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10413 clearViewports();
10414 prepareDisplay(orientation);
10415 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010416 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010417 processPosition(mapper, 100, 200);
10418 processSync(mapper);
10419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010420 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10421 // compared to the display. This is so that when the window transform (which contains the
10422 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10423 // window's coordinate space.
10424 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010425 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010426
10427 // Release finger.
10428 processSync(mapper);
10429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010430 }
10431}
10432
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010433TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010434 prepareAxes(POSITION);
10435 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010436 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010437 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10438 // so mix these.
10439 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10440 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10441 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10442 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10443 NotifyMotionArgs motionArgs;
10444
Michael Wrighta9cf4192022-12-01 23:46:39 +000010445 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010446 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010447 processPosition(mapper, 100, 200);
10448 processSync(mapper);
10449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010450 ASSERT_EQ(frames, motionArgs.videoFrames);
10451}
10452
10453TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10454 prepareAxes(POSITION);
10455 addConfigurationProperty("touch.deviceType", "touchScreen");
10456 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10457 // orientation-aware are affected by display rotation.
10458 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010459 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010460 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10461 // so mix these.
10462 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10463 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10464 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10465 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10466 NotifyMotionArgs motionArgs;
10467
Michael Wrighta9cf4192022-12-01 23:46:39 +000010468 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010469 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10470 processPosition(mapper, 100, 200);
10471 processSync(mapper);
10472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10473 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10474 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10475 // compared to the display. This is so that when the window transform (which contains the
10476 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10477 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010478 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010479 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010480 ASSERT_EQ(frames, motionArgs.videoFrames);
10481}
10482
Arthur Hung9da14732019-09-02 16:16:58 +080010483/**
10484 * If we had defined port associations, but the viewport is not ready, the touch device would be
10485 * expected to be disabled, and it should be enabled after the viewport has found.
10486 */
10487TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010488 constexpr uint8_t hdmi2 = 1;
10489 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010490 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010491
10492 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10493
10494 addConfigurationProperty("touch.deviceType", "touchScreen");
10495 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010496 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010497
10498 ASSERT_EQ(mDevice->isEnabled(), false);
10499
10500 // Add display on hdmi2, the device should be enabled and can receive touch event.
10501 prepareSecondaryDisplay(type, hdmi2);
10502 ASSERT_EQ(mDevice->isEnabled(), true);
10503
10504 // Send a touch event.
10505 processPosition(mapper, 100, 100);
10506 processSync(mapper);
10507
10508 NotifyMotionArgs args;
10509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10510 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10511}
10512
Arthur Hung421eb1c2020-01-16 00:09:42 +080010513TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010514 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010515 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010516 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010517 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010518
10519 NotifyMotionArgs motionArgs;
10520
10521 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10522 // finger down
10523 processId(mapper, 1);
10524 processPosition(mapper, x1, y1);
10525 processSync(mapper);
10526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10527 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010528 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010529
10530 // finger move
10531 processId(mapper, 1);
10532 processPosition(mapper, x2, y2);
10533 processSync(mapper);
10534 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10535 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010536 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010537
10538 // finger up.
10539 processId(mapper, -1);
10540 processSync(mapper);
10541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10542 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010543 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010544
10545 // new finger down
10546 processId(mapper, 1);
10547 processPosition(mapper, x3, y3);
10548 processSync(mapper);
10549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10550 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010551 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010552}
10553
10554/**
arthurhungcc7f9802020-04-30 17:55:40 +080010555 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10556 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010557 */
arthurhungcc7f9802020-04-30 17:55:40 +080010558TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010559 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010560 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010561 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010562 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010563
10564 NotifyMotionArgs motionArgs;
10565
10566 // default tool type is finger
10567 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010568 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010569 processPosition(mapper, x1, y1);
10570 processSync(mapper);
10571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010573 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010574
10575 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10576 processToolType(mapper, MT_TOOL_PALM);
10577 processSync(mapper);
10578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10579 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10580
10581 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010582 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010583 processPosition(mapper, x2, y2);
10584 processSync(mapper);
10585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10586
10587 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010588 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010589 processSync(mapper);
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10591
10592 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010593 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010594 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010595 processPosition(mapper, x3, y3);
10596 processSync(mapper);
10597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10598 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010599 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010600}
10601
arthurhungbf89a482020-04-17 17:37:55 +080010602/**
arthurhungcc7f9802020-04-30 17:55:40 +080010603 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10604 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010605 */
arthurhungcc7f9802020-04-30 17:55:40 +080010606TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010607 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010608 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010609 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010610 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010611
10612 NotifyMotionArgs motionArgs;
10613
10614 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010615 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10616 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010617 processPosition(mapper, x1, y1);
10618 processSync(mapper);
10619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10620 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010621 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010622
10623 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010624 processSlot(mapper, SECOND_SLOT);
10625 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010626 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010627 processSync(mapper);
10628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010629 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010630 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010631
10632 // If the tool type of the first finger changes to MT_TOOL_PALM,
10633 // we expect to receive ACTION_POINTER_UP with cancel flag.
10634 processSlot(mapper, FIRST_SLOT);
10635 processId(mapper, FIRST_TRACKING_ID);
10636 processToolType(mapper, MT_TOOL_PALM);
10637 processSync(mapper);
10638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010639 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010640 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10641
10642 // The following MOVE events of second finger should be processed.
10643 processSlot(mapper, SECOND_SLOT);
10644 processId(mapper, SECOND_TRACKING_ID);
10645 processPosition(mapper, x2 + 1, y2 + 1);
10646 processSync(mapper);
10647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010649 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010650
10651 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10652 // it. Second finger receive move.
10653 processSlot(mapper, FIRST_SLOT);
10654 processId(mapper, INVALID_TRACKING_ID);
10655 processSync(mapper);
10656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10657 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010658 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010659
10660 // Second finger keeps moving.
10661 processSlot(mapper, SECOND_SLOT);
10662 processId(mapper, SECOND_TRACKING_ID);
10663 processPosition(mapper, x2 + 2, y2 + 2);
10664 processSync(mapper);
10665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10666 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010667 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010668
10669 // Second finger up.
10670 processId(mapper, INVALID_TRACKING_ID);
10671 processSync(mapper);
10672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10673 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10674 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10675}
10676
10677/**
10678 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10679 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10680 */
10681TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10682 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010683 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010684 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010685 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010686
10687 NotifyMotionArgs motionArgs;
10688
10689 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10690 // First finger down.
10691 processId(mapper, FIRST_TRACKING_ID);
10692 processPosition(mapper, x1, y1);
10693 processSync(mapper);
10694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10695 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010696 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010697
10698 // Second finger down.
10699 processSlot(mapper, SECOND_SLOT);
10700 processId(mapper, SECOND_TRACKING_ID);
10701 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010702 processSync(mapper);
10703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010704 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010705 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010706
arthurhungcc7f9802020-04-30 17:55:40 +080010707 // If the tool type of the first finger changes to MT_TOOL_PALM,
10708 // we expect to receive ACTION_POINTER_UP with cancel flag.
10709 processSlot(mapper, FIRST_SLOT);
10710 processId(mapper, FIRST_TRACKING_ID);
10711 processToolType(mapper, MT_TOOL_PALM);
10712 processSync(mapper);
10713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010714 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010715 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10716
10717 // Second finger keeps moving.
10718 processSlot(mapper, SECOND_SLOT);
10719 processId(mapper, SECOND_TRACKING_ID);
10720 processPosition(mapper, x2 + 1, y2 + 1);
10721 processSync(mapper);
10722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10724
10725 // second finger becomes palm, receive cancel due to only 1 finger is active.
10726 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010727 processToolType(mapper, MT_TOOL_PALM);
10728 processSync(mapper);
10729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10730 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10731
arthurhungcc7f9802020-04-30 17:55:40 +080010732 // third finger down.
10733 processSlot(mapper, THIRD_SLOT);
10734 processId(mapper, THIRD_TRACKING_ID);
10735 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010736 processPosition(mapper, x3, y3);
10737 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10739 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010740 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010741 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010742
10743 // third finger move
10744 processId(mapper, THIRD_TRACKING_ID);
10745 processPosition(mapper, x3 + 1, y3 + 1);
10746 processSync(mapper);
10747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10749
10750 // first finger up, third finger receive move.
10751 processSlot(mapper, FIRST_SLOT);
10752 processId(mapper, INVALID_TRACKING_ID);
10753 processSync(mapper);
10754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010756 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010757
10758 // second finger up, third finger receive move.
10759 processSlot(mapper, SECOND_SLOT);
10760 processId(mapper, INVALID_TRACKING_ID);
10761 processSync(mapper);
10762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010764 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010765
10766 // third finger up.
10767 processSlot(mapper, THIRD_SLOT);
10768 processId(mapper, INVALID_TRACKING_ID);
10769 processSync(mapper);
10770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10771 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10772 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10773}
10774
10775/**
10776 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10777 * and the active finger could still be allowed to receive the events
10778 */
10779TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10780 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010781 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010782 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010783 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010784
10785 NotifyMotionArgs motionArgs;
10786
10787 // default tool type is finger
10788 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10789 processId(mapper, FIRST_TRACKING_ID);
10790 processPosition(mapper, x1, y1);
10791 processSync(mapper);
10792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010794 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010795
10796 // Second finger down.
10797 processSlot(mapper, SECOND_SLOT);
10798 processId(mapper, SECOND_TRACKING_ID);
10799 processPosition(mapper, x2, y2);
10800 processSync(mapper);
10801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010802 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010803 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010804
10805 // If the tool type of the second finger changes to MT_TOOL_PALM,
10806 // we expect to receive ACTION_POINTER_UP with cancel flag.
10807 processId(mapper, SECOND_TRACKING_ID);
10808 processToolType(mapper, MT_TOOL_PALM);
10809 processSync(mapper);
10810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010811 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010812 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10813
10814 // The following MOVE event should be processed.
10815 processSlot(mapper, FIRST_SLOT);
10816 processId(mapper, FIRST_TRACKING_ID);
10817 processPosition(mapper, x1 + 1, y1 + 1);
10818 processSync(mapper);
10819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10820 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010821 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010822
10823 // second finger up.
10824 processSlot(mapper, SECOND_SLOT);
10825 processId(mapper, INVALID_TRACKING_ID);
10826 processSync(mapper);
10827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10829
10830 // first finger keep moving
10831 processSlot(mapper, FIRST_SLOT);
10832 processId(mapper, FIRST_TRACKING_ID);
10833 processPosition(mapper, x1 + 2, y1 + 2);
10834 processSync(mapper);
10835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10837
10838 // first finger up.
10839 processId(mapper, INVALID_TRACKING_ID);
10840 processSync(mapper);
10841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10842 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10843 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010844}
10845
Arthur Hung9ad18942021-06-19 02:04:46 +000010846/**
10847 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10848 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10849 * cause slot be valid again.
10850 */
10851TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10852 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010853 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010854 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010855 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010856
10857 NotifyMotionArgs motionArgs;
10858
10859 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10860 // First finger down.
10861 processId(mapper, FIRST_TRACKING_ID);
10862 processPosition(mapper, x1, y1);
10863 processPressure(mapper, RAW_PRESSURE_MAX);
10864 processSync(mapper);
10865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010867 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010868
10869 // First finger move.
10870 processId(mapper, FIRST_TRACKING_ID);
10871 processPosition(mapper, x1 + 1, y1 + 1);
10872 processPressure(mapper, RAW_PRESSURE_MAX);
10873 processSync(mapper);
10874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10875 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010876 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010877
10878 // Second finger down.
10879 processSlot(mapper, SECOND_SLOT);
10880 processId(mapper, SECOND_TRACKING_ID);
10881 processPosition(mapper, x2, y2);
10882 processPressure(mapper, RAW_PRESSURE_MAX);
10883 processSync(mapper);
10884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010885 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010886 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010887
10888 // second finger up with some unexpected data.
10889 processSlot(mapper, SECOND_SLOT);
10890 processId(mapper, INVALID_TRACKING_ID);
10891 processPosition(mapper, x2, y2);
10892 processSync(mapper);
10893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010894 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010895 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010896
10897 // first finger up with some unexpected data.
10898 processSlot(mapper, FIRST_SLOT);
10899 processId(mapper, INVALID_TRACKING_ID);
10900 processPosition(mapper, x2, y2);
10901 processPressure(mapper, RAW_PRESSURE_MAX);
10902 processSync(mapper);
10903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10904 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010905 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010906}
10907
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010908TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10909 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010910 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010911 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010912 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010913
10914 // First finger down.
10915 processId(mapper, FIRST_TRACKING_ID);
10916 processPosition(mapper, 100, 200);
10917 processPressure(mapper, RAW_PRESSURE_MAX);
10918 processSync(mapper);
10919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10920 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10921
10922 // Second finger down.
10923 processSlot(mapper, SECOND_SLOT);
10924 processId(mapper, SECOND_TRACKING_ID);
10925 processPosition(mapper, 300, 400);
10926 processPressure(mapper, RAW_PRESSURE_MAX);
10927 processSync(mapper);
10928 ASSERT_NO_FATAL_FAILURE(
10929 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10930
10931 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010932 // preserved. Resetting should cancel the ongoing gesture.
10933 resetMapper(mapper, ARBITRARY_TIME);
10934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10935 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010936
10937 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10938 // the existing touch state to generate a down event.
10939 processPosition(mapper, 301, 302);
10940 processSync(mapper);
10941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10944 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10945
10946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10947}
10948
10949TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10950 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010951 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010952 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010953 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010954
10955 // First finger touches down and releases.
10956 processId(mapper, FIRST_TRACKING_ID);
10957 processPosition(mapper, 100, 200);
10958 processPressure(mapper, RAW_PRESSURE_MAX);
10959 processSync(mapper);
10960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10961 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10962 processId(mapper, INVALID_TRACKING_ID);
10963 processSync(mapper);
10964 ASSERT_NO_FATAL_FAILURE(
10965 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10966
10967 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10968 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010969 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10971
10972 // Send an empty sync frame. Since there are no pointers, no events are generated.
10973 processSync(mapper);
10974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10975}
10976
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010977TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010978 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010979 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010980 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010981 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010983
10984 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10985 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10986 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10987 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10988 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10989
10990 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010991 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010992 processId(mapper, FIRST_TRACKING_ID);
10993 processToolType(mapper, MT_TOOL_PEN);
10994 processPosition(mapper, 100, 200);
10995 processPressure(mapper, RAW_PRESSURE_MAX);
10996 processSync(mapper);
10997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10998 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10999 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011000 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011001
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011002 // Now that we know the device supports styluses, ensure that the device is re-configured with
11003 // the stylus source.
11004 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
11005 {
11006 const auto& devices = mReader->getInputDevices();
11007 auto deviceInfo =
11008 std::find_if(devices.begin(), devices.end(),
11009 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
11010 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
11011 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
11012 }
11013
11014 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
11015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
11016
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011017 processId(mapper, INVALID_TRACKING_ID);
11018 processSync(mapper);
11019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11020 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11021 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011022 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011023}
11024
Seunghwan Choi356026c2023-02-01 14:37:25 +090011025TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
11026 addConfigurationProperty("touch.deviceType", "touchScreen");
11027 prepareDisplay(ui::ROTATION_0);
11028 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11029 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11030 // indicate stylus presence dynamically.
11031 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11032 std::shared_ptr<FakePointerController> fakePointerController =
11033 std::make_shared<FakePointerController>();
11034 mFakePolicy->setPointerController(fakePointerController);
11035 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000011036 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011037
11038 processId(mapper, FIRST_TRACKING_ID);
11039 processPressure(mapper, RAW_PRESSURE_MIN);
11040 processPosition(mapper, 100, 200);
11041 processToolType(mapper, MT_TOOL_PEN);
11042 processSync(mapper);
11043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11044 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011045 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011046 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11047 ASSERT_TRUE(fakePointerController->isPointerShown());
11048 ASSERT_NO_FATAL_FAILURE(
11049 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
11050}
11051
11052TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
11053 addConfigurationProperty("touch.deviceType", "touchScreen");
11054 prepareDisplay(ui::ROTATION_0);
11055 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11056 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11057 // indicate stylus presence dynamically.
11058 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11059 std::shared_ptr<FakePointerController> fakePointerController =
11060 std::make_shared<FakePointerController>();
11061 mFakePolicy->setPointerController(fakePointerController);
11062 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011063 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011064
11065 processId(mapper, FIRST_TRACKING_ID);
11066 processPressure(mapper, RAW_PRESSURE_MIN);
11067 processPosition(mapper, 100, 200);
11068 processToolType(mapper, MT_TOOL_PEN);
11069 processSync(mapper);
11070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11071 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011072 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011073 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11074 ASSERT_FALSE(fakePointerController->isPointerShown());
11075}
11076
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011077// --- MultiTouchInputMapperTest_ExternalDevice ---
11078
11079class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
11080protected:
Chris Yea52ade12020-08-27 16:49:20 -070011081 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011082};
11083
11084/**
11085 * Expect fallback to internal viewport if device is external and external viewport is not present.
11086 */
11087TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
11088 prepareAxes(POSITION);
11089 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011090 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011091 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011092
11093 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
11094
11095 NotifyMotionArgs motionArgs;
11096
11097 // Expect the event to be sent to the internal viewport,
11098 // because an external viewport is not present.
11099 processPosition(mapper, 100, 100);
11100 processSync(mapper);
11101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11102 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
11103
11104 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010011105 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011106 processPosition(mapper, 100, 100);
11107 processSync(mapper);
11108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11109 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
11110}
Arthur Hung4197f6b2020-03-16 15:39:59 +080011111
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011112TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
11113 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
11114 std::shared_ptr<FakePointerController> fakePointerController =
11115 std::make_shared<FakePointerController>();
11116 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11117 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011118
11119 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011120 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011121 prepareAxes(POSITION | ID | SLOT);
11122 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11123 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
11124 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011125 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011126 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011127
11128 // captured touchpad should be a touchpad source
11129 NotifyDeviceResetArgs resetArgs;
11130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
11131 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11132
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011133 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070011134
11135 const InputDeviceInfo::MotionRange* relRangeX =
11136 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
11137 ASSERT_NE(relRangeX, nullptr);
11138 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
11139 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
11140 const InputDeviceInfo::MotionRange* relRangeY =
11141 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
11142 ASSERT_NE(relRangeY, nullptr);
11143 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
11144 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
11145
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011146 // run captured pointer tests - note that this is unscaled, so input listener events should be
11147 // identical to what the hardware sends (accounting for any
11148 // calibration).
11149 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070011150 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011151 processId(mapper, 1);
11152 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
11153 processKey(mapper, BTN_TOUCH, 1);
11154 processSync(mapper);
11155
11156 // expect coord[0] to contain initial location of touch 0
11157 NotifyMotionArgs args;
11158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11159 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011160 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011161 ASSERT_EQ(0, args.pointerProperties[0].id);
11162 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
11163 ASSERT_NO_FATAL_FAILURE(
11164 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11165
11166 // FINGER 1 DOWN
11167 processSlot(mapper, 1);
11168 processId(mapper, 2);
11169 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
11170 processSync(mapper);
11171
11172 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11173 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080011174 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011175 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011176 ASSERT_EQ(0, args.pointerProperties[0].id);
11177 ASSERT_EQ(1, args.pointerProperties[1].id);
11178 ASSERT_NO_FATAL_FAILURE(
11179 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11180 ASSERT_NO_FATAL_FAILURE(
11181 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
11182
11183 // FINGER 1 MOVE
11184 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
11185 processSync(mapper);
11186
11187 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11188 // from move
11189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11190 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11191 ASSERT_NO_FATAL_FAILURE(
11192 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11193 ASSERT_NO_FATAL_FAILURE(
11194 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11195
11196 // FINGER 0 MOVE
11197 processSlot(mapper, 0);
11198 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
11199 processSync(mapper);
11200
11201 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
11202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11203 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11204 ASSERT_NO_FATAL_FAILURE(
11205 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
11206 ASSERT_NO_FATAL_FAILURE(
11207 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11208
11209 // BUTTON DOWN
11210 processKey(mapper, BTN_LEFT, 1);
11211 processSync(mapper);
11212
11213 // touchinputmapper design sends a move before button press
11214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11215 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11218
11219 // BUTTON UP
11220 processKey(mapper, BTN_LEFT, 0);
11221 processSync(mapper);
11222
11223 // touchinputmapper design sends a move after button release
11224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11225 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11228
11229 // FINGER 0 UP
11230 processId(mapper, -1);
11231 processSync(mapper);
11232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11233 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
11234
11235 // FINGER 1 MOVE
11236 processSlot(mapper, 1);
11237 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
11238 processSync(mapper);
11239
11240 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
11241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011243 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011244 ASSERT_EQ(1, args.pointerProperties[0].id);
11245 ASSERT_NO_FATAL_FAILURE(
11246 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
11247
11248 // FINGER 1 UP
11249 processId(mapper, -1);
11250 processKey(mapper, BTN_TOUCH, 0);
11251 processSync(mapper);
11252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11253 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
11254
Josep del Río2d8c79a2023-01-23 19:33:50 +000011255 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011256 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011257 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000011259 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011260}
11261
11262TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
11263 std::shared_ptr<FakePointerController> fakePointerController =
11264 std::make_shared<FakePointerController>();
11265 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11266 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011267
11268 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011269 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011270 prepareAxes(POSITION | ID | SLOT);
11271 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11272 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011273 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011274 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011275 // run uncaptured pointer tests - pushes out generic events
11276 // FINGER 0 DOWN
11277 processId(mapper, 3);
11278 processPosition(mapper, 100, 100);
11279 processKey(mapper, BTN_TOUCH, 1);
11280 processSync(mapper);
11281
11282 // start at (100,100), cursor should be at (0,0) * scale
11283 NotifyMotionArgs args;
11284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11285 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11286 ASSERT_NO_FATAL_FAILURE(
11287 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
11288
11289 // FINGER 0 MOVE
11290 processPosition(mapper, 200, 200);
11291 processSync(mapper);
11292
11293 // compute scaling to help with touch position checking
11294 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11295 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11296 float scale =
11297 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11298
11299 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
11300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11301 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11302 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
11303 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080011304
11305 // BUTTON DOWN
11306 processKey(mapper, BTN_LEFT, 1);
11307 processSync(mapper);
11308
11309 // touchinputmapper design sends a move before button press
11310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11311 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
11312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11313 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11314
11315 // BUTTON UP
11316 processKey(mapper, BTN_LEFT, 0);
11317 processSync(mapper);
11318
11319 // touchinputmapper design sends a move after button release
11320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11321 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11323 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011324}
11325
11326TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
11327 std::shared_ptr<FakePointerController> fakePointerController =
11328 std::make_shared<FakePointerController>();
11329
Michael Wrighta9cf4192022-12-01 23:46:39 +000011330 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011331 prepareAxes(POSITION | ID | SLOT);
11332 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011333 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011334 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011335 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011336
Josep del Río2d8c79a2023-01-23 19:33:50 +000011337 // uncaptured touchpad should be a pointer device
11338 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011339
Josep del Río2d8c79a2023-01-23 19:33:50 +000011340 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011341 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011342 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011343 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11344}
11345
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011346// --- BluetoothMultiTouchInputMapperTest ---
11347
11348class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
11349protected:
11350 void SetUp() override {
11351 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
11352 }
11353};
11354
11355TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
11356 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011357 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011358 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000011359 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011360
11361 nsecs_t kernelEventTime = ARBITRARY_TIME;
11362 nsecs_t expectedEventTime = ARBITRARY_TIME;
11363 // Touch down.
11364 processId(mapper, FIRST_TRACKING_ID);
11365 processPosition(mapper, 100, 200);
11366 processPressure(mapper, RAW_PRESSURE_MAX);
11367 processSync(mapper, ARBITRARY_TIME);
11368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11369 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
11370
11371 // Process several events that come in quick succession, according to their timestamps.
11372 for (int i = 0; i < 3; i++) {
11373 constexpr static nsecs_t delta = ms2ns(1);
11374 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
11375 kernelEventTime += delta;
11376 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
11377
11378 processPosition(mapper, 101 + i, 201 + i);
11379 processSync(mapper, kernelEventTime);
11380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11382 WithEventTime(expectedEventTime))));
11383 }
11384
11385 // Release the touch.
11386 processId(mapper, INVALID_TRACKING_ID);
11387 processPressure(mapper, RAW_PRESSURE_MIN);
11388 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
11389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11390 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11391 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
11392}
11393
11394// --- MultiTouchPointerModeTest ---
11395
HQ Liue6983c72022-04-19 22:14:56 +000011396class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
11397protected:
11398 float mPointerMovementScale;
11399 float mPointerXZoomScale;
11400 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
11401 addConfigurationProperty("touch.deviceType", "pointer");
11402 std::shared_ptr<FakePointerController> fakePointerController =
11403 std::make_shared<FakePointerController>();
11404 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11405 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000011406 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000011407
11408 prepareAxes(POSITION);
11409 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
11410 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
11411 // needs to be disabled, and the pointer gesture needs to be enabled.
11412 mFakePolicy->setPointerCapture(false);
11413 mFakePolicy->setPointerGestureEnabled(true);
11414 mFakePolicy->setPointerController(fakePointerController);
11415
11416 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11417 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11418 mPointerMovementScale =
11419 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11420 mPointerXZoomScale =
11421 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
11422 }
11423
11424 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
11425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
11426 /*flat*/ 0,
11427 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
11428 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
11429 /*flat*/ 0,
11430 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
11431 }
11432};
11433
11434/**
11435 * Two fingers down on a pointer mode touch pad. The width
11436 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
11437 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
11438 * be greater than the both value to be freeform gesture, so that after two
11439 * fingers start to move downwards, the gesture should be swipe.
11440 */
11441TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
11442 // The min freeform gesture width is 25units/mm x 30mm = 750
11443 // which is greater than fraction of the diagnal length of the touchpad (349).
11444 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000011445 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011446 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011447 NotifyMotionArgs motionArgs;
11448
11449 // Two fingers down at once.
11450 // The two fingers are 450 units apart, expects the current gesture to be PRESS
11451 // Pointer's initial position is used the [0,0] coordinate.
11452 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11453
11454 processId(mapper, FIRST_TRACKING_ID);
11455 processPosition(mapper, x1, y1);
11456 processMTSync(mapper);
11457 processId(mapper, SECOND_TRACKING_ID);
11458 processPosition(mapper, x2, y2);
11459 processMTSync(mapper);
11460 processSync(mapper);
11461
11462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011463 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011464 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011465 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011466 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011467 ASSERT_NO_FATAL_FAILURE(
11468 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11469
11470 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11471 // that there should be 1 pointer.
11472 int32_t movingDistance = 200;
11473 y1 += movingDistance;
11474 y2 += movingDistance;
11475
11476 processId(mapper, FIRST_TRACKING_ID);
11477 processPosition(mapper, x1, y1);
11478 processMTSync(mapper);
11479 processId(mapper, SECOND_TRACKING_ID);
11480 processPosition(mapper, x2, y2);
11481 processMTSync(mapper);
11482 processSync(mapper);
11483
11484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011485 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011486 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011487 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011488 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11490 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11491 0, 0, 0, 0));
11492}
11493
11494/**
11495 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11496 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11497 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11498 * value to be freeform gesture, so that after two fingers start to move downwards,
11499 * the gesture should be swipe.
11500 */
11501TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11502 // The min freeform gesture width is 5units/mm x 30mm = 150
11503 // which is greater than fraction of the diagnal length of the touchpad (349).
11504 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000011505 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000011506 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011507 NotifyMotionArgs motionArgs;
11508
11509 // Two fingers down at once.
11510 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11511 // Pointer's initial position is used the [0,0] coordinate.
11512 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11513
11514 processId(mapper, FIRST_TRACKING_ID);
11515 processPosition(mapper, x1, y1);
11516 processMTSync(mapper);
11517 processId(mapper, SECOND_TRACKING_ID);
11518 processPosition(mapper, x2, y2);
11519 processMTSync(mapper);
11520 processSync(mapper);
11521
11522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011523 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011524 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011525 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011526 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011527 ASSERT_NO_FATAL_FAILURE(
11528 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11529
11530 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11531 // and there should be 1 pointer.
11532 int32_t movingDistance = 200;
11533 y1 += movingDistance;
11534 y2 += movingDistance;
11535
11536 processId(mapper, FIRST_TRACKING_ID);
11537 processPosition(mapper, x1, y1);
11538 processMTSync(mapper);
11539 processId(mapper, SECOND_TRACKING_ID);
11540 processPosition(mapper, x2, y2);
11541 processMTSync(mapper);
11542 processSync(mapper);
11543
11544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011545 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011546 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011547 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011548 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011549 // New coordinate is the scaled relative coordinate from the initial coordinate.
11550 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11551 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11552 0, 0, 0, 0));
11553}
11554
11555/**
11556 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11557 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11558 * freeform gestures after two fingers start to move downwards.
11559 */
11560TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011561 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011562 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011563
11564 NotifyMotionArgs motionArgs;
11565
11566 // Two fingers down at once. Wider than the max swipe width.
11567 // The gesture is expected to be PRESS, then transformed to FREEFORM
11568 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11569
11570 processId(mapper, FIRST_TRACKING_ID);
11571 processPosition(mapper, x1, y1);
11572 processMTSync(mapper);
11573 processId(mapper, SECOND_TRACKING_ID);
11574 processPosition(mapper, x2, y2);
11575 processMTSync(mapper);
11576 processSync(mapper);
11577
11578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011579 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011581 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011582 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011583 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11584 ASSERT_NO_FATAL_FAILURE(
11585 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11586
11587 int32_t movingDistance = 200;
11588
11589 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11590 // then two down events for two pointers.
11591 y1 += movingDistance;
11592 y2 += movingDistance;
11593
11594 processId(mapper, FIRST_TRACKING_ID);
11595 processPosition(mapper, x1, y1);
11596 processMTSync(mapper);
11597 processId(mapper, SECOND_TRACKING_ID);
11598 processPosition(mapper, x2, y2);
11599 processMTSync(mapper);
11600 processSync(mapper);
11601
11602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11603 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011604 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011605 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011608 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011609 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011611 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011612 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011613 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011614 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011615 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011616 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011617 // Two pointers' scaled relative coordinates from their initial centroid.
11618 // Initial y coordinates are 0 as y1 and y2 have the same value.
11619 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11620 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11621 // When pointers move, the new coordinates equal to the initial coordinates plus
11622 // scaled moving distance.
11623 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11624 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11625 0, 0, 0, 0));
11626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11627 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11628 0, 0, 0, 0));
11629
11630 // Move two fingers down again, expect one MOVE motion event.
11631 y1 += movingDistance;
11632 y2 += movingDistance;
11633
11634 processId(mapper, FIRST_TRACKING_ID);
11635 processPosition(mapper, x1, y1);
11636 processMTSync(mapper);
11637 processId(mapper, SECOND_TRACKING_ID);
11638 processPosition(mapper, x2, y2);
11639 processMTSync(mapper);
11640 processSync(mapper);
11641
11642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011643 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011645 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011646 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011647 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11648 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11649 0, 0, 0, 0, 0));
11650 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11651 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11652 0, 0, 0, 0, 0));
11653}
11654
Harry Cutts39b7ca22022-10-05 15:55:48 +000011655TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011656 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011657 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011658 NotifyMotionArgs motionArgs;
11659
11660 // Place two fingers down.
11661 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11662
11663 processId(mapper, FIRST_TRACKING_ID);
11664 processPosition(mapper, x1, y1);
11665 processMTSync(mapper);
11666 processId(mapper, SECOND_TRACKING_ID);
11667 processPosition(mapper, x2, y2);
11668 processMTSync(mapper);
11669 processSync(mapper);
11670
11671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011672 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011673 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11674 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11675 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11676 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11677
11678 // Move the two fingers down and to the left.
11679 int32_t movingDistance = 200;
11680 x1 -= movingDistance;
11681 y1 += movingDistance;
11682 x2 -= movingDistance;
11683 y2 += movingDistance;
11684
11685 processId(mapper, FIRST_TRACKING_ID);
11686 processPosition(mapper, x1, y1);
11687 processMTSync(mapper);
11688 processId(mapper, SECOND_TRACKING_ID);
11689 processPosition(mapper, x2, y2);
11690 processMTSync(mapper);
11691 processSync(mapper);
11692
11693 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011694 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011695 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11696 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11697 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11698 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11699}
11700
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011701TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011702 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011703 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011704 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11706
11707 // Start a stylus gesture.
11708 processKey(mapper, BTN_TOOL_PEN, 1);
11709 processId(mapper, FIRST_TRACKING_ID);
11710 processPosition(mapper, 100, 200);
11711 processSync(mapper);
11712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11713 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11714 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011715 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011716 // TODO(b/257078296): Pointer mode generates extra event.
11717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11718 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11719 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011720 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11722
11723 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11724 // gesture should be disabled.
11725 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11726 viewport->isActive = false;
11727 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011728 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11730 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11731 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011732 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011733 // TODO(b/257078296): Pointer mode generates extra event.
11734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11735 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11736 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011737 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11739}
11740
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011741// --- JoystickInputMapperTest ---
11742
11743class JoystickInputMapperTest : public InputMapperTest {
11744protected:
11745 static const int32_t RAW_X_MIN;
11746 static const int32_t RAW_X_MAX;
11747 static const int32_t RAW_Y_MIN;
11748 static const int32_t RAW_Y_MAX;
11749
11750 void SetUp() override {
11751 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11752 }
11753 void prepareAxes() {
11754 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11755 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11756 }
11757
11758 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11759 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11760 }
11761
11762 void processSync(JoystickInputMapper& mapper) {
11763 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11764 }
11765
Michael Wrighta9cf4192022-12-01 23:46:39 +000011766 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011767 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11768 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11769 NO_PORT, ViewportType::VIRTUAL);
11770 }
11771};
11772
11773const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11774const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11775const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11776const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11777
11778TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11779 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011780 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011781
11782 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11783
Michael Wrighta9cf4192022-12-01 23:46:39 +000011784 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011785
11786 // Send an axis event
11787 processAxis(mapper, ABS_X, 100);
11788 processSync(mapper);
11789
11790 NotifyMotionArgs args;
11791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11792 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11793
11794 // Send another axis event
11795 processAxis(mapper, ABS_Y, 100);
11796 processSync(mapper);
11797
11798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11799 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11800}
11801
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011802// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011803
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011804class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011805protected:
11806 static const char* DEVICE_NAME;
11807 static const char* DEVICE_LOCATION;
11808 static const int32_t DEVICE_ID;
11809 static const int32_t DEVICE_GENERATION;
11810 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011811 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011812 static const int32_t EVENTHUB_ID;
11813
11814 std::shared_ptr<FakeEventHub> mFakeEventHub;
11815 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011816 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011817 std::unique_ptr<InstrumentedInputReader> mReader;
11818 std::shared_ptr<InputDevice> mDevice;
11819
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011820 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011821 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011822 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011823 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011824 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011825 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011826 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11827 }
11828
11829 void SetUp() override { SetUp(DEVICE_CLASSES); }
11830
11831 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011832 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011833 mFakePolicy.clear();
11834 }
11835
Chris Yee2b1e5c2021-03-10 22:45:12 -080011836 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11837 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011838 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011839 InputDeviceIdentifier identifier;
11840 identifier.name = name;
11841 identifier.location = location;
11842 std::shared_ptr<InputDevice> device =
11843 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11844 identifier);
11845 mReader->pushNextDevice(device);
11846 mFakeEventHub->addDevice(eventHubId, name, classes);
11847 mReader->loopOnce();
11848 return device;
11849 }
11850
11851 template <class T, typename... Args>
11852 T& addControllerAndConfigure(Args... args) {
11853 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11854
11855 return controller;
11856 }
11857};
11858
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011859const char* PeripheralControllerTest::DEVICE_NAME = "device";
11860const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11861const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11862const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11863const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011864const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11865 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011866const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011867
11868// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011869class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011870protected:
11871 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011872 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011873 }
11874};
11875
11876TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011877 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011878
Harry Cuttsa5b71292022-11-28 12:56:17 +000011879 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11880 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11881 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011882}
11883
11884TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011885 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011886
Harry Cuttsa5b71292022-11-28 12:56:17 +000011887 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11888 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11889 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011890}
11891
11892// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011893class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011894protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011895 void SetUp() override {
11896 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11897 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011898};
11899
Chris Ye85758332021-05-16 23:05:17 -070011900TEST_F(LightControllerTest, MonoLight) {
11901 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011902 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011903 .maxBrightness = 255,
11904 .flags = InputLightClass::BRIGHTNESS,
11905 .path = ""};
11906 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011907
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011908 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011909 InputDeviceInfo info;
11910 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011911 std::vector<InputDeviceLightInfo> lights = info.getLights();
11912 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011913 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11914 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11915
11916 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11917 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11918}
11919
11920TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11921 RawLightInfo infoMono = {.id = 1,
11922 .name = "mono_keyboard_backlight",
11923 .maxBrightness = 255,
11924 .flags = InputLightClass::BRIGHTNESS |
11925 InputLightClass::KEYBOARD_BACKLIGHT,
11926 .path = ""};
11927 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11928
11929 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11930 InputDeviceInfo info;
11931 controller.populateDeviceInfo(&info);
11932 std::vector<InputDeviceLightInfo> lights = info.getLights();
11933 ASSERT_EQ(1U, lights.size());
11934 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11935 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011936
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011937 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11938 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011939}
11940
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011941TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11942 RawLightInfo infoMono = {.id = 1,
11943 .name = "mono_light",
11944 .maxBrightness = 255,
11945 .flags = InputLightClass::BRIGHTNESS,
11946 .path = ""};
11947 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11948 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11949 "0,100,200");
11950
11951 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11952 std::list<NotifyArgs> unused =
11953 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11954 /*changes=*/{});
11955
11956 InputDeviceInfo info;
11957 controller.populateDeviceInfo(&info);
11958 std::vector<InputDeviceLightInfo> lights = info.getLights();
11959 ASSERT_EQ(1U, lights.size());
11960 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11961}
11962
11963TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11964 RawLightInfo infoMono = {.id = 1,
11965 .name = "mono_keyboard_backlight",
11966 .maxBrightness = 255,
11967 .flags = InputLightClass::BRIGHTNESS |
11968 InputLightClass::KEYBOARD_BACKLIGHT,
11969 .path = ""};
11970 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11971
11972 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11973 std::list<NotifyArgs> unused =
11974 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11975 /*changes=*/{});
11976
11977 InputDeviceInfo info;
11978 controller.populateDeviceInfo(&info);
11979 std::vector<InputDeviceLightInfo> lights = info.getLights();
11980 ASSERT_EQ(1U, lights.size());
11981 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11982}
11983
11984TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11985 RawLightInfo infoMono = {.id = 1,
11986 .name = "mono_keyboard_backlight",
11987 .maxBrightness = 255,
11988 .flags = InputLightClass::BRIGHTNESS |
11989 InputLightClass::KEYBOARD_BACKLIGHT,
11990 .path = ""};
11991 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11992 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11993 "0,100,200");
11994
11995 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11996 std::list<NotifyArgs> unused =
11997 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11998 /*changes=*/{});
11999
12000 InputDeviceInfo info;
12001 controller.populateDeviceInfo(&info);
12002 std::vector<InputDeviceLightInfo> lights = info.getLights();
12003 ASSERT_EQ(1U, lights.size());
12004 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
12005 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
12006 ASSERT_EQ(BrightnessLevel(0), *it);
12007 std::advance(it, 1);
12008 ASSERT_EQ(BrightnessLevel(100), *it);
12009 std::advance(it, 1);
12010 ASSERT_EQ(BrightnessLevel(200), *it);
12011}
12012
12013TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
12014 RawLightInfo infoMono = {.id = 1,
12015 .name = "mono_keyboard_backlight",
12016 .maxBrightness = 255,
12017 .flags = InputLightClass::BRIGHTNESS |
12018 InputLightClass::KEYBOARD_BACKLIGHT,
12019 .path = ""};
12020 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12021 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
12022 "0,100,200,300,400,500");
12023
12024 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12025 std::list<NotifyArgs> unused =
12026 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12027 /*changes=*/{});
12028
12029 InputDeviceInfo info;
12030 controller.populateDeviceInfo(&info);
12031 std::vector<InputDeviceLightInfo> lights = info.getLights();
12032 ASSERT_EQ(1U, lights.size());
12033 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
12034}
12035
Chris Yee2b1e5c2021-03-10 22:45:12 -080012036TEST_F(LightControllerTest, RGBLight) {
12037 RawLightInfo infoRed = {.id = 1,
12038 .name = "red",
12039 .maxBrightness = 255,
12040 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12041 .path = ""};
12042 RawLightInfo infoGreen = {.id = 2,
12043 .name = "green",
12044 .maxBrightness = 255,
12045 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12046 .path = ""};
12047 RawLightInfo infoBlue = {.id = 3,
12048 .name = "blue",
12049 .maxBrightness = 255,
12050 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12051 .path = ""};
12052 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12053 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12054 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12055
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012056 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012057 InputDeviceInfo info;
12058 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012059 std::vector<InputDeviceLightInfo> lights = info.getLights();
12060 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012061 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12062 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12063 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12064
12065 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12066 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12067}
12068
12069TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
12070 RawLightInfo infoRed = {.id = 1,
12071 .name = "red_keyboard_backlight",
12072 .maxBrightness = 255,
12073 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
12074 InputLightClass::KEYBOARD_BACKLIGHT,
12075 .path = ""};
12076 RawLightInfo infoGreen = {.id = 2,
12077 .name = "green_keyboard_backlight",
12078 .maxBrightness = 255,
12079 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
12080 InputLightClass::KEYBOARD_BACKLIGHT,
12081 .path = ""};
12082 RawLightInfo infoBlue = {.id = 3,
12083 .name = "blue_keyboard_backlight",
12084 .maxBrightness = 255,
12085 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
12086 InputLightClass::KEYBOARD_BACKLIGHT,
12087 .path = ""};
12088 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12089 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12090 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12091
12092 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12093 InputDeviceInfo info;
12094 controller.populateDeviceInfo(&info);
12095 std::vector<InputDeviceLightInfo> lights = info.getLights();
12096 ASSERT_EQ(1U, lights.size());
12097 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12098 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12099 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12100
12101 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12102 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12103}
12104
12105TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
12106 RawLightInfo infoRed = {.id = 1,
12107 .name = "red",
12108 .maxBrightness = 255,
12109 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12110 .path = ""};
12111 RawLightInfo infoGreen = {.id = 2,
12112 .name = "green",
12113 .maxBrightness = 255,
12114 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12115 .path = ""};
12116 RawLightInfo infoBlue = {.id = 3,
12117 .name = "blue",
12118 .maxBrightness = 255,
12119 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12120 .path = ""};
12121 RawLightInfo infoGlobal = {.id = 3,
12122 .name = "global_keyboard_backlight",
12123 .maxBrightness = 255,
12124 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
12125 InputLightClass::KEYBOARD_BACKLIGHT,
12126 .path = ""};
12127 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12128 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12129 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12130 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
12131
12132 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12133 InputDeviceInfo info;
12134 controller.populateDeviceInfo(&info);
12135 std::vector<InputDeviceLightInfo> lights = info.getLights();
12136 ASSERT_EQ(1U, lights.size());
12137 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12138 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12139 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012140
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012141 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12142 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012143}
12144
12145TEST_F(LightControllerTest, MultiColorRGBLight) {
12146 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012147 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080012148 .maxBrightness = 255,
12149 .flags = InputLightClass::BRIGHTNESS |
12150 InputLightClass::MULTI_INTENSITY |
12151 InputLightClass::MULTI_INDEX,
12152 .path = ""};
12153
12154 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12155
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012156 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012157 InputDeviceInfo info;
12158 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012159 std::vector<InputDeviceLightInfo> lights = info.getLights();
12160 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012161 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12162 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12163 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12164
12165 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12166 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12167}
12168
12169TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
12170 RawLightInfo infoColor = {.id = 1,
12171 .name = "multi_color_keyboard_backlight",
12172 .maxBrightness = 255,
12173 .flags = InputLightClass::BRIGHTNESS |
12174 InputLightClass::MULTI_INTENSITY |
12175 InputLightClass::MULTI_INDEX |
12176 InputLightClass::KEYBOARD_BACKLIGHT,
12177 .path = ""};
12178
12179 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12180
12181 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12182 InputDeviceInfo info;
12183 controller.populateDeviceInfo(&info);
12184 std::vector<InputDeviceLightInfo> lights = info.getLights();
12185 ASSERT_EQ(1U, lights.size());
12186 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12187 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12188 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012189
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012190 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12191 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012192}
12193
12194TEST_F(LightControllerTest, PlayerIdLight) {
12195 RawLightInfo info1 = {.id = 1,
12196 .name = "player1",
12197 .maxBrightness = 255,
12198 .flags = InputLightClass::BRIGHTNESS,
12199 .path = ""};
12200 RawLightInfo info2 = {.id = 2,
12201 .name = "player2",
12202 .maxBrightness = 255,
12203 .flags = InputLightClass::BRIGHTNESS,
12204 .path = ""};
12205 RawLightInfo info3 = {.id = 3,
12206 .name = "player3",
12207 .maxBrightness = 255,
12208 .flags = InputLightClass::BRIGHTNESS,
12209 .path = ""};
12210 RawLightInfo info4 = {.id = 4,
12211 .name = "player4",
12212 .maxBrightness = 255,
12213 .flags = InputLightClass::BRIGHTNESS,
12214 .path = ""};
12215 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
12216 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
12217 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
12218 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
12219
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012220 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012221 InputDeviceInfo info;
12222 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012223 std::vector<InputDeviceLightInfo> lights = info.getLights();
12224 ASSERT_EQ(1U, lights.size());
12225 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012226 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12227 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012228
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012229 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12230 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
12231 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012232}
12233
Michael Wrightd02c5b62014-02-10 15:10:22 -080012234} // namespace android