blob: e8b779adf70385c48223766c9050d4d32e405cbf [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
Prabir Pradhancc7268a2023-11-16 18:54:13 +00004925// TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging
4926// logic can be removed.
Byoungho Jungda10dd32023-10-06 17:03:45 +09004927class CursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00004928protected:
4929 void SetUp() override {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004930 input_flags::enable_pointer_choreographer(true);
4931 CursorInputMapperTestBase::SetUp();
4932 }
4933};
4934
Prabir Pradhan19767602023-11-03 16:53:31 +00004935TEST_F(CursorInputMapperTestWithChoreographer, PopulateDeviceInfoReturnsRangeFromPolicy) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004936 addConfigurationProperty("cursor.mode", "pointer");
4937 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4938
4939 InputDeviceInfo info;
4940 mapper.populateDeviceInfo(info);
4941
4942 // Initially there may not be a valid motion range.
4943 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4944 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
4945 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, AINPUT_MOTION_RANGE_PRESSURE,
4946 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4947
4948 // When the viewport and the default pointer display ID is set, then there should be a valid
4949 // motion range.
4950 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4951 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
4952 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
4953 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
4954
4955 InputDeviceInfo info2;
4956 mapper.populateDeviceInfo(info2);
4957
4958 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE, 0,
4959 DISPLAY_WIDTH - 1, 0.0f, 0.0f));
4960 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE, 0,
4961 DISPLAY_HEIGHT - 1, 0.0f, 0.0f));
4962 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_PRESSURE,
4963 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4964}
4965
Prabir Pradhan19767602023-11-03 16:53:31 +00004966TEST_F(CursorInputMapperTestWithChoreographer, ProcessShouldHandleAllButtonsWithZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09004967 addConfigurationProperty("cursor.mode", "pointer");
4968 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
4969
4970 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4971 prepareDisplay(ui::ROTATION_0);
4972
4973 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4974 mFakePointerController->setPosition(100, 200);
4975
4976 NotifyMotionArgs motionArgs;
4977 NotifyKeyArgs keyArgs;
4978
4979 // press BTN_LEFT, release BTN_LEFT
4980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4983 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4984 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4985 ASSERT_NO_FATAL_FAILURE(
4986 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
4987
4988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4989 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4990 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
4991 ASSERT_NO_FATAL_FAILURE(
4992 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
4993
4994 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4997 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4998 ASSERT_EQ(0, motionArgs.buttonState);
4999 ASSERT_NO_FATAL_FAILURE(
5000 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5001
5002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5003 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5004 ASSERT_EQ(0, motionArgs.buttonState);
5005 ASSERT_NO_FATAL_FAILURE(
5006 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5007
5008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5010 ASSERT_EQ(0, motionArgs.buttonState);
5011 ASSERT_NO_FATAL_FAILURE(
5012 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5013
5014 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5015 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5017 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5019 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5020 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5021 motionArgs.buttonState);
5022 ASSERT_NO_FATAL_FAILURE(
5023 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5024
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5026 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5027 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5028 ASSERT_NO_FATAL_FAILURE(
5029 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5030
5031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5032 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5033 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5034 motionArgs.buttonState);
5035 ASSERT_NO_FATAL_FAILURE(
5036 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5037
5038 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5042 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5043 ASSERT_NO_FATAL_FAILURE(
5044 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5045
5046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5047 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5048 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5049 ASSERT_NO_FATAL_FAILURE(
5050 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5051
5052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5053 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5055 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5056 ASSERT_EQ(0, motionArgs.buttonState);
5057 ASSERT_NO_FATAL_FAILURE(
5058 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5061
5062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5063 ASSERT_EQ(0, motionArgs.buttonState);
5064 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5065 ASSERT_NO_FATAL_FAILURE(
5066 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5067
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5069 ASSERT_EQ(0, motionArgs.buttonState);
5070 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5071 ASSERT_NO_FATAL_FAILURE(
5072 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5073
5074 // press BTN_BACK, release BTN_BACK
5075 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5076 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5079 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5080
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5083 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5084 ASSERT_NO_FATAL_FAILURE(
5085 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5086
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5089 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5090 ASSERT_NO_FATAL_FAILURE(
5091 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5092
5093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5096 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5097 ASSERT_EQ(0, motionArgs.buttonState);
5098 ASSERT_NO_FATAL_FAILURE(
5099 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5100
5101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5102 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5103 ASSERT_EQ(0, motionArgs.buttonState);
5104
5105 ASSERT_NO_FATAL_FAILURE(
5106 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5108 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5109 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5110
5111 // press BTN_SIDE, release BTN_SIDE
5112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5113 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5115 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5116 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5117
5118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5119 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5120 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5121 ASSERT_NO_FATAL_FAILURE(
5122 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5123
5124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5125 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5126 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5127 ASSERT_NO_FATAL_FAILURE(
5128 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5129
5130 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5134 ASSERT_EQ(0, motionArgs.buttonState);
5135 ASSERT_NO_FATAL_FAILURE(
5136 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5137
5138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5139 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5140 ASSERT_EQ(0, motionArgs.buttonState);
5141 ASSERT_NO_FATAL_FAILURE(
5142 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5143
5144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5145 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5146 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5147
5148 // press BTN_FORWARD, release BTN_FORWARD
5149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5150 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5152 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5153 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5154
5155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5156 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5158 ASSERT_NO_FATAL_FAILURE(
5159 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5160
5161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5163 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5164 ASSERT_NO_FATAL_FAILURE(
5165 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5166
5167 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5168 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5170 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5171 ASSERT_EQ(0, motionArgs.buttonState);
5172 ASSERT_NO_FATAL_FAILURE(
5173 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5174
5175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5176 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5177 ASSERT_EQ(0, motionArgs.buttonState);
5178 ASSERT_NO_FATAL_FAILURE(
5179 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5180
5181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5182 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5183 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5184
5185 // press BTN_EXTRA, release BTN_EXTRA
5186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5189 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5190 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5191
5192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5193 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5194 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5195 ASSERT_NO_FATAL_FAILURE(
5196 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5197
5198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5199 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5200 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5201 ASSERT_NO_FATAL_FAILURE(
5202 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5203
5204 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5205 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5208 ASSERT_EQ(0, motionArgs.buttonState);
5209 ASSERT_NO_FATAL_FAILURE(
5210 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5211
5212 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5213 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5214 ASSERT_EQ(0, motionArgs.buttonState);
5215 ASSERT_NO_FATAL_FAILURE(
5216 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5217
5218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5219 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5220 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5221}
5222
Prabir Pradhan19767602023-11-03 16:53:31 +00005223TEST_F(CursorInputMapperTestWithChoreographer, ProcessWhenModeIsPointerShouldKeepZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005224 addConfigurationProperty("cursor.mode", "pointer");
5225 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5226
5227 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5228 prepareDisplay(ui::ROTATION_0);
5229
5230 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5231 mFakePointerController->setPosition(100, 200);
5232
5233 NotifyMotionArgs args;
5234
5235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5236 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5237 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5239 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5240 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5241 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
5242 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5243}
5244
5245TEST_F(CursorInputMapperTestWithChoreographer, PointerCaptureDisablesVelocityProcessing) {
5246 addConfigurationProperty("cursor.mode", "pointer");
Prabir Pradhan19767602023-11-03 16:53:31 +00005247 const VelocityControlParameters testParams(/*scale=*/5.f, /*lowThreshold=*/0.f,
5248 /*highThreshold=*/100.f, /*acceleration=*/10.f);
Byoungho Jungda10dd32023-10-06 17:03:45 +09005249 mFakePolicy->setVelocityControlParams(testParams);
5250 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5251
5252 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5253 prepareDisplay(ui::ROTATION_0);
5254
5255 NotifyDeviceResetArgs resetArgs;
5256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5257 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5258 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5259
5260 NotifyMotionArgs args;
5261
5262 // Move and verify scale is applied.
5263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5267 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5268 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5269 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5270 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5271 ASSERT_GT(relX, 10);
5272 ASSERT_GT(relY, 20);
5273
5274 // Enable Pointer Capture
5275 mFakePolicy->setPointerCapture(true);
5276 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
5277 NotifyPointerCaptureChangedArgs captureArgs;
5278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5279 ASSERT_TRUE(captureArgs.request.enable);
5280
5281 // Move and verify scale is not applied.
5282 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5286 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5287 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5288 const float relX2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5289 const float relY2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5290 ASSERT_EQ(10, relX2);
5291 ASSERT_EQ(20, relY2);
5292}
5293
Prabir Pradhan19767602023-11-03 16:53:31 +00005294TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdNoAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005295 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5296
5297 // Set up the default display.
5298 prepareDisplay(ui::ROTATION_90);
5299
5300 // Set up the secondary display as the display on which the pointer should be shown.
5301 // The InputDevice is not associated with any display.
5302 prepareSecondaryDisplay();
5303 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5304 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5305
5306 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5307 mFakePointerController->setPosition(100, 200);
5308
5309 // Ensure input events are generated without display ID and coords,
5310 // because they will be decided later by PointerChoreographer.
5311 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5312 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5313 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5315 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5316 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(ADISPLAY_ID_NONE),
5317 WithCoords(0.0f, 0.0f))));
5318}
5319
Prabir Pradhan19767602023-11-03 16:53:31 +00005320TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdWithAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005321 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5322
5323 // Set up the default display.
5324 prepareDisplay(ui::ROTATION_90);
5325
5326 // Set up the secondary display as the display on which the pointer should be shown,
5327 // and associate the InputDevice with the secondary display.
5328 prepareSecondaryDisplay();
5329 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5330 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5331 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5332
5333 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5334 mFakePointerController->setPosition(100, 200);
5335
5336 // Ensure input events are generated with associated display ID but not with coords,
5337 // because the coords will be decided later by PointerChoreographer.
5338 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5339 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5340 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5342 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5343 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5344 WithCoords(0.0f, 0.0f))));
5345}
5346
5347TEST_F(CursorInputMapperTestWithChoreographer,
Prabir Pradhan19767602023-11-03 16:53:31 +00005348 ConfigureDisplayIdShouldGenerateEventWithMismatchedPointerDisplay) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005349 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5350
5351 // Set up the default display as the display on which the pointer should be shown.
5352 prepareDisplay(ui::ROTATION_90);
5353 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5354
5355 // Associate the InputDevice with the secondary display.
5356 prepareSecondaryDisplay();
5357 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5358 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5359
5360 // With PointerChoreographer enabled, there could be a PointerController for the associated
5361 // display even if it is different from the pointer display. So the mapper should generate an
5362 // event.
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5364 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5365 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5366 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5367 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5368 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5369 WithCoords(0.0f, 0.0f))));
5370}
5371
5372// --- BluetoothCursorInputMapperTest ---
5373
5374class BluetoothCursorInputMapperTest : public CursorInputMapperTestBase {
5375protected:
5376 void SetUp() override {
5377 input_flags::enable_pointer_choreographer(false);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005378 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5379
5380 mFakePointerController = std::make_shared<FakePointerController>();
5381 mFakePolicy->setPointerController(mFakePointerController);
5382 }
5383};
5384
5385TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5386 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005387 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005388
5389 nsecs_t kernelEventTime = ARBITRARY_TIME;
5390 nsecs_t expectedEventTime = ARBITRARY_TIME;
5391 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5392 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5395 WithEventTime(expectedEventTime))));
5396
5397 // Process several events that come in quick succession, according to their timestamps.
5398 for (int i = 0; i < 3; i++) {
5399 constexpr static nsecs_t delta = ms2ns(1);
5400 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5401 kernelEventTime += delta;
5402 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5403
5404 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5405 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5407 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5408 WithEventTime(expectedEventTime))));
5409 }
5410}
5411
5412TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5413 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005414 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005415
5416 nsecs_t expectedEventTime = ARBITRARY_TIME;
5417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5420 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5421 WithEventTime(expectedEventTime))));
5422
5423 // Process several events with the same timestamp from the kernel.
5424 // Ensure that we do not generate events too far into the future.
5425 constexpr static int32_t numEvents =
5426 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5427 for (int i = 0; i < numEvents; i++) {
5428 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5429
5430 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5431 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5433 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5434 WithEventTime(expectedEventTime))));
5435 }
5436
5437 // By processing more events with the same timestamp, we should not generate events with a
5438 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5439 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5440 for (int i = 0; i < 3; i++) {
5441 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5444 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5445 WithEventTime(cappedEventTime))));
5446 }
5447}
5448
5449TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5450 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005451 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005452
5453 nsecs_t kernelEventTime = ARBITRARY_TIME;
5454 nsecs_t expectedEventTime = ARBITRARY_TIME;
5455 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5456 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5458 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5459 WithEventTime(expectedEventTime))));
5460
5461 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5462 // smoothening is not needed, its timestamp is not affected.
5463 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5464 expectedEventTime = kernelEventTime;
5465
5466 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5467 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5469 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5470 WithEventTime(expectedEventTime))));
5471}
5472
Byoungho Jungda10dd32023-10-06 17:03:45 +09005473// --- BluetoothCursorInputMapperTestWithChoreographer ---
5474
5475class BluetoothCursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
5476protected:
5477 void SetUp() override {
5478 input_flags::enable_pointer_choreographer(true);
5479 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5480
5481 mFakePointerController = std::make_shared<FakePointerController>();
5482 mFakePolicy->setPointerController(mFakePointerController);
5483 }
5484};
5485
5486TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmoothening) {
5487 addConfigurationProperty("cursor.mode", "pointer");
5488 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5489
5490 // Set up the default display.
5491 prepareDisplay(ui::ROTATION_0);
5492 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5493 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5494
5495 nsecs_t kernelEventTime = ARBITRARY_TIME;
5496 nsecs_t expectedEventTime = ARBITRARY_TIME;
5497 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5498 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5500 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5501 WithEventTime(expectedEventTime))));
5502
5503 // Process several events that come in quick succession, according to their timestamps.
5504 for (int i = 0; i < 3; i++) {
5505 constexpr static nsecs_t delta = ms2ns(1);
5506 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5507 kernelEventTime += delta;
5508 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5509
5510 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5511 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5513 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5514 WithEventTime(expectedEventTime))));
5515 }
5516}
5517
5518TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningIsCapped) {
5519 addConfigurationProperty("cursor.mode", "pointer");
5520 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5521
5522 // Set up the default display.
5523 prepareDisplay(ui::ROTATION_0);
5524 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5525 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5526
5527 nsecs_t expectedEventTime = ARBITRARY_TIME;
5528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5529 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5531 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5532 WithEventTime(expectedEventTime))));
5533
5534 // Process several events with the same timestamp from the kernel.
5535 // Ensure that we do not generate events too far into the future.
5536 constexpr static int32_t numEvents =
5537 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5538 for (int i = 0; i < numEvents; i++) {
5539 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5540
5541 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5542 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5544 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5545 WithEventTime(expectedEventTime))));
5546 }
5547
5548 // By processing more events with the same timestamp, we should not generate events with a
5549 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5550 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5551 for (int i = 0; i < 3; i++) {
5552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5553 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5555 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5556 WithEventTime(cappedEventTime))));
5557 }
5558}
5559
5560TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningNotUsed) {
5561 addConfigurationProperty("cursor.mode", "pointer");
5562 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5563
5564 // Set up the default display.
5565 prepareDisplay(ui::ROTATION_0);
5566 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5567 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5568
5569 nsecs_t kernelEventTime = ARBITRARY_TIME;
5570 nsecs_t expectedEventTime = ARBITRARY_TIME;
5571 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5572 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5574 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5575 WithEventTime(expectedEventTime))));
5576
5577 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5578 // smoothening is not needed, its timestamp is not affected.
5579 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5580 expectedEventTime = kernelEventTime;
5581
5582 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5583 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5585 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5586 WithEventTime(expectedEventTime))));
5587}
5588
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589// --- TouchInputMapperTest ---
5590
5591class TouchInputMapperTest : public InputMapperTest {
5592protected:
5593 static const int32_t RAW_X_MIN;
5594 static const int32_t RAW_X_MAX;
5595 static const int32_t RAW_Y_MIN;
5596 static const int32_t RAW_Y_MAX;
5597 static const int32_t RAW_TOUCH_MIN;
5598 static const int32_t RAW_TOUCH_MAX;
5599 static const int32_t RAW_TOOL_MIN;
5600 static const int32_t RAW_TOOL_MAX;
5601 static const int32_t RAW_PRESSURE_MIN;
5602 static const int32_t RAW_PRESSURE_MAX;
5603 static const int32_t RAW_ORIENTATION_MIN;
5604 static const int32_t RAW_ORIENTATION_MAX;
5605 static const int32_t RAW_DISTANCE_MIN;
5606 static const int32_t RAW_DISTANCE_MAX;
5607 static const int32_t RAW_TILT_MIN;
5608 static const int32_t RAW_TILT_MAX;
5609 static const int32_t RAW_ID_MIN;
5610 static const int32_t RAW_ID_MAX;
5611 static const int32_t RAW_SLOT_MIN;
5612 static const int32_t RAW_SLOT_MAX;
5613 static const float X_PRECISION;
5614 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005615 static const float X_PRECISION_VIRTUAL;
5616 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617
5618 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005619 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620
5621 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5622
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005623 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005624 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005625
Michael Wrightd02c5b62014-02-10 15:10:22 -08005626 enum Axes {
5627 POSITION = 1 << 0,
5628 TOUCH = 1 << 1,
5629 TOOL = 1 << 2,
5630 PRESSURE = 1 << 3,
5631 ORIENTATION = 1 << 4,
5632 MINOR = 1 << 5,
5633 ID = 1 << 6,
5634 DISTANCE = 1 << 7,
5635 TILT = 1 << 8,
5636 SLOT = 1 << 9,
5637 TOOL_TYPE = 1 << 10,
5638 };
5639
Michael Wrighta9cf4192022-12-01 23:46:39 +00005640 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005641 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005642 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005643 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005644 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 int32_t toRawX(float displayX);
5646 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005647 int32_t toRotatedRawX(float displayX);
5648 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005649 float toCookedX(float rawX, float rawY);
5650 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005651 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005652 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005653 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005654 float toDisplayY(int32_t rawY, int32_t displayHeight);
5655
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656};
5657
5658const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5659const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5660const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5661const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5662const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5663const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5664const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5665const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005666const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5667const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5669const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5670const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5671const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5672const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5673const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5674const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5675const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5676const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5677const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5678const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5679const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005680const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5681 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5682const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5683 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005684const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5685 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686
5687const float TouchInputMapperTest::GEOMETRIC_SCALE =
5688 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5689 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5690
5691const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5692 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5693 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5694};
5695
Michael Wrighta9cf4192022-12-01 23:46:39 +00005696void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005697 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5698 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005699}
5700
5701void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5702 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005703 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704}
5705
Michael Wrighta9cf4192022-12-01 23:46:39 +00005706void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005707 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5708 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5709 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005710}
5711
Michael Wrightd02c5b62014-02-10 15:10:22 -08005712void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005713 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5714 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5715 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5716 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717}
5718
Jason Gerecke489fda82012-09-07 17:19:40 -07005719void TouchInputMapperTest::prepareLocationCalibration() {
5720 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5721}
5722
Michael Wrightd02c5b62014-02-10 15:10:22 -08005723int32_t TouchInputMapperTest::toRawX(float displayX) {
5724 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5725}
5726
5727int32_t TouchInputMapperTest::toRawY(float displayY) {
5728 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5729}
5730
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005731int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5732 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5733}
5734
5735int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5736 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5737}
5738
Jason Gerecke489fda82012-09-07 17:19:40 -07005739float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5740 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5741 return rawX;
5742}
5743
5744float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5745 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5746 return rawY;
5747}
5748
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005750 return toDisplayX(rawX, DISPLAY_WIDTH);
5751}
5752
5753float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5754 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005755}
5756
5757float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005758 return toDisplayY(rawY, DISPLAY_HEIGHT);
5759}
5760
5761float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5762 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005763}
5764
5765
5766// --- SingleTouchInputMapperTest ---
5767
5768class SingleTouchInputMapperTest : public TouchInputMapperTest {
5769protected:
5770 void prepareButtons();
5771 void prepareAxes(int axes);
5772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005773 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5774 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5775 void processUp(SingleTouchInputMapper& mappery);
5776 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5777 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5778 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5779 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5780 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5781 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782};
5783
5784void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005785 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005786}
5787
5788void SingleTouchInputMapperTest::prepareAxes(int axes) {
5789 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005790 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5791 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005792 }
5793 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005794 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5795 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796 }
5797 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005798 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5799 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005800 }
5801 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005802 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5803 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005804 }
5805 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005806 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5807 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005808 }
5809}
5810
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005811void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005812 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5813 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5814 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815}
5816
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005817void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005818 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5819 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005820}
5821
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005822void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005823 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005824}
5825
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005826void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005827 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828}
5829
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005830void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5831 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005832 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005833}
5834
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005835void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005836 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005837}
5838
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005839void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5840 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005841 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5842 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005843}
5844
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005845void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5846 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005847 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005848}
5849
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005850void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005851 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005852}
5853
Michael Wrightd02c5b62014-02-10 15:10:22 -08005854TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005855 prepareButtons();
5856 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005857 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005858
Josep del Río2d8c79a2023-01-23 19:33:50 +00005859 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005860}
5861
Michael Wrightd02c5b62014-02-10 15:10:22 -08005862TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005863 prepareButtons();
5864 prepareAxes(POSITION);
5865 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005866 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005867
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005868 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005869}
5870
5871TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005872 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005873 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005874 prepareButtons();
5875 prepareAxes(POSITION);
5876 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005877 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005878
5879 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005880 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005881
5882 // Virtual key is down.
5883 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5884 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5885 processDown(mapper, x, y);
5886 processSync(mapper);
5887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5888
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005889 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890
5891 // Virtual key is up.
5892 processUp(mapper);
5893 processSync(mapper);
5894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005896 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005897}
5898
5899TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005900 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005901 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005902 prepareButtons();
5903 prepareAxes(POSITION);
5904 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005905 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005906
5907 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005909
5910 // Virtual key is down.
5911 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5912 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5913 processDown(mapper, x, y);
5914 processSync(mapper);
5915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5916
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005917 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005918
5919 // Virtual key is up.
5920 processUp(mapper);
5921 processSync(mapper);
5922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005924 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005925}
5926
5927TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005928 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005929 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005930 prepareButtons();
5931 prepareAxes(POSITION);
5932 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005933 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934
Michael Wrightd02c5b62014-02-10 15:10:22 -08005935 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07005936 ASSERT_TRUE(
5937 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005938 ASSERT_TRUE(flags[0]);
5939 ASSERT_FALSE(flags[1]);
5940}
5941
5942TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005943 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005944 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005945 prepareButtons();
5946 prepareAxes(POSITION);
5947 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005948 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005949
arthurhungdcef2dc2020-08-11 14:47:50 +08005950 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951
5952 NotifyKeyArgs args;
5953
5954 // Press virtual key.
5955 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5956 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5957 processDown(mapper, x, y);
5958 processSync(mapper);
5959
5960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5961 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5962 ASSERT_EQ(DEVICE_ID, args.deviceId);
5963 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5964 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5965 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
5966 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5967 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5968 ASSERT_EQ(KEY_HOME, args.scanCode);
5969 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5970 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5971
5972 // Release virtual key.
5973 processUp(mapper);
5974 processSync(mapper);
5975
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
5977 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
5978 ASSERT_EQ(DEVICE_ID, args.deviceId);
5979 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
5980 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
5981 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
5982 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
5983 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
5984 ASSERT_EQ(KEY_HOME, args.scanCode);
5985 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
5986 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
5987
5988 // Should not have sent any motions.
5989 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5990}
5991
5992TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005993 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005994 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005995 prepareButtons();
5996 prepareAxes(POSITION);
5997 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005998 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005999
arthurhungdcef2dc2020-08-11 14:47:50 +08006000 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006001
6002 NotifyKeyArgs keyArgs;
6003
6004 // Press virtual key.
6005 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6006 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6007 processDown(mapper, x, y);
6008 processSync(mapper);
6009
6010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6011 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6012 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6013 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6014 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6015 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6016 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6017 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6018 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6019 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6020 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6021
6022 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6023 // into the display area.
6024 y -= 100;
6025 processMove(mapper, x, y);
6026 processSync(mapper);
6027
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6029 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6030 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6031 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6032 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6033 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6034 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6035 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6036 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6037 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6038 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6039 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6040
6041 NotifyMotionArgs motionArgs;
6042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6043 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6044 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6045 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6046 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6048 ASSERT_EQ(0, motionArgs.flags);
6049 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6050 ASSERT_EQ(0, motionArgs.buttonState);
6051 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006052 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006053 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006054 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006055 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6056 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6057 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6058 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6059 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6060
6061 // Keep moving out of bounds. Should generate a pointer move.
6062 y -= 50;
6063 processMove(mapper, x, y);
6064 processSync(mapper);
6065
6066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6071 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6072 ASSERT_EQ(0, motionArgs.flags);
6073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6074 ASSERT_EQ(0, motionArgs.buttonState);
6075 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006076 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6080 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6084
6085 // Release out of bounds. Should generate a pointer up.
6086 processUp(mapper);
6087 processSync(mapper);
6088
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6090 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6091 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6093 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6094 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6095 ASSERT_EQ(0, motionArgs.flags);
6096 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6097 ASSERT_EQ(0, motionArgs.buttonState);
6098 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006099 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006100 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006101 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6103 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6104 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6105 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6107
6108 // Should not have sent any more keys or motions.
6109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6111}
6112
6113TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006114 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006115 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006116 prepareButtons();
6117 prepareAxes(POSITION);
6118 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006119 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006120
arthurhungdcef2dc2020-08-11 14:47:50 +08006121 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006122
6123 NotifyMotionArgs motionArgs;
6124
6125 // Initially go down out of bounds.
6126 int32_t x = -10;
6127 int32_t y = -10;
6128 processDown(mapper, x, y);
6129 processSync(mapper);
6130
6131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6132
6133 // Move into the display area. Should generate a pointer down.
6134 x = 50;
6135 y = 75;
6136 processMove(mapper, x, y);
6137 processSync(mapper);
6138
6139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6140 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6141 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6142 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6143 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6144 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6145 ASSERT_EQ(0, motionArgs.flags);
6146 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6147 ASSERT_EQ(0, motionArgs.buttonState);
6148 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006149 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006150 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006151 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006152 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6153 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6154 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6155 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6156 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6157
6158 // Release. Should generate a pointer up.
6159 processUp(mapper);
6160 processSync(mapper);
6161
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6163 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6164 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6165 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6166 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6167 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6168 ASSERT_EQ(0, motionArgs.flags);
6169 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6170 ASSERT_EQ(0, motionArgs.buttonState);
6171 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006172 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006173 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006174 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6176 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6177 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6178 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6179 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6180
6181 // Should not have sent any more keys or motions.
6182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6184}
6185
Santos Cordonfa5cf462017-04-05 10:37:00 -07006186TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006187 addConfigurationProperty("touch.deviceType", "touchScreen");
6188 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6189
Michael Wrighta9cf4192022-12-01 23:46:39 +00006190 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006191 prepareButtons();
6192 prepareAxes(POSITION);
6193 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006194 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006195
arthurhungdcef2dc2020-08-11 14:47:50 +08006196 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006197
6198 NotifyMotionArgs motionArgs;
6199
6200 // Down.
6201 int32_t x = 100;
6202 int32_t y = 125;
6203 processDown(mapper, x, y);
6204 processSync(mapper);
6205
6206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6207 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6208 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6209 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6210 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6211 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6212 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6213 ASSERT_EQ(0, motionArgs.flags);
6214 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6215 ASSERT_EQ(0, motionArgs.buttonState);
6216 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006217 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006218 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006219 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6221 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6222 1, 0, 0, 0, 0, 0, 0, 0));
6223 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6224 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6225 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6226
6227 // Move.
6228 x += 50;
6229 y += 75;
6230 processMove(mapper, x, y);
6231 processSync(mapper);
6232
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6234 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6235 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6236 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6237 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6238 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6239 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6240 ASSERT_EQ(0, motionArgs.flags);
6241 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6242 ASSERT_EQ(0, motionArgs.buttonState);
6243 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006244 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006245 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006246 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6248 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6249 1, 0, 0, 0, 0, 0, 0, 0));
6250 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6251 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6252 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6253
6254 // Up.
6255 processUp(mapper);
6256 processSync(mapper);
6257
6258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6259 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6260 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6261 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6262 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6263 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6264 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6265 ASSERT_EQ(0, motionArgs.flags);
6266 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6267 ASSERT_EQ(0, motionArgs.buttonState);
6268 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006269 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006270 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006271 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006272 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6273 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6274 1, 0, 0, 0, 0, 0, 0, 0));
6275 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6276 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6277 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6278
6279 // Should not have sent any more keys or motions.
6280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6282}
6283
Michael Wrightd02c5b62014-02-10 15:10:22 -08006284TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006285 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006286 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006287 prepareButtons();
6288 prepareAxes(POSITION);
6289 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006290 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006291
arthurhungdcef2dc2020-08-11 14:47:50 +08006292 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006293
6294 NotifyMotionArgs motionArgs;
6295
6296 // Down.
6297 int32_t x = 100;
6298 int32_t y = 125;
6299 processDown(mapper, x, y);
6300 processSync(mapper);
6301
6302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6303 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6304 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6305 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6306 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6307 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6308 ASSERT_EQ(0, motionArgs.flags);
6309 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6310 ASSERT_EQ(0, motionArgs.buttonState);
6311 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006312 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006313 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006314 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6316 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6317 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6318 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6319 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6320
6321 // Move.
6322 x += 50;
6323 y += 75;
6324 processMove(mapper, x, y);
6325 processSync(mapper);
6326
6327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6328 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6329 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6330 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6331 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6332 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6333 ASSERT_EQ(0, motionArgs.flags);
6334 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6335 ASSERT_EQ(0, motionArgs.buttonState);
6336 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006337 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006338 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006339 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006340 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6341 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6342 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6343 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6344 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6345
6346 // Up.
6347 processUp(mapper);
6348 processSync(mapper);
6349
6350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6351 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6352 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6353 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6354 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6355 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6356 ASSERT_EQ(0, motionArgs.flags);
6357 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6358 ASSERT_EQ(0, motionArgs.buttonState);
6359 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006360 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006361 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6364 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6365 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6366 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6367 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6368
6369 // Should not have sent any more keys or motions.
6370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6372}
6373
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006374TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006375 addConfigurationProperty("touch.deviceType", "touchScreen");
6376 prepareButtons();
6377 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006378 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6379 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00006380 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006381
6382 NotifyMotionArgs args;
6383
6384 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006385 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006386 processDown(mapper, toRawX(50), toRawY(75));
6387 processSync(mapper);
6388
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6390 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6391 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6392
6393 processUp(mapper);
6394 processSync(mapper);
6395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6396}
6397
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006398TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006399 addConfigurationProperty("touch.deviceType", "touchScreen");
6400 prepareButtons();
6401 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006402 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6403 // orientation-aware are affected by display rotation.
6404 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00006405 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006406
6407 NotifyMotionArgs args;
6408
6409 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006410 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006411 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006412 processDown(mapper, toRawX(50), toRawY(75));
6413 processSync(mapper);
6414
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6416 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6417 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6418
6419 processUp(mapper);
6420 processSync(mapper);
6421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6422
6423 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006424 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006425 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006426 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006427 processSync(mapper);
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6430 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6431 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6432
6433 processUp(mapper);
6434 processSync(mapper);
6435 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6436
6437 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006438 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006439 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6441 processSync(mapper);
6442
6443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6444 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6445 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6446
6447 processUp(mapper);
6448 processSync(mapper);
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6450
6451 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006452 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006453 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006454 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006455 processSync(mapper);
6456
6457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6458 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6459 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6460
6461 processUp(mapper);
6462 processSync(mapper);
6463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6464}
6465
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006466TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6467 addConfigurationProperty("touch.deviceType", "touchScreen");
6468 prepareButtons();
6469 prepareAxes(POSITION);
6470 addConfigurationProperty("touch.orientationAware", "1");
6471 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6472 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006473 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006474 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006475 NotifyMotionArgs args;
6476
6477 // Orientation 0.
6478 processDown(mapper, toRawX(50), toRawY(75));
6479 processSync(mapper);
6480
6481 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6482 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6483 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6484
6485 processUp(mapper);
6486 processSync(mapper);
6487 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6488}
6489
6490TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6491 addConfigurationProperty("touch.deviceType", "touchScreen");
6492 prepareButtons();
6493 prepareAxes(POSITION);
6494 addConfigurationProperty("touch.orientationAware", "1");
6495 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6496 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006497 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006498 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006499 NotifyMotionArgs args;
6500
6501 // Orientation 90.
6502 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6503 processSync(mapper);
6504
6505 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6506 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6507 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6508
6509 processUp(mapper);
6510 processSync(mapper);
6511 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6512}
6513
6514TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6515 addConfigurationProperty("touch.deviceType", "touchScreen");
6516 prepareButtons();
6517 prepareAxes(POSITION);
6518 addConfigurationProperty("touch.orientationAware", "1");
6519 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6520 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006521 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006522 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006523 NotifyMotionArgs args;
6524
6525 // Orientation 180.
6526 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6527 processSync(mapper);
6528
6529 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6530 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6531 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6532
6533 processUp(mapper);
6534 processSync(mapper);
6535 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6536}
6537
6538TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6539 addConfigurationProperty("touch.deviceType", "touchScreen");
6540 prepareButtons();
6541 prepareAxes(POSITION);
6542 addConfigurationProperty("touch.orientationAware", "1");
6543 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6544 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006545 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006546 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006547 NotifyMotionArgs args;
6548
6549 // Orientation 270.
6550 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6551 processSync(mapper);
6552
6553 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6554 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6555 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6556
6557 processUp(mapper);
6558 processSync(mapper);
6559 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6560}
6561
6562TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6563 addConfigurationProperty("touch.deviceType", "touchScreen");
6564 prepareButtons();
6565 prepareAxes(POSITION);
6566 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6567 // orientation-aware are affected by display rotation.
6568 addConfigurationProperty("touch.orientationAware", "0");
6569 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006570 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006571
6572 NotifyMotionArgs args;
6573
6574 // Orientation 90, Rotation 0.
6575 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006576 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006577 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6578 processSync(mapper);
6579
6580 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6581 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6582 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6583
6584 processUp(mapper);
6585 processSync(mapper);
6586 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6587
6588 // Orientation 90, Rotation 90.
6589 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006590 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006591 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006592 processSync(mapper);
6593
6594 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6595 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6596 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6597
6598 processUp(mapper);
6599 processSync(mapper);
6600 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6601
6602 // Orientation 90, Rotation 180.
6603 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006604 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006605 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6606 processSync(mapper);
6607
6608 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6609 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6610 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6611
6612 processUp(mapper);
6613 processSync(mapper);
6614 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6615
6616 // Orientation 90, Rotation 270.
6617 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006618 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006619 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 -07006620 processSync(mapper);
6621
6622 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6623 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6624 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6625
6626 processUp(mapper);
6627 processSync(mapper);
6628 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6629}
6630
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006631TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6632 addConfigurationProperty("touch.deviceType", "touchScreen");
6633 prepareButtons();
6634 prepareAxes(POSITION);
6635 addConfigurationProperty("touch.orientationAware", "1");
6636 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006637 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006638
6639 // Set a physical frame in the display viewport.
6640 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6641 viewport->physicalLeft = 20;
6642 viewport->physicalTop = 600;
6643 viewport->physicalRight = 30;
6644 viewport->physicalBottom = 610;
6645 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006646 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006647
6648 // Start the touch.
6649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6650 processSync(mapper);
6651
6652 // Expect all input starting outside the physical frame to be ignored.
6653 const std::array<Point, 6> outsidePoints = {
6654 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6655 for (const auto& p : outsidePoints) {
6656 processMove(mapper, toRawX(p.x), toRawY(p.y));
6657 processSync(mapper);
6658 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6659 }
6660
6661 // Move the touch into the physical frame.
6662 processMove(mapper, toRawX(25), toRawY(605));
6663 processSync(mapper);
6664 NotifyMotionArgs args;
6665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6666 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6667 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6668 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6669
6670 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6671 for (const auto& p : outsidePoints) {
6672 processMove(mapper, toRawX(p.x), toRawY(p.y));
6673 processSync(mapper);
6674 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6675 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6676 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6677 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6678 }
6679
6680 processUp(mapper);
6681 processSync(mapper);
6682 EXPECT_NO_FATAL_FAILURE(
6683 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6684}
6685
Harry Cutts1db43992023-06-19 17:05:07 +00006686TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6687 std::shared_ptr<FakePointerController> fakePointerController =
6688 std::make_shared<FakePointerController>();
6689 mFakePolicy->setPointerController(fakePointerController);
6690
6691 addConfigurationProperty("touch.deviceType", "pointer");
6692 prepareAxes(POSITION);
6693 prepareDisplay(ui::ROTATION_0);
6694 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6695
6696 // Set a physical frame in the display viewport.
6697 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6698 viewport->physicalLeft = 20;
6699 viewport->physicalTop = 600;
6700 viewport->physicalRight = 30;
6701 viewport->physicalBottom = 610;
6702 mFakePolicy->updateViewport(*viewport);
6703 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6704
6705 // Start the touch.
6706 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6707 processSync(mapper);
6708
6709 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6710 // produced.
6711 const std::array<Point, 6> outsidePoints = {
6712 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6713 for (const auto& p : outsidePoints) {
6714 processMove(mapper, toRawX(p.x), toRawY(p.y));
6715 processSync(mapper);
6716 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6717 }
6718}
6719
Michael Wrightd02c5b62014-02-10 15:10:22 -08006720TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006721 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006722 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006723 prepareButtons();
6724 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006725 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006726
6727 // These calculations are based on the input device calibration documentation.
6728 int32_t rawX = 100;
6729 int32_t rawY = 200;
6730 int32_t rawPressure = 10;
6731 int32_t rawToolMajor = 12;
6732 int32_t rawDistance = 2;
6733 int32_t rawTiltX = 30;
6734 int32_t rawTiltY = 110;
6735
6736 float x = toDisplayX(rawX);
6737 float y = toDisplayY(rawY);
6738 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6739 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6740 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6741 float distance = float(rawDistance);
6742
6743 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6744 float tiltScale = M_PI / 180;
6745 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6746 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6747 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6748 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6749
6750 processDown(mapper, rawX, rawY);
6751 processPressure(mapper, rawPressure);
6752 processToolMajor(mapper, rawToolMajor);
6753 processDistance(mapper, rawDistance);
6754 processTilt(mapper, rawTiltX, rawTiltY);
6755 processSync(mapper);
6756
6757 NotifyMotionArgs args;
6758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6760 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6761 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6762}
6763
Jason Gerecke489fda82012-09-07 17:19:40 -07006764TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006765 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006766 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006767 prepareLocationCalibration();
6768 prepareButtons();
6769 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006770 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006771
6772 int32_t rawX = 100;
6773 int32_t rawY = 200;
6774
6775 float x = toDisplayX(toCookedX(rawX, rawY));
6776 float y = toDisplayY(toCookedY(rawX, rawY));
6777
6778 processDown(mapper, rawX, rawY);
6779 processSync(mapper);
6780
6781 NotifyMotionArgs args;
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6783 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6784 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6785}
6786
Michael Wrightd02c5b62014-02-10 15:10:22 -08006787TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006788 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006789 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006790 prepareButtons();
6791 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006792 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006793
6794 NotifyMotionArgs motionArgs;
6795 NotifyKeyArgs keyArgs;
6796
6797 processDown(mapper, 100, 200);
6798 processSync(mapper);
6799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6800 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6801 ASSERT_EQ(0, motionArgs.buttonState);
6802
6803 // press BTN_LEFT, release BTN_LEFT
6804 processKey(mapper, BTN_LEFT, 1);
6805 processSync(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6808 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6809
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6811 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6812 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6813
Michael Wrightd02c5b62014-02-10 15:10:22 -08006814 processKey(mapper, BTN_LEFT, 0);
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006817 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006818 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006819
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006822 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006823
6824 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6825 processKey(mapper, BTN_RIGHT, 1);
6826 processKey(mapper, BTN_MIDDLE, 1);
6827 processSync(mapper);
6828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6830 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6831 motionArgs.buttonState);
6832
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6834 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6835 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6836
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6838 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6839 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6840 motionArgs.buttonState);
6841
Michael Wrightd02c5b62014-02-10 15:10:22 -08006842 processKey(mapper, BTN_RIGHT, 0);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006845 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006846 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006847
6848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006849 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006850 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006851
6852 processKey(mapper, BTN_MIDDLE, 0);
6853 processSync(mapper);
6854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006855 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006856 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006857
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006859 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006860 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006861
6862 // press BTN_BACK, release BTN_BACK
6863 processKey(mapper, BTN_BACK, 1);
6864 processSync(mapper);
6865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6866 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6867 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006868
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006871 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6872
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6874 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6875 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006876
6877 processKey(mapper, BTN_BACK, 0);
6878 processSync(mapper);
6879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006880 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006881 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006882
6883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006884 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006885 ASSERT_EQ(0, motionArgs.buttonState);
6886
Michael Wrightd02c5b62014-02-10 15:10:22 -08006887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6888 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6889 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6890
6891 // press BTN_SIDE, release BTN_SIDE
6892 processKey(mapper, BTN_SIDE, 1);
6893 processSync(mapper);
6894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6895 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6896 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006897
Michael Wrightd02c5b62014-02-10 15:10:22 -08006898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006899 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006900 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6901
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6903 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6904 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006905
6906 processKey(mapper, BTN_SIDE, 0);
6907 processSync(mapper);
6908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006909 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006910 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006911
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006913 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006914 ASSERT_EQ(0, motionArgs.buttonState);
6915
Michael Wrightd02c5b62014-02-10 15:10:22 -08006916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6917 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6918 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6919
6920 // press BTN_FORWARD, release BTN_FORWARD
6921 processKey(mapper, BTN_FORWARD, 1);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6924 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6925 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006926
Michael Wrightd02c5b62014-02-10 15:10:22 -08006927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006929 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6930
6931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6932 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6933 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006934
6935 processKey(mapper, BTN_FORWARD, 0);
6936 processSync(mapper);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006938 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006939 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006940
6941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006943 ASSERT_EQ(0, motionArgs.buttonState);
6944
Michael Wrightd02c5b62014-02-10 15:10:22 -08006945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6946 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6947 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6948
6949 // press BTN_EXTRA, release BTN_EXTRA
6950 processKey(mapper, BTN_EXTRA, 1);
6951 processSync(mapper);
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6953 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6954 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006955
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006957 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006958 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
6959
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6961 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6962 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963
6964 processKey(mapper, BTN_EXTRA, 0);
6965 processSync(mapper);
6966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006967 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006968 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006969
6970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006971 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006972 ASSERT_EQ(0, motionArgs.buttonState);
6973
Michael Wrightd02c5b62014-02-10 15:10:22 -08006974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6975 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6976 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
6977
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6979
Michael Wrightd02c5b62014-02-10 15:10:22 -08006980 // press BTN_STYLUS, release BTN_STYLUS
6981 processKey(mapper, BTN_STYLUS, 1);
6982 processSync(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006985 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
6986
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6989 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006990
6991 processKey(mapper, BTN_STYLUS, 0);
6992 processSync(mapper);
6993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006994 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006996
6997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006998 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006999 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000
7001 // press BTN_STYLUS2, release BTN_STYLUS2
7002 processKey(mapper, BTN_STYLUS2, 1);
7003 processSync(mapper);
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7005 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007006 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7007
7008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7009 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7010 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007011
7012 processKey(mapper, BTN_STYLUS2, 0);
7013 processSync(mapper);
7014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007015 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007016 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017
7018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007019 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007020 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021
7022 // release touch
7023 processUp(mapper);
7024 processSync(mapper);
7025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7026 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7027 ASSERT_EQ(0, motionArgs.buttonState);
7028}
7029
7030TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007032 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007033 prepareButtons();
7034 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007035 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036
7037 NotifyMotionArgs motionArgs;
7038
7039 // default tool type is finger
7040 processDown(mapper, 100, 200);
7041 processSync(mapper);
7042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7043 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007044 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045
7046 // eraser
7047 processKey(mapper, BTN_TOOL_RUBBER, 1);
7048 processSync(mapper);
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007051 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007052
7053 // stylus
7054 processKey(mapper, BTN_TOOL_RUBBER, 0);
7055 processKey(mapper, BTN_TOOL_PEN, 1);
7056 processSync(mapper);
7057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7058 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007059 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060
7061 // brush
7062 processKey(mapper, BTN_TOOL_PEN, 0);
7063 processKey(mapper, BTN_TOOL_BRUSH, 1);
7064 processSync(mapper);
7065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7066 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007067 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068
7069 // pencil
7070 processKey(mapper, BTN_TOOL_BRUSH, 0);
7071 processKey(mapper, BTN_TOOL_PENCIL, 1);
7072 processSync(mapper);
7073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7074 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007075 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007076
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007077 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007078 processKey(mapper, BTN_TOOL_PENCIL, 0);
7079 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7080 processSync(mapper);
7081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007083 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007084
7085 // mouse
7086 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7087 processKey(mapper, BTN_TOOL_MOUSE, 1);
7088 processSync(mapper);
7089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007091 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092
7093 // lens
7094 processKey(mapper, BTN_TOOL_MOUSE, 0);
7095 processKey(mapper, BTN_TOOL_LENS, 1);
7096 processSync(mapper);
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007099 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100
7101 // double-tap
7102 processKey(mapper, BTN_TOOL_LENS, 0);
7103 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7104 processSync(mapper);
7105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7106 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007107 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108
7109 // triple-tap
7110 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7111 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7112 processSync(mapper);
7113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7114 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007115 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116
7117 // quad-tap
7118 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7119 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124
7125 // finger
7126 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7127 processKey(mapper, BTN_TOOL_FINGER, 1);
7128 processSync(mapper);
7129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007131 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132
7133 // stylus trumps finger
7134 processKey(mapper, BTN_TOOL_PEN, 1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007138 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139
7140 // eraser trumps stylus
7141 processKey(mapper, BTN_TOOL_RUBBER, 1);
7142 processSync(mapper);
7143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007145 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146
7147 // mouse trumps eraser
7148 processKey(mapper, BTN_TOOL_MOUSE, 1);
7149 processSync(mapper);
7150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007152 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153
7154 // back to default tool type
7155 processKey(mapper, BTN_TOOL_MOUSE, 0);
7156 processKey(mapper, BTN_TOOL_RUBBER, 0);
7157 processKey(mapper, BTN_TOOL_PEN, 0);
7158 processKey(mapper, BTN_TOOL_FINGER, 0);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007162 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163}
7164
7165TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007167 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007168 prepareButtons();
7169 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007170 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00007171 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007172
7173 NotifyMotionArgs motionArgs;
7174
7175 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7176 processKey(mapper, BTN_TOOL_FINGER, 1);
7177 processMove(mapper, 100, 200);
7178 processSync(mapper);
7179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7180 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7186 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7187 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7188
7189 // move a little
7190 processMove(mapper, 150, 250);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7194 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7195 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7196
7197 // down when BTN_TOUCH is pressed, pressure defaults to 1
7198 processKey(mapper, BTN_TOUCH, 1);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7203 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7204
7205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7206 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7207 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7208 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7209
7210 // up when BTN_TOUCH is released, hover restored
7211 processKey(mapper, BTN_TOUCH, 0);
7212 processSync(mapper);
7213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7214 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7215 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7216 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7217
7218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7219 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7221 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7222
7223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7224 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7225 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7226 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7227
7228 // exit hover when pointer goes away
7229 processKey(mapper, BTN_TOOL_FINGER, 0);
7230 processSync(mapper);
7231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7232 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7234 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7235}
7236
7237TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007238 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007239 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007240 prepareButtons();
7241 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007242 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243
7244 NotifyMotionArgs motionArgs;
7245
7246 // initially hovering because pressure is 0
7247 processDown(mapper, 100, 200);
7248 processPressure(mapper, 0);
7249 processSync(mapper);
7250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7251 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7253 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7254
7255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7256 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7257 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7258 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7259
7260 // move a little
7261 processMove(mapper, 150, 250);
7262 processSync(mapper);
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7266 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7267
7268 // down when pressure is non-zero
7269 processPressure(mapper, RAW_PRESSURE_MAX);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7275
7276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7279 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7280
7281 // up when pressure becomes 0, hover restored
7282 processPressure(mapper, 0);
7283 processSync(mapper);
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7288
7289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7290 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7291 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7292 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7293
7294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7295 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7297 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7298
7299 // exit hover when pointer goes away
7300 processUp(mapper);
7301 processSync(mapper);
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7305 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7306}
7307
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007308TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7309 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007310 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007311 prepareButtons();
7312 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007313 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007314
7315 // Touch down.
7316 processDown(mapper, 100, 200);
7317 processPressure(mapper, 1);
7318 processSync(mapper);
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7320 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7321
7322 // Reset the mapper. This should cancel the ongoing gesture.
7323 resetMapper(mapper, ARBITRARY_TIME);
7324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7325 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7326
7327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7328}
7329
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007330TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7331 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007332 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007333 prepareButtons();
7334 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007335 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007336
7337 // Set the initial state for the touch pointer.
7338 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7339 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7340 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7341 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7342
7343 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007344 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7345 // does not generate any events.
7346 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007347
7348 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7349 // the recreated touch state to generate a down event.
7350 processSync(mapper);
7351 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7352 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7353
7354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7355}
7356
lilinnan687e58f2022-07-19 16:00:50 +08007357TEST_F(SingleTouchInputMapperTest,
7358 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7359 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007360 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08007361 prepareButtons();
7362 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007363 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08007364 NotifyMotionArgs motionArgs;
7365
7366 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007367 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007368 processSync(mapper);
7369
7370 // We should receive a down event
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7373
7374 // Change display id
7375 clearViewports();
7376 prepareSecondaryDisplay(ViewportType::INTERNAL);
7377
7378 // We should receive a cancel event
7379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7380 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7381 // Then receive reset called
7382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7383}
7384
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007385TEST_F(SingleTouchInputMapperTest,
7386 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7387 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007388 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007389 prepareButtons();
7390 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007391 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007392 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7393 NotifyMotionArgs motionArgs;
7394
7395 // Start a new gesture.
7396 processDown(mapper, 100, 200);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7399 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7400
7401 // Make the viewport inactive. This will put the device in disabled mode.
7402 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7403 viewport->isActive = false;
7404 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007405 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007406
7407 // We should receive a cancel event for the ongoing gesture.
7408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7409 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7410 // Then we should be notified that the device was reset.
7411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7412
7413 // No events are generated while the viewport is inactive.
7414 processMove(mapper, 101, 201);
7415 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007416 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007417 processSync(mapper);
7418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7419
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007420 // Start a new gesture while the viewport is still inactive.
7421 processDown(mapper, 300, 400);
7422 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7423 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7424 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7425 processSync(mapper);
7426
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007427 // Make the viewport active again. The device should resume processing events.
7428 viewport->isActive = true;
7429 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007430 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007431
7432 // The device is reset because it changes back to direct mode, without generating any events.
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7435
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007436 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007437 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7439 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007440
7441 // No more events.
7442 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7444}
7445
Prabir Pradhan211ba622022-10-31 21:09:21 +00007446TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7447 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007448 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00007449 prepareButtons();
7450 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007451 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00007452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7453
7454 // Press a stylus button.
7455 processKey(mapper, BTN_STYLUS, 1);
7456 processSync(mapper);
7457
7458 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7459 processDown(mapper, 100, 200);
7460 processSync(mapper);
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7463 WithCoords(toDisplayX(100), toDisplayY(200)),
7464 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7467 WithCoords(toDisplayX(100), toDisplayY(200)),
7468 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7469
7470 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7471 // the button has not actually been released, since there will be no pointers through which the
7472 // button state can be reported. The event is generated at the location of the pointer before
7473 // it went up.
7474 processUp(mapper);
7475 processSync(mapper);
7476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7477 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7478 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7480 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7481 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7482}
7483
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007484TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
7485 addConfigurationProperty("touch.deviceType", "touchScreen");
7486 prepareDisplay(ui::ROTATION_0);
7487 prepareButtons();
7488 prepareAxes(POSITION);
7489
7490 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
7491
Arpit Singha8c236b2023-04-25 13:56:05 +00007492 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7494
7495 // Press a stylus button.
7496 processKey(mapper, BTN_STYLUS, 1);
7497 processSync(mapper);
7498
7499 // Start a touch gesture and ensure that the stylus button is not reported.
7500 processDown(mapper, 100, 200);
7501 processSync(mapper);
7502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7503 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
7504
7505 // Release and press the stylus button again.
7506 processKey(mapper, BTN_STYLUS, 0);
7507 processSync(mapper);
7508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7509 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7510 processKey(mapper, BTN_STYLUS, 1);
7511 processSync(mapper);
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7513 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7514
7515 // Release the touch gesture.
7516 processUp(mapper);
7517 processSync(mapper);
7518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7519 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7522}
7523
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007524TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
7525 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7526 prepareDisplay(ui::ROTATION_0);
7527 prepareButtons();
7528 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007529 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7531
7532 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
7533}
7534
Seunghwan Choi356026c2023-02-01 14:37:25 +09007535TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
7536 std::shared_ptr<FakePointerController> fakePointerController =
7537 std::make_shared<FakePointerController>();
7538 addConfigurationProperty("touch.deviceType", "touchScreen");
7539 prepareDisplay(ui::ROTATION_0);
7540 prepareButtons();
7541 prepareAxes(POSITION);
7542 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7543 mFakePolicy->setPointerController(fakePointerController);
7544 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007545 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007546
7547 processKey(mapper, BTN_TOOL_PEN, 1);
7548 processMove(mapper, 100, 200);
7549 processSync(mapper);
7550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7551 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007552 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007553 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7554 ASSERT_TRUE(fakePointerController->isPointerShown());
7555 ASSERT_NO_FATAL_FAILURE(
7556 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7557}
7558
7559TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7560 std::shared_ptr<FakePointerController> fakePointerController =
7561 std::make_shared<FakePointerController>();
7562 addConfigurationProperty("touch.deviceType", "touchScreen");
7563 prepareDisplay(ui::ROTATION_0);
7564 prepareButtons();
7565 prepareAxes(POSITION);
7566 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7567 mFakePolicy->setPointerController(fakePointerController);
7568 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007569 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007570
7571 processKey(mapper, BTN_TOOL_PEN, 1);
7572 processMove(mapper, 100, 200);
7573 processSync(mapper);
7574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7575 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007576 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007577 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7578 ASSERT_FALSE(fakePointerController->isPointerShown());
7579}
7580
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007581TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7582 // Initialize the device without setting device source to touch navigation.
7583 addConfigurationProperty("touch.deviceType", "touchScreen");
7584 prepareDisplay(ui::ROTATION_0);
7585 prepareButtons();
7586 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007587 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007588
7589 // Ensure that the device is created as a touchscreen, not touch navigation.
7590 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7591
7592 // Add device type association after the device was created.
7593 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7594
7595 // Send update to the mapper.
7596 std::list<NotifyArgs> unused2 =
7597 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007598 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007599
7600 // Check whether device type update was successful.
7601 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7602}
7603
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007604TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7605 // Initialize the device without setting device source to touch navigation.
7606 addConfigurationProperty("touch.deviceType", "touchScreen");
7607 prepareDisplay(ui::ROTATION_0);
7608 prepareButtons();
7609 prepareAxes(POSITION);
7610 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7611
7612 // Set a physical frame in the display viewport.
7613 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7614 viewport->physicalLeft = 0;
7615 viewport->physicalTop = 0;
7616 viewport->physicalRight = DISPLAY_WIDTH / 2;
7617 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7618 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007619 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007620
Arpit Singha8c236b2023-04-25 13:56:05 +00007621 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007622
7623 // Hovering inside the physical frame produces events.
7624 processKey(mapper, BTN_TOOL_PEN, 1);
7625 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7626 processSync(mapper);
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7628 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7630 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7631
7632 // Leaving the physical frame ends the hovering gesture.
7633 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7634 processSync(mapper);
7635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7636 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7637
7638 // Moving outside the physical frame does not produce events.
7639 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7640 processSync(mapper);
7641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7642
7643 // Re-entering the physical frame produces events.
7644 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7645 processSync(mapper);
7646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7647 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7648 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7649 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7650}
7651
Prabir Pradhan5632d622021-09-06 07:57:20 -07007652// --- TouchDisplayProjectionTest ---
7653
7654class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7655public:
7656 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7657 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7658 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007659 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7660 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7661 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007662 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007663 auto rotatedWidth = naturalDisplayWidth;
7664 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007665 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007666 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007667 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007668 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007669 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007670 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007671 inverseRotationFlags = ui::Transform::ROT_180;
7672 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007673 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007674 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007675 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007676 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007677 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007678 inverseRotationFlags = ui::Transform::ROT_0;
7679 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007680 }
7681
Prabir Pradhana9df3162022-12-05 23:57:27 +00007682 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007683 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7684
7685 std::optional<DisplayViewport> internalViewport =
7686 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7687 DisplayViewport& v = *internalViewport;
7688 v.displayId = DISPLAY_ID;
7689 v.orientation = orientation;
7690
7691 v.logicalLeft = 0;
7692 v.logicalTop = 0;
7693 v.logicalRight = 100;
7694 v.logicalBottom = 100;
7695
7696 v.physicalLeft = rotatedPhysicalDisplay.left;
7697 v.physicalTop = rotatedPhysicalDisplay.top;
7698 v.physicalRight = rotatedPhysicalDisplay.right;
7699 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7700
Prabir Pradhana9df3162022-12-05 23:57:27 +00007701 v.deviceWidth = rotatedWidth;
7702 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007703
7704 v.isActive = true;
7705 v.uniqueId = UNIQUE_ID;
7706 v.type = ViewportType::INTERNAL;
7707 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007708 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007709 }
7710
7711 void assertReceivedMove(const Point& point) {
7712 NotifyMotionArgs motionArgs;
7713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7714 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007715 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7717 1, 0, 0, 0, 0, 0, 0, 0));
7718 }
7719};
7720
7721TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7722 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007723 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007724
7725 prepareButtons();
7726 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007727 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007728
7729 NotifyMotionArgs motionArgs;
7730
7731 // Configure the DisplayViewport such that the logical display maps to a subsection of
7732 // the display panel called the physical display. Here, the physical display is bounded by the
7733 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7734 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7735 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7736 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7737
Michael Wrighta9cf4192022-12-01 23:46:39 +00007738 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007739 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7740
7741 // Touches outside the physical display should be ignored, and should not generate any
7742 // events. Ensure touches at the following points that lie outside of the physical display
7743 // area do not generate any events.
7744 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7745 processDown(mapper, toRawX(point.x), toRawY(point.y));
7746 processSync(mapper);
7747 processUp(mapper);
7748 processSync(mapper);
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7750 << "Unexpected event generated for touch outside physical display at point: "
7751 << point.x << ", " << point.y;
7752 }
7753 }
7754}
7755
7756TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7757 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007758 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007759
7760 prepareButtons();
7761 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007762 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007763
7764 NotifyMotionArgs motionArgs;
7765
7766 // Configure the DisplayViewport such that the logical display maps to a subsection of
7767 // the display panel called the physical display. Here, the physical display is bounded by the
7768 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7769 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7770
Michael Wrighta9cf4192022-12-01 23:46:39 +00007771 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007772 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7773
7774 // Touches that start outside the physical display should be ignored until it enters the
7775 // physical display bounds, at which point it should generate a down event. Start a touch at
7776 // the point (5, 100), which is outside the physical display bounds.
7777 static const Point kOutsidePoint{5, 100};
7778 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7779 processSync(mapper);
7780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7781
7782 // Move the touch into the physical display area. This should generate a pointer down.
7783 processMove(mapper, toRawX(11), toRawY(21));
7784 processSync(mapper);
7785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7786 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007787 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007788 ASSERT_NO_FATAL_FAILURE(
7789 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7790
7791 // Move the touch inside the physical display area. This should generate a pointer move.
7792 processMove(mapper, toRawX(69), toRawY(159));
7793 processSync(mapper);
7794 assertReceivedMove({69, 159});
7795
7796 // Move outside the physical display area. Since the pointer is already down, this should
7797 // now continue generating events.
7798 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7799 processSync(mapper);
7800 assertReceivedMove(kOutsidePoint);
7801
7802 // Release. This should generate a pointer up.
7803 processUp(mapper);
7804 processSync(mapper);
7805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7806 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7807 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7808 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7809
7810 // Ensure no more events were generated.
7811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7813 }
7814}
7815
Prabir Pradhana9df3162022-12-05 23:57:27 +00007816// --- TouchscreenPrecisionTests ---
7817
7818// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7819// in various orientations and with different display rotations. We configure the touchscreen to
7820// have a higher resolution than that of the display by an integer scale factor in each axis so that
7821// we can enforce that coordinates match precisely as expected.
7822class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7823 public ::testing::WithParamInterface<ui::Rotation> {
7824public:
7825 void SetUp() override {
7826 SingleTouchInputMapperTest::SetUp();
7827
7828 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7829 // four times the resolution of the display in the Y axis.
7830 prepareButtons();
7831 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007832 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7833 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007834 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007835 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7836 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007837 }
7838
7839 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7840 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7841 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7842 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7843
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007844 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7845 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7846
7847 static const int32_t PRECISION_RAW_X_FLAT = 16;
7848 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7849
7850 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7851 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7852
Prabir Pradhana9df3162022-12-05 23:57:27 +00007853 static const std::array<Point, 4> kRawCorners;
7854};
7855
7856const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7857 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7858 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7859 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7860 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7861}};
7862
7863// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7864// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7865// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7866TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7867 enum class Orientation {
7868 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7869 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7870 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7871 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7872 ftl_last = ORIENTATION_270,
7873 };
7874 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7875 Orientation::ORIENTATION_270;
7876 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7877 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7878 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7879 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7880 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7881 };
7882
7883 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7884
7885 // Configure the touchscreen as being installed in the one of the four different orientations
7886 // relative to the display.
7887 addConfigurationProperty("touch.deviceType", "touchScreen");
7888 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7889 prepareDisplay(ui::ROTATION_0);
7890
Arpit Singha8c236b2023-04-25 13:56:05 +00007891 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007892
7893 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7894 // orientations of either 90 or 270) this means the display's natural resolution will be
7895 // flipped.
7896 const bool displayRotated =
7897 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7898 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7899 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7900 const Rect physicalFrame{0, 0, width, height};
7901 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7902
7903 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7904 const float expectedPrecisionX = displayRotated ? 4 : 2;
7905 const float expectedPrecisionY = displayRotated ? 2 : 4;
7906
7907 // Test all four corners.
7908 for (int i = 0; i < 4; i++) {
7909 const auto& raw = kRawCorners[i];
7910 processDown(mapper, raw.x, raw.y);
7911 processSync(mapper);
7912 const auto& expected = expectedPoints[i];
7913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7914 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7915 WithCoords(expected.x, expected.y),
7916 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7917 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7918 << "with touchscreen orientation "
7919 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7920 << expected.x << ", " << expected.y << ").";
7921 processUp(mapper);
7922 processSync(mapper);
7923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7924 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7925 WithCoords(expected.x, expected.y))));
7926 }
7927}
7928
Prabir Pradhan82687402022-12-06 01:32:53 +00007929TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
7930 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
7931 kMappedCorners = {
7932 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7933 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
7934 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
7935 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
7936 };
7937
7938 const ui::Rotation displayRotation = GetParam();
7939
7940 addConfigurationProperty("touch.deviceType", "touchScreen");
7941 prepareDisplay(displayRotation);
7942
Arpit Singha8c236b2023-04-25 13:56:05 +00007943 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00007944
7945 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7946
7947 // Test all four corners.
7948 for (int i = 0; i < 4; i++) {
7949 const auto& expected = expectedPoints[i];
7950 const auto& raw = kRawCorners[i];
7951 processDown(mapper, raw.x, raw.y);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7955 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
7956 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7957 << "with display rotation " << ui::toCString(displayRotation)
7958 << ", expected point (" << expected.x << ", " << expected.y << ").";
7959 processUp(mapper);
7960 processSync(mapper);
7961 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7962 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7963 WithCoords(expected.x, expected.y))));
7964 }
7965}
7966
Prabir Pradhan3e798762022-12-02 21:02:11 +00007967TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
7968 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
7969 kMappedCorners = {
7970 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7971 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
7972 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
7973 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
7974 };
7975
7976 const ui::Rotation displayRotation = GetParam();
7977
7978 addConfigurationProperty("touch.deviceType", "touchScreen");
7979 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
7980
Arpit Singha8c236b2023-04-25 13:56:05 +00007981 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00007982
7983 // Ori 270, so width and height swapped
7984 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
7985 prepareDisplay(displayRotation);
7986 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
7987
7988 const auto& expectedPoints = kMappedCorners.at(displayRotation);
7989
7990 // Test all four corners.
7991 for (int i = 0; i < 4; i++) {
7992 const auto& expected = expectedPoints[i];
7993 const auto& raw = kRawCorners[i];
7994 processDown(mapper, raw.x, raw.y);
7995 processSync(mapper);
7996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7997 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7998 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
7999 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
8000 << "with display rotation " << ui::toCString(displayRotation)
8001 << ", expected point (" << expected.x << ", " << expected.y << ").";
8002 processUp(mapper);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8005 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
8006 WithCoords(expected.x, expected.y))));
8007 }
8008}
8009
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008010TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
8011 const ui::Rotation displayRotation = GetParam();
8012
8013 addConfigurationProperty("touch.deviceType", "touchScreen");
8014 prepareDisplay(displayRotation);
8015
8016 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00008017 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008018
8019 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
8020 // MotionRanges use display pixels as their units
8021 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
8022 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
8023
8024 // The MotionRanges should be oriented in the rotated display's coordinate space
8025 const bool displayRotated =
8026 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
8027
8028 constexpr float MAX_X = 479.5;
8029 constexpr float MAX_Y = 799.75;
8030 EXPECT_EQ(xRange->min, 0.f);
8031 EXPECT_EQ(yRange->min, 0.f);
8032 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
8033 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
8034
8035 EXPECT_EQ(xRange->flat, 8.f);
8036 EXPECT_EQ(yRange->flat, 8.f);
8037
8038 EXPECT_EQ(xRange->fuzz, 2.f);
8039 EXPECT_EQ(yRange->fuzz, 2.f);
8040
8041 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
8042 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
8043}
8044
Prabir Pradhana9df3162022-12-05 23:57:27 +00008045// Run the precision tests for all rotations.
8046INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
8047 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
8048 ui::ROTATION_270),
8049 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
8050 return ftl::enum_string(testParamInfo.param);
8051 });
8052
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008053// --- ExternalStylusFusionTest ---
8054
8055class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
8056public:
8057 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
8058 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008059 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008060 prepareButtons();
8061 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008062 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008063
8064 mStylusState.when = ARBITRARY_TIME;
8065 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008066 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008067 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008068 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008069 processExternalStylusState(mapper);
8070 return mapper;
8071 }
8072
8073 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
8074 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
8075 for (const NotifyArgs& args : generatedArgs) {
8076 mFakeListener->notify(args);
8077 }
8078 // Loop the reader to flush the input listener queue.
8079 mReader->loopOnce();
8080 return generatedArgs;
8081 }
8082
8083protected:
8084 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008085
8086 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
8087 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008088 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008089
8090 // The first pointer is withheld.
8091 processDown(mapper, 100, 200);
8092 processSync(mapper);
8093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8094 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8095 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8096
8097 // The external stylus reports pressure. The withheld finger pointer is released as a
8098 // stylus.
8099 mStylusState.pressure = 1.f;
8100 processExternalStylusState(mapper);
8101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8102 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8103 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8104
8105 // Subsequent pointer events are not withheld.
8106 processMove(mapper, 101, 201);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8109 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8110
8111 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8113 }
8114
8115 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
8116 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8117
8118 // Releasing the touch pointer ends the gesture.
8119 processUp(mapper);
8120 processSync(mapper);
8121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008123 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008124
8125 mStylusState.pressure = 0.f;
8126 processExternalStylusState(mapper);
8127 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8129 }
8130
8131 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008132 // When stylus fusion is not successful, events should be reported with the original source.
8133 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008134 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008135 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008136
8137 // The first pointer is withheld when an external stylus is connected,
8138 // and a timeout is requested.
8139 processDown(mapper, 100, 200);
8140 processSync(mapper);
8141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8142 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8143 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8144
8145 // If the timeout expires early, it is requested again.
8146 handleTimeout(mapper, ARBITRARY_TIME + 1);
8147 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8148 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8149
8150 // When the timeout expires, the withheld touch is released as a finger pointer.
8151 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8153 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8154
8155 // Subsequent pointer events are not withheld.
8156 processMove(mapper, 101, 201);
8157 processSync(mapper);
8158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8159 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8160 processUp(mapper);
8161 processSync(mapper);
8162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8163 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8164
8165 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8167 }
8168
8169private:
8170 InputDeviceInfo mExternalStylusDeviceInfo{};
8171};
8172
8173TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
8174 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008175 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008176}
8177
8178TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
8179 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8180 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8181}
8182
8183TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
8184 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8185 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8186}
8187
8188// Test a successful stylus fusion gesture where the pressure is reported by the external
8189// before the touch is reported by the touchscreen.
8190TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
8191 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008192 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008193
8194 // The external stylus reports pressure first. It is ignored for now.
8195 mStylusState.pressure = 1.f;
8196 processExternalStylusState(mapper);
8197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8198 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8199
8200 // When the touch goes down afterwards, it is reported as a stylus pointer.
8201 processDown(mapper, 100, 200);
8202 processSync(mapper);
8203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8204 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8205 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8206
8207 processMove(mapper, 101, 201);
8208 processSync(mapper);
8209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8210 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8211 processUp(mapper);
8212 processSync(mapper);
8213 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8214 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8215
8216 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8218}
8219
8220TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
8221 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8222
8223 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8224 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8225
8226 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8227 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8228 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8229 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8230}
8231
8232TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
8233 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008234 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008235
8236 mStylusState.pressure = 0.8f;
8237 processExternalStylusState(mapper);
8238 processDown(mapper, 100, 200);
8239 processSync(mapper);
8240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8241 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8242 WithPressure(0.8f))));
8243 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8244
8245 // The external stylus reports a pressure change. We wait for some time for a touch event.
8246 mStylusState.pressure = 0.6f;
8247 processExternalStylusState(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8249 ASSERT_NO_FATAL_FAILURE(
8250 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8251
8252 // If a touch is reported within the timeout, it reports the updated pressure.
8253 processMove(mapper, 101, 201);
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8256 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8257 WithPressure(0.6f))));
8258 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8259
8260 // There is another pressure change.
8261 mStylusState.pressure = 0.5f;
8262 processExternalStylusState(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8264 ASSERT_NO_FATAL_FAILURE(
8265 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8266
8267 // If a touch is not reported within the timeout, a move event is generated to report
8268 // the new pressure.
8269 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8271 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8272 WithPressure(0.5f))));
8273
8274 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8275 // repeated indefinitely.
8276 mStylusState.pressure = 0.0f;
8277 processExternalStylusState(mapper);
8278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8279 ASSERT_NO_FATAL_FAILURE(
8280 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8281 processMove(mapper, 102, 202);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8284 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8285 WithPressure(0.5f))));
8286 processMove(mapper, 103, 203);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8289 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8290 WithPressure(0.5f))));
8291
8292 processUp(mapper);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008295 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008296 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008297
8298 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8300}
8301
8302TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8303 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008304 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008305
8306 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008307 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008308 processExternalStylusState(mapper);
8309 processDown(mapper, 100, 200);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8312 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008314 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8315
8316 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008317 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008318 processExternalStylusState(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8320 ASSERT_NO_FATAL_FAILURE(
8321 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8322
8323 // If a touch is reported within the timeout, it reports the updated pressure.
8324 processMove(mapper, 101, 201);
8325 processSync(mapper);
8326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8327 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008328 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008329 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8330
8331 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008332 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008333 processExternalStylusState(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8335 ASSERT_NO_FATAL_FAILURE(
8336 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8337
8338 // If a touch is not reported within the timeout, a move event is generated to report
8339 // the new tool type.
8340 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8342 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008343 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008344
8345 processUp(mapper);
8346 processSync(mapper);
8347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8348 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008349 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008350
8351 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8353}
8354
8355TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8356 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008357 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008358
8359 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8360
8361 // The external stylus reports a button change. We wait for some time for a touch event.
8362 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8363 processExternalStylusState(mapper);
8364 ASSERT_NO_FATAL_FAILURE(
8365 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8366
8367 // If a touch is reported within the timeout, it reports the updated button state.
8368 processMove(mapper, 101, 201);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8371 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8372 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8374 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8375 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8376 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8377
8378 // The button is now released.
8379 mStylusState.buttons = 0;
8380 processExternalStylusState(mapper);
8381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8382 ASSERT_NO_FATAL_FAILURE(
8383 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8384
8385 // If a touch is not reported within the timeout, a move event is generated to report
8386 // the new button state.
8387 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8389 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8390 WithButtonState(0))));
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008392 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8393 WithButtonState(0))));
8394
8395 processUp(mapper);
8396 processSync(mapper);
8397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008398 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8399
8400 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8402}
8403
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404// --- MultiTouchInputMapperTest ---
8405
8406class MultiTouchInputMapperTest : public TouchInputMapperTest {
8407protected:
8408 void prepareAxes(int axes);
8409
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008410 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8411 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8412 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8413 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8414 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8415 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8416 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8417 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8418 void processId(MultiTouchInputMapper& mapper, int32_t id);
8419 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8420 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8421 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008422 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008423 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008424 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
8425 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426};
8427
8428void MultiTouchInputMapperTest::prepareAxes(int axes) {
8429 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008430 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008432 }
8433 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8435 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008437 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8438 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008439 }
8440 }
8441 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008442 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8443 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008444 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008445 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008446 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447 }
8448 }
8449 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008450 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8451 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008452 }
8453 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8455 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008456 }
8457 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008458 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8459 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008460 }
8461 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008462 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8463 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 }
8465 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008466 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8467 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008468 }
8469 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008471 }
8472}
8473
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008474void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8475 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008478}
8479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008480void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8481 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008482 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483}
8484
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008485void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8486 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008488}
8489
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008490void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008492}
8493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008494void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008496}
8497
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008498void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8499 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008501}
8502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008503void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505}
8506
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008507void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008508 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509}
8510
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008511void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008513}
8514
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008515void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008516 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008517}
8518
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008519void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008521}
8522
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008523void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8524 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008525 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526}
8527
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008528void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8529 int32_t value) {
8530 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8531 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8532}
8533
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008534void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008535 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536}
8537
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008538void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8539 nsecs_t readTime) {
8540 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008541}
8542
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008545 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008546 prepareAxes(POSITION);
8547 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008548 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008549
arthurhungdcef2dc2020-08-11 14:47:50 +08008550 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008551
8552 NotifyMotionArgs motionArgs;
8553
8554 // Two fingers down at once.
8555 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8556 processPosition(mapper, x1, y1);
8557 processMTSync(mapper);
8558 processPosition(mapper, x2, y2);
8559 processMTSync(mapper);
8560 processSync(mapper);
8561
8562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8563 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8564 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8566 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8567 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8568 ASSERT_EQ(0, motionArgs.flags);
8569 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8570 ASSERT_EQ(0, motionArgs.buttonState);
8571 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008572 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008573 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008574 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8576 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8577 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8578 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8579 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8580
8581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8582 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8583 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8584 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8585 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008586 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008587 ASSERT_EQ(0, motionArgs.flags);
8588 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8589 ASSERT_EQ(0, motionArgs.buttonState);
8590 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008591 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008593 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008594 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8597 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8600 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8601 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8602 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8603
8604 // Move.
8605 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8606 processPosition(mapper, x1, y1);
8607 processMTSync(mapper);
8608 processPosition(mapper, x2, y2);
8609 processMTSync(mapper);
8610 processSync(mapper);
8611
8612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8613 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8614 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8615 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8616 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8618 ASSERT_EQ(0, motionArgs.flags);
8619 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8620 ASSERT_EQ(0, motionArgs.buttonState);
8621 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008622 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008624 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008626 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008627 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8628 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8629 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8630 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8631 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8632 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8633 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8634
8635 // First finger up.
8636 x2 += 15; y2 -= 20;
8637 processPosition(mapper, x2, y2);
8638 processMTSync(mapper);
8639 processSync(mapper);
8640
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008646 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008647 ASSERT_EQ(0, motionArgs.flags);
8648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8649 ASSERT_EQ(0, motionArgs.buttonState);
8650 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008651 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008655 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8657 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8659 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8660 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8661 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8662 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8663
8664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8665 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8666 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8667 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8668 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8670 ASSERT_EQ(0, motionArgs.flags);
8671 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8672 ASSERT_EQ(0, motionArgs.buttonState);
8673 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008674 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008676 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8682
8683 // Move.
8684 x2 += 20; y2 -= 25;
8685 processPosition(mapper, x2, y2);
8686 processMTSync(mapper);
8687 processSync(mapper);
8688
8689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8690 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8691 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8692 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8693 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8695 ASSERT_EQ(0, motionArgs.flags);
8696 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8697 ASSERT_EQ(0, motionArgs.buttonState);
8698 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008699 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008700 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008701 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8703 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8704 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8705 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8706 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8707
8708 // New finger down.
8709 int32_t x3 = 700, y3 = 300;
8710 processPosition(mapper, x2, y2);
8711 processMTSync(mapper);
8712 processPosition(mapper, x3, y3);
8713 processMTSync(mapper);
8714 processSync(mapper);
8715
8716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8717 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8718 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8719 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8720 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008721 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008722 ASSERT_EQ(0, motionArgs.flags);
8723 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8724 ASSERT_EQ(0, motionArgs.buttonState);
8725 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008726 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008727 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008728 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008729 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008730 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8732 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8734 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8735 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8736 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8738
8739 // Second finger up.
8740 x3 += 30; y3 -= 20;
8741 processPosition(mapper, x3, y3);
8742 processMTSync(mapper);
8743 processSync(mapper);
8744
8745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8746 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8747 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8748 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8749 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008750 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008751 ASSERT_EQ(0, motionArgs.flags);
8752 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8753 ASSERT_EQ(0, motionArgs.buttonState);
8754 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008755 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008758 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008759 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008760 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8761 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8762 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8763 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8764 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8765 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8766 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8767
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8770 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8771 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8772 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8774 ASSERT_EQ(0, motionArgs.flags);
8775 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8776 ASSERT_EQ(0, motionArgs.buttonState);
8777 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008778 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8782 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8783 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8784 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8786
8787 // Last finger up.
8788 processMTSync(mapper);
8789 processSync(mapper);
8790
8791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8793 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8794 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8795 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8796 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8797 ASSERT_EQ(0, motionArgs.flags);
8798 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8799 ASSERT_EQ(0, motionArgs.buttonState);
8800 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008801 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008802 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008803 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8805 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8806 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8807 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8808 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8809
8810 // Should not have sent any more keys or motions.
8811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8813}
8814
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008815TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8816 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008817 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008818
8819 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8820 /*fuzz*/ 0, /*resolution*/ 10);
8821 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8822 /*fuzz*/ 0, /*resolution*/ 11);
8823 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8824 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8825 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8826 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8827 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8828 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8829 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8830 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8831
Arpit Singha8c236b2023-04-25 13:56:05 +00008832 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008833
8834 // X and Y axes
8835 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8836 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8837 // Touch major and minor
8838 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8839 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8840 // Tool major and minor
8841 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8842 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8843}
8844
8845TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8846 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008847 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008848
8849 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8850 /*fuzz*/ 0, /*resolution*/ 10);
8851 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8852 /*fuzz*/ 0, /*resolution*/ 11);
8853
8854 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8855
Arpit Singha8c236b2023-04-25 13:56:05 +00008856 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008857
8858 // Touch major and minor
8859 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8860 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8861 // Tool major and minor
8862 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8863 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8864}
8865
Michael Wrightd02c5b62014-02-10 15:10:22 -08008866TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008868 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008869 prepareAxes(POSITION | ID);
8870 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008871 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008872
arthurhungdcef2dc2020-08-11 14:47:50 +08008873 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008874
8875 NotifyMotionArgs motionArgs;
8876
8877 // Two fingers down at once.
8878 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8879 processPosition(mapper, x1, y1);
8880 processId(mapper, 1);
8881 processMTSync(mapper);
8882 processPosition(mapper, x2, y2);
8883 processId(mapper, 2);
8884 processMTSync(mapper);
8885 processSync(mapper);
8886
8887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8888 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008889 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008890 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008891 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008892 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8893 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8894
8895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008896 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008897 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008900 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008901 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8903 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8904 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8905 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8906
8907 // Move.
8908 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8909 processPosition(mapper, x1, y1);
8910 processId(mapper, 1);
8911 processMTSync(mapper);
8912 processPosition(mapper, x2, y2);
8913 processId(mapper, 2);
8914 processMTSync(mapper);
8915 processSync(mapper);
8916
8917 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8918 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008919 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008920 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008921 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008922 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008923 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8925 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8927 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8928
8929 // First finger up.
8930 x2 += 15; y2 -= 20;
8931 processPosition(mapper, x2, y2);
8932 processId(mapper, 2);
8933 processMTSync(mapper);
8934 processSync(mapper);
8935
8936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008937 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008938 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008939 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008940 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008941 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008942 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8944 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8946 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8947
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008950 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008952 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8954 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8955
8956 // Move.
8957 x2 += 20; y2 -= 25;
8958 processPosition(mapper, x2, y2);
8959 processId(mapper, 2);
8960 processMTSync(mapper);
8961 processSync(mapper);
8962
8963 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8964 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008965 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008966 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8969 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8970
8971 // New finger down.
8972 int32_t x3 = 700, y3 = 300;
8973 processPosition(mapper, x2, y2);
8974 processId(mapper, 2);
8975 processMTSync(mapper);
8976 processPosition(mapper, x3, y3);
8977 processId(mapper, 3);
8978 processMTSync(mapper);
8979 processSync(mapper);
8980
8981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008982 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008983 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008984 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008985 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008986 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008987 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008988 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8989 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8991 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8992
8993 // Second finger up.
8994 x3 += 30; y3 -= 20;
8995 processPosition(mapper, x3, y3);
8996 processId(mapper, 3);
8997 processMTSync(mapper);
8998 processSync(mapper);
8999
9000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009001 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009002 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009004 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009005 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009006 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9008 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9009 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9010 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9011
9012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9013 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009014 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009015 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009016 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9018 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9019
9020 // Last finger up.
9021 processMTSync(mapper);
9022 processSync(mapper);
9023
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009026 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009027 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009028 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009029 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9030 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9031
9032 // Should not have sent any more keys or motions.
9033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9035}
9036
9037TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009038 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009039 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009040 prepareAxes(POSITION | ID | SLOT);
9041 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00009042 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009043
arthurhungdcef2dc2020-08-11 14:47:50 +08009044 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009045
9046 NotifyMotionArgs motionArgs;
9047
9048 // Two fingers down at once.
9049 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9050 processPosition(mapper, x1, y1);
9051 processId(mapper, 1);
9052 processSlot(mapper, 1);
9053 processPosition(mapper, x2, y2);
9054 processId(mapper, 2);
9055 processSync(mapper);
9056
9057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9058 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009059 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009060 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009061 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009062 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9063 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9064
9065 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009066 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009067 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009068 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009069 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009070 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009071 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009072 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9073 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9075 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9076
9077 // Move.
9078 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
9079 processSlot(mapper, 0);
9080 processPosition(mapper, x1, y1);
9081 processSlot(mapper, 1);
9082 processPosition(mapper, x2, y2);
9083 processSync(mapper);
9084
9085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9086 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009087 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009088 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009089 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009090 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009091 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009092 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9093 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9094 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9095 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9096
9097 // First finger up.
9098 x2 += 15; y2 -= 20;
9099 processSlot(mapper, 0);
9100 processId(mapper, -1);
9101 processSlot(mapper, 1);
9102 processPosition(mapper, x2, y2);
9103 processSync(mapper);
9104
9105 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009106 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009107 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009109 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009110 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009111 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009112 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9113 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9114 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9115 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9116
9117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009119 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009120 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009121 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9123 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9124
9125 // Move.
9126 x2 += 20; y2 -= 25;
9127 processPosition(mapper, x2, y2);
9128 processSync(mapper);
9129
9130 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9131 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009132 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009133 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009134 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009135 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9136 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9137
9138 // New finger down.
9139 int32_t x3 = 700, y3 = 300;
9140 processPosition(mapper, x2, y2);
9141 processSlot(mapper, 0);
9142 processId(mapper, 3);
9143 processPosition(mapper, x3, y3);
9144 processSync(mapper);
9145
9146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009147 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009148 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009150 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009152 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9154 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9155 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9156 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9157
9158 // Second finger up.
9159 x3 += 30; y3 -= 20;
9160 processSlot(mapper, 1);
9161 processId(mapper, -1);
9162 processSlot(mapper, 0);
9163 processPosition(mapper, x3, y3);
9164 processSync(mapper);
9165
9166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009167 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009168 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009169 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009170 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009171 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009172 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9174 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9175 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9176 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9177
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009180 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009181 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009182 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009183 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9184 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9185
9186 // Last finger up.
9187 processId(mapper, -1);
9188 processSync(mapper);
9189
9190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9191 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009192 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009193 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009194 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009195 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9196 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9197
9198 // Should not have sent any more keys or motions.
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9201}
9202
9203TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009204 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009205 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009206 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009207 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009208
9209 // These calculations are based on the input device calibration documentation.
9210 int32_t rawX = 100;
9211 int32_t rawY = 200;
9212 int32_t rawTouchMajor = 7;
9213 int32_t rawTouchMinor = 6;
9214 int32_t rawToolMajor = 9;
9215 int32_t rawToolMinor = 8;
9216 int32_t rawPressure = 11;
9217 int32_t rawDistance = 0;
9218 int32_t rawOrientation = 3;
9219 int32_t id = 5;
9220
9221 float x = toDisplayX(rawX);
9222 float y = toDisplayY(rawY);
9223 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
9224 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9225 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9226 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9227 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9228 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9229 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
9230 float distance = float(rawDistance);
9231
9232 processPosition(mapper, rawX, rawY);
9233 processTouchMajor(mapper, rawTouchMajor);
9234 processTouchMinor(mapper, rawTouchMinor);
9235 processToolMajor(mapper, rawToolMajor);
9236 processToolMinor(mapper, rawToolMinor);
9237 processPressure(mapper, rawPressure);
9238 processOrientation(mapper, rawOrientation);
9239 processDistance(mapper, rawDistance);
9240 processId(mapper, id);
9241 processMTSync(mapper);
9242 processSync(mapper);
9243
9244 NotifyMotionArgs args;
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9246 ASSERT_EQ(0, args.pointerProperties[0].id);
9247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9248 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
9249 orientation, distance));
9250}
9251
9252TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009253 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009254 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009255 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9256 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00009257 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009258
9259 // These calculations are based on the input device calibration documentation.
9260 int32_t rawX = 100;
9261 int32_t rawY = 200;
9262 int32_t rawTouchMajor = 140;
9263 int32_t rawTouchMinor = 120;
9264 int32_t rawToolMajor = 180;
9265 int32_t rawToolMinor = 160;
9266
9267 float x = toDisplayX(rawX);
9268 float y = toDisplayY(rawY);
9269 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9270 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9271 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9272 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9273 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9274
9275 processPosition(mapper, rawX, rawY);
9276 processTouchMajor(mapper, rawTouchMajor);
9277 processTouchMinor(mapper, rawTouchMinor);
9278 processToolMajor(mapper, rawToolMajor);
9279 processToolMinor(mapper, rawToolMinor);
9280 processMTSync(mapper);
9281 processSync(mapper);
9282
9283 NotifyMotionArgs args;
9284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9286 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9287}
9288
9289TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009290 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009291 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009292 prepareAxes(POSITION | TOUCH | TOOL);
9293 addConfigurationProperty("touch.size.calibration", "diameter");
9294 addConfigurationProperty("touch.size.scale", "10");
9295 addConfigurationProperty("touch.size.bias", "160");
9296 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00009297 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009298
9299 // These calculations are based on the input device calibration documentation.
9300 // Note: We only provide a single common touch/tool value because the device is assumed
9301 // not to emit separate values for each pointer (isSummed = 1).
9302 int32_t rawX = 100;
9303 int32_t rawY = 200;
9304 int32_t rawX2 = 150;
9305 int32_t rawY2 = 250;
9306 int32_t rawTouchMajor = 5;
9307 int32_t rawToolMajor = 8;
9308
9309 float x = toDisplayX(rawX);
9310 float y = toDisplayY(rawY);
9311 float x2 = toDisplayX(rawX2);
9312 float y2 = toDisplayY(rawY2);
9313 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9314 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9315 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9316
9317 processPosition(mapper, rawX, rawY);
9318 processTouchMajor(mapper, rawTouchMajor);
9319 processToolMajor(mapper, rawToolMajor);
9320 processMTSync(mapper);
9321 processPosition(mapper, rawX2, rawY2);
9322 processTouchMajor(mapper, rawTouchMajor);
9323 processToolMajor(mapper, rawToolMajor);
9324 processMTSync(mapper);
9325 processSync(mapper);
9326
9327 NotifyMotionArgs args;
9328 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9329 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9330
9331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009332 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009333 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9335 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9337 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9338}
9339
9340TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009341 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009342 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009343 prepareAxes(POSITION | TOUCH | TOOL);
9344 addConfigurationProperty("touch.size.calibration", "area");
9345 addConfigurationProperty("touch.size.scale", "43");
9346 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00009347 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009348
9349 // These calculations are based on the input device calibration documentation.
9350 int32_t rawX = 100;
9351 int32_t rawY = 200;
9352 int32_t rawTouchMajor = 5;
9353 int32_t rawToolMajor = 8;
9354
9355 float x = toDisplayX(rawX);
9356 float y = toDisplayY(rawY);
9357 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9358 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9359 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9360
9361 processPosition(mapper, rawX, rawY);
9362 processTouchMajor(mapper, rawTouchMajor);
9363 processToolMajor(mapper, rawToolMajor);
9364 processMTSync(mapper);
9365 processSync(mapper);
9366
9367 NotifyMotionArgs args;
9368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9369 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9370 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9371}
9372
9373TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009374 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009375 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009376 prepareAxes(POSITION | PRESSURE);
9377 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9378 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00009379 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009380
Michael Wrightaa449c92017-12-13 21:21:43 +00009381 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00009382 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009383 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9384 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9385 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9386
Michael Wrightd02c5b62014-02-10 15:10:22 -08009387 // These calculations are based on the input device calibration documentation.
9388 int32_t rawX = 100;
9389 int32_t rawY = 200;
9390 int32_t rawPressure = 60;
9391
9392 float x = toDisplayX(rawX);
9393 float y = toDisplayY(rawY);
9394 float pressure = float(rawPressure) * 0.01f;
9395
9396 processPosition(mapper, rawX, rawY);
9397 processPressure(mapper, rawPressure);
9398 processMTSync(mapper);
9399 processSync(mapper);
9400
9401 NotifyMotionArgs args;
9402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9404 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9405}
9406
9407TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009408 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009409 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009410 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009411 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009412
9413 NotifyMotionArgs motionArgs;
9414 NotifyKeyArgs keyArgs;
9415
9416 processId(mapper, 1);
9417 processPosition(mapper, 100, 200);
9418 processSync(mapper);
9419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9420 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9421 ASSERT_EQ(0, motionArgs.buttonState);
9422
9423 // press BTN_LEFT, release BTN_LEFT
9424 processKey(mapper, BTN_LEFT, 1);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9427 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9428 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9429
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9431 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9432 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9433
Michael Wrightd02c5b62014-02-10 15:10:22 -08009434 processKey(mapper, BTN_LEFT, 0);
9435 processSync(mapper);
9436 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009437 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009438 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009439
9440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009441 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009442 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009443
9444 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9445 processKey(mapper, BTN_RIGHT, 1);
9446 processKey(mapper, BTN_MIDDLE, 1);
9447 processSync(mapper);
9448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9449 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9450 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9451 motionArgs.buttonState);
9452
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009453 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9454 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9455 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9456
9457 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9458 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9459 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9460 motionArgs.buttonState);
9461
Michael Wrightd02c5b62014-02-10 15:10:22 -08009462 processKey(mapper, BTN_RIGHT, 0);
9463 processSync(mapper);
9464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009465 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009466 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009467
9468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009469 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009470 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009471
9472 processKey(mapper, BTN_MIDDLE, 0);
9473 processSync(mapper);
9474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009475 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009476 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009477
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009480 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009481
9482 // press BTN_BACK, release BTN_BACK
9483 processKey(mapper, BTN_BACK, 1);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9486 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9487 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009488
Michael Wrightd02c5b62014-02-10 15:10:22 -08009489 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009490 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009491 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9492
9493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9495 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009496
9497 processKey(mapper, BTN_BACK, 0);
9498 processSync(mapper);
9499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009500 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009501 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009502
9503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009504 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009505 ASSERT_EQ(0, motionArgs.buttonState);
9506
Michael Wrightd02c5b62014-02-10 15:10:22 -08009507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9508 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9509 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9510
9511 // press BTN_SIDE, release BTN_SIDE
9512 processKey(mapper, BTN_SIDE, 1);
9513 processSync(mapper);
9514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9515 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9516 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009517
Michael Wrightd02c5b62014-02-10 15:10:22 -08009518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009520 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9521
9522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9524 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009525
9526 processKey(mapper, BTN_SIDE, 0);
9527 processSync(mapper);
9528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009530 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009531
9532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009534 ASSERT_EQ(0, motionArgs.buttonState);
9535
Michael Wrightd02c5b62014-02-10 15:10:22 -08009536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9537 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9538 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9539
9540 // press BTN_FORWARD, release BTN_FORWARD
9541 processKey(mapper, BTN_FORWARD, 1);
9542 processSync(mapper);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9544 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9545 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009546
Michael Wrightd02c5b62014-02-10 15:10:22 -08009547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009549 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9550
9551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9553 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009554
9555 processKey(mapper, BTN_FORWARD, 0);
9556 processSync(mapper);
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009560
9561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009563 ASSERT_EQ(0, motionArgs.buttonState);
9564
Michael Wrightd02c5b62014-02-10 15:10:22 -08009565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9567 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9568
9569 // press BTN_EXTRA, release BTN_EXTRA
9570 processKey(mapper, BTN_EXTRA, 1);
9571 processSync(mapper);
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9574 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009575
Michael Wrightd02c5b62014-02-10 15:10:22 -08009576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009578 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9579
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9582 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009583
9584 processKey(mapper, BTN_EXTRA, 0);
9585 processSync(mapper);
9586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009588 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009589
9590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009592 ASSERT_EQ(0, motionArgs.buttonState);
9593
Michael Wrightd02c5b62014-02-10 15:10:22 -08009594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9595 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9596 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9597
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009598 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9599
Michael Wrightd02c5b62014-02-10 15:10:22 -08009600 // press BTN_STYLUS, release BTN_STYLUS
9601 processKey(mapper, BTN_STYLUS, 1);
9602 processSync(mapper);
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009605 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9606
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9609 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009610
9611 processKey(mapper, BTN_STYLUS, 0);
9612 processSync(mapper);
9613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009614 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009615 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009616
9617 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009618 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009619 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009620
9621 // press BTN_STYLUS2, release BTN_STYLUS2
9622 processKey(mapper, BTN_STYLUS2, 1);
9623 processSync(mapper);
9624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9625 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009626 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9627
9628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9630 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009631
9632 processKey(mapper, BTN_STYLUS2, 0);
9633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009635 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009636 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009637
9638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009639 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009640 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009641
9642 // release touch
9643 processId(mapper, -1);
9644 processSync(mapper);
9645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9646 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9647 ASSERT_EQ(0, motionArgs.buttonState);
9648}
9649
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009650TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9651 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009652 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009653 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009654 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009655
9656 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9657 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9658
9659 // Touch down.
9660 processId(mapper, 1);
9661 processPosition(mapper, 100, 200);
9662 processSync(mapper);
9663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9664 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9665
9666 // Press and release button mapped to the primary stylus button.
9667 processKey(mapper, BTN_A, 1);
9668 processSync(mapper);
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9670 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9671 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9673 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9674 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9675
9676 processKey(mapper, BTN_A, 0);
9677 processSync(mapper);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9680 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9681 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9682
9683 // Press and release the HID usage mapped to the secondary stylus button.
9684 processHidUsage(mapper, 0xabcd, 1);
9685 processSync(mapper);
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9687 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9688 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9691 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9692
9693 processHidUsage(mapper, 0xabcd, 0);
9694 processSync(mapper);
9695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9696 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9698 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9699
9700 // Release touch.
9701 processId(mapper, -1);
9702 processSync(mapper);
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9704 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9705}
9706
Michael Wrightd02c5b62014-02-10 15:10:22 -08009707TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009708 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009709 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009710 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009711 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009712
9713 NotifyMotionArgs motionArgs;
9714
9715 // default tool type is finger
9716 processId(mapper, 1);
9717 processPosition(mapper, 100, 200);
9718 processSync(mapper);
9719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9720 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009721 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009722
9723 // eraser
9724 processKey(mapper, BTN_TOOL_RUBBER, 1);
9725 processSync(mapper);
9726 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9727 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009728 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009729
9730 // stylus
9731 processKey(mapper, BTN_TOOL_RUBBER, 0);
9732 processKey(mapper, BTN_TOOL_PEN, 1);
9733 processSync(mapper);
9734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009736 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009737
9738 // brush
9739 processKey(mapper, BTN_TOOL_PEN, 0);
9740 processKey(mapper, BTN_TOOL_BRUSH, 1);
9741 processSync(mapper);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9743 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009744 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009745
9746 // pencil
9747 processKey(mapper, BTN_TOOL_BRUSH, 0);
9748 processKey(mapper, BTN_TOOL_PENCIL, 1);
9749 processSync(mapper);
9750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009752 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009753
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009754 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009755 processKey(mapper, BTN_TOOL_PENCIL, 0);
9756 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9757 processSync(mapper);
9758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9759 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009760 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009761
9762 // mouse
9763 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9764 processKey(mapper, BTN_TOOL_MOUSE, 1);
9765 processSync(mapper);
9766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009768 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009769
9770 // lens
9771 processKey(mapper, BTN_TOOL_MOUSE, 0);
9772 processKey(mapper, BTN_TOOL_LENS, 1);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9775 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009776 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009777
9778 // double-tap
9779 processKey(mapper, BTN_TOOL_LENS, 0);
9780 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9781 processSync(mapper);
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009784 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009785
9786 // triple-tap
9787 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9788 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9789 processSync(mapper);
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009793
9794 // quad-tap
9795 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9796 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009800 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009801
9802 // finger
9803 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9804 processKey(mapper, BTN_TOOL_FINGER, 1);
9805 processSync(mapper);
9806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9807 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009808 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009809
9810 // stylus trumps finger
9811 processKey(mapper, BTN_TOOL_PEN, 1);
9812 processSync(mapper);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009815 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009816
9817 // eraser trumps stylus
9818 processKey(mapper, BTN_TOOL_RUBBER, 1);
9819 processSync(mapper);
9820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9821 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009822 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009823
9824 // mouse trumps eraser
9825 processKey(mapper, BTN_TOOL_MOUSE, 1);
9826 processSync(mapper);
9827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009829 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009830
9831 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9832 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9833 processSync(mapper);
9834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9835 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009837
9838 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9839 processToolType(mapper, MT_TOOL_PEN);
9840 processSync(mapper);
9841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9842 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009843 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009844
9845 // back to default tool type
9846 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9847 processKey(mapper, BTN_TOOL_MOUSE, 0);
9848 processKey(mapper, BTN_TOOL_RUBBER, 0);
9849 processKey(mapper, BTN_TOOL_PEN, 0);
9850 processKey(mapper, BTN_TOOL_FINGER, 0);
9851 processSync(mapper);
9852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9853 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009854 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009855}
9856
9857TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009858 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009859 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009860 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009861 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009862 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009863
9864 NotifyMotionArgs motionArgs;
9865
9866 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9867 processId(mapper, 1);
9868 processPosition(mapper, 100, 200);
9869 processSync(mapper);
9870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9871 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9872 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9873 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9874
9875 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9876 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9877 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9878 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9879
9880 // move a little
9881 processPosition(mapper, 150, 250);
9882 processSync(mapper);
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9884 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9885 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9886 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9887
9888 // down when BTN_TOUCH is pressed, pressure defaults to 1
9889 processKey(mapper, BTN_TOUCH, 1);
9890 processSync(mapper);
9891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9892 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9893 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9894 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9895
9896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9897 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9898 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9899 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9900
9901 // up when BTN_TOUCH is released, hover restored
9902 processKey(mapper, BTN_TOUCH, 0);
9903 processSync(mapper);
9904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9905 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9907 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9908
9909 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9910 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9911 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9912 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9913
9914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9915 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9916 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9917 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9918
9919 // exit hover when pointer goes away
9920 processId(mapper, -1);
9921 processSync(mapper);
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9923 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9924 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9925 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9926}
9927
9928TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009929 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009930 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009931 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009932 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009933
9934 NotifyMotionArgs motionArgs;
9935
9936 // initially hovering because pressure is 0
9937 processId(mapper, 1);
9938 processPosition(mapper, 100, 200);
9939 processPressure(mapper, 0);
9940 processSync(mapper);
9941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9942 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9943 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9944 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9945
9946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9947 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9948 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9949 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9950
9951 // move a little
9952 processPosition(mapper, 150, 250);
9953 processSync(mapper);
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9957 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9958
9959 // down when pressure becomes non-zero
9960 processPressure(mapper, RAW_PRESSURE_MAX);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9966
9967 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9968 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9970 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9971
9972 // up when pressure becomes 0, hover restored
9973 processPressure(mapper, 0);
9974 processSync(mapper);
9975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9976 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9978 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9979
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9981 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9982 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9983 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9984
9985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9986 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9987 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9988 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9989
9990 // exit hover when pointer goes away
9991 processId(mapper, -1);
9992 processSync(mapper);
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9997}
9998
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07009999/**
10000 * Set the input device port <--> display port associations, and check that the
10001 * events are routed to the display that matches the display port.
10002 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
10003 */
10004TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010005 const std::string usb2 = "USB2";
10006 const uint8_t hdmi1 = 0;
10007 const uint8_t hdmi2 = 1;
10008 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010009 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010010
10011 addConfigurationProperty("touch.deviceType", "touchScreen");
10012 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010013 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010014
10015 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10016 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
10017
10018 // We are intentionally not adding the viewport for display 1 yet. Since the port association
10019 // for this input device is specified, and the matching viewport is not present,
10020 // the input device should be disabled (at the mapper level).
10021
10022 // Add viewport for display 2 on hdmi2
10023 prepareSecondaryDisplay(type, hdmi2);
10024 // Send a touch event
10025 processPosition(mapper, 100, 100);
10026 processSync(mapper);
10027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10028
10029 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +000010030 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010031 // Send a touch event again
10032 processPosition(mapper, 100, 100);
10033 processSync(mapper);
10034
10035 NotifyMotionArgs args;
10036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10037 ASSERT_EQ(DISPLAY_ID, args.displayId);
10038}
Michael Wrightd02c5b62014-02-10 15:10:22 -080010039
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010040TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
10041 addConfigurationProperty("touch.deviceType", "touchScreen");
10042 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010043 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010044
10045 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10046
Michael Wrighta9cf4192022-12-01 23:46:39 +000010047 prepareDisplay(ui::ROTATION_0);
10048 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010049
10050 // Send a touch event
10051 processPosition(mapper, 100, 100);
10052 processSync(mapper);
10053
10054 NotifyMotionArgs args;
10055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10056 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10057}
10058
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010059TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -080010060 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +010010061 std::shared_ptr<FakePointerController> fakePointerController =
10062 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -080010063 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010064 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010065 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010066
Garfield Tan888a6a42020-01-09 11:39:16 -080010067 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010068 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -080010069
Michael Wrighta9cf4192022-12-01 23:46:39 +000010070 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010071 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010072 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010073
Josep del Río2d8c79a2023-01-23 19:33:50 +000010074 // Check source is mouse that would obtain the PointerController.
10075 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010076
10077 NotifyMotionArgs motionArgs;
10078 processPosition(mapper, 100, 100);
10079 processSync(mapper);
10080
10081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10082 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10083 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10084}
10085
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010086/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010087 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
10088 */
10089TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
10090 addConfigurationProperty("touch.deviceType", "touchScreen");
10091 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010092 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010093
Michael Wrighta9cf4192022-12-01 23:46:39 +000010094 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +000010095 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
10096 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
10097 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
10098 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010099
10100 NotifyMotionArgs args;
10101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10102 ASSERT_EQ(26, args.readTime);
10103
Harry Cutts33476232023-01-30 19:57:29 +000010104 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
10105 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
10106 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010107
10108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10109 ASSERT_EQ(33, args.readTime);
10110}
10111
10112/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010113 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
10114 * events should not be delivered to the listener.
10115 */
10116TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
10117 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010118 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010119 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010120 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010121 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010122 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010123 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010124
10125 NotifyMotionArgs motionArgs;
10126 processPosition(mapper, 100, 100);
10127 processSync(mapper);
10128
10129 mFakeListener->assertNotifyMotionWasNotCalled();
10130}
10131
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010132/**
10133 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
10134 * the touch mapper can process the events and the events can be delivered to the listener.
10135 */
10136TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
10137 addConfigurationProperty("touch.deviceType", "touchScreen");
10138 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010139 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010140 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010141 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010142 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010143 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010144
10145 NotifyMotionArgs motionArgs;
10146 processPosition(mapper, 100, 100);
10147 processSync(mapper);
10148
10149 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10150 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10151}
10152
Josh Thielene986aed2023-06-01 14:17:30 +000010153/**
10154 * When the viewport is deactivated (isActive transitions from true to false),
10155 * and touch.enableForInactiveViewport is false, touches prior to the transition
10156 * should be cancelled.
10157 */
Garfield Tanc734e4f2021-01-15 20:01:39 -080010158TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
10159 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010160 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010161 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010162 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010163 std::optional<DisplayViewport> optionalDisplayViewport =
10164 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10165 ASSERT_TRUE(optionalDisplayViewport.has_value());
10166 DisplayViewport displayViewport = *optionalDisplayViewport;
10167
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010168 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010169 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010170 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -080010171
10172 // Finger down
10173 int32_t x = 100, y = 100;
10174 processPosition(mapper, x, y);
10175 processSync(mapper);
10176
10177 NotifyMotionArgs motionArgs;
10178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10179 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10180
10181 // Deactivate display viewport
10182 displayViewport.isActive = false;
10183 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010184 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010185
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010186 // The ongoing touch should be canceled immediately
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10188 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10189
10190 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -080010191 x += 10, y += 10;
10192 processPosition(mapper, x, y);
10193 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -080010195
10196 // Reactivate display viewport
10197 displayViewport.isActive = true;
10198 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010199 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010200
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010201 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -080010202 x += 10, y += 10;
10203 processPosition(mapper, x, y);
10204 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010205 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10206 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010207}
10208
Josh Thielene986aed2023-06-01 14:17:30 +000010209/**
10210 * When the viewport is deactivated (isActive transitions from true to false),
10211 * and touch.enableForInactiveViewport is true, touches prior to the transition
10212 * should not be cancelled.
10213 */
10214TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
10215 addConfigurationProperty("touch.deviceType", "touchScreen");
10216 addConfigurationProperty("touch.enableForInactiveViewport", "1");
10217 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
10218 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
10219 std::optional<DisplayViewport> optionalDisplayViewport =
10220 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10221 ASSERT_TRUE(optionalDisplayViewport.has_value());
10222 DisplayViewport displayViewport = *optionalDisplayViewport;
10223
10224 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10225 prepareAxes(POSITION);
10226 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
10227
10228 // Finger down
10229 int32_t x = 100, y = 100;
10230 processPosition(mapper, x, y);
10231 processSync(mapper);
10232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10233 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10234
10235 // Deactivate display viewport
10236 displayViewport.isActive = false;
10237 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10238 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10239
10240 // The ongoing touch should not be canceled
10241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10242
10243 // Finger move is not ignored
10244 x += 10, y += 10;
10245 processPosition(mapper, x, y);
10246 processSync(mapper);
10247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10248 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10249
10250 // Reactivate display viewport
10251 displayViewport.isActive = true;
10252 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10253 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10254
10255 // Finger move continues and does not start new gesture
10256 x += 10, y += 10;
10257 processPosition(mapper, x, y);
10258 processSync(mapper);
10259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10260 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10261}
10262
Arthur Hung7c645402019-01-25 17:45:42 +080010263TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
10264 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +080010265 prepareAxes(POSITION | ID | SLOT);
10266 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010267 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +080010268
10269 // Create the second touch screen device, and enable multi fingers.
10270 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +080010271 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +080010272 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010273 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +080010274 std::shared_ptr<InputDevice> device2 =
10275 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010276 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +080010277
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010278 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_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_POSITION_Y, RAW_Y_MIN, RAW_Y_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_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010283 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010284 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010285 /*flat=*/0, /*fuzz=*/0);
10286 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010287 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
10288 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +080010289
10290 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +000010291 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
10292 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
10293 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010294 std::list<NotifyArgs> unused =
10295 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010296 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010297 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +080010298
10299 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +010010300 std::shared_ptr<FakePointerController> fakePointerController =
10301 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010302 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +080010303
10304 // Setup policy for associated displays and show touches.
10305 const uint8_t hdmi1 = 0;
10306 const uint8_t hdmi2 = 1;
10307 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10308 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10309 mFakePolicy->setShowTouches(true);
10310
10311 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010312 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010313 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010314
10315 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010316 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010317 InputReaderConfiguration::Change::DISPLAY_INFO |
10318 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010319
10320 // Two fingers down at default display.
10321 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10322 processPosition(mapper, x1, y1);
10323 processId(mapper, 1);
10324 processSlot(mapper, 1);
10325 processPosition(mapper, x2, y2);
10326 processId(mapper, 2);
10327 processSync(mapper);
10328
10329 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10330 fakePointerController->getSpots().find(DISPLAY_ID);
10331 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10332 ASSERT_EQ(size_t(2), iter->second.size());
10333
10334 // Two fingers down at second display.
10335 processPosition(mapper2, x1, y1);
10336 processId(mapper2, 1);
10337 processSlot(mapper2, 1);
10338 processPosition(mapper2, x2, y2);
10339 processId(mapper2, 2);
10340 processSync(mapper2);
10341
10342 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10343 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10344 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010345
10346 // Disable the show touches configuration and ensure the spots are cleared.
10347 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010348 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010349 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010350
10351 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010352}
10353
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010354TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010355 prepareAxes(POSITION);
10356 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010357 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010358 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010359
10360 NotifyMotionArgs motionArgs;
10361 // Unrotated video frame
10362 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10363 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010364 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010365 processPosition(mapper, 100, 200);
10366 processSync(mapper);
10367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10368 ASSERT_EQ(frames, motionArgs.videoFrames);
10369
10370 // Subsequent touch events should not have any videoframes
10371 // This is implemented separately in FakeEventHub,
10372 // but that should match the behaviour of TouchVideoDevice.
10373 processPosition(mapper, 200, 200);
10374 processSync(mapper);
10375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10376 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10377}
10378
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010379TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010380 prepareAxes(POSITION);
10381 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010382 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010383 // Unrotated video frame
10384 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10385 NotifyMotionArgs motionArgs;
10386
10387 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010388 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010389 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10390 clearViewports();
10391 prepareDisplay(orientation);
10392 std::vector<TouchVideoFrame> frames{frame};
10393 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10394 processPosition(mapper, 100, 200);
10395 processSync(mapper);
10396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10397 ASSERT_EQ(frames, motionArgs.videoFrames);
10398 }
10399}
10400
10401TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10402 prepareAxes(POSITION);
10403 addConfigurationProperty("touch.deviceType", "touchScreen");
10404 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10405 // orientation-aware are affected by display rotation.
10406 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010407 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010408 // Unrotated video frame
10409 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10410 NotifyMotionArgs motionArgs;
10411
10412 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010413 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010414 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10415 clearViewports();
10416 prepareDisplay(orientation);
10417 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010418 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010419 processPosition(mapper, 100, 200);
10420 processSync(mapper);
10421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010422 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10423 // compared to the display. This is so that when the window transform (which contains the
10424 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10425 // window's coordinate space.
10426 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010427 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010428
10429 // Release finger.
10430 processSync(mapper);
10431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010432 }
10433}
10434
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010435TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010436 prepareAxes(POSITION);
10437 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010438 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010439 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10440 // so mix these.
10441 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10442 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10443 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10444 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10445 NotifyMotionArgs motionArgs;
10446
Michael Wrighta9cf4192022-12-01 23:46:39 +000010447 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010448 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010449 processPosition(mapper, 100, 200);
10450 processSync(mapper);
10451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010452 ASSERT_EQ(frames, motionArgs.videoFrames);
10453}
10454
10455TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10456 prepareAxes(POSITION);
10457 addConfigurationProperty("touch.deviceType", "touchScreen");
10458 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10459 // orientation-aware are affected by display rotation.
10460 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010461 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010462 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10463 // so mix these.
10464 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10465 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10466 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10467 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10468 NotifyMotionArgs motionArgs;
10469
Michael Wrighta9cf4192022-12-01 23:46:39 +000010470 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010471 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10472 processPosition(mapper, 100, 200);
10473 processSync(mapper);
10474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10475 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10476 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10477 // compared to the display. This is so that when the window transform (which contains the
10478 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10479 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010480 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010481 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010482 ASSERT_EQ(frames, motionArgs.videoFrames);
10483}
10484
Arthur Hung9da14732019-09-02 16:16:58 +080010485/**
10486 * If we had defined port associations, but the viewport is not ready, the touch device would be
10487 * expected to be disabled, and it should be enabled after the viewport has found.
10488 */
10489TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010490 constexpr uint8_t hdmi2 = 1;
10491 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010492 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010493
10494 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10495
10496 addConfigurationProperty("touch.deviceType", "touchScreen");
10497 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010498 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010499
10500 ASSERT_EQ(mDevice->isEnabled(), false);
10501
10502 // Add display on hdmi2, the device should be enabled and can receive touch event.
10503 prepareSecondaryDisplay(type, hdmi2);
10504 ASSERT_EQ(mDevice->isEnabled(), true);
10505
10506 // Send a touch event.
10507 processPosition(mapper, 100, 100);
10508 processSync(mapper);
10509
10510 NotifyMotionArgs args;
10511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10512 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10513}
10514
Arthur Hung421eb1c2020-01-16 00:09:42 +080010515TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010517 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010518 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010519 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010520
10521 NotifyMotionArgs motionArgs;
10522
10523 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10524 // finger down
10525 processId(mapper, 1);
10526 processPosition(mapper, x1, y1);
10527 processSync(mapper);
10528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10529 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010530 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010531
10532 // finger move
10533 processId(mapper, 1);
10534 processPosition(mapper, x2, y2);
10535 processSync(mapper);
10536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010538 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010539
10540 // finger up.
10541 processId(mapper, -1);
10542 processSync(mapper);
10543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10544 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010546
10547 // new finger down
10548 processId(mapper, 1);
10549 processPosition(mapper, x3, y3);
10550 processSync(mapper);
10551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10552 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010553 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010554}
10555
10556/**
arthurhungcc7f9802020-04-30 17:55:40 +080010557 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10558 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010559 */
arthurhungcc7f9802020-04-30 17:55:40 +080010560TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010561 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010562 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010563 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010564 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010565
10566 NotifyMotionArgs motionArgs;
10567
10568 // default tool type is finger
10569 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010570 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010571 processPosition(mapper, x1, y1);
10572 processSync(mapper);
10573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10574 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010575 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010576
10577 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10578 processToolType(mapper, MT_TOOL_PALM);
10579 processSync(mapper);
10580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10581 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10582
10583 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010584 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010585 processPosition(mapper, x2, y2);
10586 processSync(mapper);
10587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10588
10589 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010590 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010591 processSync(mapper);
10592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10593
10594 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010595 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010596 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010597 processPosition(mapper, x3, y3);
10598 processSync(mapper);
10599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10600 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010601 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010602}
10603
arthurhungbf89a482020-04-17 17:37:55 +080010604/**
arthurhungcc7f9802020-04-30 17:55:40 +080010605 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10606 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010607 */
arthurhungcc7f9802020-04-30 17:55:40 +080010608TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010609 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010610 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010611 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010612 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010613
10614 NotifyMotionArgs motionArgs;
10615
10616 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010617 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10618 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010619 processPosition(mapper, x1, y1);
10620 processSync(mapper);
10621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10622 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010623 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010624
10625 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010626 processSlot(mapper, SECOND_SLOT);
10627 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010628 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010629 processSync(mapper);
10630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010631 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010633
10634 // If the tool type of the first finger changes to MT_TOOL_PALM,
10635 // we expect to receive ACTION_POINTER_UP with cancel flag.
10636 processSlot(mapper, FIRST_SLOT);
10637 processId(mapper, FIRST_TRACKING_ID);
10638 processToolType(mapper, MT_TOOL_PALM);
10639 processSync(mapper);
10640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010641 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010642 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10643
10644 // The following MOVE events of second finger should be processed.
10645 processSlot(mapper, SECOND_SLOT);
10646 processId(mapper, SECOND_TRACKING_ID);
10647 processPosition(mapper, x2 + 1, y2 + 1);
10648 processSync(mapper);
10649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10650 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010651 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010652
10653 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10654 // it. Second finger receive move.
10655 processSlot(mapper, FIRST_SLOT);
10656 processId(mapper, INVALID_TRACKING_ID);
10657 processSync(mapper);
10658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10659 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010660 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010661
10662 // Second finger keeps moving.
10663 processSlot(mapper, SECOND_SLOT);
10664 processId(mapper, SECOND_TRACKING_ID);
10665 processPosition(mapper, x2 + 2, y2 + 2);
10666 processSync(mapper);
10667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010669 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010670
10671 // Second finger up.
10672 processId(mapper, INVALID_TRACKING_ID);
10673 processSync(mapper);
10674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10676 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10677}
10678
10679/**
10680 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10681 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10682 */
10683TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10684 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010685 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010686 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010687 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010688
10689 NotifyMotionArgs motionArgs;
10690
10691 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10692 // First finger down.
10693 processId(mapper, FIRST_TRACKING_ID);
10694 processPosition(mapper, x1, y1);
10695 processSync(mapper);
10696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10697 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010698 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010699
10700 // Second finger down.
10701 processSlot(mapper, SECOND_SLOT);
10702 processId(mapper, SECOND_TRACKING_ID);
10703 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010704 processSync(mapper);
10705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010706 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010707 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010708
arthurhungcc7f9802020-04-30 17:55:40 +080010709 // If the tool type of the first finger changes to MT_TOOL_PALM,
10710 // we expect to receive ACTION_POINTER_UP with cancel flag.
10711 processSlot(mapper, FIRST_SLOT);
10712 processId(mapper, FIRST_TRACKING_ID);
10713 processToolType(mapper, MT_TOOL_PALM);
10714 processSync(mapper);
10715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010716 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010717 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10718
10719 // Second finger keeps moving.
10720 processSlot(mapper, SECOND_SLOT);
10721 processId(mapper, SECOND_TRACKING_ID);
10722 processPosition(mapper, x2 + 1, y2 + 1);
10723 processSync(mapper);
10724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10726
10727 // second finger becomes palm, receive cancel due to only 1 finger is active.
10728 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010729 processToolType(mapper, MT_TOOL_PALM);
10730 processSync(mapper);
10731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10732 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10733
arthurhungcc7f9802020-04-30 17:55:40 +080010734 // third finger down.
10735 processSlot(mapper, THIRD_SLOT);
10736 processId(mapper, THIRD_TRACKING_ID);
10737 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010738 processPosition(mapper, x3, y3);
10739 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10741 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010742 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010743 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010744
10745 // third finger move
10746 processId(mapper, THIRD_TRACKING_ID);
10747 processPosition(mapper, x3 + 1, y3 + 1);
10748 processSync(mapper);
10749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10750 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10751
10752 // first finger up, third finger receive move.
10753 processSlot(mapper, FIRST_SLOT);
10754 processId(mapper, INVALID_TRACKING_ID);
10755 processSync(mapper);
10756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10757 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010758 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010759
10760 // second finger up, third finger receive move.
10761 processSlot(mapper, SECOND_SLOT);
10762 processId(mapper, INVALID_TRACKING_ID);
10763 processSync(mapper);
10764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10765 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010766 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010767
10768 // third finger up.
10769 processSlot(mapper, THIRD_SLOT);
10770 processId(mapper, INVALID_TRACKING_ID);
10771 processSync(mapper);
10772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10773 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10774 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10775}
10776
10777/**
10778 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10779 * and the active finger could still be allowed to receive the events
10780 */
10781TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10782 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010783 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010784 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010785 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010786
10787 NotifyMotionArgs motionArgs;
10788
10789 // default tool type is finger
10790 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10791 processId(mapper, FIRST_TRACKING_ID);
10792 processPosition(mapper, x1, y1);
10793 processSync(mapper);
10794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10795 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010796 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010797
10798 // Second finger down.
10799 processSlot(mapper, SECOND_SLOT);
10800 processId(mapper, SECOND_TRACKING_ID);
10801 processPosition(mapper, x2, y2);
10802 processSync(mapper);
10803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010804 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010806
10807 // If the tool type of the second finger changes to MT_TOOL_PALM,
10808 // we expect to receive ACTION_POINTER_UP with cancel flag.
10809 processId(mapper, SECOND_TRACKING_ID);
10810 processToolType(mapper, MT_TOOL_PALM);
10811 processSync(mapper);
10812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010813 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010814 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10815
10816 // The following MOVE event should be processed.
10817 processSlot(mapper, FIRST_SLOT);
10818 processId(mapper, FIRST_TRACKING_ID);
10819 processPosition(mapper, x1 + 1, y1 + 1);
10820 processSync(mapper);
10821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010823 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010824
10825 // second finger up.
10826 processSlot(mapper, SECOND_SLOT);
10827 processId(mapper, INVALID_TRACKING_ID);
10828 processSync(mapper);
10829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10831
10832 // first finger keep moving
10833 processSlot(mapper, FIRST_SLOT);
10834 processId(mapper, FIRST_TRACKING_ID);
10835 processPosition(mapper, x1 + 2, y1 + 2);
10836 processSync(mapper);
10837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10839
10840 // first finger up.
10841 processId(mapper, INVALID_TRACKING_ID);
10842 processSync(mapper);
10843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10844 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10845 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010846}
10847
Arthur Hung9ad18942021-06-19 02:04:46 +000010848/**
10849 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10850 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10851 * cause slot be valid again.
10852 */
10853TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10854 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010855 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010856 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010857 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010858
10859 NotifyMotionArgs motionArgs;
10860
10861 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10862 // First finger down.
10863 processId(mapper, FIRST_TRACKING_ID);
10864 processPosition(mapper, x1, y1);
10865 processPressure(mapper, RAW_PRESSURE_MAX);
10866 processSync(mapper);
10867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10868 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010869 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010870
10871 // First finger move.
10872 processId(mapper, FIRST_TRACKING_ID);
10873 processPosition(mapper, x1 + 1, y1 + 1);
10874 processPressure(mapper, RAW_PRESSURE_MAX);
10875 processSync(mapper);
10876 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10877 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010878 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010879
10880 // Second finger down.
10881 processSlot(mapper, SECOND_SLOT);
10882 processId(mapper, SECOND_TRACKING_ID);
10883 processPosition(mapper, x2, y2);
10884 processPressure(mapper, RAW_PRESSURE_MAX);
10885 processSync(mapper);
10886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010887 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010888 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010889
10890 // second finger up with some unexpected data.
10891 processSlot(mapper, SECOND_SLOT);
10892 processId(mapper, INVALID_TRACKING_ID);
10893 processPosition(mapper, x2, y2);
10894 processSync(mapper);
10895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010896 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010897 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010898
10899 // first finger up with some unexpected data.
10900 processSlot(mapper, FIRST_SLOT);
10901 processId(mapper, INVALID_TRACKING_ID);
10902 processPosition(mapper, x2, y2);
10903 processPressure(mapper, RAW_PRESSURE_MAX);
10904 processSync(mapper);
10905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10906 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010907 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010908}
10909
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010910TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
10911 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010912 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010913 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010914 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010915
10916 // First finger down.
10917 processId(mapper, FIRST_TRACKING_ID);
10918 processPosition(mapper, 100, 200);
10919 processPressure(mapper, RAW_PRESSURE_MAX);
10920 processSync(mapper);
10921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10922 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10923
10924 // Second finger down.
10925 processSlot(mapper, SECOND_SLOT);
10926 processId(mapper, SECOND_TRACKING_ID);
10927 processPosition(mapper, 300, 400);
10928 processPressure(mapper, RAW_PRESSURE_MAX);
10929 processSync(mapper);
10930 ASSERT_NO_FATAL_FAILURE(
10931 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
10932
10933 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010934 // preserved. Resetting should cancel the ongoing gesture.
10935 resetMapper(mapper, ARBITRARY_TIME);
10936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10937 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010938
10939 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
10940 // the existing touch state to generate a down event.
10941 processPosition(mapper, 301, 302);
10942 processSync(mapper);
10943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
10945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10946 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
10947
10948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10949}
10950
10951TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
10952 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010953 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010954 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010955 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010956
10957 // First finger touches down and releases.
10958 processId(mapper, FIRST_TRACKING_ID);
10959 processPosition(mapper, 100, 200);
10960 processPressure(mapper, RAW_PRESSURE_MAX);
10961 processSync(mapper);
10962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10963 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10964 processId(mapper, INVALID_TRACKING_ID);
10965 processSync(mapper);
10966 ASSERT_NO_FATAL_FAILURE(
10967 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
10968
10969 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
10970 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000010971 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10973
10974 // Send an empty sync frame. Since there are no pointers, no events are generated.
10975 processSync(mapper);
10976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10977}
10978
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010979TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010980 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010981 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010982 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010983 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010985
10986 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
10987 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
10988 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
10989 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
10990 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
10991
10992 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000010993 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000010994 processId(mapper, FIRST_TRACKING_ID);
10995 processToolType(mapper, MT_TOOL_PEN);
10996 processPosition(mapper, 100, 200);
10997 processPressure(mapper, RAW_PRESSURE_MAX);
10998 processSync(mapper);
10999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11000 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11001 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011002 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011003
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011004 // Now that we know the device supports styluses, ensure that the device is re-configured with
11005 // the stylus source.
11006 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
11007 {
11008 const auto& devices = mReader->getInputDevices();
11009 auto deviceInfo =
11010 std::find_if(devices.begin(), devices.end(),
11011 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
11012 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
11013 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
11014 }
11015
11016 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
11017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
11018
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011019 processId(mapper, INVALID_TRACKING_ID);
11020 processSync(mapper);
11021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11022 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11023 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011024 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011025}
11026
Seunghwan Choi356026c2023-02-01 14:37:25 +090011027TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
11028 addConfigurationProperty("touch.deviceType", "touchScreen");
11029 prepareDisplay(ui::ROTATION_0);
11030 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11031 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11032 // indicate stylus presence dynamically.
11033 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11034 std::shared_ptr<FakePointerController> fakePointerController =
11035 std::make_shared<FakePointerController>();
11036 mFakePolicy->setPointerController(fakePointerController);
11037 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000011038 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011039
11040 processId(mapper, FIRST_TRACKING_ID);
11041 processPressure(mapper, RAW_PRESSURE_MIN);
11042 processPosition(mapper, 100, 200);
11043 processToolType(mapper, MT_TOOL_PEN);
11044 processSync(mapper);
11045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11046 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011047 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011048 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11049 ASSERT_TRUE(fakePointerController->isPointerShown());
11050 ASSERT_NO_FATAL_FAILURE(
11051 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
11052}
11053
11054TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
11055 addConfigurationProperty("touch.deviceType", "touchScreen");
11056 prepareDisplay(ui::ROTATION_0);
11057 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11058 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11059 // indicate stylus presence dynamically.
11060 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11061 std::shared_ptr<FakePointerController> fakePointerController =
11062 std::make_shared<FakePointerController>();
11063 mFakePolicy->setPointerController(fakePointerController);
11064 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011065 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011066
11067 processId(mapper, FIRST_TRACKING_ID);
11068 processPressure(mapper, RAW_PRESSURE_MIN);
11069 processPosition(mapper, 100, 200);
11070 processToolType(mapper, MT_TOOL_PEN);
11071 processSync(mapper);
11072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11073 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011074 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011075 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11076 ASSERT_FALSE(fakePointerController->isPointerShown());
11077}
11078
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011079// --- MultiTouchInputMapperTest_ExternalDevice ---
11080
11081class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
11082protected:
Chris Yea52ade12020-08-27 16:49:20 -070011083 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011084};
11085
11086/**
11087 * Expect fallback to internal viewport if device is external and external viewport is not present.
11088 */
11089TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
11090 prepareAxes(POSITION);
11091 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011092 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011093 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011094
11095 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
11096
11097 NotifyMotionArgs motionArgs;
11098
11099 // Expect the event to be sent to the internal viewport,
11100 // because an external viewport is not present.
11101 processPosition(mapper, 100, 100);
11102 processSync(mapper);
11103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11104 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
11105
11106 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010011107 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011108 processPosition(mapper, 100, 100);
11109 processSync(mapper);
11110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11111 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
11112}
Arthur Hung4197f6b2020-03-16 15:39:59 +080011113
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011114TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
11115 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
11116 std::shared_ptr<FakePointerController> fakePointerController =
11117 std::make_shared<FakePointerController>();
11118 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11119 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011120
11121 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011122 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011123 prepareAxes(POSITION | ID | SLOT);
11124 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11125 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
11126 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011127 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011128 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011129
11130 // captured touchpad should be a touchpad source
11131 NotifyDeviceResetArgs resetArgs;
11132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
11133 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11134
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011135 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070011136
11137 const InputDeviceInfo::MotionRange* relRangeX =
11138 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
11139 ASSERT_NE(relRangeX, nullptr);
11140 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
11141 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
11142 const InputDeviceInfo::MotionRange* relRangeY =
11143 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
11144 ASSERT_NE(relRangeY, nullptr);
11145 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
11146 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
11147
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011148 // run captured pointer tests - note that this is unscaled, so input listener events should be
11149 // identical to what the hardware sends (accounting for any
11150 // calibration).
11151 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070011152 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011153 processId(mapper, 1);
11154 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
11155 processKey(mapper, BTN_TOUCH, 1);
11156 processSync(mapper);
11157
11158 // expect coord[0] to contain initial location of touch 0
11159 NotifyMotionArgs args;
11160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11161 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011162 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011163 ASSERT_EQ(0, args.pointerProperties[0].id);
11164 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
11165 ASSERT_NO_FATAL_FAILURE(
11166 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11167
11168 // FINGER 1 DOWN
11169 processSlot(mapper, 1);
11170 processId(mapper, 2);
11171 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
11172 processSync(mapper);
11173
11174 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080011176 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011177 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011178 ASSERT_EQ(0, args.pointerProperties[0].id);
11179 ASSERT_EQ(1, args.pointerProperties[1].id);
11180 ASSERT_NO_FATAL_FAILURE(
11181 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11182 ASSERT_NO_FATAL_FAILURE(
11183 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
11184
11185 // FINGER 1 MOVE
11186 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
11187 processSync(mapper);
11188
11189 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11190 // from move
11191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11192 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11193 ASSERT_NO_FATAL_FAILURE(
11194 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11195 ASSERT_NO_FATAL_FAILURE(
11196 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11197
11198 // FINGER 0 MOVE
11199 processSlot(mapper, 0);
11200 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
11201 processSync(mapper);
11202
11203 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
11204 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11205 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11206 ASSERT_NO_FATAL_FAILURE(
11207 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
11208 ASSERT_NO_FATAL_FAILURE(
11209 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11210
11211 // BUTTON DOWN
11212 processKey(mapper, BTN_LEFT, 1);
11213 processSync(mapper);
11214
11215 // touchinputmapper design sends a move before button press
11216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11217 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11219 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11220
11221 // BUTTON UP
11222 processKey(mapper, BTN_LEFT, 0);
11223 processSync(mapper);
11224
11225 // touchinputmapper design sends a move after button release
11226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11227 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11230
11231 // FINGER 0 UP
11232 processId(mapper, -1);
11233 processSync(mapper);
11234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11235 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
11236
11237 // FINGER 1 MOVE
11238 processSlot(mapper, 1);
11239 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
11240 processSync(mapper);
11241
11242 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
11243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11244 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011245 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011246 ASSERT_EQ(1, args.pointerProperties[0].id);
11247 ASSERT_NO_FATAL_FAILURE(
11248 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
11249
11250 // FINGER 1 UP
11251 processId(mapper, -1);
11252 processKey(mapper, BTN_TOUCH, 0);
11253 processSync(mapper);
11254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11255 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
11256
Josep del Río2d8c79a2023-01-23 19:33:50 +000011257 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011258 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011259 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000011261 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011262}
11263
11264TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
11265 std::shared_ptr<FakePointerController> fakePointerController =
11266 std::make_shared<FakePointerController>();
11267 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11268 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011269
11270 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011271 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011272 prepareAxes(POSITION | ID | SLOT);
11273 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11274 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011275 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011276 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011277 // run uncaptured pointer tests - pushes out generic events
11278 // FINGER 0 DOWN
11279 processId(mapper, 3);
11280 processPosition(mapper, 100, 100);
11281 processKey(mapper, BTN_TOUCH, 1);
11282 processSync(mapper);
11283
11284 // start at (100,100), cursor should be at (0,0) * scale
11285 NotifyMotionArgs args;
11286 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11287 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11288 ASSERT_NO_FATAL_FAILURE(
11289 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
11290
11291 // FINGER 0 MOVE
11292 processPosition(mapper, 200, 200);
11293 processSync(mapper);
11294
11295 // compute scaling to help with touch position checking
11296 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11297 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11298 float scale =
11299 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11300
11301 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
11302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
11305 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080011306
11307 // BUTTON DOWN
11308 processKey(mapper, BTN_LEFT, 1);
11309 processSync(mapper);
11310
11311 // touchinputmapper design sends a move before button press
11312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11313 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
11314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11315 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11316
11317 // BUTTON UP
11318 processKey(mapper, BTN_LEFT, 0);
11319 processSync(mapper);
11320
11321 // touchinputmapper design sends a move after button release
11322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11323 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11325 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011326}
11327
11328TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
11329 std::shared_ptr<FakePointerController> fakePointerController =
11330 std::make_shared<FakePointerController>();
11331
Michael Wrighta9cf4192022-12-01 23:46:39 +000011332 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011333 prepareAxes(POSITION | ID | SLOT);
11334 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011335 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011336 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011337 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011338
Josep del Río2d8c79a2023-01-23 19:33:50 +000011339 // uncaptured touchpad should be a pointer device
11340 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011341
Josep del Río2d8c79a2023-01-23 19:33:50 +000011342 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011343 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011344 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011345 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11346}
11347
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011348// --- BluetoothMultiTouchInputMapperTest ---
11349
11350class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
11351protected:
11352 void SetUp() override {
11353 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
11354 }
11355};
11356
11357TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
11358 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011359 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011360 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000011361 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011362
11363 nsecs_t kernelEventTime = ARBITRARY_TIME;
11364 nsecs_t expectedEventTime = ARBITRARY_TIME;
11365 // Touch down.
11366 processId(mapper, FIRST_TRACKING_ID);
11367 processPosition(mapper, 100, 200);
11368 processPressure(mapper, RAW_PRESSURE_MAX);
11369 processSync(mapper, ARBITRARY_TIME);
11370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11371 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
11372
11373 // Process several events that come in quick succession, according to their timestamps.
11374 for (int i = 0; i < 3; i++) {
11375 constexpr static nsecs_t delta = ms2ns(1);
11376 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
11377 kernelEventTime += delta;
11378 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
11379
11380 processPosition(mapper, 101 + i, 201 + i);
11381 processSync(mapper, kernelEventTime);
11382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11384 WithEventTime(expectedEventTime))));
11385 }
11386
11387 // Release the touch.
11388 processId(mapper, INVALID_TRACKING_ID);
11389 processPressure(mapper, RAW_PRESSURE_MIN);
11390 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
11391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11392 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11393 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
11394}
11395
11396// --- MultiTouchPointerModeTest ---
11397
HQ Liue6983c72022-04-19 22:14:56 +000011398class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
11399protected:
11400 float mPointerMovementScale;
11401 float mPointerXZoomScale;
11402 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
11403 addConfigurationProperty("touch.deviceType", "pointer");
11404 std::shared_ptr<FakePointerController> fakePointerController =
11405 std::make_shared<FakePointerController>();
11406 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11407 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000011408 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000011409
11410 prepareAxes(POSITION);
11411 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
11412 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
11413 // needs to be disabled, and the pointer gesture needs to be enabled.
11414 mFakePolicy->setPointerCapture(false);
11415 mFakePolicy->setPointerGestureEnabled(true);
11416 mFakePolicy->setPointerController(fakePointerController);
11417
11418 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11419 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11420 mPointerMovementScale =
11421 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11422 mPointerXZoomScale =
11423 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
11424 }
11425
11426 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
11427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
11428 /*flat*/ 0,
11429 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
11430 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
11431 /*flat*/ 0,
11432 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
11433 }
11434};
11435
11436/**
11437 * Two fingers down on a pointer mode touch pad. The width
11438 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
11439 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
11440 * be greater than the both value to be freeform gesture, so that after two
11441 * fingers start to move downwards, the gesture should be swipe.
11442 */
11443TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
11444 // The min freeform gesture width is 25units/mm x 30mm = 750
11445 // which is greater than fraction of the diagnal length of the touchpad (349).
11446 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000011447 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011448 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011449 NotifyMotionArgs motionArgs;
11450
11451 // Two fingers down at once.
11452 // The two fingers are 450 units apart, expects the current gesture to be PRESS
11453 // Pointer's initial position is used the [0,0] coordinate.
11454 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11455
11456 processId(mapper, FIRST_TRACKING_ID);
11457 processPosition(mapper, x1, y1);
11458 processMTSync(mapper);
11459 processId(mapper, SECOND_TRACKING_ID);
11460 processPosition(mapper, x2, y2);
11461 processMTSync(mapper);
11462 processSync(mapper);
11463
11464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011465 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011466 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011467 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011468 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011469 ASSERT_NO_FATAL_FAILURE(
11470 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11471
11472 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11473 // that there should be 1 pointer.
11474 int32_t movingDistance = 200;
11475 y1 += movingDistance;
11476 y2 += movingDistance;
11477
11478 processId(mapper, FIRST_TRACKING_ID);
11479 processPosition(mapper, x1, y1);
11480 processMTSync(mapper);
11481 processId(mapper, SECOND_TRACKING_ID);
11482 processPosition(mapper, x2, y2);
11483 processMTSync(mapper);
11484 processSync(mapper);
11485
11486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011487 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011488 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011489 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011490 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011491 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11492 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11493 0, 0, 0, 0));
11494}
11495
11496/**
11497 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11498 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11499 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11500 * value to be freeform gesture, so that after two fingers start to move downwards,
11501 * the gesture should be swipe.
11502 */
11503TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11504 // The min freeform gesture width is 5units/mm x 30mm = 150
11505 // which is greater than fraction of the diagnal length of the touchpad (349).
11506 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000011507 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000011508 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011509 NotifyMotionArgs motionArgs;
11510
11511 // Two fingers down at once.
11512 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11513 // Pointer's initial position is used the [0,0] coordinate.
11514 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11515
11516 processId(mapper, FIRST_TRACKING_ID);
11517 processPosition(mapper, x1, y1);
11518 processMTSync(mapper);
11519 processId(mapper, SECOND_TRACKING_ID);
11520 processPosition(mapper, x2, y2);
11521 processMTSync(mapper);
11522 processSync(mapper);
11523
11524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011525 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011526 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011527 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011528 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011529 ASSERT_NO_FATAL_FAILURE(
11530 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11531
11532 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11533 // and there should be 1 pointer.
11534 int32_t movingDistance = 200;
11535 y1 += movingDistance;
11536 y2 += movingDistance;
11537
11538 processId(mapper, FIRST_TRACKING_ID);
11539 processPosition(mapper, x1, y1);
11540 processMTSync(mapper);
11541 processId(mapper, SECOND_TRACKING_ID);
11542 processPosition(mapper, x2, y2);
11543 processMTSync(mapper);
11544 processSync(mapper);
11545
11546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011547 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011549 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011550 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011551 // New coordinate is the scaled relative coordinate from the initial coordinate.
11552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11553 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11554 0, 0, 0, 0));
11555}
11556
11557/**
11558 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11559 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11560 * freeform gestures after two fingers start to move downwards.
11561 */
11562TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011563 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011564 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011565
11566 NotifyMotionArgs motionArgs;
11567
11568 // Two fingers down at once. Wider than the max swipe width.
11569 // The gesture is expected to be PRESS, then transformed to FREEFORM
11570 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11571
11572 processId(mapper, FIRST_TRACKING_ID);
11573 processPosition(mapper, x1, y1);
11574 processMTSync(mapper);
11575 processId(mapper, SECOND_TRACKING_ID);
11576 processPosition(mapper, x2, y2);
11577 processMTSync(mapper);
11578 processSync(mapper);
11579
11580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011581 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011582 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011583 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011584 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011585 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11586 ASSERT_NO_FATAL_FAILURE(
11587 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11588
11589 int32_t movingDistance = 200;
11590
11591 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11592 // then two down events for two pointers.
11593 y1 += movingDistance;
11594 y2 += movingDistance;
11595
11596 processId(mapper, FIRST_TRACKING_ID);
11597 processPosition(mapper, x1, y1);
11598 processMTSync(mapper);
11599 processId(mapper, SECOND_TRACKING_ID);
11600 processPosition(mapper, x2, y2);
11601 processMTSync(mapper);
11602 processSync(mapper);
11603
11604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11605 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011606 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011607 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011609 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011610 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011611 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011613 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011614 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011615 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011616 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011618 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011619 // Two pointers' scaled relative coordinates from their initial centroid.
11620 // Initial y coordinates are 0 as y1 and y2 have the same value.
11621 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11622 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11623 // When pointers move, the new coordinates equal to the initial coordinates plus
11624 // scaled moving distance.
11625 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11626 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11627 0, 0, 0, 0));
11628 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11629 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11630 0, 0, 0, 0));
11631
11632 // Move two fingers down again, expect one MOVE motion event.
11633 y1 += movingDistance;
11634 y2 += movingDistance;
11635
11636 processId(mapper, FIRST_TRACKING_ID);
11637 processPosition(mapper, x1, y1);
11638 processMTSync(mapper);
11639 processId(mapper, SECOND_TRACKING_ID);
11640 processPosition(mapper, x2, y2);
11641 processMTSync(mapper);
11642 processSync(mapper);
11643
11644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011645 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011647 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011648 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011649 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11650 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11651 0, 0, 0, 0, 0));
11652 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11653 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11654 0, 0, 0, 0, 0));
11655}
11656
Harry Cutts39b7ca22022-10-05 15:55:48 +000011657TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011658 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011659 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011660 NotifyMotionArgs motionArgs;
11661
11662 // Place two fingers down.
11663 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11664
11665 processId(mapper, FIRST_TRACKING_ID);
11666 processPosition(mapper, x1, y1);
11667 processMTSync(mapper);
11668 processId(mapper, SECOND_TRACKING_ID);
11669 processPosition(mapper, x2, y2);
11670 processMTSync(mapper);
11671 processSync(mapper);
11672
11673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011674 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011675 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11676 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11677 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11678 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11679
11680 // Move the two fingers down and to the left.
11681 int32_t movingDistance = 200;
11682 x1 -= movingDistance;
11683 y1 += movingDistance;
11684 x2 -= movingDistance;
11685 y2 += movingDistance;
11686
11687 processId(mapper, FIRST_TRACKING_ID);
11688 processPosition(mapper, x1, y1);
11689 processMTSync(mapper);
11690 processId(mapper, SECOND_TRACKING_ID);
11691 processPosition(mapper, x2, y2);
11692 processMTSync(mapper);
11693 processSync(mapper);
11694
11695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011696 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11698 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11699 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11700 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11701}
11702
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011703TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011704 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011705 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011706 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11708
11709 // Start a stylus gesture.
11710 processKey(mapper, BTN_TOOL_PEN, 1);
11711 processId(mapper, FIRST_TRACKING_ID);
11712 processPosition(mapper, 100, 200);
11713 processSync(mapper);
11714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11715 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11716 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011717 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011718 // TODO(b/257078296): Pointer mode generates extra event.
11719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11720 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11721 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011722 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11724
11725 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11726 // gesture should be disabled.
11727 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11728 viewport->isActive = false;
11729 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011730 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11732 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11733 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011734 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011735 // TODO(b/257078296): Pointer mode generates extra event.
11736 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11737 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11738 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011739 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11741}
11742
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011743// --- JoystickInputMapperTest ---
11744
11745class JoystickInputMapperTest : public InputMapperTest {
11746protected:
11747 static const int32_t RAW_X_MIN;
11748 static const int32_t RAW_X_MAX;
11749 static const int32_t RAW_Y_MIN;
11750 static const int32_t RAW_Y_MAX;
11751
11752 void SetUp() override {
11753 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11754 }
11755 void prepareAxes() {
11756 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11757 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11758 }
11759
11760 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11761 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11762 }
11763
11764 void processSync(JoystickInputMapper& mapper) {
11765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11766 }
11767
Michael Wrighta9cf4192022-12-01 23:46:39 +000011768 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011769 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11770 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11771 NO_PORT, ViewportType::VIRTUAL);
11772 }
11773};
11774
11775const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11776const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11777const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11778const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11779
11780TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11781 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011782 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011783
11784 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11785
Michael Wrighta9cf4192022-12-01 23:46:39 +000011786 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011787
11788 // Send an axis event
11789 processAxis(mapper, ABS_X, 100);
11790 processSync(mapper);
11791
11792 NotifyMotionArgs args;
11793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11794 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11795
11796 // Send another axis event
11797 processAxis(mapper, ABS_Y, 100);
11798 processSync(mapper);
11799
11800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11801 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11802}
11803
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011804// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011805
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011806class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011807protected:
11808 static const char* DEVICE_NAME;
11809 static const char* DEVICE_LOCATION;
11810 static const int32_t DEVICE_ID;
11811 static const int32_t DEVICE_GENERATION;
11812 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011813 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011814 static const int32_t EVENTHUB_ID;
11815
11816 std::shared_ptr<FakeEventHub> mFakeEventHub;
11817 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011818 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011819 std::unique_ptr<InstrumentedInputReader> mReader;
11820 std::shared_ptr<InputDevice> mDevice;
11821
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011822 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011823 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011824 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011825 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011826 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011827 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011828 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11829 }
11830
11831 void SetUp() override { SetUp(DEVICE_CLASSES); }
11832
11833 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011834 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011835 mFakePolicy.clear();
11836 }
11837
Chris Yee2b1e5c2021-03-10 22:45:12 -080011838 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11839 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011840 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011841 InputDeviceIdentifier identifier;
11842 identifier.name = name;
11843 identifier.location = location;
11844 std::shared_ptr<InputDevice> device =
11845 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11846 identifier);
11847 mReader->pushNextDevice(device);
11848 mFakeEventHub->addDevice(eventHubId, name, classes);
11849 mReader->loopOnce();
11850 return device;
11851 }
11852
11853 template <class T, typename... Args>
11854 T& addControllerAndConfigure(Args... args) {
11855 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11856
11857 return controller;
11858 }
11859};
11860
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011861const char* PeripheralControllerTest::DEVICE_NAME = "device";
11862const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11863const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11864const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11865const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011866const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11867 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011868const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011869
11870// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011871class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011872protected:
11873 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011874 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011875 }
11876};
11877
11878TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011879 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011880
Harry Cuttsa5b71292022-11-28 12:56:17 +000011881 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11882 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11883 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011884}
11885
11886TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011887 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011888
Harry Cuttsa5b71292022-11-28 12:56:17 +000011889 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11890 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11891 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011892}
11893
11894// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011895class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011896protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011897 void SetUp() override {
11898 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11899 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011900};
11901
Chris Ye85758332021-05-16 23:05:17 -070011902TEST_F(LightControllerTest, MonoLight) {
11903 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011904 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011905 .maxBrightness = 255,
11906 .flags = InputLightClass::BRIGHTNESS,
11907 .path = ""};
11908 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011909
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011910 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011911 InputDeviceInfo info;
11912 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011913 std::vector<InputDeviceLightInfo> lights = info.getLights();
11914 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011915 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11916 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11917
11918 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11919 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11920}
11921
11922TEST_F(LightControllerTest, MonoKeyboardBacklight) {
11923 RawLightInfo infoMono = {.id = 1,
11924 .name = "mono_keyboard_backlight",
11925 .maxBrightness = 255,
11926 .flags = InputLightClass::BRIGHTNESS |
11927 InputLightClass::KEYBOARD_BACKLIGHT,
11928 .path = ""};
11929 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11930
11931 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11932 InputDeviceInfo info;
11933 controller.populateDeviceInfo(&info);
11934 std::vector<InputDeviceLightInfo> lights = info.getLights();
11935 ASSERT_EQ(1U, lights.size());
11936 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
11937 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011938
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011939 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11940 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011941}
11942
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000011943TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
11944 RawLightInfo infoMono = {.id = 1,
11945 .name = "mono_light",
11946 .maxBrightness = 255,
11947 .flags = InputLightClass::BRIGHTNESS,
11948 .path = ""};
11949 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11950 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11951 "0,100,200");
11952
11953 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11954 std::list<NotifyArgs> unused =
11955 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11956 /*changes=*/{});
11957
11958 InputDeviceInfo info;
11959 controller.populateDeviceInfo(&info);
11960 std::vector<InputDeviceLightInfo> lights = info.getLights();
11961 ASSERT_EQ(1U, lights.size());
11962 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11963}
11964
11965TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
11966 RawLightInfo infoMono = {.id = 1,
11967 .name = "mono_keyboard_backlight",
11968 .maxBrightness = 255,
11969 .flags = InputLightClass::BRIGHTNESS |
11970 InputLightClass::KEYBOARD_BACKLIGHT,
11971 .path = ""};
11972 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11973
11974 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11975 std::list<NotifyArgs> unused =
11976 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
11977 /*changes=*/{});
11978
11979 InputDeviceInfo info;
11980 controller.populateDeviceInfo(&info);
11981 std::vector<InputDeviceLightInfo> lights = info.getLights();
11982 ASSERT_EQ(1U, lights.size());
11983 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
11984}
11985
11986TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
11987 RawLightInfo infoMono = {.id = 1,
11988 .name = "mono_keyboard_backlight",
11989 .maxBrightness = 255,
11990 .flags = InputLightClass::BRIGHTNESS |
11991 InputLightClass::KEYBOARD_BACKLIGHT,
11992 .path = ""};
11993 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
11994 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
11995 "0,100,200");
11996
11997 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
11998 std::list<NotifyArgs> unused =
11999 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12000 /*changes=*/{});
12001
12002 InputDeviceInfo info;
12003 controller.populateDeviceInfo(&info);
12004 std::vector<InputDeviceLightInfo> lights = info.getLights();
12005 ASSERT_EQ(1U, lights.size());
12006 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
12007 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
12008 ASSERT_EQ(BrightnessLevel(0), *it);
12009 std::advance(it, 1);
12010 ASSERT_EQ(BrightnessLevel(100), *it);
12011 std::advance(it, 1);
12012 ASSERT_EQ(BrightnessLevel(200), *it);
12013}
12014
12015TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
12016 RawLightInfo infoMono = {.id = 1,
12017 .name = "mono_keyboard_backlight",
12018 .maxBrightness = 255,
12019 .flags = InputLightClass::BRIGHTNESS |
12020 InputLightClass::KEYBOARD_BACKLIGHT,
12021 .path = ""};
12022 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12023 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
12024 "0,100,200,300,400,500");
12025
12026 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12027 std::list<NotifyArgs> unused =
12028 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12029 /*changes=*/{});
12030
12031 InputDeviceInfo info;
12032 controller.populateDeviceInfo(&info);
12033 std::vector<InputDeviceLightInfo> lights = info.getLights();
12034 ASSERT_EQ(1U, lights.size());
12035 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
12036}
12037
Chris Yee2b1e5c2021-03-10 22:45:12 -080012038TEST_F(LightControllerTest, RGBLight) {
12039 RawLightInfo infoRed = {.id = 1,
12040 .name = "red",
12041 .maxBrightness = 255,
12042 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12043 .path = ""};
12044 RawLightInfo infoGreen = {.id = 2,
12045 .name = "green",
12046 .maxBrightness = 255,
12047 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12048 .path = ""};
12049 RawLightInfo infoBlue = {.id = 3,
12050 .name = "blue",
12051 .maxBrightness = 255,
12052 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12053 .path = ""};
12054 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12055 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12056 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12057
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012058 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012059 InputDeviceInfo info;
12060 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012061 std::vector<InputDeviceLightInfo> lights = info.getLights();
12062 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012063 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12064 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12065 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12066
12067 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12068 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12069}
12070
12071TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
12072 RawLightInfo infoRed = {.id = 1,
12073 .name = "red_keyboard_backlight",
12074 .maxBrightness = 255,
12075 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
12076 InputLightClass::KEYBOARD_BACKLIGHT,
12077 .path = ""};
12078 RawLightInfo infoGreen = {.id = 2,
12079 .name = "green_keyboard_backlight",
12080 .maxBrightness = 255,
12081 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
12082 InputLightClass::KEYBOARD_BACKLIGHT,
12083 .path = ""};
12084 RawLightInfo infoBlue = {.id = 3,
12085 .name = "blue_keyboard_backlight",
12086 .maxBrightness = 255,
12087 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
12088 InputLightClass::KEYBOARD_BACKLIGHT,
12089 .path = ""};
12090 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12091 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12092 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12093
12094 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12095 InputDeviceInfo info;
12096 controller.populateDeviceInfo(&info);
12097 std::vector<InputDeviceLightInfo> lights = info.getLights();
12098 ASSERT_EQ(1U, lights.size());
12099 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12100 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12101 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12102
12103 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12104 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12105}
12106
12107TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
12108 RawLightInfo infoRed = {.id = 1,
12109 .name = "red",
12110 .maxBrightness = 255,
12111 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12112 .path = ""};
12113 RawLightInfo infoGreen = {.id = 2,
12114 .name = "green",
12115 .maxBrightness = 255,
12116 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12117 .path = ""};
12118 RawLightInfo infoBlue = {.id = 3,
12119 .name = "blue",
12120 .maxBrightness = 255,
12121 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12122 .path = ""};
12123 RawLightInfo infoGlobal = {.id = 3,
12124 .name = "global_keyboard_backlight",
12125 .maxBrightness = 255,
12126 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
12127 InputLightClass::KEYBOARD_BACKLIGHT,
12128 .path = ""};
12129 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12130 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12131 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12132 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
12133
12134 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12135 InputDeviceInfo info;
12136 controller.populateDeviceInfo(&info);
12137 std::vector<InputDeviceLightInfo> lights = info.getLights();
12138 ASSERT_EQ(1U, lights.size());
12139 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12140 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12141 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012142
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012143 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12144 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012145}
12146
12147TEST_F(LightControllerTest, MultiColorRGBLight) {
12148 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012149 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080012150 .maxBrightness = 255,
12151 .flags = InputLightClass::BRIGHTNESS |
12152 InputLightClass::MULTI_INTENSITY |
12153 InputLightClass::MULTI_INDEX,
12154 .path = ""};
12155
12156 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12157
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012158 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012159 InputDeviceInfo info;
12160 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012161 std::vector<InputDeviceLightInfo> lights = info.getLights();
12162 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012163 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12164 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12165 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12166
12167 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12168 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12169}
12170
12171TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
12172 RawLightInfo infoColor = {.id = 1,
12173 .name = "multi_color_keyboard_backlight",
12174 .maxBrightness = 255,
12175 .flags = InputLightClass::BRIGHTNESS |
12176 InputLightClass::MULTI_INTENSITY |
12177 InputLightClass::MULTI_INDEX |
12178 InputLightClass::KEYBOARD_BACKLIGHT,
12179 .path = ""};
12180
12181 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12182
12183 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12184 InputDeviceInfo info;
12185 controller.populateDeviceInfo(&info);
12186 std::vector<InputDeviceLightInfo> lights = info.getLights();
12187 ASSERT_EQ(1U, lights.size());
12188 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12189 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12190 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012191
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012192 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12193 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012194}
12195
12196TEST_F(LightControllerTest, PlayerIdLight) {
12197 RawLightInfo info1 = {.id = 1,
12198 .name = "player1",
12199 .maxBrightness = 255,
12200 .flags = InputLightClass::BRIGHTNESS,
12201 .path = ""};
12202 RawLightInfo info2 = {.id = 2,
12203 .name = "player2",
12204 .maxBrightness = 255,
12205 .flags = InputLightClass::BRIGHTNESS,
12206 .path = ""};
12207 RawLightInfo info3 = {.id = 3,
12208 .name = "player3",
12209 .maxBrightness = 255,
12210 .flags = InputLightClass::BRIGHTNESS,
12211 .path = ""};
12212 RawLightInfo info4 = {.id = 4,
12213 .name = "player4",
12214 .maxBrightness = 255,
12215 .flags = InputLightClass::BRIGHTNESS,
12216 .path = ""};
12217 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
12218 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
12219 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
12220 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
12221
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012222 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012223 InputDeviceInfo info;
12224 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012225 std::vector<InputDeviceLightInfo> lights = info.getLights();
12226 ASSERT_EQ(1U, lights.size());
12227 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012228 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12229 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012230
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012231 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12232 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
12233 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012234}
12235
Michael Wrightd02c5b62014-02-10 15:10:22 -080012236} // namespace android