blob: 92489aec8d81761de8f5bc7b64721daf02290e46 [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 Cuttse6512e12022-11-28 18:44:01 +000049#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000050#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000051#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000052#include "input/DisplayViewport.h"
53#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010054
Michael Wrightd02c5b62014-02-10 15:10:22 -080055namespace android {
56
Dominik Laskowski2f01d772022-03-23 16:01:29 -070057using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000058using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080060using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070061
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);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800152 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
361 virtual std::optional<int32_t> getAssociatedDisplay() {
362 if (mViewport) {
363 return std::make_optional(mViewport->displayId);
364 }
365 return std::nullopt;
366 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800367};
368
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700369// --- InputReaderPolicyTest ---
370class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700371protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700372 sp<FakeInputReaderPolicy> mFakePolicy;
373
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700374 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700375 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700376};
377
378/**
379 * Check that empty set of viewports is an acceptable configuration.
380 * Also try to get internal viewport two different ways - by type and by uniqueId.
381 *
382 * There will be confusion if two viewports with empty uniqueId and identical type are present.
383 * Such configuration is not currently allowed.
384 */
385TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700386 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700387
388 // We didn't add any viewports yet, so there shouldn't be any.
389 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100390 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700391 ASSERT_FALSE(internalViewport);
392
393 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000394 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000395 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700396
397 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700398 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100400 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401
402 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100403 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700405 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406
407 mFakePolicy->clearViewports();
408 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700409 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100411 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
413}
414
415TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
416 const std::string internalUniqueId = "local:0";
417 const std::string externalUniqueId = "local:1";
418 const std::string virtualUniqueId1 = "virtual:2";
419 const std::string virtualUniqueId2 = "virtual:3";
420 constexpr int32_t virtualDisplayId1 = 2;
421 constexpr int32_t virtualDisplayId2 = 3;
422
423 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000425 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700427 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000429 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700431 // Add an virtual viewport
432 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000433 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000434 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700435 // Add another virtual viewport
436 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000437 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000438 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700439
440 // Check matching by type for internal
441 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100442 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700443 ASSERT_TRUE(internalViewport);
444 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
445
446 // Check matching by type for external
447 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100448 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700449 ASSERT_TRUE(externalViewport);
450 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
451
452 // Check matching by uniqueId for virtual viewport #1
453 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700454 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100456 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
458 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
459
460 // Check matching by uniqueId for virtual viewport #2
461 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700462 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100464 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
466 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
467}
468
469
470/**
471 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
472 * that lookup works by checking display id.
473 * Check that 2 viewports of each kind is possible, for all existing viewport types.
474 */
475TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
476 const std::string uniqueId1 = "uniqueId1";
477 const std::string uniqueId2 = "uniqueId2";
478 constexpr int32_t displayId1 = 2;
479 constexpr int32_t displayId2 = 3;
480
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100481 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
482 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700483 for (const ViewportType& type : types) {
484 mFakePolicy->clearViewports();
485 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000486 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000487 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700488 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000489 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000490 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700491
492 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700493 std::optional<DisplayViewport> viewport1 =
494 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700495 ASSERT_TRUE(viewport1);
496 ASSERT_EQ(displayId1, viewport1->displayId);
497 ASSERT_EQ(type, viewport1->type);
498
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700499 std::optional<DisplayViewport> viewport2 =
500 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700501 ASSERT_TRUE(viewport2);
502 ASSERT_EQ(displayId2, viewport2->displayId);
503 ASSERT_EQ(type, viewport2->type);
504
505 // When there are multiple viewports of the same kind, and uniqueId is not specified
506 // in the call to getDisplayViewport, then that situation is not supported.
507 // The viewports can be stored in any order, so we cannot rely on the order, since that
508 // is just implementation detail.
509 // However, we can check that it still returns *a* viewport, we just cannot assert
510 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700511 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700512 ASSERT_TRUE(someViewport);
513 }
514}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800515
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700516/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000517 * When we have multiple internal displays make sure we always return the default display when
518 * querying by type.
519 */
520TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
521 const std::string uniqueId1 = "uniqueId1";
522 const std::string uniqueId2 = "uniqueId2";
523 constexpr int32_t nonDefaultDisplayId = 2;
524 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
525 "Test display ID should not be ADISPLAY_ID_DEFAULT");
526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
529 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000530 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000531 ViewportType::INTERNAL);
532 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000533 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000534 ViewportType::INTERNAL);
535
536 std::optional<DisplayViewport> viewport =
537 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
538 ASSERT_TRUE(viewport);
539 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
540 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
541
542 // Add the default display second to make sure order doesn't matter.
543 mFakePolicy->clearViewports();
544 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000545 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000546 ViewportType::INTERNAL);
547 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000548 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000549 ViewportType::INTERNAL);
550
551 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
552 ASSERT_TRUE(viewport);
553 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
554 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
555}
556
557/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700558 * Check getDisplayViewportByPort
559 */
560TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100561 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700562 const std::string uniqueId1 = "uniqueId1";
563 const std::string uniqueId2 = "uniqueId2";
564 constexpr int32_t displayId1 = 1;
565 constexpr int32_t displayId2 = 2;
566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Hiroki Sato25040232024-02-22 17:21:22 +09001167 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001171 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
Hiroki Sato25040232024-02-22 17:21:22 +09001174 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001178 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Chris Ye87143712020-11-10 05:05:58 +00001187class FakeVibratorInputMapper : public FakeInputMapper {
1188public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001189 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1190 const InputReaderConfiguration& readerConfig, uint32_t sources)
1191 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001192
1193 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1194};
1195
1196TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1203 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001204 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1205 mFakePolicy->getReaderConfiguration(),
1206 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001207 mReader->pushNextDevice(device);
1208
1209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1210 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1211
1212 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1213 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1214}
1215
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001216// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001217
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001219public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001220 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001222 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223
Andy Chenf9f1a022022-08-29 20:07:10 -04001224 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1225
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1227
1228 void dump(std::string& dump) override {}
1229
1230 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1231 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001232 }
1233
Chris Yee2b1e5c2021-03-10 22:45:12 -08001234 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1235 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001236 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237
1238 bool setLightColor(int32_t lightId, int32_t color) override {
1239 getDeviceContext().setLightBrightness(lightId, color >> 24);
1240 return true;
1241 }
1242
1243 std::optional<int32_t> getLightColor(int32_t lightId) override {
1244 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1245 if (!result.has_value()) {
1246 return std::nullopt;
1247 }
1248 return result.value() << 24;
1249 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001250
1251 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1252
1253 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1254
1255private:
1256 InputDeviceContext& mDeviceContext;
1257 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1258 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001259 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260};
1261
Chris Yee2b1e5c2021-03-10 22:45:12 -08001262TEST_F(InputReaderTest, BatteryGetCapacity) {
1263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001264 ftl::Flags<InputDeviceClass> deviceClass =
1265 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 constexpr int32_t eventHubId = 1;
1267 const char* DEVICE_LOCATION = "BLUETOOTH";
1268 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001269 FakePeripheralController& controller =
1270 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001271 mReader->pushNextDevice(device);
1272
1273 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1274
Harry Cuttsa5b71292022-11-28 12:56:17 +00001275 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1276 FakeEventHub::BATTERY_CAPACITY);
1277 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278}
1279
1280TEST_F(InputReaderTest, BatteryGetStatus) {
1281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001282 ftl::Flags<InputDeviceClass> deviceClass =
1283 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 constexpr int32_t eventHubId = 1;
1285 const char* DEVICE_LOCATION = "BLUETOOTH";
1286 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001287 FakePeripheralController& controller =
1288 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001289 mReader->pushNextDevice(device);
1290
1291 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1292
Harry Cuttsa5b71292022-11-28 12:56:17 +00001293 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1294 FakeEventHub::BATTERY_STATUS);
1295 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001296}
1297
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001298TEST_F(InputReaderTest, BatteryGetDevicePath) {
1299 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1300 ftl::Flags<InputDeviceClass> deviceClass =
1301 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1305 device->addController<FakePeripheralController>(eventHubId);
1306 mReader->pushNextDevice(device);
1307
1308 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1309
Harry Cuttsa5b71292022-11-28 12:56:17 +00001310 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001311}
1312
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313TEST_F(InputReaderTest, LightGetColor) {
1314 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001315 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001316 constexpr int32_t eventHubId = 1;
1317 const char* DEVICE_LOCATION = "BLUETOOTH";
1318 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001319 FakePeripheralController& controller =
1320 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001321 mReader->pushNextDevice(device);
1322 RawLightInfo info = {.id = 1,
1323 .name = "Mono",
1324 .maxBrightness = 255,
1325 .flags = InputLightClass::BRIGHTNESS,
1326 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001327 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1328 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001329
1330 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001331
Harry Cutts33476232023-01-30 19:57:29 +00001332 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1333 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1334 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1335 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336}
1337
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001338// --- InputReaderIntegrationTest ---
1339
1340// These tests create and interact with the InputReader only through its interface.
1341// The InputReader is started during SetUp(), which starts its processing in its own
1342// thread. The tests use linux uinput to emulate input devices.
1343// NOTE: Interacting with the physical device while these tests are running may cause
1344// the tests to fail.
1345class InputReaderIntegrationTest : public testing::Test {
1346protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001347 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001351 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1352 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1353
Chris Yea52ade12020-08-27 16:49:20 -07001354 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001355#if !defined(__ANDROID__)
1356 GTEST_SKIP();
1357#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001358 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359
Arpit Singh440bf652023-08-09 09:23:43 +00001360 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001361 }
1362
Chris Yea52ade12020-08-27 16:49:20 -07001363 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001364#if !defined(__ANDROID__)
1365 return;
1366#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001367 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001368 mReader.reset();
1369 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370 mFakePolicy.clear();
1371 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001372
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001373 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1374 std::chrono::time_point start = std::chrono::steady_clock::now();
1375 while (true) {
1376 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1377 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1378 [&deviceName](const InputDeviceInfo& info) {
1379 return info.getIdentifier().name == deviceName;
1380 });
1381 if (it != inputDevices.end()) {
1382 return std::make_optional(*it);
1383 }
1384 std::this_thread::sleep_for(1ms);
1385 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1386 if (elapsed > 5s) {
1387 return {};
1388 }
1389 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001390 }
Arpit Singh440bf652023-08-09 09:23:43 +00001391
1392 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001393 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1394 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001395
1396 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1397 *mTestListener);
1398 ASSERT_EQ(mReader->start(), OK);
1399
1400 // Since this test is run on a real device, all the input devices connected
1401 // to the test device will show up in mReader. We wait for those input devices to
1402 // show up before beginning the tests.
1403 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1405 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1406 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001407};
1408
1409TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1410 // An invalid input device that is only used for this test.
1411 class InvalidUinputDevice : public UinputDevice {
1412 public:
Harry Cutts33476232023-01-30 19:57:29 +00001413 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001414
1415 private:
1416 void configureDevice(int fd, uinput_user_dev* device) override {}
1417 };
1418
1419 const size_t numDevices = mFakePolicy->getInputDevices().size();
1420
1421 // UinputDevice does not set any event or key bits, so InputReader should not
1422 // consider it as a valid device.
1423 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1424 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1426 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1427
1428 invalidDevice.reset();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1431 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1432}
1433
1434TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1435 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1436
1437 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1438 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1440 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1441
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001442 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001443 ASSERT_TRUE(device.has_value());
1444 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1446 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447
1448 keyboard.reset();
1449 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1451 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1452}
1453
1454TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1455 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1456 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1457
1458 NotifyConfigurationChangedArgs configChangedArgs;
1459 ASSERT_NO_FATAL_FAILURE(
1460 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001461 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001462 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1463
1464 NotifyKeyArgs keyArgs;
1465 keyboard->pressAndReleaseHomeKey();
1466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1467 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001468 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001469 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001470 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001471 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001472 prevTimestamp = keyArgs.eventTime;
1473
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1475 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001476 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001477 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001478 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001479}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001480
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001481TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1482 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1483 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1484
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001485 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001486 ASSERT_TRUE(device.has_value());
1487
Prabir Pradhana3621852022-10-14 18:57:23 +00001488 // An external stylus with buttons should also be recognized as a keyboard.
1489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001490 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1491 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1492
1493 const auto DOWN =
1494 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1495 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1496
1497 stylus->pressAndReleaseKey(BTN_STYLUS);
1498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1502
1503 stylus->pressAndReleaseKey(BTN_STYLUS2);
1504 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1505 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1507 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1508
1509 stylus->pressAndReleaseKey(BTN_STYLUS3);
1510 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1511 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1512 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1513 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1514}
1515
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001516TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1517 std::unique_ptr<UinputKeyboard> keyboard =
1518 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1519 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1520 KEY_R, KEY_T, KEY_Y,
1521 BTN_STYLUS, BTN_STYLUS2,
1522 BTN_STYLUS3});
1523 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1524
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001525 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001526 ASSERT_TRUE(device.has_value());
1527
1528 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1529 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1530 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1531 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1532}
1533
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001534TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1535 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1536 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1537 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1538 createUinputDevice<UinputKeyboardWithHidUsage>(
1539 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1540 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1541
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001542 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001543 ASSERT_TRUE(device.has_value());
1544
1545 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1546 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1547
1548 // If a device supports reporting HID usage codes, it shouldn't automatically support
1549 // stylus keys.
1550 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1551 uint8_t outFlags[] = {0};
1552 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1553 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1554}
1555
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001556/**
1557 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1558 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1559 * are passed to the listener.
1560 */
1561static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1562TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1563 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1564 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1565 NotifyKeyArgs keyArgs;
1566
1567 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1570 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1571
1572 controller->pressAndReleaseKey(BTN_GEAR_UP);
1573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1575 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1576}
1577
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001578// --- TouchIntegrationTest ---
1579
Arpit Singh440bf652023-08-09 09:23:43 +00001580class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001581protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001582 const std::string UNIQUE_ID = "local:0";
1583
Chris Yea52ade12020-08-27 16:49:20 -07001584 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001585#if !defined(__ANDROID__)
1586 GTEST_SKIP();
1587#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001588 InputReaderIntegrationTest::SetUp();
1589 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001590 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1591 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001592
1593 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1594 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001596 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001597 ASSERT_TRUE(info);
1598 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001599 }
1600
1601 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001602 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001603 std::optional<uint8_t> physicalPort,
1604 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001605 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001606 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001607 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001608 }
1609
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001610 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1611 NotifyMotionArgs args;
1612 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1613 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001614 ASSERT_EQ(points.size(), args.getPointerCount());
1615 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1617 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1618 }
1619 }
1620
Arthur Hungaab25622020-01-16 11:22:11 +08001621 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001622 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001623};
1624
Arpit Singh440bf652023-08-09 09:23:43 +00001625enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1626
1627class TouchIntegrationTest : public BaseTouchIntegrationTest,
1628 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1629protected:
1630 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1631 const std::string INPUT_PORT = "uinput_touch/input0";
1632
1633 void SetUp() override {
1634#if !defined(__ANDROID__)
1635 GTEST_SKIP();
1636#endif
1637 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1638 BaseTouchIntegrationTest::SetUp();
1639 return;
1640 }
1641
1642 // setup policy with a input-port or UniqueId association to the display
1643 bool isInputPortAssociation =
1644 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1645
1646 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1647 if (isInputPortAssociation) {
1648 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1649 } else {
1650 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1651 }
Arpit Singh440bf652023-08-09 09:23:43 +00001652
1653 InputReaderIntegrationTest::setupInputReader();
1654
1655 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1656 INPUT_PORT);
1657 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1658
1659 // Add a display linked to a physical port or UniqueId.
1660 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1661 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1662 ViewportType::INTERNAL);
1663 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1664 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001665 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001666 ASSERT_TRUE(info);
1667 mDeviceInfo = *info;
1668 }
1669};
1670
1671TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001672 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1673 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1674 // presses).
1675 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1676 mDeviceInfo.getSources());
1677}
1678
Arpit Singh440bf652023-08-09 09:23:43 +00001679TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001684 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001685 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001686 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001687 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1689
1690 // ACTION_MOVE
1691 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001693 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1695
1696 // ACTION_UP
1697 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1701}
1702
Arpit Singh440bf652023-08-09 09:23:43 +00001703TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001704 NotifyMotionArgs args;
1705 const Point centerPoint = mDevice->getCenterPoint();
1706
1707 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001708 mDevice->sendSlot(FIRST_SLOT);
1709 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001710 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001711 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1714
1715 // ACTION_POINTER_DOWN (Second slot)
1716 const Point secondPoint = centerPoint + Point(100, 100);
1717 mDevice->sendSlot(SECOND_SLOT);
1718 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001719 mDevice->sendDown(secondPoint);
1720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001722 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001723
1724 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001725 mDevice->sendMove(secondPoint + Point(1, 1));
1726 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001727 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1729
1730 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001731 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001734 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001735
1736 // ACTION_UP
1737 mDevice->sendSlot(FIRST_SLOT);
1738 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001739 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1741 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1742}
1743
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001744/**
1745 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1746 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1747 * data?
1748 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1749 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1750 * for Pointer 0 only is generated after.
1751 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1752 * events, we will not miss any information.
1753 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1754 * event generated afterwards that contains the newest movement of pointer 0.
1755 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1756 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1757 * losing information about non-palm pointers.
1758 */
Arpit Singh440bf652023-08-09 09:23:43 +00001759TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001760 NotifyMotionArgs args;
1761 const Point centerPoint = mDevice->getCenterPoint();
1762
1763 // ACTION_DOWN
1764 mDevice->sendSlot(FIRST_SLOT);
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1766 mDevice->sendDown(centerPoint);
1767 mDevice->sendSync();
1768 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1769
1770 // ACTION_POINTER_DOWN (Second slot)
1771 const Point secondPoint = centerPoint + Point(100, 100);
1772 mDevice->sendSlot(SECOND_SLOT);
1773 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1774 mDevice->sendDown(secondPoint);
1775 mDevice->sendSync();
1776 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1777
1778 // ACTION_MOVE (First slot)
1779 mDevice->sendSlot(FIRST_SLOT);
1780 mDevice->sendMove(centerPoint + Point(5, 5));
1781 // ACTION_POINTER_UP (Second slot)
1782 mDevice->sendSlot(SECOND_SLOT);
1783 mDevice->sendPointerUp();
1784 // Send a single sync for the above 2 pointer updates
1785 mDevice->sendSync();
1786
1787 // First, we should get POINTER_UP for the second pointer
1788 assertReceivedMotion(ACTION_POINTER_1_UP,
1789 {/*first pointer */ centerPoint + Point(5, 5),
1790 /*second pointer*/ secondPoint});
1791
1792 // Next, the MOVE event for the first pointer
1793 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1794}
1795
1796/**
1797 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1798 * move, and then it will go up, all in the same frame.
1799 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1800 * gets sent to the listener.
1801 */
Arpit Singh440bf652023-08-09 09:23:43 +00001802TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001803 NotifyMotionArgs args;
1804 const Point centerPoint = mDevice->getCenterPoint();
1805
1806 // ACTION_DOWN
1807 mDevice->sendSlot(FIRST_SLOT);
1808 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1809 mDevice->sendDown(centerPoint);
1810 mDevice->sendSync();
1811 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1812
1813 // ACTION_POINTER_DOWN (Second slot)
1814 const Point secondPoint = centerPoint + Point(100, 100);
1815 mDevice->sendSlot(SECOND_SLOT);
1816 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1817 mDevice->sendDown(secondPoint);
1818 mDevice->sendSync();
1819 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1820
1821 // ACTION_MOVE (First slot)
1822 mDevice->sendSlot(FIRST_SLOT);
1823 mDevice->sendMove(centerPoint + Point(5, 5));
1824 // ACTION_POINTER_UP (Second slot)
1825 mDevice->sendSlot(SECOND_SLOT);
1826 mDevice->sendMove(secondPoint + Point(6, 6));
1827 mDevice->sendPointerUp();
1828 // Send a single sync for the above 2 pointer updates
1829 mDevice->sendSync();
1830
1831 // First, we should get POINTER_UP for the second pointer
1832 // The movement of the second pointer during the liftoff frame is ignored.
1833 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1834 assertReceivedMotion(ACTION_POINTER_1_UP,
1835 {/*first pointer */ centerPoint + Point(5, 5),
1836 /*second pointer*/ secondPoint});
1837
1838 // Next, the MOVE event for the first pointer
1839 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1840}
1841
Arpit Singh440bf652023-08-09 09:23:43 +00001842TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001843 NotifyMotionArgs args;
1844 const Point centerPoint = mDevice->getCenterPoint();
1845
1846 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001847 mDevice->sendSlot(FIRST_SLOT);
1848 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001849 mDevice->sendDown(centerPoint);
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_DOWN, args.action);
1853
arthurhungcc7f9802020-04-30 17:55:40 +08001854 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001855 const Point secondPoint = centerPoint + Point(100, 100);
1856 mDevice->sendSlot(SECOND_SLOT);
1857 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1858 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001859 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001861 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001862
arthurhungcc7f9802020-04-30 17:55:40 +08001863 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001864 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001865 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001866 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1868
arthurhungcc7f9802020-04-30 17:55:40 +08001869 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1870 // a palm event.
1871 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001872 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001873 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001875 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001876 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001877
arthurhungcc7f9802020-04-30 17:55:40 +08001878 // Send up to second slot, expect first slot send moving.
1879 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001880 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001883
arthurhungcc7f9802020-04-30 17:55:40 +08001884 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001885 mDevice->sendSlot(FIRST_SLOT);
1886 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001887 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001888
arthurhungcc7f9802020-04-30 17:55:40 +08001889 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1890 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001891}
1892
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001893/**
1894 * Some drivers historically have reported axis values outside of the range specified in the
1895 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1896 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1897 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1898 * and no units for pressure (resolution) is specified by the evdev documentation.
1899 */
1900TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1901 const Point centerPoint = mDevice->getCenterPoint();
1902
1903 // Down with pressure outside the reported range
1904 mDevice->sendSlot(FIRST_SLOT);
1905 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1906 mDevice->sendDown(centerPoint);
1907 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1908 mDevice->sendSync();
1909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1910 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1911
1912 // Move to a point outside the reported range
1913 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1914 mDevice->sendSync();
1915 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1916 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1917
1918 // Up
1919 mDevice->sendUp();
1920 mDevice->sendSync();
1921 ASSERT_NO_FATAL_FAILURE(
1922 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1923}
1924
Arpit Singh440bf652023-08-09 09:23:43 +00001925TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001926 const Point centerPoint = mDevice->getCenterPoint();
1927
1928 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1929 mDevice->sendSlot(FIRST_SLOT);
1930 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1931 mDevice->sendToolType(MT_TOOL_PEN);
1932 mDevice->sendDown(centerPoint);
1933 mDevice->sendSync();
1934 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001936 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001937
1938 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1939
1940 // Release the stylus touch.
1941 mDevice->sendUp();
1942 mDevice->sendSync();
1943 ASSERT_NO_FATAL_FAILURE(
1944 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1945
1946 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1947
1948 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1949 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1950 mDevice->sendToolType(MT_TOOL_FINGER);
1951 mDevice->sendDown(centerPoint);
1952 mDevice->sendSync();
1953 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001955 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001956
1957 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1958
1959 mDevice->sendUp();
1960 mDevice->sendSync();
1961 ASSERT_NO_FATAL_FAILURE(
1962 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1963
1964 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1965 // The policy should be notified of the stylus presence.
1966 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1967 mDevice->sendToolType(MT_TOOL_PEN);
1968 mDevice->sendMove(centerPoint);
1969 mDevice->sendSync();
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1971 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001972 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001973
1974 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1975}
1976
Arpit Singh440bf652023-08-09 09:23:43 +00001977TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001978 const Point centerPoint = mDevice->getCenterPoint();
1979
1980 // Down
1981 mDevice->sendSlot(FIRST_SLOT);
1982 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1983 mDevice->sendDown(centerPoint);
1984 mDevice->sendSync();
1985 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1986 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1987
1988 // Move
1989 mDevice->sendMove(centerPoint + Point(1, 1));
1990 mDevice->sendSync();
1991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1992 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1993
1994 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1995 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1996 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001998 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001999 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002000
2001 // Move
2002 mDevice->sendMove(centerPoint + Point(2, 2));
2003 mDevice->sendSync();
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2005 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2006
2007 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2008 externalStylus.reset();
2009 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2012
2013 // Up
2014 mDevice->sendUp();
2015 mDevice->sendSync();
2016 ASSERT_NO_FATAL_FAILURE(
2017 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2018
2019 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2020}
2021
Arpit Singh440bf652023-08-09 09:23:43 +00002022INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2023 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2024 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2025 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2026
Prabir Pradhan124ea442022-10-28 20:27:44 +00002027// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002028
Prabir Pradhan124ea442022-10-28 20:27:44 +00002029// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2030// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2031// stylus.
2032template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002033class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034protected:
2035 void SetUp() override {
2036#if !defined(__ANDROID__)
2037 GTEST_SKIP();
2038#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002039 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 mTouchscreen = mDevice.get();
2041 mTouchscreenInfo = mDeviceInfo;
2042
2043 setUpStylusDevice();
2044 }
2045
2046 UinputStylusDevice* mStylus{nullptr};
2047 InputDeviceInfo mStylusInfo{};
2048
2049 UinputTouchScreen* mTouchscreen{nullptr};
2050 InputDeviceInfo mTouchscreenInfo{};
2051
2052private:
2053 // When we are attempting to test stylus button events that are sent from the touchscreen,
2054 // use the same Uinput device for the touchscreen and the stylus.
2055 template <typename T = UinputStylusDevice>
2056 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2057 mStylus = mDevice.get();
2058 mStylusInfo = mDeviceInfo;
2059 }
2060
2061 // When we are attempting to stylus buttons from an external stylus being merged with touches
2062 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2063 template <typename T = UinputStylusDevice>
2064 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2065 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2066 mStylus = mStylusDeviceLifecycleTracker.get();
2067 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2068 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002069 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002070 ASSERT_TRUE(info);
2071 mStylusInfo = *info;
2072 }
2073
2074 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2075
2076 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002077 using BaseTouchIntegrationTest::mDevice;
2078 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002079};
2080
2081using StylusButtonIntegrationTestTypes =
2082 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2083TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2084
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002085TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002086 const auto stylusId = TestFixture::mStylusInfo.getId();
2087
2088 TestFixture::mStylus->pressKey(BTN_STYLUS);
2089 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2090 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2091 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2092
2093 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002097}
2098
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002099TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002100 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2101 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2102 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002103
2104 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002105 TestFixture::mStylus->pressKey(BTN_STYLUS);
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002107 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002108 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002109
2110 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002111 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2112 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2114 TestFixture::mTouchscreen->sendDown(centerPoint);
2115 TestFixture::mTouchscreen->sendSync();
2116 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002118 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002119 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2120 WithDeviceId(touchscreenId))));
2121 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002123 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2125 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126
Prabir Pradhan124ea442022-10-28 20:27:44 +00002127 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2128 TestFixture::mTouchscreen->sendSync();
2129 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002131 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002132 WithDeviceId(touchscreenId))));
2133 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002135 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002136 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137
2138 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002142 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002143}
2144
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002145TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002146 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2147 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2148 const auto stylusId = TestFixture::mStylusInfo.getId();
2149 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002150 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002151
2152 // Press the stylus button.
2153 TestFixture::mStylus->pressKey(BTN_STYLUS);
2154 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2155 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2156 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2157
2158 // Start hovering with the stylus.
2159 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2160 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2161 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2162 TestFixture::mTouchscreen->sendMove(centerPoint);
2163 TestFixture::mTouchscreen->sendSync();
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2165 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2166 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2167 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2168 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2169 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2170 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2171 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2172 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2173
2174 // Touch down with the stylus.
2175 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2176 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2177 TestFixture::mTouchscreen->sendDown(centerPoint);
2178 TestFixture::mTouchscreen->sendSync();
2179 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2180 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2181 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2182
2183 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2184 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2185 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2186
2187 // Stop touching with the stylus, and start hovering.
2188 TestFixture::mTouchscreen->sendUp();
2189 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2190 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2191 TestFixture::mTouchscreen->sendMove(centerPoint);
2192 TestFixture::mTouchscreen->sendSync();
2193 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2194 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2195 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2196 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2197 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2198 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2199 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2200 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2201 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2202
2203 // Stop hovering.
2204 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2205 TestFixture::mTouchscreen->sendSync();
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2208 WithButtonState(0))));
2209 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2210 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2211 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2212 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2213
2214 // Release the stylus button.
2215 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2216 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2217 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2218 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2219}
2220
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002221TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002222 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2223 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2224 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002225
2226 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002227 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2228 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2229 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2230 TestFixture::mTouchscreen->sendDown(centerPoint);
2231 TestFixture::mTouchscreen->sendSync();
2232 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002234 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002235 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002236
2237 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002238 TestFixture::mStylus->pressKey(BTN_STYLUS);
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), 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_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002244 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002245 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2246 WithDeviceId(touchscreenId))));
2247 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002248 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002249 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002250 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2251 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002252
Prabir Pradhan124ea442022-10-28 20:27:44 +00002253 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002256 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2257 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002259 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 WithDeviceId(touchscreenId))));
2261 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002262 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002263 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002264 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002265
2266 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002267 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2268 TestFixture::mTouchscreen->sendSync();
2269 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002271 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002272 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002273}
2274
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002275TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002276 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2277 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002278 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002279
2280 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2281 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2282 const auto stylusId = TestFixture::mStylusInfo.getId();
2283
2284 // Start a stylus gesture. By the time this event is processed, the configuration change that
2285 // was requested is guaranteed to be completed.
2286 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2287 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2288 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2289 TestFixture::mTouchscreen->sendDown(centerPoint);
2290 TestFixture::mTouchscreen->sendSync();
2291 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2292 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002293 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002294 WithDeviceId(touchscreenId))));
2295
2296 // Press and release a stylus button. Each change only generates a MOVE motion event.
2297 // Key events are unaffected.
2298 TestFixture::mStylus->pressKey(BTN_STYLUS);
2299 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2300 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2301 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2302 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2303 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002304 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002305 WithDeviceId(touchscreenId))));
2306
2307 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2308 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2309 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2310 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2311 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002313 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002314 WithDeviceId(touchscreenId))));
2315
2316 // Finish the stylus gesture.
2317 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2318 TestFixture::mTouchscreen->sendSync();
2319 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2320 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002321 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002322 WithDeviceId(touchscreenId))));
2323}
2324
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002325// --- ExternalStylusIntegrationTest ---
2326
2327// Verify the behavior of an external stylus. An external stylus can report pressure or button
2328// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2329// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002330using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002331
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002332TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2333 // Create an external stylus capable of reporting pressure data that
2334 // should be fused with a touch pointer.
2335 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2336 createUinputDevice<UinputExternalStylusWithPressure>();
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002339 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002340 ASSERT_TRUE(stylusInfo);
2341
2342 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002343 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002344 ASSERT_TRUE(deviceInfo);
2345 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2346}
2347
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002348TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002349 const Point centerPoint = mDevice->getCenterPoint();
2350
2351 // Create an external stylus capable of reporting pressure data that
2352 // should be fused with a touch pointer.
2353 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2354 createUinputDevice<UinputExternalStylusWithPressure>();
2355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002357 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002358 ASSERT_TRUE(stylusInfo);
2359
2360 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2361
2362 const auto touchscreenId = mDeviceInfo.getId();
2363
2364 // Set a pressure value on the stylus. It doesn't generate any events.
2365 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2366 stylus->setPressure(100);
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2368
2369 // Start a finger gesture, and ensure it shows up as stylus gesture
2370 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002371 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002372 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002373 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002374 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375 mDevice->sendSync();
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002377 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2378 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2379 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002380
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002381 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2382 // event with the updated pressure.
2383 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2386 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2387 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002388
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002389 // The external stylus did not generate any events.
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2392}
2393
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002394TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002395 const Point centerPoint = mDevice->getCenterPoint();
2396
2397 // Create an external stylus capable of reporting pressure data that
2398 // should be fused with a touch pointer.
2399 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2400 createUinputDevice<UinputExternalStylusWithPressure>();
2401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002403 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002404 ASSERT_TRUE(stylusInfo);
2405
2406 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2407
2408 const auto touchscreenId = mDeviceInfo.getId();
2409
2410 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2411 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002412 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2413 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002414 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002416
2417 // Start a finger gesture. The touch device will withhold generating any touches for
2418 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2419 mDevice->sendSlot(FIRST_SLOT);
2420 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2421 mDevice->sendToolType(MT_TOOL_FINGER);
2422 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002423 const auto syncTime = std::chrono::system_clock::now();
2424 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2425 // is generated in that period, there will be a race condition between the event being generated
2426 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2427 // will reduce the liklihood of the race condition occurring.
2428 const auto waitUntilTimeForNoEvent =
2429 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002430 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002433 // Since the external stylus did not report a pressure value within the timeout,
2434 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002435 const auto waitUntilTimeForEvent = syncTime +
2436 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2437 ASSERT_NO_FATAL_FAILURE(
2438 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2439 AMOTION_EVENT_ACTION_DOWN),
2440 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2441 AINPUT_SOURCE_STYLUS),
2442 WithToolType(ToolType::FINGER),
2443 WithDeviceId(touchscreenId),
2444 WithPressure(1.f)),
2445 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002446
2447 // Change the pressure on the external stylus. Since the pressure was not present at the start
2448 // of the gesture, it is ignored for now.
2449 stylus->setPressure(200);
2450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2451
2452 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2454 mDevice->sendSync();
2455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2456 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002457 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002458 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002459
2460 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2461 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2462 mDevice->sendToolType(MT_TOOL_FINGER);
2463 mDevice->sendDown(centerPoint);
2464 mDevice->sendSync();
2465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2467 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2468 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002469
2470 // The external stylus did not generate any events.
2471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002474
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002475TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002476 const Point centerPoint = mDevice->getCenterPoint();
2477
2478 // Create an external stylus device that does not support pressure. It should not affect any
2479 // touch pointers.
2480 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2481 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002483 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002484 ASSERT_TRUE(stylusInfo);
2485
2486 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2487
2488 const auto touchscreenId = mDeviceInfo.getId();
2489
2490 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2491 // pressure data from the external stylus.
2492 mDevice->sendSlot(FIRST_SLOT);
2493 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2494 mDevice->sendToolType(MT_TOOL_FINGER);
2495 mDevice->sendDown(centerPoint);
2496 auto waitUntil = std::chrono::system_clock::now() +
2497 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2498 mDevice->sendSync();
2499 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002500 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2501 AMOTION_EVENT_ACTION_DOWN),
2502 WithToolType(ToolType::FINGER),
2503 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2504 AINPUT_SOURCE_STYLUS),
2505 WithButtonState(0),
2506 WithDeviceId(touchscreenId),
2507 WithPressure(1.f)),
2508 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002509
2510 // The external stylus did not generate any events.
2511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2512 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2513}
2514
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515// --- InputDeviceTest ---
2516class InputDeviceTest : public testing::Test {
2517protected:
2518 static const char* DEVICE_NAME;
2519 static const char* DEVICE_LOCATION;
2520 static const int32_t DEVICE_ID;
2521 static const int32_t DEVICE_GENERATION;
2522 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002523 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 static const int32_t EVENTHUB_ID;
2525 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2526
2527 std::shared_ptr<FakeEventHub> mFakeEventHub;
2528 sp<FakeInputReaderPolicy> mFakePolicy;
2529 std::unique_ptr<TestInputListener> mFakeListener;
2530 std::unique_ptr<InstrumentedInputReader> mReader;
2531 std::shared_ptr<InputDevice> mDevice;
2532
2533 void SetUp() override {
2534 mFakeEventHub = std::make_unique<FakeEventHub>();
2535 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2536 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002537 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002539 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 identifier.name = DEVICE_NAME;
2541 identifier.location = DEVICE_LOCATION;
2542 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2543 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2544 identifier);
2545 mReader->pushNextDevice(mDevice);
2546 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002547 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002549
2550 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 mFakeListener.reset();
2552 mFakePolicy.clear();
2553 }
2554};
2555
2556const char* InputDeviceTest::DEVICE_NAME = "device";
2557const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2558const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2559const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002561const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2562 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2565
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002566TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002568 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2569 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002571
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2573 ASSERT_EQ(mDevice->isEnabled(), false);
2574}
2575
2576TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2577 // Configuration.
2578 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002579 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580
2581 // Reset.
2582 unused += mDevice->reset(ARBITRARY_TIME);
2583
2584 NotifyDeviceResetArgs resetArgs;
2585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2586 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2587 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2588
2589 // Metadata.
2590 ASSERT_TRUE(mDevice->isIgnored());
2591 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2592
2593 InputDeviceInfo info = mDevice->getDeviceInfo();
2594 ASSERT_EQ(DEVICE_ID, info.getId());
2595 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2596 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2597 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2598
2599 // State queries.
2600 ASSERT_EQ(0, mDevice->getMetaState());
2601
2602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2603 << "Ignored device should return unknown key code state.";
2604 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2605 << "Ignored device should return unknown scan code state.";
2606 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2607 << "Ignored device should return unknown switch state.";
2608
2609 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2610 uint8_t flags[2] = { 0, 1 };
2611 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2612 << "Ignored device should never mark any key codes.";
2613 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2614 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2615}
2616
2617TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2618 // Configuration.
2619 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2620
2621 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002622 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2623 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2625 mapper1.setMetaState(AMETA_ALT_ON);
2626 mapper1.addSupportedKeyCode(AKEYCODE_A);
2627 mapper1.addSupportedKeyCode(AKEYCODE_B);
2628 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2629 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2630 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2631 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2632 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2633
2634 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002635 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2636 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 mapper2.setMetaState(AMETA_SHIFT_ON);
2638
2639 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002640 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Harry Cuttsf13161a2023-03-08 14:15:49 +00002642 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2643 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002645 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002647 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2648 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
2650 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002651 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002652 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2653 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
2655 NotifyDeviceResetArgs resetArgs;
2656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2657 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2658 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2659
2660 // Metadata.
2661 ASSERT_FALSE(mDevice->isIgnored());
2662 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2663
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002664 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002666 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2668 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2669
2670 // State queries.
2671 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2672 << "Should query mappers and combine meta states.";
2673
2674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2675 << "Should return unknown key code state when source not supported.";
2676 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2677 << "Should return unknown scan code state when source not supported.";
2678 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2679 << "Should return unknown switch state when source not supported.";
2680
2681 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2682 << "Should query mapper when source is supported.";
2683 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2684 << "Should query mapper when source is supported.";
2685 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2686 << "Should query mapper when source is supported.";
2687
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002688 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002690 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 << "Should do nothing when source is unsupported.";
2692 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2693 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2694 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2695 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2696
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002697 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 << "Should query mapper when source is supported.";
2699 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2700 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2701 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2702 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2703
2704 // Event handling.
2705 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002707 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002709 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2710 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711}
2712
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002713TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2714 // Set some behavior to force the configuration to be update.
2715 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2716 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2717 AINPUT_SOURCE_KEYBOARD);
2718
2719 std::list<NotifyArgs> unused =
2720 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2721 /*changes=*/{});
2722
2723 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2724}
2725
2726TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2727 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2728 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2729 AINPUT_SOURCE_KEYBOARD);
2730
2731 std::list<NotifyArgs> unused =
2732 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2733 /*changes=*/{});
2734
2735 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2736}
2737
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002738TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2739 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2740 FakeInputMapper& mapper =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2742 AINPUT_SOURCE_KEYBOARD);
2743 NotifyMotionArgs args1;
2744 NotifySwitchArgs args2;
2745 NotifyKeyArgs args3;
2746 mapper.setProcessResult({args1, args2, args3});
2747
2748 InputReaderConfiguration config;
2749 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2750
2751 RawEvent event;
2752 event.deviceId = EVENTHUB_ID;
2753 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2754
2755 for (auto& arg : notifyArgs) {
2756 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2757 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2758 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2759 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2760 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2761 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2762 }
2763 }
2764}
2765
2766TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2767 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2768 FakeInputMapper& mapper =
2769 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2770 AINPUT_SOURCE_KEYBOARD);
2771 NotifyMotionArgs args;
2772 mapper.setProcessResult({args});
2773
2774 InputReaderConfiguration config;
2775 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2776
2777 RawEvent event;
2778 event.deviceId = EVENTHUB_ID;
2779 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2780
2781 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2782 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2783}
2784
2785TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2786 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2787 FakeInputMapper& mapper =
2788 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2789 AINPUT_SOURCE_KEYBOARD);
2790 NotifyMotionArgs args;
2791 args.policyFlags = POLICY_FLAG_WAKE;
2792 mapper.setProcessResult({args});
2793
2794 InputReaderConfiguration config;
2795 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2796
2797 RawEvent event;
2798 event.deviceId = EVENTHUB_ID;
2799 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2800
2801 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2802 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2803}
2804
Arthur Hung2c9a3342019-07-23 14:18:59 +08002805// A single input device is associated with a specific display. Check that:
2806// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002807// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002808TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002809 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2810 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002811
2812 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002813 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002814 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2815 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002816
2817 // Device should be enabled by default.
2818 ASSERT_TRUE(mDevice->isEnabled());
2819
2820 // Prepare associated info.
2821 constexpr uint8_t hdmi = 1;
2822 const std::string UNIQUE_ID = "local:1";
2823
2824 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002825 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002826 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002827 // Device should be disabled because it is associated with a specific display via
2828 // input port <-> display port association, but the corresponding display is not found
2829 ASSERT_FALSE(mDevice->isEnabled());
2830
2831 // Prepare displays.
2832 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002833 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002834 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002835 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002836 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002837 ASSERT_TRUE(mDevice->isEnabled());
2838
2839 // Device should be disabled after set disable.
2840 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002841 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002842 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002843 ASSERT_FALSE(mDevice->isEnabled());
2844
2845 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002846 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002847 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002848 ASSERT_FALSE(mDevice->isEnabled());
2849}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
Christine Franks1ba71cc2021-04-07 14:37:42 -07002851TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2852 // Device should be enabled by default.
2853 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002854 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2855 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002856 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled because it is associated with a specific display, but the
2862 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002863 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002864 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002865 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866 ASSERT_FALSE(mDevice->isEnabled());
2867
2868 // Device should be enabled when a display is found.
2869 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002870 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002871 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002872 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002873 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002874 ASSERT_TRUE(mDevice->isEnabled());
2875
2876 // Device should be disabled after set disable.
2877 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002878 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002879 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002880 ASSERT_FALSE(mDevice->isEnabled());
2881
2882 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002883 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002884 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002885 ASSERT_FALSE(mDevice->isEnabled());
2886}
2887
Christine Franks2a2293c2022-01-18 11:51:16 -08002888TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2889 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002890 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2891 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002892 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2894 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002895
Christine Franks2a2293c2022-01-18 11:51:16 -08002896 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2897 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002898 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002899 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002900 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002901 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002902 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002903 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002904 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2905 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002906}
2907
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002908/**
2909 * This test reproduces a crash caused by a dangling reference that remains after device is added
2910 * and removed. The reference is accessed in InputDevice::dump(..);
2911 */
2912TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2913 constexpr int32_t TEST_EVENTHUB_ID = 10;
2914 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2915
Harry Cutts33476232023-01-30 19:57:29 +00002916 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002917 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2918 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002919 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2920 std::string dumpStr, eventHubDevStr;
2921 device.dump(dumpStr, eventHubDevStr);
2922}
2923
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002924TEST_F(InputDeviceTest, GetBluetoothAddress) {
2925 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2926 ASSERT_TRUE(address);
2927 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2928}
2929
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002930TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2931 mFakePolicy->clearViewports();
2932 FakeInputMapper& mapper =
2933 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2934 AINPUT_SOURCE_KEYBOARD);
2935 std::list<NotifyArgs> unused =
2936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2937 /*changes=*/{});
2938
2939 mapper.assertConfigureWasCalled();
2940 mapper.assertResetWasNotCalled();
2941
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002942 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002943 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002944 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002945 .type = EV_SYN,
2946 .code = SYN_REPORT,
2947 .value = 0};
2948
2949 // Events are processed normally.
2950 unused = mDevice->process(&event, /*count=*/1);
2951 mapper.assertProcessWasCalled();
2952
2953 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002954 event.type = EV_SYN;
2955 event.code = SYN_DROPPED;
2956 event.value = 0;
2957 unused = mDevice->process(&event, /*count=*/1);
2958 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002959
2960 // All events until the next SYN_REPORT should be dropped.
2961 event.type = EV_KEY;
2962 event.code = KEY_A;
2963 event.value = 1;
2964 unused = mDevice->process(&event, /*count=*/1);
2965 mapper.assertProcessWasNotCalled();
2966
2967 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002968 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002969 event.type = EV_SYN;
2970 event.code = SYN_REPORT;
2971 event.value = 0;
2972 unused = mDevice->process(&event, /*count=*/1);
2973 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002974 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002975
2976 // The mapper receives events normally now.
2977 event.type = EV_KEY;
2978 event.code = KEY_B;
2979 event.value = 1;
2980 unused = mDevice->process(&event, /*count=*/1);
2981 mapper.assertProcessWasCalled();
2982}
2983
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984// --- SwitchInputMapperTest ---
2985
2986class SwitchInputMapperTest : public InputMapperTest {
2987protected:
2988};
2989
2990TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002991 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994}
2995
2996TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002997 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002999 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003002 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003003 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004}
3005
3006TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003007 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003008 std::list<NotifyArgs> out;
3009 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3010 ASSERT_TRUE(out.empty());
3011 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3012 ASSERT_TRUE(out.empty());
3013 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3014 ASSERT_TRUE(out.empty());
3015 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003017 ASSERT_EQ(1u, out.size());
3018 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003020 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3021 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 args.switchMask);
3023 ASSERT_EQ(uint32_t(0), args.policyFlags);
3024}
3025
Chris Ye87143712020-11-10 05:05:58 +00003026// --- VibratorInputMapperTest ---
3027class VibratorInputMapperTest : public InputMapperTest {
3028protected:
3029 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3030};
3031
3032TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003033 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003034
3035 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3036}
3037
3038TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003039 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003040
3041 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3042}
3043
3044TEST_F(VibratorInputMapperTest, Vibrate) {
3045 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003046 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003047 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003048
3049 VibrationElement pattern(2);
3050 VibrationSequence sequence(2);
3051 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003052 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3053 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003054 sequence.addElement(pattern);
3055 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003056 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3057 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003058 sequence.addElement(pattern);
3059
3060 std::vector<int64_t> timings = {0, 1};
3061 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3062
3063 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003064 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003065 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003066 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003067 // Verify vibrator state listener was notified.
3068 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003069 ASSERT_EQ(1u, out.size());
3070 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3071 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3072 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003073 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003074 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003075 ASSERT_FALSE(mapper.isVibrating());
3076 // Verify vibrator state listener was notified.
3077 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003078 ASSERT_EQ(1u, out.size());
3079 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3080 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3081 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003082}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Chris Yef59a2f42020-10-16 12:55:26 -07003084// --- SensorInputMapperTest ---
3085
3086class SensorInputMapperTest : public InputMapperTest {
3087protected:
3088 static const int32_t ACCEL_RAW_MIN;
3089 static const int32_t ACCEL_RAW_MAX;
3090 static const int32_t ACCEL_RAW_FUZZ;
3091 static const int32_t ACCEL_RAW_FLAT;
3092 static const int32_t ACCEL_RAW_RESOLUTION;
3093
3094 static const int32_t GYRO_RAW_MIN;
3095 static const int32_t GYRO_RAW_MAX;
3096 static const int32_t GYRO_RAW_FUZZ;
3097 static const int32_t GYRO_RAW_FLAT;
3098 static const int32_t GYRO_RAW_RESOLUTION;
3099
3100 static const float GRAVITY_MS2_UNIT;
3101 static const float DEGREE_RADIAN_UNIT;
3102
3103 void prepareAccelAxes();
3104 void prepareGyroAxes();
3105 void setAccelProperties();
3106 void setGyroProperties();
3107 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3108};
3109
3110const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3111const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3112const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3113const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3114const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3115
3116const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3117const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3118const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3119const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3120const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3121
3122const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3123const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3124
3125void SensorInputMapperTest::prepareAccelAxes() {
3126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3127 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3129 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3131 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3132}
3133
3134void SensorInputMapperTest::prepareGyroAxes() {
3135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3136 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3138 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3140 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3141}
3142
3143void SensorInputMapperTest::setAccelProperties() {
3144 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3145 /* sensorDataIndex */ 0);
3146 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3147 /* sensorDataIndex */ 1);
3148 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3149 /* sensorDataIndex */ 2);
3150 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3151 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3152 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3153 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3154 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3155}
3156
3157void SensorInputMapperTest::setGyroProperties() {
3158 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3159 /* sensorDataIndex */ 0);
3160 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3161 /* sensorDataIndex */ 1);
3162 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3163 /* sensorDataIndex */ 2);
3164 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3165 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3166 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3167 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3168 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3169}
3170
3171TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003172 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003173
3174 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3175}
3176
3177TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3178 setAccelProperties();
3179 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003180 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003181
3182 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3183 std::chrono::microseconds(10000),
3184 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003185 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003191
3192 NotifySensorArgs args;
3193 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3194 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3195 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3196
3197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3198 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3199 ASSERT_EQ(args.deviceId, DEVICE_ID);
3200 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3201 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3202 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3203 ASSERT_EQ(args.values, values);
3204 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3205}
3206
3207TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3208 setGyroProperties();
3209 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003210 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003211
3212 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3213 std::chrono::microseconds(10000),
3214 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003215 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003221
3222 NotifySensorArgs args;
3223 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3224 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3225 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3226
3227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3228 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3229 ASSERT_EQ(args.deviceId, DEVICE_ID);
3230 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3231 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3232 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3233 ASSERT_EQ(args.values, values);
3234 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3235}
3236
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237// --- KeyboardInputMapperTest ---
3238
3239class KeyboardInputMapperTest : public InputMapperTest {
3240protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003241 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003242 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003243 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003246 int32_t originalKeyCode, int32_t rotatedKeyCode,
3247 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248};
3249
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003250/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3251 * orientation.
3252 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003253void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003254 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3255 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003256}
3257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003258void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003259 int32_t originalScanCode, int32_t originalKeyCode,
3260 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 NotifyKeyArgs args;
3262
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003263 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3265 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3266 ASSERT_EQ(originalScanCode, args.scanCode);
3267 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003268 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003269
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003270 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3272 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3273 ASSERT_EQ(originalScanCode, args.scanCode);
3274 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003275 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003276}
3277
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003280 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003281 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003282
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003283 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003284}
3285
3286TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3287 const int32_t USAGE_A = 0x070004;
3288 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003289 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3290 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003291 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3292 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3293 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003294
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003295 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003296 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003297 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003298 // Initial metastate is AMETA_NONE.
3299 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003300
3301 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 NotifyKeyArgs args;
3304 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3305 ASSERT_EQ(DEVICE_ID, args.deviceId);
3306 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3307 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3308 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3309 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3310 ASSERT_EQ(KEY_HOME, args.scanCode);
3311 ASSERT_EQ(AMETA_NONE, args.metaState);
3312 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3313 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3314 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3315
3316 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003317 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3319 ASSERT_EQ(DEVICE_ID, args.deviceId);
3320 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3321 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3322 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3323 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3324 ASSERT_EQ(KEY_HOME, args.scanCode);
3325 ASSERT_EQ(AMETA_NONE, args.metaState);
3326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3327 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3328 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3329
3330 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3334 ASSERT_EQ(DEVICE_ID, args.deviceId);
3335 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3336 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3338 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3339 ASSERT_EQ(0, args.scanCode);
3340 ASSERT_EQ(AMETA_NONE, args.metaState);
3341 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3342 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3343 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3344
3345 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3347 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(DEVICE_ID, args.deviceId);
3350 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3351 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3353 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3354 ASSERT_EQ(0, args.scanCode);
3355 ASSERT_EQ(AMETA_NONE, args.metaState);
3356 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3357 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3358 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3359
3360 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3364 ASSERT_EQ(DEVICE_ID, args.deviceId);
3365 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3366 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3367 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3368 ASSERT_EQ(0, args.keyCode);
3369 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3370 ASSERT_EQ(AMETA_NONE, args.metaState);
3371 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3372 ASSERT_EQ(0U, args.policyFlags);
3373 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3374
3375 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3377 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3379 ASSERT_EQ(DEVICE_ID, args.deviceId);
3380 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3381 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3382 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3383 ASSERT_EQ(0, args.keyCode);
3384 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3385 ASSERT_EQ(AMETA_NONE, args.metaState);
3386 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3387 ASSERT_EQ(0U, args.policyFlags);
3388 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3389}
3390
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003391TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3392 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3394 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3395
3396 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003397 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003398 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3399
3400 // Key down by scan code.
3401 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3402 NotifyKeyArgs args;
3403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3404 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3405
3406 // Key up by scan code.
3407 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3409 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3410}
3411
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003412/**
3413 * Ensure that the readTime is set to the time when the EV_KEY is received.
3414 */
3415TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3416 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3417
3418 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003419 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003420 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3421 NotifyKeyArgs args;
3422
3423 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003424 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3426 ASSERT_EQ(12, args.readTime);
3427
3428 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003429 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3431 ASSERT_EQ(15, args.readTime);
3432}
3433
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003435 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3436 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003437 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3438 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3439 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003441 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003442 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003443 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444
Arthur Hung95f68612022-04-07 14:08:22 +08003445 // Initial metastate is AMETA_NONE.
3446 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003447
3448 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003449 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003450 NotifyKeyArgs args;
3451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3452 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003454 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
3456 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003457 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3459 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461
3462 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003463 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3465 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003467
3468 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003469 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3471 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003472 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003473 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003474}
3475
3476TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003477 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003482 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003483 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003484 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485
Michael Wrighta9cf4192022-12-01 23:46:39 +00003486 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3488 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3490 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3491 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3492 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3493 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3494 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3495}
3496
3497TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003498 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3500 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003502
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003504 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003505 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003506 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003507
Michael Wrighta9cf4192022-12-01 23:46:39 +00003508 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003509 ASSERT_NO_FATAL_FAILURE(
3510 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3511 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3512 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3513 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3514 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3515 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3516 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003518 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003519 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003520 ASSERT_NO_FATAL_FAILURE(
3521 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3523 AKEYCODE_DPAD_UP, DISPLAY_ID));
3524 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3525 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3526 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3527 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003528
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003529 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003530 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003531 ASSERT_NO_FATAL_FAILURE(
3532 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3534 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3535 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3536 AKEYCODE_DPAD_UP, DISPLAY_ID));
3537 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3538 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003539
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003540 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003541 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003542 ASSERT_NO_FATAL_FAILURE(
3543 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3545 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3546 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3547 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3548 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3549 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003550
3551 // Special case: if orientation changes while key is down, we still emit the same keycode
3552 // in the key up as we did in the key down.
3553 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003554 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003555 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3558 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3559 ASSERT_EQ(KEY_UP, args.scanCode);
3560 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3561
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003562 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003563 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003564 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3567 ASSERT_EQ(KEY_UP, args.scanCode);
3568 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3569}
3570
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003571TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3572 // If the keyboard is not orientation aware,
3573 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003574 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003575
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003576 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003577 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003578 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003579 NotifyKeyArgs args;
3580
3581 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3586 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3587
Michael Wrighta9cf4192022-12-01 23:46:39 +00003588 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003589 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3593 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3594}
3595
3596TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3597 // If the keyboard is orientation aware,
3598 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003599 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003600
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003601 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003602 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003603 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003604 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003605 NotifyKeyArgs args;
3606
3607 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3608 // ^--- already checked by the previous test
3609
Michael Wrighta9cf4192022-12-01 23:46:39 +00003610 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003611 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3616 ASSERT_EQ(DISPLAY_ID, args.displayId);
3617
3618 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003619 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003620 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003621 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003622 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003624 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3626 ASSERT_EQ(newDisplayId, args.displayId);
3627}
3628
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003630 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003631 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003632 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003633
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003634 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003635 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003636
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003637 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003638 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003639}
3640
Philip Junker4af3b3d2021-12-14 10:36:55 +01003641TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3642 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003643 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003644 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3645
3646 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3647 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3648 << "If a mapping is available, the result is equal to the mapping";
3649
3650 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3651 << "If no mapping is available, the result is the key location";
3652}
3653
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003655 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003656 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003658
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003659 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003660 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003661
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003662 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003663 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003664}
3665
3666TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003667 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003668 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003669 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003670
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003671 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003672
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003674 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675 ASSERT_TRUE(flags[0]);
3676 ASSERT_FALSE(flags[1]);
3677}
3678
3679TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003680 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3681 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3682 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3683 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3684 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003686
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003687 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003688 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003689 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003690 // Initial metastate is AMETA_NONE.
3691 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003692
3693 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003694 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3695 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3696 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003697
3698 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003699 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003701 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3702 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003704 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003705
3706 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003707 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003709 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3710 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3711 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003712 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003713
3714 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003715 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003717 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3718 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3719 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003720 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003721
3722 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003723 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003725 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3726 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3727 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003728 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003729
3730 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003731 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003733 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3734 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3735 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003736 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003737
3738 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003739 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003741 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3742 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3743 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003744 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003745}
3746
Chris Yea52ade12020-08-27 16:49:20 -07003747TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3748 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3749 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3750 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3751 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3752
3753 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003754 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003755 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3756
Chris Yea52ade12020-08-27 16:49:20 -07003757 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003758 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003759 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3760 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3761 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3762 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3763
3764 NotifyKeyArgs args;
3765 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003766 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3768 ASSERT_EQ(AMETA_NONE, args.metaState);
3769 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3770 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3771 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3772
3773 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003774 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3776 ASSERT_EQ(AMETA_NONE, args.metaState);
3777 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3778 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3779 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3780}
3781
Arthur Hung2c9a3342019-07-23 14:18:59 +08003782TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3783 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003784 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003788
3789 // keyboard 2.
3790 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003791 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003792 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003793 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003794 std::shared_ptr<InputDevice> device2 =
3795 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003796 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003797
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003798 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3799 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3800 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3801 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003802
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003803 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003804 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003805 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003806
Arpit Singh67ca6842023-04-26 14:43:16 +00003807 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003808 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003809 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3810 mFakePolicy
3811 ->getReaderConfiguration(),
3812 AINPUT_SOURCE_KEYBOARD,
3813 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003814 std::list<NotifyArgs> unused =
3815 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003816 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003817 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003818
3819 // Prepared displays and associated info.
3820 constexpr uint8_t hdmi1 = 0;
3821 constexpr uint8_t hdmi2 = 1;
3822 const std::string SECONDARY_UNIQUE_ID = "local:1";
3823
3824 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3825 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3826
3827 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003828 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003829 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003830 ASSERT_FALSE(device2->isEnabled());
3831
3832 // Prepare second display.
3833 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003834 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003835 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003836 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003837 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003838 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003839 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003840 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003841
3842 // Device should be enabled after the associated display is found.
3843 ASSERT_TRUE(mDevice->isEnabled());
3844 ASSERT_TRUE(device2->isEnabled());
3845
3846 // Test pad key events
3847 ASSERT_NO_FATAL_FAILURE(
3848 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3849 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3850 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3851 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3852 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3853 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3854 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3855
3856 ASSERT_NO_FATAL_FAILURE(
3857 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3858 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3859 AKEYCODE_DPAD_RIGHT, newDisplayId));
3860 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3861 AKEYCODE_DPAD_DOWN, newDisplayId));
3862 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3863 AKEYCODE_DPAD_LEFT, newDisplayId));
3864}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003865
arthurhungc903df12020-08-11 15:08:42 +08003866TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3867 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3868 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3869 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3870 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3873
3874 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003875 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003876 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003877 // Initial metastate is AMETA_NONE.
3878 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003879
3880 // Initialization should have turned all of the lights off.
3881 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3884
3885 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003886 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003888 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3889 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3890
3891 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003894 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3895 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3896
3897 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003900 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3901 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3902
3903 mFakeEventHub->removeDevice(EVENTHUB_ID);
3904 mReader->loopOnce();
3905
3906 // keyboard 2 should default toggle keys.
3907 const std::string USB2 = "USB2";
3908 const std::string DEVICE_NAME2 = "KEYBOARD2";
3909 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3910 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3911 std::shared_ptr<InputDevice> device2 =
3912 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003913 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003914 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3915 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3916 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3917 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3918 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3919 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3920
Arpit Singh67ca6842023-04-26 14:43:16 +00003921 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003922 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003923 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3924 mFakePolicy
3925 ->getReaderConfiguration(),
3926 AINPUT_SOURCE_KEYBOARD,
3927 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003928 std::list<NotifyArgs> unused =
3929 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003930 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003931 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003932
3933 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3934 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3935 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003936 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3937 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003938}
3939
Arthur Hungcb40a002021-08-03 14:31:01 +00003940TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3941 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3942 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3943 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3944
3945 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003946 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003947 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3948 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003949 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003950 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003951 // Initial metastate is AMETA_NONE.
3952 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003953
3954 mReader->toggleCapsLockState(DEVICE_ID);
3955 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3956}
3957
Arthur Hungfb3cc112022-04-13 07:39:50 +00003958TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3959 // keyboard 1.
3960 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3961 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3962 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3963 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3966
3967 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003968 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003969 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3970
3971 // keyboard 2.
3972 const std::string USB2 = "USB2";
3973 const std::string DEVICE_NAME2 = "KEYBOARD2";
3974 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3975 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3976 std::shared_ptr<InputDevice> device2 =
3977 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3978 ftl::Flags<InputDeviceClass>(0));
3979 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3980 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3981 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3982 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3983 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3984 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3985
Arpit Singh67ca6842023-04-26 14:43:16 +00003986 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003987 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003988 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3989 mFakePolicy
3990 ->getReaderConfiguration(),
3991 AINPUT_SOURCE_KEYBOARD,
3992 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003993 std::list<NotifyArgs> unused =
3994 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003995 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003996 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003997
Arthur Hung95f68612022-04-07 14:08:22 +08003998 // Initial metastate is AMETA_NONE.
3999 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4000 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4001
4002 // Toggle num lock on and off.
4003 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4004 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004005 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4006 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4007 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4008
4009 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4010 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4011 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4012 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4013 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4014
4015 // Toggle caps lock on and off.
4016 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4017 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4018 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4019 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4020 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4021
4022 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4023 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4024 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4025 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4026 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4027
4028 // Toggle scroll lock on and off.
4029 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4030 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4031 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4032 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4033 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4034
4035 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4036 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4037 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4038 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4039 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4040}
4041
Arthur Hung2141d542022-08-23 07:45:21 +00004042TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4043 const int32_t USAGE_A = 0x070004;
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4045 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4046
4047 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004048 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004049 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4050 // Key down by scan code.
4051 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4052 NotifyKeyArgs args;
4053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4054 ASSERT_EQ(DEVICE_ID, args.deviceId);
4055 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4056 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4057 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4058 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4059 ASSERT_EQ(KEY_HOME, args.scanCode);
4060 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4061
4062 // Disable device, it should synthesize cancellation events for down events.
4063 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004064 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004065
4066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4067 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4068 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4069 ASSERT_EQ(KEY_HOME, args.scanCode);
4070 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4071}
4072
Zixuan Qufecb6062022-11-12 04:44:31 +00004073TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004074 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4075 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004076 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004077 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4078 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004079
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004080 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004081 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4082
4083 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004084 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004085
4086 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4087 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4088 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4089 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4090 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004091 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4092
4093 // Call change layout association with the same values: Generation shouldn't change
4094 generation = mReader->getContext()->getGeneration();
4095 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4096 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4097 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4098 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004099}
4100
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004101TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4102 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4103 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4104
4105 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004106 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004107 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4108 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004109 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004110
4111 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4112 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4113}
4114
Justin Chung71ddb432023-03-27 04:29:07 +00004115TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4116 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4117 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004118 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004119 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4120 NotifyKeyArgs args;
4121
4122 // Key down
4123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4126}
4127
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004128// --- KeyboardInputMapperTest_ExternalDevice ---
4129
4130class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4131protected:
Chris Yea52ade12020-08-27 16:49:20 -07004132 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004133};
4134
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004135TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004136 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4137 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004138
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004139 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4140 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4141 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4142 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004144 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004145 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004146 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004147
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004148 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004149 NotifyKeyArgs args;
4150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4155 ASSERT_EQ(uint32_t(0), args.policyFlags);
4156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(uint32_t(0), args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4172}
4173
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004174TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4175 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4176 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4177
4178 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4179 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4180 POLICY_FLAG_WAKE);
4181
4182 KeyboardInputMapper& mapper =
4183 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4184 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4185
4186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4187 NotifyKeyArgs args;
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4189 ASSERT_EQ(uint32_t(0), args.policyFlags);
4190
4191 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4193 ASSERT_EQ(uint32_t(0), args.policyFlags);
4194
4195 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4197 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4198
4199 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4201 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4202}
4203
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004204TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004205 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004206
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004207 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004210
Powei Fengd041c5d2019-05-03 17:11:33 -07004211 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004212 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004213 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004214 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004215
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004217 NotifyKeyArgs args;
4218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4219 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4220
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004221 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4227 ASSERT_EQ(uint32_t(0), args.policyFlags);
4228
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004229 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4231 ASSERT_EQ(uint32_t(0), args.policyFlags);
4232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4235 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4236
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004237 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4239 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4240}
4241
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242// --- TouchInputMapperTest ---
4243
4244class TouchInputMapperTest : public InputMapperTest {
4245protected:
4246 static const int32_t RAW_X_MIN;
4247 static const int32_t RAW_X_MAX;
4248 static const int32_t RAW_Y_MIN;
4249 static const int32_t RAW_Y_MAX;
4250 static const int32_t RAW_TOUCH_MIN;
4251 static const int32_t RAW_TOUCH_MAX;
4252 static const int32_t RAW_TOOL_MIN;
4253 static const int32_t RAW_TOOL_MAX;
4254 static const int32_t RAW_PRESSURE_MIN;
4255 static const int32_t RAW_PRESSURE_MAX;
4256 static const int32_t RAW_ORIENTATION_MIN;
4257 static const int32_t RAW_ORIENTATION_MAX;
4258 static const int32_t RAW_DISTANCE_MIN;
4259 static const int32_t RAW_DISTANCE_MAX;
4260 static const int32_t RAW_TILT_MIN;
4261 static const int32_t RAW_TILT_MAX;
4262 static const int32_t RAW_ID_MIN;
4263 static const int32_t RAW_ID_MAX;
4264 static const int32_t RAW_SLOT_MIN;
4265 static const int32_t RAW_SLOT_MAX;
4266 static const float X_PRECISION;
4267 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004268 static const float X_PRECISION_VIRTUAL;
4269 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270
4271 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004272 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273
4274 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4275
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004276 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004277 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004278
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 enum Axes {
4280 POSITION = 1 << 0,
4281 TOUCH = 1 << 1,
4282 TOOL = 1 << 2,
4283 PRESSURE = 1 << 3,
4284 ORIENTATION = 1 << 4,
4285 MINOR = 1 << 5,
4286 ID = 1 << 6,
4287 DISTANCE = 1 << 7,
4288 TILT = 1 << 8,
4289 SLOT = 1 << 9,
4290 TOOL_TYPE = 1 << 10,
4291 };
4292
Michael Wrighta9cf4192022-12-01 23:46:39 +00004293 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004294 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004295 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004296 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004297 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004298 int32_t toRawX(float displayX);
4299 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004300 int32_t toRotatedRawX(float displayX);
4301 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004302 float toCookedX(float rawX, float rawY);
4303 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004304 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004305 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004306 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004307 float toDisplayY(int32_t rawY, int32_t displayHeight);
4308
Michael Wrightd02c5b62014-02-10 15:10:22 -08004309};
4310
4311const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4312const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4313const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4314const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4315const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4316const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4317const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4318const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004319const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4320const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4322const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4323const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4324const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4325const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4326const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4327const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4328const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4329const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4330const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4331const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4332const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004333const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4334 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4335const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4336 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004337const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4338 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004339
4340const float TouchInputMapperTest::GEOMETRIC_SCALE =
4341 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4342 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4343
4344const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4345 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4346 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4347};
4348
Michael Wrighta9cf4192022-12-01 23:46:39 +00004349void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004350 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4351 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004352}
4353
4354void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4355 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004356 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357}
4358
Michael Wrighta9cf4192022-12-01 23:46:39 +00004359void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004360 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4361 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4362 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004363}
4364
Michael Wrightd02c5b62014-02-10 15:10:22 -08004365void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004366 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4367 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4368 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004370}
4371
Jason Gerecke489fda82012-09-07 17:19:40 -07004372void TouchInputMapperTest::prepareLocationCalibration() {
4373 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4374}
4375
Michael Wrightd02c5b62014-02-10 15:10:22 -08004376int32_t TouchInputMapperTest::toRawX(float displayX) {
4377 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4378}
4379
4380int32_t TouchInputMapperTest::toRawY(float displayY) {
4381 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4382}
4383
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004384int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4385 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4386}
4387
4388int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4389 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4390}
4391
Jason Gerecke489fda82012-09-07 17:19:40 -07004392float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4393 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4394 return rawX;
4395}
4396
4397float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4398 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4399 return rawY;
4400}
4401
Michael Wrightd02c5b62014-02-10 15:10:22 -08004402float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004403 return toDisplayX(rawX, DISPLAY_WIDTH);
4404}
4405
4406float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4407 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004408}
4409
4410float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004411 return toDisplayY(rawY, DISPLAY_HEIGHT);
4412}
4413
4414float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4415 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004416}
4417
4418
4419// --- SingleTouchInputMapperTest ---
4420
4421class SingleTouchInputMapperTest : public TouchInputMapperTest {
4422protected:
4423 void prepareButtons();
4424 void prepareAxes(int axes);
4425
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004426 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4427 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4428 void processUp(SingleTouchInputMapper& mappery);
4429 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4430 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4431 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4432 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4433 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4434 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435};
4436
4437void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004438 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004439}
4440
4441void SingleTouchInputMapperTest::prepareAxes(int axes) {
4442 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 }
4446 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4448 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 }
4450 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4452 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 }
4454 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4456 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 }
4458 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004459 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4460 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 }
4462}
4463
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004464void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004465 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004468}
4469
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004470void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004471 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473}
4474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004475void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477}
4478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004479void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004480 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004481}
4482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004483void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4484 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486}
4487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004488void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004489 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004490}
4491
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004492void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4493 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004494 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004496}
4497
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004498void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4499 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501}
4502
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004503void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004505}
4506
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508 prepareButtons();
4509 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004510 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511
Josep del Río2d8c79a2023-01-23 19:33:50 +00004512 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004513}
4514
Michael Wrightd02c5b62014-02-10 15:10:22 -08004515TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 prepareButtons();
4517 prepareAxes(POSITION);
4518 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004519 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004521 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004522}
4523
4524TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004525 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004526 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527 prepareButtons();
4528 prepareAxes(POSITION);
4529 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004530 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004531
4532 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004533 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 // Virtual key is down.
4536 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4537 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4538 processDown(mapper, x, y);
4539 processSync(mapper);
4540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4541
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004542 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004543
4544 // Virtual key is up.
4545 processUp(mapper);
4546 processSync(mapper);
4547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4548
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004549 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004550}
4551
4552TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004553 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004554 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555 prepareButtons();
4556 prepareAxes(POSITION);
4557 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004558 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004559
4560 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004561 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004562
4563 // Virtual key is down.
4564 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4565 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4566 processDown(mapper, x, y);
4567 processSync(mapper);
4568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4569
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004570 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004571
4572 // Virtual key is up.
4573 processUp(mapper);
4574 processSync(mapper);
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004577 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004578}
4579
4580TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004581 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004582 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583 prepareButtons();
4584 prepareAxes(POSITION);
4585 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004586 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004589 ASSERT_TRUE(
4590 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004591 ASSERT_TRUE(flags[0]);
4592 ASSERT_FALSE(flags[1]);
4593}
4594
4595TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004596 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004597 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598 prepareButtons();
4599 prepareAxes(POSITION);
4600 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004601 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004602
arthurhungdcef2dc2020-08-11 14:47:50 +08004603 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004604
4605 NotifyKeyArgs args;
4606
4607 // Press virtual key.
4608 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4609 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4610 processDown(mapper, x, y);
4611 processSync(mapper);
4612
4613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4614 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4615 ASSERT_EQ(DEVICE_ID, args.deviceId);
4616 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4617 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4618 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4619 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4620 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4621 ASSERT_EQ(KEY_HOME, args.scanCode);
4622 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4623 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4624
4625 // Release virtual key.
4626 processUp(mapper);
4627 processSync(mapper);
4628
4629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4630 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4631 ASSERT_EQ(DEVICE_ID, args.deviceId);
4632 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4633 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4634 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4635 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4636 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4637 ASSERT_EQ(KEY_HOME, args.scanCode);
4638 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4639 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4640
4641 // Should not have sent any motions.
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4643}
4644
4645TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004646 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004647 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648 prepareButtons();
4649 prepareAxes(POSITION);
4650 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004651 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004652
arthurhungdcef2dc2020-08-11 14:47:50 +08004653 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004654
4655 NotifyKeyArgs keyArgs;
4656
4657 // Press virtual key.
4658 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4659 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4660 processDown(mapper, x, y);
4661 processSync(mapper);
4662
4663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4664 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4665 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4666 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4667 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4668 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4669 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4670 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4671 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4672 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4673 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4674
4675 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4676 // into the display area.
4677 y -= 100;
4678 processMove(mapper, x, y);
4679 processSync(mapper);
4680
4681 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4682 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4683 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4684 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4685 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4686 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4687 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4688 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4689 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4690 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4691 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4692 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4693
4694 NotifyMotionArgs motionArgs;
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4697 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4699 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4701 ASSERT_EQ(0, motionArgs.flags);
4702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4703 ASSERT_EQ(0, motionArgs.buttonState);
4704 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004705 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004707 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4709 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4710 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4711 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4713
4714 // Keep moving out of bounds. Should generate a pointer move.
4715 y -= 50;
4716 processMove(mapper, x, y);
4717 processSync(mapper);
4718
4719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4720 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4721 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4722 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4723 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4725 ASSERT_EQ(0, motionArgs.flags);
4726 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4727 ASSERT_EQ(0, motionArgs.buttonState);
4728 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004729 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004730 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004731 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004732 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4733 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4734 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4735 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4736 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4737
4738 // Release out of bounds. Should generate a pointer up.
4739 processUp(mapper);
4740 processSync(mapper);
4741
4742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4743 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4744 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4745 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4746 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4747 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4748 ASSERT_EQ(0, motionArgs.flags);
4749 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4750 ASSERT_EQ(0, motionArgs.buttonState);
4751 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004752 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004754 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004755 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4756 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4757 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4758 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4759 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4760
4761 // Should not have sent any more keys or motions.
4762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4764}
4765
4766TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004767 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004768 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769 prepareButtons();
4770 prepareAxes(POSITION);
4771 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004772 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004773
arthurhungdcef2dc2020-08-11 14:47:50 +08004774 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004775
4776 NotifyMotionArgs motionArgs;
4777
4778 // Initially go down out of bounds.
4779 int32_t x = -10;
4780 int32_t y = -10;
4781 processDown(mapper, x, y);
4782 processSync(mapper);
4783
4784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4785
4786 // Move into the display area. Should generate a pointer down.
4787 x = 50;
4788 y = 75;
4789 processMove(mapper, x, y);
4790 processSync(mapper);
4791
4792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4793 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4794 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4795 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4796 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4798 ASSERT_EQ(0, motionArgs.flags);
4799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4800 ASSERT_EQ(0, motionArgs.buttonState);
4801 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004802 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4806 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4807 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4808 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4809 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4810
4811 // Release. Should generate a pointer up.
4812 processUp(mapper);
4813 processSync(mapper);
4814
4815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4817 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4818 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4819 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4820 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4821 ASSERT_EQ(0, motionArgs.flags);
4822 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4823 ASSERT_EQ(0, motionArgs.buttonState);
4824 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004825 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004827 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4829 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4830 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4831 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4832 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4833
4834 // Should not have sent any more keys or motions.
4835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4837}
4838
Santos Cordonfa5cf462017-04-05 10:37:00 -07004839TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004840 addConfigurationProperty("touch.deviceType", "touchScreen");
4841 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4842
Michael Wrighta9cf4192022-12-01 23:46:39 +00004843 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004844 prepareButtons();
4845 prepareAxes(POSITION);
4846 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004847 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004848
arthurhungdcef2dc2020-08-11 14:47:50 +08004849 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004850
4851 NotifyMotionArgs motionArgs;
4852
4853 // Down.
4854 int32_t x = 100;
4855 int32_t y = 125;
4856 processDown(mapper, x, y);
4857 processSync(mapper);
4858
4859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4860 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4861 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4862 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4863 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4866 ASSERT_EQ(0, motionArgs.flags);
4867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4868 ASSERT_EQ(0, motionArgs.buttonState);
4869 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004870 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004872 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4874 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4875 1, 0, 0, 0, 0, 0, 0, 0));
4876 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4877 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4878 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4879
4880 // Move.
4881 x += 50;
4882 y += 75;
4883 processMove(mapper, x, y);
4884 processSync(mapper);
4885
4886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4888 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4889 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4890 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4891 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4892 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4893 ASSERT_EQ(0, motionArgs.flags);
4894 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4895 ASSERT_EQ(0, motionArgs.buttonState);
4896 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004897 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004898 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004899 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004900 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4901 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4902 1, 0, 0, 0, 0, 0, 0, 0));
4903 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4904 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4905 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4906
4907 // Up.
4908 processUp(mapper);
4909 processSync(mapper);
4910
4911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4912 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4913 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4914 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4915 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4916 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4917 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4918 ASSERT_EQ(0, motionArgs.flags);
4919 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4920 ASSERT_EQ(0, motionArgs.buttonState);
4921 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004922 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004923 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004924 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004925 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4926 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4927 1, 0, 0, 0, 0, 0, 0, 0));
4928 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4929 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4930 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4931
4932 // Should not have sent any more keys or motions.
4933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4935}
4936
Michael Wrightd02c5b62014-02-10 15:10:22 -08004937TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004939 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940 prepareButtons();
4941 prepareAxes(POSITION);
4942 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004943 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004944
arthurhungdcef2dc2020-08-11 14:47:50 +08004945 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004946
4947 NotifyMotionArgs motionArgs;
4948
4949 // Down.
4950 int32_t x = 100;
4951 int32_t y = 125;
4952 processDown(mapper, x, y);
4953 processSync(mapper);
4954
4955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4956 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4957 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4958 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4959 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4960 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4961 ASSERT_EQ(0, motionArgs.flags);
4962 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4963 ASSERT_EQ(0, motionArgs.buttonState);
4964 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004965 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004966 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004967 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004968 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4969 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4970 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4971 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4972 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4973
4974 // Move.
4975 x += 50;
4976 y += 75;
4977 processMove(mapper, x, y);
4978 processSync(mapper);
4979
4980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4981 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4982 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4983 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4984 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4985 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4986 ASSERT_EQ(0, motionArgs.flags);
4987 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4988 ASSERT_EQ(0, motionArgs.buttonState);
4989 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004990 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004991 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004992 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004993 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4994 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4995 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4996 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4997 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4998
4999 // Up.
5000 processUp(mapper);
5001 processSync(mapper);
5002
5003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5004 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5005 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5006 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5007 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5008 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5009 ASSERT_EQ(0, motionArgs.flags);
5010 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5011 ASSERT_EQ(0, motionArgs.buttonState);
5012 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005013 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005014 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005015 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005016 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5017 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5018 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5019 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5020 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5021
5022 // Should not have sent any more keys or motions.
5023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5025}
5026
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005027TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005028 addConfigurationProperty("touch.deviceType", "touchScreen");
5029 prepareButtons();
5030 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005031 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5032 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005033 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005034
5035 NotifyMotionArgs args;
5036
5037 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005038 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005039 processDown(mapper, toRawX(50), toRawY(75));
5040 processSync(mapper);
5041
5042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5043 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5044 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5045
5046 processUp(mapper);
5047 processSync(mapper);
5048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5049}
5050
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005051TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005052 addConfigurationProperty("touch.deviceType", "touchScreen");
5053 prepareButtons();
5054 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005055 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5056 // orientation-aware are affected by display rotation.
5057 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005058 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005059
5060 NotifyMotionArgs args;
5061
5062 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005063 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005064 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005065 processDown(mapper, toRawX(50), toRawY(75));
5066 processSync(mapper);
5067
5068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5069 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5070 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5071
5072 processUp(mapper);
5073 processSync(mapper);
5074 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5075
5076 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005077 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005078 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005079 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005080 processSync(mapper);
5081
5082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5083 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5084 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5085
5086 processUp(mapper);
5087 processSync(mapper);
5088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5089
5090 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005091 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005092 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005093 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5094 processSync(mapper);
5095
5096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5097 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5098 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5099
5100 processUp(mapper);
5101 processSync(mapper);
5102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5103
5104 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005105 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005106 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005107 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005108 processSync(mapper);
5109
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5111 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5112 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5113
5114 processUp(mapper);
5115 processSync(mapper);
5116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5117}
5118
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005119TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5120 addConfigurationProperty("touch.deviceType", "touchScreen");
5121 prepareButtons();
5122 prepareAxes(POSITION);
5123 addConfigurationProperty("touch.orientationAware", "1");
5124 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5125 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005126 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005127 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005128 NotifyMotionArgs args;
5129
5130 // Orientation 0.
5131 processDown(mapper, toRawX(50), toRawY(75));
5132 processSync(mapper);
5133
5134 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5135 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5136 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5137
5138 processUp(mapper);
5139 processSync(mapper);
5140 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5141}
5142
5143TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5144 addConfigurationProperty("touch.deviceType", "touchScreen");
5145 prepareButtons();
5146 prepareAxes(POSITION);
5147 addConfigurationProperty("touch.orientationAware", "1");
5148 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5149 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005150 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005151 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005152 NotifyMotionArgs args;
5153
5154 // Orientation 90.
5155 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5156 processSync(mapper);
5157
5158 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5159 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5160 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5161
5162 processUp(mapper);
5163 processSync(mapper);
5164 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5165}
5166
5167TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5168 addConfigurationProperty("touch.deviceType", "touchScreen");
5169 prepareButtons();
5170 prepareAxes(POSITION);
5171 addConfigurationProperty("touch.orientationAware", "1");
5172 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5173 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005174 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005175 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005176 NotifyMotionArgs args;
5177
5178 // Orientation 180.
5179 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5180 processSync(mapper);
5181
5182 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5183 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5184 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5185
5186 processUp(mapper);
5187 processSync(mapper);
5188 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5189}
5190
5191TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5192 addConfigurationProperty("touch.deviceType", "touchScreen");
5193 prepareButtons();
5194 prepareAxes(POSITION);
5195 addConfigurationProperty("touch.orientationAware", "1");
5196 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5197 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005198 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005199 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005200 NotifyMotionArgs args;
5201
5202 // Orientation 270.
5203 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5204 processSync(mapper);
5205
5206 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5207 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5208 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5209
5210 processUp(mapper);
5211 processSync(mapper);
5212 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5213}
5214
5215TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5216 addConfigurationProperty("touch.deviceType", "touchScreen");
5217 prepareButtons();
5218 prepareAxes(POSITION);
5219 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5220 // orientation-aware are affected by display rotation.
5221 addConfigurationProperty("touch.orientationAware", "0");
5222 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005223 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005224
5225 NotifyMotionArgs args;
5226
5227 // Orientation 90, Rotation 0.
5228 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005229 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005230 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5231 processSync(mapper);
5232
5233 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5234 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5235 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5236
5237 processUp(mapper);
5238 processSync(mapper);
5239 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5240
5241 // Orientation 90, Rotation 90.
5242 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005243 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005244 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005245 processSync(mapper);
5246
5247 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5248 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5249 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5250
5251 processUp(mapper);
5252 processSync(mapper);
5253 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5254
5255 // Orientation 90, Rotation 180.
5256 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005257 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005258 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5259 processSync(mapper);
5260
5261 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5262 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5263 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5264
5265 processUp(mapper);
5266 processSync(mapper);
5267 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5268
5269 // Orientation 90, Rotation 270.
5270 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005271 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005272 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 -07005273 processSync(mapper);
5274
5275 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5276 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5277 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5278
5279 processUp(mapper);
5280 processSync(mapper);
5281 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5282}
5283
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005284TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5285 addConfigurationProperty("touch.deviceType", "touchScreen");
5286 prepareButtons();
5287 prepareAxes(POSITION);
5288 addConfigurationProperty("touch.orientationAware", "1");
5289 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005290 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005291
5292 // Set a physical frame in the display viewport.
5293 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5294 viewport->physicalLeft = 20;
5295 viewport->physicalTop = 600;
5296 viewport->physicalRight = 30;
5297 viewport->physicalBottom = 610;
5298 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005299 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005300
5301 // Start the touch.
5302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5303 processSync(mapper);
5304
5305 // Expect all input starting outside the physical frame to be ignored.
5306 const std::array<Point, 6> outsidePoints = {
5307 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5308 for (const auto& p : outsidePoints) {
5309 processMove(mapper, toRawX(p.x), toRawY(p.y));
5310 processSync(mapper);
5311 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5312 }
5313
5314 // Move the touch into the physical frame.
5315 processMove(mapper, toRawX(25), toRawY(605));
5316 processSync(mapper);
5317 NotifyMotionArgs args;
5318 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5319 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5320 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5321 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5322
5323 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5324 for (const auto& p : outsidePoints) {
5325 processMove(mapper, toRawX(p.x), toRawY(p.y));
5326 processSync(mapper);
5327 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5328 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5329 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5330 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5331 }
5332
5333 processUp(mapper);
5334 processSync(mapper);
5335 EXPECT_NO_FATAL_FAILURE(
5336 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5337}
5338
Harry Cutts1db43992023-06-19 17:05:07 +00005339TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005340 addConfigurationProperty("touch.deviceType", "pointer");
5341 prepareAxes(POSITION);
5342 prepareDisplay(ui::ROTATION_0);
5343 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5344
5345 // Set a physical frame in the display viewport.
5346 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5347 viewport->physicalLeft = 20;
5348 viewport->physicalTop = 600;
5349 viewport->physicalRight = 30;
5350 viewport->physicalBottom = 610;
5351 mFakePolicy->updateViewport(*viewport);
5352 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5353
5354 // Start the touch.
5355 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5356 processSync(mapper);
5357
5358 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5359 // produced.
5360 const std::array<Point, 6> outsidePoints = {
5361 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5362 for (const auto& p : outsidePoints) {
5363 processMove(mapper, toRawX(p.x), toRawY(p.y));
5364 processSync(mapper);
5365 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5366 }
5367}
5368
Michael Wrightd02c5b62014-02-10 15:10:22 -08005369TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005371 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005372 prepareButtons();
5373 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005374 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005375
5376 // These calculations are based on the input device calibration documentation.
5377 int32_t rawX = 100;
5378 int32_t rawY = 200;
5379 int32_t rawPressure = 10;
5380 int32_t rawToolMajor = 12;
5381 int32_t rawDistance = 2;
5382 int32_t rawTiltX = 30;
5383 int32_t rawTiltY = 110;
5384
5385 float x = toDisplayX(rawX);
5386 float y = toDisplayY(rawY);
5387 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5388 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5389 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5390 float distance = float(rawDistance);
5391
5392 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5393 float tiltScale = M_PI / 180;
5394 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5395 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5396 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5397 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5398
5399 processDown(mapper, rawX, rawY);
5400 processPressure(mapper, rawPressure);
5401 processToolMajor(mapper, rawToolMajor);
5402 processDistance(mapper, rawDistance);
5403 processTilt(mapper, rawTiltX, rawTiltY);
5404 processSync(mapper);
5405
5406 NotifyMotionArgs args;
5407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5409 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5410 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5411}
5412
Jason Gerecke489fda82012-09-07 17:19:40 -07005413TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005414 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005415 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005416 prepareLocationCalibration();
5417 prepareButtons();
5418 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005419 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005420
5421 int32_t rawX = 100;
5422 int32_t rawY = 200;
5423
5424 float x = toDisplayX(toCookedX(rawX, rawY));
5425 float y = toDisplayY(toCookedY(rawX, rawY));
5426
5427 processDown(mapper, rawX, rawY);
5428 processSync(mapper);
5429
5430 NotifyMotionArgs args;
5431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5432 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5433 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5434}
5435
Michael Wrightd02c5b62014-02-10 15:10:22 -08005436TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005438 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005439 prepareButtons();
5440 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005441 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442
5443 NotifyMotionArgs motionArgs;
5444 NotifyKeyArgs keyArgs;
5445
5446 processDown(mapper, 100, 200);
5447 processSync(mapper);
5448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5449 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5450 ASSERT_EQ(0, motionArgs.buttonState);
5451
5452 // press BTN_LEFT, release BTN_LEFT
5453 processKey(mapper, BTN_LEFT, 1);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5457 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5458
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5460 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5461 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5462
Michael Wrightd02c5b62014-02-10 15:10:22 -08005463 processKey(mapper, BTN_LEFT, 0);
5464 processSync(mapper);
5465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005466 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005467 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005468
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005471 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005472
5473 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5474 processKey(mapper, BTN_RIGHT, 1);
5475 processKey(mapper, BTN_MIDDLE, 1);
5476 processSync(mapper);
5477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5479 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5480 motionArgs.buttonState);
5481
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5483 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5484 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5485
5486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5488 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5489 motionArgs.buttonState);
5490
Michael Wrightd02c5b62014-02-10 15:10:22 -08005491 processKey(mapper, BTN_RIGHT, 0);
5492 processSync(mapper);
5493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005494 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005496
5497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005498 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005499 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500
5501 processKey(mapper, BTN_MIDDLE, 0);
5502 processSync(mapper);
5503 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005504 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005505 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005506
5507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005508 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005509 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005510
5511 // press BTN_BACK, release BTN_BACK
5512 processKey(mapper, BTN_BACK, 1);
5513 processSync(mapper);
5514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5515 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5516 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005517
Michael Wrightd02c5b62014-02-10 15:10:22 -08005518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005520 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5521
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5523 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5524 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005525
5526 processKey(mapper, BTN_BACK, 0);
5527 processSync(mapper);
5528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005529 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531
5532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005533 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005534 ASSERT_EQ(0, motionArgs.buttonState);
5535
Michael Wrightd02c5b62014-02-10 15:10:22 -08005536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5537 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5538 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5539
5540 // press BTN_SIDE, release BTN_SIDE
5541 processKey(mapper, BTN_SIDE, 1);
5542 processSync(mapper);
5543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5544 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5545 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005546
Michael Wrightd02c5b62014-02-10 15:10:22 -08005547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005549 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5550
5551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5552 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5553 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005554
5555 processKey(mapper, BTN_SIDE, 0);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005558 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005559 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005560
5561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005562 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005563 ASSERT_EQ(0, motionArgs.buttonState);
5564
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5567 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5568
5569 // press BTN_FORWARD, release BTN_FORWARD
5570 processKey(mapper, BTN_FORWARD, 1);
5571 processSync(mapper);
5572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5573 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5574 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005575
Michael Wrightd02c5b62014-02-10 15:10:22 -08005576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005578 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5579
5580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5581 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5582 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005583
5584 processKey(mapper, BTN_FORWARD, 0);
5585 processSync(mapper);
5586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005587 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005588 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005589
5590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005591 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005592 ASSERT_EQ(0, motionArgs.buttonState);
5593
Michael Wrightd02c5b62014-02-10 15:10:22 -08005594 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5595 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5596 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5597
5598 // press BTN_EXTRA, release BTN_EXTRA
5599 processKey(mapper, BTN_EXTRA, 1);
5600 processSync(mapper);
5601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5602 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5603 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005604
Michael Wrightd02c5b62014-02-10 15:10:22 -08005605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005607 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5608
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5610 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5611 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005612
5613 processKey(mapper, BTN_EXTRA, 0);
5614 processSync(mapper);
5615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005616 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005617 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005618
5619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005620 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005621 ASSERT_EQ(0, motionArgs.buttonState);
5622
Michael Wrightd02c5b62014-02-10 15:10:22 -08005623 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5624 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5625 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5626
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5628
Michael Wrightd02c5b62014-02-10 15:10:22 -08005629 // press BTN_STYLUS, release BTN_STYLUS
5630 processKey(mapper, BTN_STYLUS, 1);
5631 processSync(mapper);
5632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5633 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005634 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5635
5636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5638 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005639
5640 processKey(mapper, BTN_STYLUS, 0);
5641 processSync(mapper);
5642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005643 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005644 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005645
5646 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005647 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005648 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005649
5650 // press BTN_STYLUS2, release BTN_STYLUS2
5651 processKey(mapper, BTN_STYLUS2, 1);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005655 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5656
5657 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5658 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5659 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005660
5661 processKey(mapper, BTN_STYLUS2, 0);
5662 processSync(mapper);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005664 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005665 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005666
5667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005669 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005670
5671 // release touch
5672 processUp(mapper);
5673 processSync(mapper);
5674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5675 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5676 ASSERT_EQ(0, motionArgs.buttonState);
5677}
5678
5679TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005680 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005681 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005682 prepareButtons();
5683 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005684 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685
5686 NotifyMotionArgs motionArgs;
5687
5688 // default tool type is finger
5689 processDown(mapper, 100, 200);
5690 processSync(mapper);
5691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5692 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005693 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005694
5695 // eraser
5696 processKey(mapper, BTN_TOOL_RUBBER, 1);
5697 processSync(mapper);
5698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5699 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005700 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005701
5702 // stylus
5703 processKey(mapper, BTN_TOOL_RUBBER, 0);
5704 processKey(mapper, BTN_TOOL_PEN, 1);
5705 processSync(mapper);
5706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5707 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005708 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005709
5710 // brush
5711 processKey(mapper, BTN_TOOL_PEN, 0);
5712 processKey(mapper, BTN_TOOL_BRUSH, 1);
5713 processSync(mapper);
5714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5715 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005716 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005717
5718 // pencil
5719 processKey(mapper, BTN_TOOL_BRUSH, 0);
5720 processKey(mapper, BTN_TOOL_PENCIL, 1);
5721 processSync(mapper);
5722 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5723 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005724 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005725
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005726 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005727 processKey(mapper, BTN_TOOL_PENCIL, 0);
5728 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5729 processSync(mapper);
5730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5731 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005732 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005733
5734 // mouse
5735 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5736 processKey(mapper, BTN_TOOL_MOUSE, 1);
5737 processSync(mapper);
5738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5739 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005740 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005741
5742 // lens
5743 processKey(mapper, BTN_TOOL_MOUSE, 0);
5744 processKey(mapper, BTN_TOOL_LENS, 1);
5745 processSync(mapper);
5746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005748 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005749
5750 // double-tap
5751 processKey(mapper, BTN_TOOL_LENS, 0);
5752 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5753 processSync(mapper);
5754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5755 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005756 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005757
5758 // triple-tap
5759 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5760 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5761 processSync(mapper);
5762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5763 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005764 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766 // quad-tap
5767 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5768 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5769 processSync(mapper);
5770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5771 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005772 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005773
5774 // finger
5775 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5776 processKey(mapper, BTN_TOOL_FINGER, 1);
5777 processSync(mapper);
5778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5779 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005781
5782 // stylus trumps finger
5783 processKey(mapper, BTN_TOOL_PEN, 1);
5784 processSync(mapper);
5785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5786 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005787 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005788
5789 // eraser trumps stylus
5790 processKey(mapper, BTN_TOOL_RUBBER, 1);
5791 processSync(mapper);
5792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005794 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005795
5796 // mouse trumps eraser
5797 processKey(mapper, BTN_TOOL_MOUSE, 1);
5798 processSync(mapper);
5799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5800 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005801 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802
5803 // back to default tool type
5804 processKey(mapper, BTN_TOOL_MOUSE, 0);
5805 processKey(mapper, BTN_TOOL_RUBBER, 0);
5806 processKey(mapper, BTN_TOOL_PEN, 0);
5807 processKey(mapper, BTN_TOOL_FINGER, 0);
5808 processSync(mapper);
5809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5810 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005811 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005812}
5813
5814TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005815 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005816 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005817 prepareButtons();
5818 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005819 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005820 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005821
5822 NotifyMotionArgs motionArgs;
5823
5824 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5825 processKey(mapper, BTN_TOOL_FINGER, 1);
5826 processMove(mapper, 100, 200);
5827 processSync(mapper);
5828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5829 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5831 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5832
5833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5836 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5837
5838 // move a little
5839 processMove(mapper, 150, 250);
5840 processSync(mapper);
5841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5842 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5843 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5844 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5845
5846 // down when BTN_TOUCH is pressed, pressure defaults to 1
5847 processKey(mapper, BTN_TOUCH, 1);
5848 processSync(mapper);
5849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5850 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5852 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5853
5854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5855 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5857 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5858
5859 // up when BTN_TOUCH is released, hover restored
5860 processKey(mapper, BTN_TOUCH, 0);
5861 processSync(mapper);
5862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5863 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5864 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5865 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5866
5867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5868 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5870 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5871
5872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5873 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5875 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5876
5877 // exit hover when pointer goes away
5878 processKey(mapper, BTN_TOOL_FINGER, 0);
5879 processSync(mapper);
5880 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5881 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5882 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5883 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5884}
5885
5886TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005888 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005889 prepareButtons();
5890 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005891 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005892
5893 NotifyMotionArgs motionArgs;
5894
5895 // initially hovering because pressure is 0
5896 processDown(mapper, 100, 200);
5897 processPressure(mapper, 0);
5898 processSync(mapper);
5899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5900 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5902 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5903
5904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5905 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5906 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5907 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5908
5909 // move a little
5910 processMove(mapper, 150, 250);
5911 processSync(mapper);
5912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5913 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5914 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5915 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5916
5917 // down when pressure is non-zero
5918 processPressure(mapper, RAW_PRESSURE_MAX);
5919 processSync(mapper);
5920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5921 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5922 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5923 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5924
5925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5926 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5927 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5928 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5929
5930 // up when pressure becomes 0, hover restored
5931 processPressure(mapper, 0);
5932 processSync(mapper);
5933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5934 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5935 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5936 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5937
5938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5939 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5941 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5942
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5944 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5945 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5946 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5947
5948 // exit hover when pointer goes away
5949 processUp(mapper);
5950 processSync(mapper);
5951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5952 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5954 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5955}
5956
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005957TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5958 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005959 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005960 prepareButtons();
5961 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005962 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005963
5964 // Touch down.
5965 processDown(mapper, 100, 200);
5966 processPressure(mapper, 1);
5967 processSync(mapper);
5968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5969 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5970
5971 // Reset the mapper. This should cancel the ongoing gesture.
5972 resetMapper(mapper, ARBITRARY_TIME);
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5974 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5975
5976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5977}
5978
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005979TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5980 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005981 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005982 prepareButtons();
5983 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005984 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005985
5986 // Set the initial state for the touch pointer.
5987 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5988 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5989 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5990 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5991
5992 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005993 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
5994 // does not generate any events.
5995 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005996
5997 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
5998 // the recreated touch state to generate a down event.
5999 processSync(mapper);
6000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6001 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6002
6003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6004}
6005
lilinnan687e58f2022-07-19 16:00:50 +08006006TEST_F(SingleTouchInputMapperTest,
6007 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006009 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006010 prepareButtons();
6011 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006012 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006013 NotifyMotionArgs motionArgs;
6014
6015 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006016 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006017 processSync(mapper);
6018
6019 // We should receive a down event
6020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6021 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6022
6023 // Change display id
6024 clearViewports();
6025 prepareSecondaryDisplay(ViewportType::INTERNAL);
6026
6027 // We should receive a cancel event
6028 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6029 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6030 // Then receive reset called
6031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6032}
6033
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006034TEST_F(SingleTouchInputMapperTest,
6035 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6036 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006037 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006038 prepareButtons();
6039 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006040 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6042 NotifyMotionArgs motionArgs;
6043
6044 // Start a new gesture.
6045 processDown(mapper, 100, 200);
6046 processSync(mapper);
6047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6048 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6049
6050 // Make the viewport inactive. This will put the device in disabled mode.
6051 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6052 viewport->isActive = false;
6053 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006054 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006055
6056 // We should receive a cancel event for the ongoing gesture.
6057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6058 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6059 // Then we should be notified that the device was reset.
6060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6061
6062 // No events are generated while the viewport is inactive.
6063 processMove(mapper, 101, 201);
6064 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006065 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006066 processSync(mapper);
6067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6068
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006069 // Start a new gesture while the viewport is still inactive.
6070 processDown(mapper, 300, 400);
6071 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6072 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6073 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6074 processSync(mapper);
6075
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006076 // Make the viewport active again. The device should resume processing events.
6077 viewport->isActive = true;
6078 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006079 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006080
6081 // The device is reset because it changes back to direct mode, without generating any events.
6082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6084
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006085 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006086 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6088 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006089
6090 // No more events.
6091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6093}
6094
Prabir Pradhan211ba622022-10-31 21:09:21 +00006095TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6096 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006097 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006098 prepareButtons();
6099 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006100 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6102
6103 // Press a stylus button.
6104 processKey(mapper, BTN_STYLUS, 1);
6105 processSync(mapper);
6106
6107 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6108 processDown(mapper, 100, 200);
6109 processSync(mapper);
6110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6111 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6112 WithCoords(toDisplayX(100), toDisplayY(200)),
6113 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6114 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6116 WithCoords(toDisplayX(100), toDisplayY(200)),
6117 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6118
6119 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6120 // the button has not actually been released, since there will be no pointers through which the
6121 // button state can be reported. The event is generated at the location of the pointer before
6122 // it went up.
6123 processUp(mapper);
6124 processSync(mapper);
6125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6127 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6129 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6130 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6131}
6132
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006133TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6134 addConfigurationProperty("touch.deviceType", "touchScreen");
6135 prepareDisplay(ui::ROTATION_0);
6136 prepareButtons();
6137 prepareAxes(POSITION);
6138
6139 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6140
Arpit Singha8c236b2023-04-25 13:56:05 +00006141 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6143
6144 // Press a stylus button.
6145 processKey(mapper, BTN_STYLUS, 1);
6146 processSync(mapper);
6147
6148 // Start a touch gesture and ensure that the stylus button is not reported.
6149 processDown(mapper, 100, 200);
6150 processSync(mapper);
6151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6153
6154 // Release and press the stylus button again.
6155 processKey(mapper, BTN_STYLUS, 0);
6156 processSync(mapper);
6157 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6158 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6159 processKey(mapper, BTN_STYLUS, 1);
6160 processSync(mapper);
6161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6162 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6163
6164 // Release the touch gesture.
6165 processUp(mapper);
6166 processSync(mapper);
6167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6168 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6169
6170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6171}
6172
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006173TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6174 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6175 prepareDisplay(ui::ROTATION_0);
6176 prepareButtons();
6177 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006178 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6180
6181 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6182}
6183
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006184TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6185 // Initialize the device without setting device source to touch navigation.
6186 addConfigurationProperty("touch.deviceType", "touchScreen");
6187 prepareDisplay(ui::ROTATION_0);
6188 prepareButtons();
6189 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006190 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006191
6192 // Ensure that the device is created as a touchscreen, not touch navigation.
6193 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6194
6195 // Add device type association after the device was created.
6196 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6197
6198 // Send update to the mapper.
6199 std::list<NotifyArgs> unused2 =
6200 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006201 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006202
6203 // Check whether device type update was successful.
6204 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6205}
6206
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006207TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6208 // Initialize the device without setting device source to touch navigation.
6209 addConfigurationProperty("touch.deviceType", "touchScreen");
6210 prepareDisplay(ui::ROTATION_0);
6211 prepareButtons();
6212 prepareAxes(POSITION);
6213 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6214
6215 // Set a physical frame in the display viewport.
6216 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6217 viewport->physicalLeft = 0;
6218 viewport->physicalTop = 0;
6219 viewport->physicalRight = DISPLAY_WIDTH / 2;
6220 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6221 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006222 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006223
Arpit Singha8c236b2023-04-25 13:56:05 +00006224 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006225
6226 // Hovering inside the physical frame produces events.
6227 processKey(mapper, BTN_TOOL_PEN, 1);
6228 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6229 processSync(mapper);
6230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6231 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6233 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6234
6235 // Leaving the physical frame ends the hovering gesture.
6236 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6237 processSync(mapper);
6238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6239 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6240
6241 // Moving outside the physical frame does not produce events.
6242 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6243 processSync(mapper);
6244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6245
6246 // Re-entering the physical frame produces events.
6247 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6248 processSync(mapper);
6249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6250 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6251 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6252 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6253}
6254
Prabir Pradhan5632d622021-09-06 07:57:20 -07006255// --- TouchDisplayProjectionTest ---
6256
6257class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6258public:
6259 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6260 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6261 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006262 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6263 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6264 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006265 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006266 auto rotatedWidth = naturalDisplayWidth;
6267 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006268 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006269 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006270 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006271 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006272 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006273 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006274 inverseRotationFlags = ui::Transform::ROT_180;
6275 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006276 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006277 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006278 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006279 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006280 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006281 inverseRotationFlags = ui::Transform::ROT_0;
6282 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006283 }
6284
Prabir Pradhana9df3162022-12-05 23:57:27 +00006285 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006286 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6287
6288 std::optional<DisplayViewport> internalViewport =
6289 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6290 DisplayViewport& v = *internalViewport;
6291 v.displayId = DISPLAY_ID;
6292 v.orientation = orientation;
6293
6294 v.logicalLeft = 0;
6295 v.logicalTop = 0;
6296 v.logicalRight = 100;
6297 v.logicalBottom = 100;
6298
6299 v.physicalLeft = rotatedPhysicalDisplay.left;
6300 v.physicalTop = rotatedPhysicalDisplay.top;
6301 v.physicalRight = rotatedPhysicalDisplay.right;
6302 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6303
Prabir Pradhana9df3162022-12-05 23:57:27 +00006304 v.deviceWidth = rotatedWidth;
6305 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006306
6307 v.isActive = true;
6308 v.uniqueId = UNIQUE_ID;
6309 v.type = ViewportType::INTERNAL;
6310 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006311 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006312 }
6313
6314 void assertReceivedMove(const Point& point) {
6315 NotifyMotionArgs motionArgs;
6316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006318 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006319 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6320 1, 0, 0, 0, 0, 0, 0, 0));
6321 }
6322};
6323
6324TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6325 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006326 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006327
6328 prepareButtons();
6329 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006330 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006331
6332 NotifyMotionArgs motionArgs;
6333
6334 // Configure the DisplayViewport such that the logical display maps to a subsection of
6335 // the display panel called the physical display. Here, the physical display is bounded by the
6336 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6337 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6338 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6339 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6340
Michael Wrighta9cf4192022-12-01 23:46:39 +00006341 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006342 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6343
6344 // Touches outside the physical display should be ignored, and should not generate any
6345 // events. Ensure touches at the following points that lie outside of the physical display
6346 // area do not generate any events.
6347 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6348 processDown(mapper, toRawX(point.x), toRawY(point.y));
6349 processSync(mapper);
6350 processUp(mapper);
6351 processSync(mapper);
6352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6353 << "Unexpected event generated for touch outside physical display at point: "
6354 << point.x << ", " << point.y;
6355 }
6356 }
6357}
6358
6359TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6360 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006361 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006362
6363 prepareButtons();
6364 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006365 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006366
6367 NotifyMotionArgs motionArgs;
6368
6369 // Configure the DisplayViewport such that the logical display maps to a subsection of
6370 // the display panel called the physical display. Here, the physical display is bounded by the
6371 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6372 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6373
Michael Wrighta9cf4192022-12-01 23:46:39 +00006374 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006375 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6376
6377 // Touches that start outside the physical display should be ignored until it enters the
6378 // physical display bounds, at which point it should generate a down event. Start a touch at
6379 // the point (5, 100), which is outside the physical display bounds.
6380 static const Point kOutsidePoint{5, 100};
6381 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6382 processSync(mapper);
6383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6384
6385 // Move the touch into the physical display area. This should generate a pointer down.
6386 processMove(mapper, toRawX(11), toRawY(21));
6387 processSync(mapper);
6388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6389 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006390 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006391 ASSERT_NO_FATAL_FAILURE(
6392 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6393
6394 // Move the touch inside the physical display area. This should generate a pointer move.
6395 processMove(mapper, toRawX(69), toRawY(159));
6396 processSync(mapper);
6397 assertReceivedMove({69, 159});
6398
6399 // Move outside the physical display area. Since the pointer is already down, this should
6400 // now continue generating events.
6401 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6402 processSync(mapper);
6403 assertReceivedMove(kOutsidePoint);
6404
6405 // Release. This should generate a pointer up.
6406 processUp(mapper);
6407 processSync(mapper);
6408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6410 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6411 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6412
6413 // Ensure no more events were generated.
6414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6416 }
6417}
6418
Prabir Pradhana9df3162022-12-05 23:57:27 +00006419// --- TouchscreenPrecisionTests ---
6420
6421// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6422// in various orientations and with different display rotations. We configure the touchscreen to
6423// have a higher resolution than that of the display by an integer scale factor in each axis so that
6424// we can enforce that coordinates match precisely as expected.
6425class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6426 public ::testing::WithParamInterface<ui::Rotation> {
6427public:
6428 void SetUp() override {
6429 SingleTouchInputMapperTest::SetUp();
6430
6431 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6432 // four times the resolution of the display in the Y axis.
6433 prepareButtons();
6434 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006435 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6436 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006437 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006438 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6439 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006440 }
6441
6442 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6443 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6444 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6445 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6446
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006447 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6448 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6449
6450 static const int32_t PRECISION_RAW_X_FLAT = 16;
6451 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6452
6453 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6454 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6455
Prabir Pradhana9df3162022-12-05 23:57:27 +00006456 static const std::array<Point, 4> kRawCorners;
6457};
6458
6459const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6460 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6461 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6462 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6463 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6464}};
6465
6466// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6467// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6468// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6469TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6470 enum class Orientation {
6471 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6472 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6473 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6474 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6475 ftl_last = ORIENTATION_270,
6476 };
6477 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6478 Orientation::ORIENTATION_270;
6479 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6480 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6481 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6482 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6483 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6484 };
6485
6486 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6487
6488 // Configure the touchscreen as being installed in the one of the four different orientations
6489 // relative to the display.
6490 addConfigurationProperty("touch.deviceType", "touchScreen");
6491 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6492 prepareDisplay(ui::ROTATION_0);
6493
Arpit Singha8c236b2023-04-25 13:56:05 +00006494 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006495
6496 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6497 // orientations of either 90 or 270) this means the display's natural resolution will be
6498 // flipped.
6499 const bool displayRotated =
6500 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6501 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6502 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6503 const Rect physicalFrame{0, 0, width, height};
6504 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6505
6506 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6507 const float expectedPrecisionX = displayRotated ? 4 : 2;
6508 const float expectedPrecisionY = displayRotated ? 2 : 4;
6509
6510 // Test all four corners.
6511 for (int i = 0; i < 4; i++) {
6512 const auto& raw = kRawCorners[i];
6513 processDown(mapper, raw.x, raw.y);
6514 processSync(mapper);
6515 const auto& expected = expectedPoints[i];
6516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6518 WithCoords(expected.x, expected.y),
6519 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6520 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6521 << "with touchscreen orientation "
6522 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6523 << expected.x << ", " << expected.y << ").";
6524 processUp(mapper);
6525 processSync(mapper);
6526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6527 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6528 WithCoords(expected.x, expected.y))));
6529 }
6530}
6531
Prabir Pradhan82687402022-12-06 01:32:53 +00006532TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6533 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6534 kMappedCorners = {
6535 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6536 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6537 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6538 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6539 };
6540
6541 const ui::Rotation displayRotation = GetParam();
6542
6543 addConfigurationProperty("touch.deviceType", "touchScreen");
6544 prepareDisplay(displayRotation);
6545
Arpit Singha8c236b2023-04-25 13:56:05 +00006546 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006547
6548 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6549
6550 // Test all four corners.
6551 for (int i = 0; i < 4; i++) {
6552 const auto& expected = expectedPoints[i];
6553 const auto& raw = kRawCorners[i];
6554 processDown(mapper, raw.x, raw.y);
6555 processSync(mapper);
6556 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6557 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6558 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6559 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6560 << "with display rotation " << ui::toCString(displayRotation)
6561 << ", expected point (" << expected.x << ", " << expected.y << ").";
6562 processUp(mapper);
6563 processSync(mapper);
6564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6565 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6566 WithCoords(expected.x, expected.y))));
6567 }
6568}
6569
Prabir Pradhan3e798762022-12-02 21:02:11 +00006570TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6571 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6572 kMappedCorners = {
6573 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6574 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6575 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6576 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6577 };
6578
6579 const ui::Rotation displayRotation = GetParam();
6580
6581 addConfigurationProperty("touch.deviceType", "touchScreen");
6582 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6583
Arpit Singha8c236b2023-04-25 13:56:05 +00006584 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006585
6586 // Ori 270, so width and height swapped
6587 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6588 prepareDisplay(displayRotation);
6589 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6590
6591 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6592
6593 // Test all four corners.
6594 for (int i = 0; i < 4; i++) {
6595 const auto& expected = expectedPoints[i];
6596 const auto& raw = kRawCorners[i];
6597 processDown(mapper, raw.x, raw.y);
6598 processSync(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6600 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6601 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6602 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6603 << "with display rotation " << ui::toCString(displayRotation)
6604 << ", expected point (" << expected.x << ", " << expected.y << ").";
6605 processUp(mapper);
6606 processSync(mapper);
6607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6608 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6609 WithCoords(expected.x, expected.y))));
6610 }
6611}
6612
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006613TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6614 const ui::Rotation displayRotation = GetParam();
6615
6616 addConfigurationProperty("touch.deviceType", "touchScreen");
6617 prepareDisplay(displayRotation);
6618
6619 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006620 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006621
6622 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6623 // MotionRanges use display pixels as their units
6624 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6625 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6626
6627 // The MotionRanges should be oriented in the rotated display's coordinate space
6628 const bool displayRotated =
6629 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6630
6631 constexpr float MAX_X = 479.5;
6632 constexpr float MAX_Y = 799.75;
6633 EXPECT_EQ(xRange->min, 0.f);
6634 EXPECT_EQ(yRange->min, 0.f);
6635 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6636 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6637
6638 EXPECT_EQ(xRange->flat, 8.f);
6639 EXPECT_EQ(yRange->flat, 8.f);
6640
6641 EXPECT_EQ(xRange->fuzz, 2.f);
6642 EXPECT_EQ(yRange->fuzz, 2.f);
6643
6644 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6645 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6646}
6647
Prabir Pradhana9df3162022-12-05 23:57:27 +00006648// Run the precision tests for all rotations.
6649INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6650 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6651 ui::ROTATION_270),
6652 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6653 return ftl::enum_string(testParamInfo.param);
6654 });
6655
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006656// --- ExternalStylusFusionTest ---
6657
6658class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6659public:
6660 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6661 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006662 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006663 prepareButtons();
6664 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006665 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006666
6667 mStylusState.when = ARBITRARY_TIME;
6668 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006669 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006670 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006671 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006672 processExternalStylusState(mapper);
6673 return mapper;
6674 }
6675
6676 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6677 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6678 for (const NotifyArgs& args : generatedArgs) {
6679 mFakeListener->notify(args);
6680 }
6681 // Loop the reader to flush the input listener queue.
6682 mReader->loopOnce();
6683 return generatedArgs;
6684 }
6685
6686protected:
6687 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006688
6689 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6690 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006691 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006692
6693 // The first pointer is withheld.
6694 processDown(mapper, 100, 200);
6695 processSync(mapper);
6696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6697 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6698 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6699
6700 // The external stylus reports pressure. The withheld finger pointer is released as a
6701 // stylus.
6702 mStylusState.pressure = 1.f;
6703 processExternalStylusState(mapper);
6704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6705 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6706 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6707
6708 // Subsequent pointer events are not withheld.
6709 processMove(mapper, 101, 201);
6710 processSync(mapper);
6711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6712 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6713
6714 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6716 }
6717
6718 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6719 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6720
6721 // Releasing the touch pointer ends the gesture.
6722 processUp(mapper);
6723 processSync(mapper);
6724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006725 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006726 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006727
6728 mStylusState.pressure = 0.f;
6729 processExternalStylusState(mapper);
6730 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6732 }
6733
6734 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006735 // When stylus fusion is not successful, events should be reported with the original source.
6736 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006737 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006738 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006739
6740 // The first pointer is withheld when an external stylus is connected,
6741 // and a timeout is requested.
6742 processDown(mapper, 100, 200);
6743 processSync(mapper);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6745 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6746 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6747
6748 // If the timeout expires early, it is requested again.
6749 handleTimeout(mapper, ARBITRARY_TIME + 1);
6750 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6751 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6752
6753 // When the timeout expires, the withheld touch is released as a finger pointer.
6754 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6756 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6757
6758 // Subsequent pointer events are not withheld.
6759 processMove(mapper, 101, 201);
6760 processSync(mapper);
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6762 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6763 processUp(mapper);
6764 processSync(mapper);
6765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6766 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6767
6768 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6770 }
6771
6772private:
6773 InputDeviceInfo mExternalStylusDeviceInfo{};
6774};
6775
6776TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6777 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006778 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006779}
6780
6781TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6782 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6783 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6784}
6785
6786TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6787 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6788 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6789}
6790
6791// Test a successful stylus fusion gesture where the pressure is reported by the external
6792// before the touch is reported by the touchscreen.
6793TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6794 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006795 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006796
6797 // The external stylus reports pressure first. It is ignored for now.
6798 mStylusState.pressure = 1.f;
6799 processExternalStylusState(mapper);
6800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6801 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6802
6803 // When the touch goes down afterwards, it is reported as a stylus pointer.
6804 processDown(mapper, 100, 200);
6805 processSync(mapper);
6806 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6807 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6808 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6809
6810 processMove(mapper, 101, 201);
6811 processSync(mapper);
6812 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6813 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6814 processUp(mapper);
6815 processSync(mapper);
6816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6817 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6818
6819 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6820 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6821}
6822
6823TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6824 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6825
6826 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6827 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6828
6829 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6830 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6831 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6832 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6833}
6834
6835TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6836 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006837 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006838
6839 mStylusState.pressure = 0.8f;
6840 processExternalStylusState(mapper);
6841 processDown(mapper, 100, 200);
6842 processSync(mapper);
6843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6844 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6845 WithPressure(0.8f))));
6846 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6847
6848 // The external stylus reports a pressure change. We wait for some time for a touch event.
6849 mStylusState.pressure = 0.6f;
6850 processExternalStylusState(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6852 ASSERT_NO_FATAL_FAILURE(
6853 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6854
6855 // If a touch is reported within the timeout, it reports the updated pressure.
6856 processMove(mapper, 101, 201);
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6859 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6860 WithPressure(0.6f))));
6861 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6862
6863 // There is another pressure change.
6864 mStylusState.pressure = 0.5f;
6865 processExternalStylusState(mapper);
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6867 ASSERT_NO_FATAL_FAILURE(
6868 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6869
6870 // If a touch is not reported within the timeout, a move event is generated to report
6871 // the new pressure.
6872 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6874 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6875 WithPressure(0.5f))));
6876
6877 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6878 // repeated indefinitely.
6879 mStylusState.pressure = 0.0f;
6880 processExternalStylusState(mapper);
6881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6882 ASSERT_NO_FATAL_FAILURE(
6883 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6884 processMove(mapper, 102, 202);
6885 processSync(mapper);
6886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6887 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6888 WithPressure(0.5f))));
6889 processMove(mapper, 103, 203);
6890 processSync(mapper);
6891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6892 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6893 WithPressure(0.5f))));
6894
6895 processUp(mapper);
6896 processSync(mapper);
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006898 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006899 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006900
6901 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6903}
6904
6905TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6906 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006907 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006908
6909 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006910 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006911 processExternalStylusState(mapper);
6912 processDown(mapper, 100, 200);
6913 processSync(mapper);
6914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6915 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006916 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006917 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6918
6919 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006920 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006921 processExternalStylusState(mapper);
6922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6923 ASSERT_NO_FATAL_FAILURE(
6924 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6925
6926 // If a touch is reported within the timeout, it reports the updated pressure.
6927 processMove(mapper, 101, 201);
6928 processSync(mapper);
6929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6930 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006931 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006932 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6933
6934 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006935 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006936 processExternalStylusState(mapper);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6938 ASSERT_NO_FATAL_FAILURE(
6939 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6940
6941 // If a touch is not reported within the timeout, a move event is generated to report
6942 // the new tool type.
6943 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6945 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006946 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006947
6948 processUp(mapper);
6949 processSync(mapper);
6950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6951 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006952 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006953
6954 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6956}
6957
6958TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6959 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006960 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006961
6962 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6963
6964 // The external stylus reports a button change. We wait for some time for a touch event.
6965 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6966 processExternalStylusState(mapper);
6967 ASSERT_NO_FATAL_FAILURE(
6968 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6969
6970 // If a touch is reported within the timeout, it reports the updated button state.
6971 processMove(mapper, 101, 201);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6974 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6975 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6977 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6978 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6979 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6980
6981 // The button is now released.
6982 mStylusState.buttons = 0;
6983 processExternalStylusState(mapper);
6984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6985 ASSERT_NO_FATAL_FAILURE(
6986 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6987
6988 // If a touch is not reported within the timeout, a move event is generated to report
6989 // the new button state.
6990 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6992 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6993 WithButtonState(0))));
6994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00006995 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6996 WithButtonState(0))));
6997
6998 processUp(mapper);
6999 processSync(mapper);
7000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007001 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7002
7003 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7005}
7006
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007// --- MultiTouchInputMapperTest ---
7008
7009class MultiTouchInputMapperTest : public TouchInputMapperTest {
7010protected:
7011 void prepareAxes(int axes);
7012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007013 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7014 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7015 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7016 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7017 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7018 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7019 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7020 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7021 void processId(MultiTouchInputMapper& mapper, int32_t id);
7022 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7023 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7024 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007025 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007026 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007027 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7028 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007029};
7030
7031void MultiTouchInputMapperTest::prepareAxes(int axes) {
7032 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007033 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7034 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 }
7036 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007037 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7038 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007039 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007040 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7041 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042 }
7043 }
7044 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007045 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7046 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007048 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007049 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 }
7051 }
7052 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007053 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7054 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055 }
7056 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007057 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7058 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 }
7060 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7062 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007063 }
7064 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007065 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7066 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007067 }
7068 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007069 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7070 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007071 }
7072 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007073 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 }
7075}
7076
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007077void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7078 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007079 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081}
7082
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007083void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7084 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007085 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007086}
7087
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007088void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7089 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007090 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091}
7092
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007093void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095}
7096
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007097void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007098 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007099}
7100
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007101void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7102 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007103 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007104}
7105
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007106void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007107 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007108}
7109
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007110void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007111 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112}
7113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007114void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007115 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007116}
7117
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007118void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120}
7121
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007122void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007123 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124}
7125
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007126void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7127 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007128 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129}
7130
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007131void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7132 int32_t value) {
7133 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7135}
7136
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007137void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139}
7140
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007141void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7142 nsecs_t readTime) {
7143 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007144}
7145
Michael Wrightd02c5b62014-02-10 15:10:22 -08007146TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007148 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007149 prepareAxes(POSITION);
7150 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007151 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007152
arthurhungdcef2dc2020-08-11 14:47:50 +08007153 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154
7155 NotifyMotionArgs motionArgs;
7156
7157 // Two fingers down at once.
7158 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7159 processPosition(mapper, x1, y1);
7160 processMTSync(mapper);
7161 processPosition(mapper, x2, y2);
7162 processMTSync(mapper);
7163 processSync(mapper);
7164
7165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7167 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7168 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7169 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7170 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7171 ASSERT_EQ(0, motionArgs.flags);
7172 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7173 ASSERT_EQ(0, motionArgs.buttonState);
7174 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007175 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007176 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007177 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007178 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7179 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7180 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7181 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7182 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7183
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7186 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7187 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7188 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007189 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190 ASSERT_EQ(0, motionArgs.flags);
7191 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7192 ASSERT_EQ(0, motionArgs.buttonState);
7193 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007194 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007196 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007197 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007198 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007199 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7200 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7203 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7204 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7205 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7206
7207 // Move.
7208 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7209 processPosition(mapper, x1, y1);
7210 processMTSync(mapper);
7211 processPosition(mapper, x2, y2);
7212 processMTSync(mapper);
7213 processSync(mapper);
7214
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7217 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7218 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7219 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7220 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7221 ASSERT_EQ(0, motionArgs.flags);
7222 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7223 ASSERT_EQ(0, motionArgs.buttonState);
7224 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007225 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007227 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007228 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007229 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007230 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7231 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7233 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7234 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7235 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7236 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7237
7238 // First finger up.
7239 x2 += 15; y2 -= 20;
7240 processPosition(mapper, x2, y2);
7241 processMTSync(mapper);
7242 processSync(mapper);
7243
7244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7245 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7246 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7247 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7248 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007249 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007250 ASSERT_EQ(0, motionArgs.flags);
7251 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7252 ASSERT_EQ(0, motionArgs.buttonState);
7253 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007254 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007256 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007257 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007258 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007259 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7260 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7261 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7262 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7263 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7264 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7265 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7266
7267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7268 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7269 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7270 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7271 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7273 ASSERT_EQ(0, motionArgs.flags);
7274 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7275 ASSERT_EQ(0, motionArgs.buttonState);
7276 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007277 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007278 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007279 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007280 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7281 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7282 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7283 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7284 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7285
7286 // Move.
7287 x2 += 20; y2 -= 25;
7288 processPosition(mapper, x2, y2);
7289 processMTSync(mapper);
7290 processSync(mapper);
7291
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7294 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7295 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7296 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7297 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7298 ASSERT_EQ(0, motionArgs.flags);
7299 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7300 ASSERT_EQ(0, motionArgs.buttonState);
7301 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007302 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7306 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7307 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7308 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7309 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7310
7311 // New finger down.
7312 int32_t x3 = 700, y3 = 300;
7313 processPosition(mapper, x2, y2);
7314 processMTSync(mapper);
7315 processPosition(mapper, x3, y3);
7316 processMTSync(mapper);
7317 processSync(mapper);
7318
7319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7320 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7321 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7322 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7323 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007324 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007325 ASSERT_EQ(0, motionArgs.flags);
7326 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7327 ASSERT_EQ(0, motionArgs.buttonState);
7328 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007329 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007330 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007331 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007332 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007333 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007334 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7335 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7337 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7338 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7339 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7340 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7341
7342 // Second finger up.
7343 x3 += 30; y3 -= 20;
7344 processPosition(mapper, x3, y3);
7345 processMTSync(mapper);
7346 processSync(mapper);
7347
7348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7349 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7350 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7351 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7352 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007353 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007354 ASSERT_EQ(0, motionArgs.flags);
7355 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7356 ASSERT_EQ(0, motionArgs.buttonState);
7357 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007358 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007359 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007360 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007361 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007362 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007363 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7364 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7366 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7367 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7368 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7369 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7370
7371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7372 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7373 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7374 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7375 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7377 ASSERT_EQ(0, motionArgs.flags);
7378 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7379 ASSERT_EQ(0, motionArgs.buttonState);
7380 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007381 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007382 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007383 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007384 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7385 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7386 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7387 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7388 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7389
7390 // Last finger up.
7391 processMTSync(mapper);
7392 processSync(mapper);
7393
7394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7395 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7396 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7397 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7398 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7399 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7400 ASSERT_EQ(0, motionArgs.flags);
7401 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7402 ASSERT_EQ(0, motionArgs.buttonState);
7403 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007404 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007405 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007406 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007407 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7408 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7409 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7410 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7411 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7412
7413 // Should not have sent any more keys or motions.
7414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7416}
7417
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007418TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7419 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007420 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007421
7422 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7423 /*fuzz*/ 0, /*resolution*/ 10);
7424 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7425 /*fuzz*/ 0, /*resolution*/ 11);
7426 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7427 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7428 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7429 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7430 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7431 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7432 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7433 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7434
Arpit Singha8c236b2023-04-25 13:56:05 +00007435 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007436
7437 // X and Y axes
7438 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7439 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7440 // Touch major and minor
7441 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7442 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7443 // Tool major and minor
7444 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7445 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7446}
7447
7448TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7449 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007450 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007451
7452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7453 /*fuzz*/ 0, /*resolution*/ 10);
7454 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7455 /*fuzz*/ 0, /*resolution*/ 11);
7456
7457 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7458
Arpit Singha8c236b2023-04-25 13:56:05 +00007459 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007460
7461 // Touch major and minor
7462 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7463 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7464 // Tool major and minor
7465 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7466 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7467}
7468
Michael Wrightd02c5b62014-02-10 15:10:22 -08007469TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007471 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472 prepareAxes(POSITION | ID);
7473 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007474 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007475
arthurhungdcef2dc2020-08-11 14:47:50 +08007476 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007477
7478 NotifyMotionArgs motionArgs;
7479
7480 // Two fingers down at once.
7481 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7482 processPosition(mapper, x1, y1);
7483 processId(mapper, 1);
7484 processMTSync(mapper);
7485 processPosition(mapper, x2, y2);
7486 processId(mapper, 2);
7487 processMTSync(mapper);
7488 processSync(mapper);
7489
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7491 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007492 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007493 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007494 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007495 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7496 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7497
7498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007499 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007500 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007501 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007502 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007503 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007504 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007505 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7506 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7507 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7508 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7509
7510 // Move.
7511 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7512 processPosition(mapper, x1, y1);
7513 processId(mapper, 1);
7514 processMTSync(mapper);
7515 processPosition(mapper, x2, y2);
7516 processId(mapper, 2);
7517 processMTSync(mapper);
7518 processSync(mapper);
7519
7520 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7521 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007522 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007524 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007525 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007526 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7528 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7529 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7530 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7531
7532 // First finger up.
7533 x2 += 15; y2 -= 20;
7534 processPosition(mapper, x2, y2);
7535 processId(mapper, 2);
7536 processMTSync(mapper);
7537 processSync(mapper);
7538
7539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007540 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007541 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007542 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007543 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007545 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7547 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7548 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7549 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7550
7551 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7552 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007553 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007554 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007555 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007556 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7557 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7558
7559 // Move.
7560 x2 += 20; y2 -= 25;
7561 processPosition(mapper, x2, y2);
7562 processId(mapper, 2);
7563 processMTSync(mapper);
7564 processSync(mapper);
7565
7566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007568 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007570 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007571 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7572 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7573
7574 // New finger down.
7575 int32_t x3 = 700, y3 = 300;
7576 processPosition(mapper, x2, y2);
7577 processId(mapper, 2);
7578 processMTSync(mapper);
7579 processPosition(mapper, x3, y3);
7580 processId(mapper, 3);
7581 processMTSync(mapper);
7582 processSync(mapper);
7583
7584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007585 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007586 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007587 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007588 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007589 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007590 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7592 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7593 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7594 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7595
7596 // Second finger up.
7597 x3 += 30; y3 -= 20;
7598 processPosition(mapper, x3, y3);
7599 processId(mapper, 3);
7600 processMTSync(mapper);
7601 processSync(mapper);
7602
7603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007604 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007605 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007606 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007607 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007608 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007609 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007610 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7611 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7613 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7614
7615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007617 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007618 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007619 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007620 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7621 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7622
7623 // Last finger up.
7624 processMTSync(mapper);
7625 processSync(mapper);
7626
7627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7628 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007629 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007630 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007631 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007632 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7633 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7634
7635 // Should not have sent any more keys or motions.
7636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7638}
7639
7640TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007641 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007642 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007643 prepareAxes(POSITION | ID | SLOT);
7644 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007645 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007646
arthurhungdcef2dc2020-08-11 14:47:50 +08007647 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007648
7649 NotifyMotionArgs motionArgs;
7650
7651 // Two fingers down at once.
7652 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7653 processPosition(mapper, x1, y1);
7654 processId(mapper, 1);
7655 processSlot(mapper, 1);
7656 processPosition(mapper, x2, y2);
7657 processId(mapper, 2);
7658 processSync(mapper);
7659
7660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007662 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007663 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007664 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007665 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7666 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7667
7668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007669 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007670 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007672 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7679
7680 // Move.
7681 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7682 processSlot(mapper, 0);
7683 processPosition(mapper, x1, y1);
7684 processSlot(mapper, 1);
7685 processPosition(mapper, x2, y2);
7686 processSync(mapper);
7687
7688 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7689 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007690 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007691 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007693 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007694 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007695 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7696 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7697 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7698 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7699
7700 // First finger up.
7701 x2 += 15; y2 -= 20;
7702 processSlot(mapper, 0);
7703 processId(mapper, -1);
7704 processSlot(mapper, 1);
7705 processPosition(mapper, x2, y2);
7706 processSync(mapper);
7707
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007709 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007710 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007712 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007713 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007714 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007715 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7716 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7717 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7718 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7719
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7721 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007722 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007723 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007724 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007725 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7726 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7727
7728 // Move.
7729 x2 += 20; y2 -= 25;
7730 processPosition(mapper, x2, y2);
7731 processSync(mapper);
7732
7733 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7734 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007735 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007736 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007737 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007738 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7739 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7740
7741 // New finger down.
7742 int32_t x3 = 700, y3 = 300;
7743 processPosition(mapper, x2, y2);
7744 processSlot(mapper, 0);
7745 processId(mapper, 3);
7746 processPosition(mapper, x3, y3);
7747 processSync(mapper);
7748
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007750 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007751 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007752 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007753 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007754 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7757 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7758 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7759 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7760
7761 // Second finger up.
7762 x3 += 30; y3 -= 20;
7763 processSlot(mapper, 1);
7764 processId(mapper, -1);
7765 processSlot(mapper, 0);
7766 processPosition(mapper, x3, y3);
7767 processSync(mapper);
7768
7769 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007770 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007771 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007772 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007773 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007774 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007775 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007776 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7777 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7778 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7779 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7780
7781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7782 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007783 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007785 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007786 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7787 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7788
7789 // Last finger up.
7790 processId(mapper, -1);
7791 processSync(mapper);
7792
7793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7794 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007795 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007796 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007797 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7799 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7800
7801 // Should not have sent any more keys or motions.
7802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7804}
7805
7806TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007807 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007808 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007809 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007810 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007811
7812 // These calculations are based on the input device calibration documentation.
7813 int32_t rawX = 100;
7814 int32_t rawY = 200;
7815 int32_t rawTouchMajor = 7;
7816 int32_t rawTouchMinor = 6;
7817 int32_t rawToolMajor = 9;
7818 int32_t rawToolMinor = 8;
7819 int32_t rawPressure = 11;
7820 int32_t rawDistance = 0;
7821 int32_t rawOrientation = 3;
7822 int32_t id = 5;
7823
7824 float x = toDisplayX(rawX);
7825 float y = toDisplayY(rawY);
7826 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7827 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7828 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7829 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7830 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7831 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7832 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7833 float distance = float(rawDistance);
7834
7835 processPosition(mapper, rawX, rawY);
7836 processTouchMajor(mapper, rawTouchMajor);
7837 processTouchMinor(mapper, rawTouchMinor);
7838 processToolMajor(mapper, rawToolMajor);
7839 processToolMinor(mapper, rawToolMinor);
7840 processPressure(mapper, rawPressure);
7841 processOrientation(mapper, rawOrientation);
7842 processDistance(mapper, rawDistance);
7843 processId(mapper, id);
7844 processMTSync(mapper);
7845 processSync(mapper);
7846
7847 NotifyMotionArgs args;
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7849 ASSERT_EQ(0, args.pointerProperties[0].id);
7850 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7851 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7852 orientation, distance));
7853}
7854
7855TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007856 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007857 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007858 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7859 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007860 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007861
7862 // These calculations are based on the input device calibration documentation.
7863 int32_t rawX = 100;
7864 int32_t rawY = 200;
7865 int32_t rawTouchMajor = 140;
7866 int32_t rawTouchMinor = 120;
7867 int32_t rawToolMajor = 180;
7868 int32_t rawToolMinor = 160;
7869
7870 float x = toDisplayX(rawX);
7871 float y = toDisplayY(rawY);
7872 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7873 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7874 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7875 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7876 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7877
7878 processPosition(mapper, rawX, rawY);
7879 processTouchMajor(mapper, rawTouchMajor);
7880 processTouchMinor(mapper, rawTouchMinor);
7881 processToolMajor(mapper, rawToolMajor);
7882 processToolMinor(mapper, rawToolMinor);
7883 processMTSync(mapper);
7884 processSync(mapper);
7885
7886 NotifyMotionArgs args;
7887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7888 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7889 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7890}
7891
7892TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007893 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007894 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007895 prepareAxes(POSITION | TOUCH | TOOL);
7896 addConfigurationProperty("touch.size.calibration", "diameter");
7897 addConfigurationProperty("touch.size.scale", "10");
7898 addConfigurationProperty("touch.size.bias", "160");
7899 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007900 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007901
7902 // These calculations are based on the input device calibration documentation.
7903 // Note: We only provide a single common touch/tool value because the device is assumed
7904 // not to emit separate values for each pointer (isSummed = 1).
7905 int32_t rawX = 100;
7906 int32_t rawY = 200;
7907 int32_t rawX2 = 150;
7908 int32_t rawY2 = 250;
7909 int32_t rawTouchMajor = 5;
7910 int32_t rawToolMajor = 8;
7911
7912 float x = toDisplayX(rawX);
7913 float y = toDisplayY(rawY);
7914 float x2 = toDisplayX(rawX2);
7915 float y2 = toDisplayY(rawY2);
7916 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7917 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7918 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7919
7920 processPosition(mapper, rawX, rawY);
7921 processTouchMajor(mapper, rawTouchMajor);
7922 processToolMajor(mapper, rawToolMajor);
7923 processMTSync(mapper);
7924 processPosition(mapper, rawX2, rawY2);
7925 processTouchMajor(mapper, rawTouchMajor);
7926 processToolMajor(mapper, rawToolMajor);
7927 processMTSync(mapper);
7928 processSync(mapper);
7929
7930 NotifyMotionArgs args;
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7933
7934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007935 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007936 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007937 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7938 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7939 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7940 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7941}
7942
7943TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007944 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007945 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 prepareAxes(POSITION | TOUCH | TOOL);
7947 addConfigurationProperty("touch.size.calibration", "area");
7948 addConfigurationProperty("touch.size.scale", "43");
7949 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007950 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007951
7952 // These calculations are based on the input device calibration documentation.
7953 int32_t rawX = 100;
7954 int32_t rawY = 200;
7955 int32_t rawTouchMajor = 5;
7956 int32_t rawToolMajor = 8;
7957
7958 float x = toDisplayX(rawX);
7959 float y = toDisplayY(rawY);
7960 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7961 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7962 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7963
7964 processPosition(mapper, rawX, rawY);
7965 processTouchMajor(mapper, rawTouchMajor);
7966 processToolMajor(mapper, rawToolMajor);
7967 processMTSync(mapper);
7968 processSync(mapper);
7969
7970 NotifyMotionArgs args;
7971 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7973 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7974}
7975
7976TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007977 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007978 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007979 prepareAxes(POSITION | PRESSURE);
7980 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7981 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00007982 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983
Michael Wrightaa449c92017-12-13 21:21:43 +00007984 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00007985 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007986 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7987 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7988 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7989
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 // These calculations are based on the input device calibration documentation.
7991 int32_t rawX = 100;
7992 int32_t rawY = 200;
7993 int32_t rawPressure = 60;
7994
7995 float x = toDisplayX(rawX);
7996 float y = toDisplayY(rawY);
7997 float pressure = float(rawPressure) * 0.01f;
7998
7999 processPosition(mapper, rawX, rawY);
8000 processPressure(mapper, rawPressure);
8001 processMTSync(mapper);
8002 processSync(mapper);
8003
8004 NotifyMotionArgs args;
8005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8006 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8007 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8008}
8009
8010TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008011 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008012 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008013 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008014 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008015
8016 NotifyMotionArgs motionArgs;
8017 NotifyKeyArgs keyArgs;
8018
8019 processId(mapper, 1);
8020 processPosition(mapper, 100, 200);
8021 processSync(mapper);
8022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8023 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8024 ASSERT_EQ(0, motionArgs.buttonState);
8025
8026 // press BTN_LEFT, release BTN_LEFT
8027 processKey(mapper, BTN_LEFT, 1);
8028 processSync(mapper);
8029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8030 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8031 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8032
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8035 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8036
Michael Wrightd02c5b62014-02-10 15:10:22 -08008037 processKey(mapper, BTN_LEFT, 0);
8038 processSync(mapper);
8039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008042
8043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008044 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008045 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008046
8047 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8048 processKey(mapper, BTN_RIGHT, 1);
8049 processKey(mapper, BTN_MIDDLE, 1);
8050 processSync(mapper);
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8052 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8053 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8054 motionArgs.buttonState);
8055
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8058 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8059
8060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8061 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8062 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8063 motionArgs.buttonState);
8064
Michael Wrightd02c5b62014-02-10 15:10:22 -08008065 processKey(mapper, BTN_RIGHT, 0);
8066 processSync(mapper);
8067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008069 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008070
8071 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008072 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008073 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008074
8075 processKey(mapper, BTN_MIDDLE, 0);
8076 processSync(mapper);
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008078 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008079 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008080
8081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008084
8085 // press BTN_BACK, release BTN_BACK
8086 processKey(mapper, BTN_BACK, 1);
8087 processSync(mapper);
8088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8089 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8090 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8095
8096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8097 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8098 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099
8100 processKey(mapper, BTN_BACK, 0);
8101 processSync(mapper);
8102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008104 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008107 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008108 ASSERT_EQ(0, motionArgs.buttonState);
8109
Michael Wrightd02c5b62014-02-10 15:10:22 -08008110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8111 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8112 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8113
8114 // press BTN_SIDE, release BTN_SIDE
8115 processKey(mapper, BTN_SIDE, 1);
8116 processSync(mapper);
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8118 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8119 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008120
Michael Wrightd02c5b62014-02-10 15:10:22 -08008121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008123 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8124
8125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8126 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8127 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128
8129 processKey(mapper, BTN_SIDE, 0);
8130 processSync(mapper);
8131 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008132 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008133 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008134
8135 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137 ASSERT_EQ(0, motionArgs.buttonState);
8138
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8140 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8141 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8142
8143 // press BTN_FORWARD, release BTN_FORWARD
8144 processKey(mapper, BTN_FORWARD, 1);
8145 processSync(mapper);
8146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8147 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8148 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008152 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8153
8154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8155 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8156 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008157
8158 processKey(mapper, BTN_FORWARD, 0);
8159 processSync(mapper);
8160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008161 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008162 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008163
8164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166 ASSERT_EQ(0, motionArgs.buttonState);
8167
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8169 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8170 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8171
8172 // press BTN_EXTRA, release BTN_EXTRA
8173 processKey(mapper, BTN_EXTRA, 1);
8174 processSync(mapper);
8175 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8176 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8177 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008181 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8182
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8184 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8185 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008186
8187 processKey(mapper, BTN_EXTRA, 0);
8188 processSync(mapper);
8189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008190 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008191 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008192
8193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008194 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195 ASSERT_EQ(0, motionArgs.buttonState);
8196
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8198 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8199 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8200
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8202
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203 // press BTN_STYLUS, release BTN_STYLUS
8204 processKey(mapper, BTN_STYLUS, 1);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8207 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008208 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8211 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8212 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213
8214 processKey(mapper, BTN_STYLUS, 0);
8215 processSync(mapper);
8216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008217 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008218 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008219
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008222 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008223
8224 // press BTN_STYLUS2, release BTN_STYLUS2
8225 processKey(mapper, BTN_STYLUS2, 1);
8226 processSync(mapper);
8227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008229 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8230
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8233 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234
8235 processKey(mapper, BTN_STYLUS2, 0);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008239 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008240
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008243 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008244
8245 // release touch
8246 processId(mapper, -1);
8247 processSync(mapper);
8248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8250 ASSERT_EQ(0, motionArgs.buttonState);
8251}
8252
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008253TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8254 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008255 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008256 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008257 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008258
8259 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8260 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8261
8262 // Touch down.
8263 processId(mapper, 1);
8264 processPosition(mapper, 100, 200);
8265 processSync(mapper);
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8267 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8268
8269 // Press and release button mapped to the primary stylus button.
8270 processKey(mapper, BTN_A, 1);
8271 processSync(mapper);
8272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8273 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8274 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8276 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8277 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8278
8279 processKey(mapper, BTN_A, 0);
8280 processSync(mapper);
8281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8282 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8284 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8285
8286 // Press and release the HID usage mapped to the secondary stylus button.
8287 processHidUsage(mapper, 0xabcd, 1);
8288 processSync(mapper);
8289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8290 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8291 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8293 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8294 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8295
8296 processHidUsage(mapper, 0xabcd, 0);
8297 processSync(mapper);
8298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8302
8303 // Release touch.
8304 processId(mapper, -1);
8305 processSync(mapper);
8306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8307 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8308}
8309
Michael Wrightd02c5b62014-02-10 15:10:22 -08008310TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008312 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008313 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008314 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008315
8316 NotifyMotionArgs motionArgs;
8317
8318 // default tool type is finger
8319 processId(mapper, 1);
8320 processPosition(mapper, 100, 200);
8321 processSync(mapper);
8322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8323 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008324 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008325
8326 // eraser
8327 processKey(mapper, BTN_TOOL_RUBBER, 1);
8328 processSync(mapper);
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8330 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008331 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008332
8333 // stylus
8334 processKey(mapper, BTN_TOOL_RUBBER, 0);
8335 processKey(mapper, BTN_TOOL_PEN, 1);
8336 processSync(mapper);
8337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8338 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008339 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008340
8341 // brush
8342 processKey(mapper, BTN_TOOL_PEN, 0);
8343 processKey(mapper, BTN_TOOL_BRUSH, 1);
8344 processSync(mapper);
8345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8346 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008347 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008348
8349 // pencil
8350 processKey(mapper, BTN_TOOL_BRUSH, 0);
8351 processKey(mapper, BTN_TOOL_PENCIL, 1);
8352 processSync(mapper);
8353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8354 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008355 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008357 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008358 processKey(mapper, BTN_TOOL_PENCIL, 0);
8359 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8360 processSync(mapper);
8361 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8362 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008363 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008364
8365 // mouse
8366 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8367 processKey(mapper, BTN_TOOL_MOUSE, 1);
8368 processSync(mapper);
8369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8370 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008371 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008372
8373 // lens
8374 processKey(mapper, BTN_TOOL_MOUSE, 0);
8375 processKey(mapper, BTN_TOOL_LENS, 1);
8376 processSync(mapper);
8377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8378 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008379 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008380
8381 // double-tap
8382 processKey(mapper, BTN_TOOL_LENS, 0);
8383 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8384 processSync(mapper);
8385 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8386 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008387 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008388
8389 // triple-tap
8390 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8391 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8392 processSync(mapper);
8393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8394 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008395 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008396
8397 // quad-tap
8398 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8399 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8400 processSync(mapper);
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008403 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404
8405 // finger
8406 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8407 processKey(mapper, BTN_TOOL_FINGER, 1);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008411 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008412
8413 // stylus trumps finger
8414 processKey(mapper, BTN_TOOL_PEN, 1);
8415 processSync(mapper);
8416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8417 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008418 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008419
8420 // eraser trumps stylus
8421 processKey(mapper, BTN_TOOL_RUBBER, 1);
8422 processSync(mapper);
8423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8424 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008425 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426
8427 // mouse trumps eraser
8428 processKey(mapper, BTN_TOOL_MOUSE, 1);
8429 processSync(mapper);
8430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008432 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008433
8434 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8435 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8436 processSync(mapper);
8437 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8438 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008439 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008440
8441 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8442 processToolType(mapper, MT_TOOL_PEN);
8443 processSync(mapper);
8444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008446 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008447
8448 // back to default tool type
8449 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8450 processKey(mapper, BTN_TOOL_MOUSE, 0);
8451 processKey(mapper, BTN_TOOL_RUBBER, 0);
8452 processKey(mapper, BTN_TOOL_PEN, 0);
8453 processKey(mapper, BTN_TOOL_FINGER, 0);
8454 processSync(mapper);
8455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8456 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008457 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008458}
8459
8460TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008461 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008462 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008463 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008464 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008465 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008466
8467 NotifyMotionArgs motionArgs;
8468
8469 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8470 processId(mapper, 1);
8471 processPosition(mapper, 100, 200);
8472 processSync(mapper);
8473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8474 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8475 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8476 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8477
8478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8479 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8480 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8481 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8482
8483 // move a little
8484 processPosition(mapper, 150, 250);
8485 processSync(mapper);
8486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8487 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8488 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8489 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8490
8491 // down when BTN_TOUCH is pressed, pressure defaults to 1
8492 processKey(mapper, BTN_TOUCH, 1);
8493 processSync(mapper);
8494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8495 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8497 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8498
8499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8500 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8501 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8502 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8503
8504 // up when BTN_TOUCH is released, hover restored
8505 processKey(mapper, BTN_TOUCH, 0);
8506 processSync(mapper);
8507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8508 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8509 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8510 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8511
8512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8513 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8514 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8515 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8516
8517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8518 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8519 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8520 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8521
8522 // exit hover when pointer goes away
8523 processId(mapper, -1);
8524 processSync(mapper);
8525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8526 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8527 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8528 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8529}
8530
8531TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008532 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008533 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008534 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008535 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008536
8537 NotifyMotionArgs motionArgs;
8538
8539 // initially hovering because pressure is 0
8540 processId(mapper, 1);
8541 processPosition(mapper, 100, 200);
8542 processPressure(mapper, 0);
8543 processSync(mapper);
8544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8545 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8546 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8547 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8548
8549 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8550 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8552 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8553
8554 // move a little
8555 processPosition(mapper, 150, 250);
8556 processSync(mapper);
8557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8558 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8559 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8560 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8561
8562 // down when pressure becomes non-zero
8563 processPressure(mapper, RAW_PRESSURE_MAX);
8564 processSync(mapper);
8565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8566 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8567 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8568 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8569
8570 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8571 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8573 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8574
8575 // up when pressure becomes 0, hover restored
8576 processPressure(mapper, 0);
8577 processSync(mapper);
8578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8579 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8580 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8581 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8582
8583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8584 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8585 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8586 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8587
8588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8589 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8590 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8591 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8592
8593 // exit hover when pointer goes away
8594 processId(mapper, -1);
8595 processSync(mapper);
8596 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8597 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8599 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8600}
8601
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008602/**
8603 * Set the input device port <--> display port associations, and check that the
8604 * events are routed to the display that matches the display port.
8605 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8606 */
8607TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008608 const std::string usb2 = "USB2";
8609 const uint8_t hdmi1 = 0;
8610 const uint8_t hdmi2 = 1;
8611 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008612 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008613
8614 addConfigurationProperty("touch.deviceType", "touchScreen");
8615 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008616 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008617
8618 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8619 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8620
8621 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8622 // for this input device is specified, and the matching viewport is not present,
8623 // the input device should be disabled (at the mapper level).
8624
8625 // Add viewport for display 2 on hdmi2
8626 prepareSecondaryDisplay(type, hdmi2);
8627 // Send a touch event
8628 processPosition(mapper, 100, 100);
8629 processSync(mapper);
8630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8631
8632 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008633 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008634 // Send a touch event again
8635 processPosition(mapper, 100, 100);
8636 processSync(mapper);
8637
8638 NotifyMotionArgs args;
8639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8640 ASSERT_EQ(DISPLAY_ID, args.displayId);
8641}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008642
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008643TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8644 addConfigurationProperty("touch.deviceType", "touchScreen");
8645 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008646 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008647
8648 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8649
Michael Wrighta9cf4192022-12-01 23:46:39 +00008650 prepareDisplay(ui::ROTATION_0);
8651 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008652
8653 // Send a touch event
8654 processPosition(mapper, 100, 100);
8655 processSync(mapper);
8656
8657 NotifyMotionArgs args;
8658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8659 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8660}
8661
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008662TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008663 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008664
Michael Wrighta9cf4192022-12-01 23:46:39 +00008665 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008666 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008667 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008668
Josep del Río2d8c79a2023-01-23 19:33:50 +00008669 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008670
8671 NotifyMotionArgs motionArgs;
8672 processPosition(mapper, 100, 100);
8673 processSync(mapper);
8674
8675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8676 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00008677 ASSERT_EQ(ADISPLAY_ID_NONE, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008678}
8679
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008680/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008681 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8682 */
8683TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8684 addConfigurationProperty("touch.deviceType", "touchScreen");
8685 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008686 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008687
Michael Wrighta9cf4192022-12-01 23:46:39 +00008688 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008689 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8690 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8691 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8692 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008693
8694 NotifyMotionArgs args;
8695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8696 ASSERT_EQ(26, args.readTime);
8697
Harry Cutts33476232023-01-30 19:57:29 +00008698 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8699 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8700 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008701
8702 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8703 ASSERT_EQ(33, args.readTime);
8704}
8705
8706/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008707 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8708 * events should not be delivered to the listener.
8709 */
8710TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8711 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008712 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008713 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008714 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008715 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008716 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008717 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008718
8719 NotifyMotionArgs motionArgs;
8720 processPosition(mapper, 100, 100);
8721 processSync(mapper);
8722
8723 mFakeListener->assertNotifyMotionWasNotCalled();
8724}
8725
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008726/**
8727 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8728 * the touch mapper can process the events and the events can be delivered to the listener.
8729 */
8730TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8731 addConfigurationProperty("touch.deviceType", "touchScreen");
8732 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008733 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008734 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008735 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008736 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008737 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008738
8739 NotifyMotionArgs motionArgs;
8740 processPosition(mapper, 100, 100);
8741 processSync(mapper);
8742
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8745}
8746
Josh Thielene986aed2023-06-01 14:17:30 +00008747/**
8748 * When the viewport is deactivated (isActive transitions from true to false),
8749 * and touch.enableForInactiveViewport is false, touches prior to the transition
8750 * should be cancelled.
8751 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008752TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8753 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008754 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008755 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008756 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008757 std::optional<DisplayViewport> optionalDisplayViewport =
8758 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8759 ASSERT_TRUE(optionalDisplayViewport.has_value());
8760 DisplayViewport displayViewport = *optionalDisplayViewport;
8761
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008762 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008763 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008764 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008765
8766 // Finger down
8767 int32_t x = 100, y = 100;
8768 processPosition(mapper, x, y);
8769 processSync(mapper);
8770
8771 NotifyMotionArgs motionArgs;
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8774
8775 // Deactivate display viewport
8776 displayViewport.isActive = false;
8777 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008778 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008779
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008780 // The ongoing touch should be canceled immediately
8781 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8782 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8783
8784 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008785 x += 10, y += 10;
8786 processPosition(mapper, x, y);
8787 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008789
8790 // Reactivate display viewport
8791 displayViewport.isActive = true;
8792 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008793 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008794
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008795 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008796 x += 10, y += 10;
8797 processPosition(mapper, x, y);
8798 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008799 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8800 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008801}
8802
Josh Thielene986aed2023-06-01 14:17:30 +00008803/**
8804 * When the viewport is deactivated (isActive transitions from true to false),
8805 * and touch.enableForInactiveViewport is true, touches prior to the transition
8806 * should not be cancelled.
8807 */
8808TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8809 addConfigurationProperty("touch.deviceType", "touchScreen");
8810 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8811 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8812 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8813 std::optional<DisplayViewport> optionalDisplayViewport =
8814 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8815 ASSERT_TRUE(optionalDisplayViewport.has_value());
8816 DisplayViewport displayViewport = *optionalDisplayViewport;
8817
8818 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8819 prepareAxes(POSITION);
8820 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8821
8822 // Finger down
8823 int32_t x = 100, y = 100;
8824 processPosition(mapper, x, y);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8827 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8828
8829 // Deactivate display viewport
8830 displayViewport.isActive = false;
8831 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8832 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8833
8834 // The ongoing touch should not be canceled
8835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8836
8837 // Finger move is not ignored
8838 x += 10, y += 10;
8839 processPosition(mapper, x, y);
8840 processSync(mapper);
8841 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8842 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8843
8844 // Reactivate display viewport
8845 displayViewport.isActive = true;
8846 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8847 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8848
8849 // Finger move continues and does not start new gesture
8850 x += 10, y += 10;
8851 processPosition(mapper, x, y);
8852 processSync(mapper);
8853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8854 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8855}
8856
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008857TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008858 prepareAxes(POSITION);
8859 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008860 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008861 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008862
8863 NotifyMotionArgs motionArgs;
8864 // Unrotated video frame
8865 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8866 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008867 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008868 processPosition(mapper, 100, 200);
8869 processSync(mapper);
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(frames, motionArgs.videoFrames);
8872
8873 // Subsequent touch events should not have any videoframes
8874 // This is implemented separately in FakeEventHub,
8875 // but that should match the behaviour of TouchVideoDevice.
8876 processPosition(mapper, 200, 200);
8877 processSync(mapper);
8878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8879 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8880}
8881
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008882TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008883 prepareAxes(POSITION);
8884 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008885 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008886 // Unrotated video frame
8887 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8888 NotifyMotionArgs motionArgs;
8889
8890 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008891 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008892 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008893 clearViewports();
8894 prepareDisplay(orientation);
8895 std::vector<TouchVideoFrame> frames{frame};
8896 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8897 processPosition(mapper, 100, 200);
8898 processSync(mapper);
8899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8900 ASSERT_EQ(frames, motionArgs.videoFrames);
8901 }
8902}
8903
8904TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8905 prepareAxes(POSITION);
8906 addConfigurationProperty("touch.deviceType", "touchScreen");
8907 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8908 // orientation-aware are affected by display rotation.
8909 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008910 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008911 // Unrotated video frame
8912 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8913 NotifyMotionArgs motionArgs;
8914
8915 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008916 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008917 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008918 clearViewports();
8919 prepareDisplay(orientation);
8920 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008921 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008922 processPosition(mapper, 100, 200);
8923 processSync(mapper);
8924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008925 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8926 // compared to the display. This is so that when the window transform (which contains the
8927 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8928 // window's coordinate space.
8929 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008930 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008931
8932 // Release finger.
8933 processSync(mapper);
8934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008935 }
8936}
8937
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008938TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008939 prepareAxes(POSITION);
8940 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008941 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008942 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8943 // so mix these.
8944 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8945 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8946 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8947 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8948 NotifyMotionArgs motionArgs;
8949
Michael Wrighta9cf4192022-12-01 23:46:39 +00008950 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008951 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008952 processPosition(mapper, 100, 200);
8953 processSync(mapper);
8954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008955 ASSERT_EQ(frames, motionArgs.videoFrames);
8956}
8957
8958TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8959 prepareAxes(POSITION);
8960 addConfigurationProperty("touch.deviceType", "touchScreen");
8961 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8962 // orientation-aware are affected by display rotation.
8963 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008964 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008965 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8966 // so mix these.
8967 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8968 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8969 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8970 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8971 NotifyMotionArgs motionArgs;
8972
Michael Wrighta9cf4192022-12-01 23:46:39 +00008973 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008974 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8975 processPosition(mapper, 100, 200);
8976 processSync(mapper);
8977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8978 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8979 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8980 // compared to the display. This is so that when the window transform (which contains the
8981 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8982 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008983 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008984 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008985 ASSERT_EQ(frames, motionArgs.videoFrames);
8986}
8987
Arthur Hung9da14732019-09-02 16:16:58 +08008988/**
8989 * If we had defined port associations, but the viewport is not ready, the touch device would be
8990 * expected to be disabled, and it should be enabled after the viewport has found.
8991 */
8992TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008993 constexpr uint8_t hdmi2 = 1;
8994 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008995 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008996
8997 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8998
8999 addConfigurationProperty("touch.deviceType", "touchScreen");
9000 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009001 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009002
9003 ASSERT_EQ(mDevice->isEnabled(), false);
9004
9005 // Add display on hdmi2, the device should be enabled and can receive touch event.
9006 prepareSecondaryDisplay(type, hdmi2);
9007 ASSERT_EQ(mDevice->isEnabled(), true);
9008
9009 // Send a touch event.
9010 processPosition(mapper, 100, 100);
9011 processSync(mapper);
9012
9013 NotifyMotionArgs args;
9014 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9015 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9016}
9017
Arthur Hung421eb1c2020-01-16 00:09:42 +08009018TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009019 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009020 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009021 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009022 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009023
9024 NotifyMotionArgs motionArgs;
9025
9026 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9027 // finger down
9028 processId(mapper, 1);
9029 processPosition(mapper, x1, y1);
9030 processSync(mapper);
9031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9032 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009033 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009034
9035 // finger move
9036 processId(mapper, 1);
9037 processPosition(mapper, x2, y2);
9038 processSync(mapper);
9039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9040 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009041 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009042
9043 // finger up.
9044 processId(mapper, -1);
9045 processSync(mapper);
9046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9047 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009048 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009049
9050 // new finger down
9051 processId(mapper, 1);
9052 processPosition(mapper, x3, y3);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009056 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009057}
9058
9059/**
arthurhungcc7f9802020-04-30 17:55:40 +08009060 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9061 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009062 */
arthurhungcc7f9802020-04-30 17:55:40 +08009063TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009064 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009065 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009066 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009067 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009068
9069 NotifyMotionArgs motionArgs;
9070
9071 // default tool type is finger
9072 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009073 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009074 processPosition(mapper, x1, y1);
9075 processSync(mapper);
9076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9077 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009079
9080 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9081 processToolType(mapper, MT_TOOL_PALM);
9082 processSync(mapper);
9083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9084 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9085
9086 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009087 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009088 processPosition(mapper, x2, y2);
9089 processSync(mapper);
9090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9091
9092 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009093 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009094 processSync(mapper);
9095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9096
9097 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009098 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009099 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009100 processPosition(mapper, x3, y3);
9101 processSync(mapper);
9102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9103 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009104 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009105}
9106
arthurhungbf89a482020-04-17 17:37:55 +08009107/**
arthurhungcc7f9802020-04-30 17:55:40 +08009108 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9109 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009110 */
arthurhungcc7f9802020-04-30 17:55:40 +08009111TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009112 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009113 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009114 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009115 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009116
9117 NotifyMotionArgs motionArgs;
9118
9119 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009120 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9121 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009122 processPosition(mapper, x1, y1);
9123 processSync(mapper);
9124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9125 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009126 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009127
9128 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009129 processSlot(mapper, SECOND_SLOT);
9130 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009131 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009132 processSync(mapper);
9133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009134 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009135 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009136
9137 // If the tool type of the first finger changes to MT_TOOL_PALM,
9138 // we expect to receive ACTION_POINTER_UP with cancel flag.
9139 processSlot(mapper, FIRST_SLOT);
9140 processId(mapper, FIRST_TRACKING_ID);
9141 processToolType(mapper, MT_TOOL_PALM);
9142 processSync(mapper);
9143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009144 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009145 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9146
9147 // The following MOVE events of second finger should be processed.
9148 processSlot(mapper, SECOND_SLOT);
9149 processId(mapper, SECOND_TRACKING_ID);
9150 processPosition(mapper, x2 + 1, y2 + 1);
9151 processSync(mapper);
9152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009154 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009155
9156 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9157 // it. Second finger receive move.
9158 processSlot(mapper, FIRST_SLOT);
9159 processId(mapper, INVALID_TRACKING_ID);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009163 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009164
9165 // Second finger keeps moving.
9166 processSlot(mapper, SECOND_SLOT);
9167 processId(mapper, SECOND_TRACKING_ID);
9168 processPosition(mapper, x2 + 2, y2 + 2);
9169 processSync(mapper);
9170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9171 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009172 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009173
9174 // Second finger up.
9175 processId(mapper, INVALID_TRACKING_ID);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9179 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9180}
9181
9182/**
9183 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9184 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9185 */
9186TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9187 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009188 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009189 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009190 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009191
9192 NotifyMotionArgs motionArgs;
9193
9194 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9195 // First finger down.
9196 processId(mapper, FIRST_TRACKING_ID);
9197 processPosition(mapper, x1, y1);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9200 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009201 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009202
9203 // Second finger down.
9204 processSlot(mapper, SECOND_SLOT);
9205 processId(mapper, SECOND_TRACKING_ID);
9206 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009209 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009211
arthurhungcc7f9802020-04-30 17:55:40 +08009212 // If the tool type of the first finger changes to MT_TOOL_PALM,
9213 // we expect to receive ACTION_POINTER_UP with cancel flag.
9214 processSlot(mapper, FIRST_SLOT);
9215 processId(mapper, FIRST_TRACKING_ID);
9216 processToolType(mapper, MT_TOOL_PALM);
9217 processSync(mapper);
9218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009219 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009220 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9221
9222 // Second finger keeps moving.
9223 processSlot(mapper, SECOND_SLOT);
9224 processId(mapper, SECOND_TRACKING_ID);
9225 processPosition(mapper, x2 + 1, y2 + 1);
9226 processSync(mapper);
9227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9228 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9229
9230 // second finger becomes palm, receive cancel due to only 1 finger is active.
9231 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009232 processToolType(mapper, MT_TOOL_PALM);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9236
arthurhungcc7f9802020-04-30 17:55:40 +08009237 // third finger down.
9238 processSlot(mapper, THIRD_SLOT);
9239 processId(mapper, THIRD_TRACKING_ID);
9240 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009241 processPosition(mapper, x3, y3);
9242 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9244 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009245 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009246 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009247
9248 // third finger move
9249 processId(mapper, THIRD_TRACKING_ID);
9250 processPosition(mapper, x3 + 1, y3 + 1);
9251 processSync(mapper);
9252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9254
9255 // first finger up, third finger receive move.
9256 processSlot(mapper, FIRST_SLOT);
9257 processId(mapper, INVALID_TRACKING_ID);
9258 processSync(mapper);
9259 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9260 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009261 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009262
9263 // second finger up, third finger receive move.
9264 processSlot(mapper, SECOND_SLOT);
9265 processId(mapper, INVALID_TRACKING_ID);
9266 processSync(mapper);
9267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9268 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009269 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009270
9271 // third finger up.
9272 processSlot(mapper, THIRD_SLOT);
9273 processId(mapper, INVALID_TRACKING_ID);
9274 processSync(mapper);
9275 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9276 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9277 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9278}
9279
9280/**
9281 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9282 * and the active finger could still be allowed to receive the events
9283 */
9284TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9285 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009286 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009287 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009288 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009289
9290 NotifyMotionArgs motionArgs;
9291
9292 // default tool type is finger
9293 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9294 processId(mapper, FIRST_TRACKING_ID);
9295 processPosition(mapper, x1, y1);
9296 processSync(mapper);
9297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9298 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009299 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009300
9301 // Second finger down.
9302 processSlot(mapper, SECOND_SLOT);
9303 processId(mapper, SECOND_TRACKING_ID);
9304 processPosition(mapper, x2, y2);
9305 processSync(mapper);
9306 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009307 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009308 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009309
9310 // If the tool type of the second finger changes to MT_TOOL_PALM,
9311 // we expect to receive ACTION_POINTER_UP with cancel flag.
9312 processId(mapper, SECOND_TRACKING_ID);
9313 processToolType(mapper, MT_TOOL_PALM);
9314 processSync(mapper);
9315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009316 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009317 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9318
9319 // The following MOVE event should be processed.
9320 processSlot(mapper, FIRST_SLOT);
9321 processId(mapper, FIRST_TRACKING_ID);
9322 processPosition(mapper, x1 + 1, y1 + 1);
9323 processSync(mapper);
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9325 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009326 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009327
9328 // second finger up.
9329 processSlot(mapper, SECOND_SLOT);
9330 processId(mapper, INVALID_TRACKING_ID);
9331 processSync(mapper);
9332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9334
9335 // first finger keep moving
9336 processSlot(mapper, FIRST_SLOT);
9337 processId(mapper, FIRST_TRACKING_ID);
9338 processPosition(mapper, x1 + 2, y1 + 2);
9339 processSync(mapper);
9340 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9341 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9342
9343 // first finger up.
9344 processId(mapper, INVALID_TRACKING_ID);
9345 processSync(mapper);
9346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9347 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9348 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009349}
9350
Arthur Hung9ad18942021-06-19 02:04:46 +00009351/**
9352 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9353 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9354 * cause slot be valid again.
9355 */
9356TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9357 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009358 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009359 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009360 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009361
9362 NotifyMotionArgs motionArgs;
9363
9364 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9365 // First finger down.
9366 processId(mapper, FIRST_TRACKING_ID);
9367 processPosition(mapper, x1, y1);
9368 processPressure(mapper, RAW_PRESSURE_MAX);
9369 processSync(mapper);
9370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9371 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009372 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009373
9374 // First finger move.
9375 processId(mapper, FIRST_TRACKING_ID);
9376 processPosition(mapper, x1 + 1, y1 + 1);
9377 processPressure(mapper, RAW_PRESSURE_MAX);
9378 processSync(mapper);
9379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9380 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009381 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009382
9383 // Second finger down.
9384 processSlot(mapper, SECOND_SLOT);
9385 processId(mapper, SECOND_TRACKING_ID);
9386 processPosition(mapper, x2, y2);
9387 processPressure(mapper, RAW_PRESSURE_MAX);
9388 processSync(mapper);
9389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009390 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009391 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009392
9393 // second finger up with some unexpected data.
9394 processSlot(mapper, SECOND_SLOT);
9395 processId(mapper, INVALID_TRACKING_ID);
9396 processPosition(mapper, x2, y2);
9397 processSync(mapper);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009399 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009400 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009401
9402 // first finger up with some unexpected data.
9403 processSlot(mapper, FIRST_SLOT);
9404 processId(mapper, INVALID_TRACKING_ID);
9405 processPosition(mapper, x2, y2);
9406 processPressure(mapper, RAW_PRESSURE_MAX);
9407 processSync(mapper);
9408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9409 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009410 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009411}
9412
Arpit Singh4b4a4572023-11-24 18:19:56 +00009413TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009414 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009415 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009416 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009417 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009418
9419 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009420 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009421 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009422 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009423 processPressure(mapper, RAW_PRESSURE_MAX);
9424 processSync(mapper);
9425 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9426 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9427
9428 // Second finger down.
9429 processSlot(mapper, SECOND_SLOT);
9430 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009431 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009432 processPressure(mapper, RAW_PRESSURE_MAX);
9433 processSync(mapper);
9434 ASSERT_NO_FATAL_FAILURE(
9435 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9436
Arpit Singh4b4a4572023-11-24 18:19:56 +00009437 // Set MT Slot state to be repopulated for the required slots
9438 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9439 mtSlotValues[0] = FIRST_TRACKING_ID;
9440 mtSlotValues[1] = SECOND_TRACKING_ID;
9441 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9442
9443 mtSlotValues[0] = x1;
9444 mtSlotValues[1] = x2;
9445 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9446
9447 mtSlotValues[0] = y1;
9448 mtSlotValues[1] = y2;
9449 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9450
9451 mtSlotValues[0] = RAW_PRESSURE_MAX;
9452 mtSlotValues[1] = RAW_PRESSURE_MAX;
9453 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9454
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009455 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009456 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009457 resetMapper(mapper, ARBITRARY_TIME);
9458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9459 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009460
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009461 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9462 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009463 processPosition(mapper, 301, 302);
9464 processSync(mapper);
9465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9468 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009469
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9471}
9472
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009473TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009474 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009475 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009476 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009477 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009478
9479 // First finger touches down and releases.
9480 processId(mapper, FIRST_TRACKING_ID);
9481 processPosition(mapper, 100, 200);
9482 processPressure(mapper, RAW_PRESSURE_MAX);
9483 processSync(mapper);
9484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9485 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9486 processId(mapper, INVALID_TRACKING_ID);
9487 processSync(mapper);
9488 ASSERT_NO_FATAL_FAILURE(
9489 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9490
9491 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9492 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009493 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9495
9496 // Send an empty sync frame. Since there are no pointers, no events are generated.
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9499}
9500
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009501TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009502 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009503 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009504 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009505 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009507
9508 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9509 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9510 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9511 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9512 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9513
9514 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009515 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009516 processId(mapper, FIRST_TRACKING_ID);
9517 processToolType(mapper, MT_TOOL_PEN);
9518 processPosition(mapper, 100, 200);
9519 processPressure(mapper, RAW_PRESSURE_MAX);
9520 processSync(mapper);
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9522 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9523 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009524 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009525
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009526 // Now that we know the device supports styluses, ensure that the device is re-configured with
9527 // the stylus source.
9528 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9529 {
9530 const auto& devices = mReader->getInputDevices();
9531 auto deviceInfo =
9532 std::find_if(devices.begin(), devices.end(),
9533 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9534 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9535 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9536 }
9537
9538 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9539 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9540
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009541 processId(mapper, INVALID_TRACKING_ID);
9542 processSync(mapper);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9544 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9545 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009546 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009547}
9548
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009549// --- MultiTouchInputMapperTest_ExternalDevice ---
9550
9551class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9552protected:
Chris Yea52ade12020-08-27 16:49:20 -07009553 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009554};
9555
9556/**
9557 * Expect fallback to internal viewport if device is external and external viewport is not present.
9558 */
9559TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9560 prepareAxes(POSITION);
9561 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009562 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009563 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009564
9565 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9566
9567 NotifyMotionArgs motionArgs;
9568
9569 // Expect the event to be sent to the internal viewport,
9570 // because an external viewport is not present.
9571 processPosition(mapper, 100, 100);
9572 processSync(mapper);
9573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9574 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9575
9576 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009577 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009578 processPosition(mapper, 100, 100);
9579 processSync(mapper);
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9581 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9582}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009583
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009584// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9585// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9586// PointerChoreographer refactor.
9587TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009588 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009589 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009590 prepareAxes(POSITION | ID | SLOT);
9591 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009593 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009594 // run uncaptured pointer tests - pushes out generic events
9595 // FINGER 0 DOWN
9596 processId(mapper, 3);
9597 processPosition(mapper, 100, 100);
9598 processKey(mapper, BTN_TOUCH, 1);
9599 processSync(mapper);
9600
9601 // start at (100,100), cursor should be at (0,0) * scale
9602 NotifyMotionArgs args;
9603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9605 ASSERT_NO_FATAL_FAILURE(
9606 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9607
9608 // FINGER 0 MOVE
9609 processPosition(mapper, 200, 200);
9610 processSync(mapper);
9611
9612 // compute scaling to help with touch position checking
9613 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9614 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9615 float scale =
9616 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9617
9618 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9620 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9622 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009623
9624 // BUTTON DOWN
9625 processKey(mapper, BTN_LEFT, 1);
9626 processSync(mapper);
9627
9628 // touchinputmapper design sends a move before button press
9629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9630 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9632 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9633
9634 // BUTTON UP
9635 processKey(mapper, BTN_LEFT, 0);
9636 processSync(mapper);
9637
9638 // touchinputmapper design sends a move after button release
9639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9640 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9642 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009643}
9644
Harry Cutts8722be92024-04-05 14:46:05 +00009645TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009646 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009647 prepareAxes(POSITION | ID | SLOT);
9648 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009649 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009650 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009651
Josep del Río2d8c79a2023-01-23 19:33:50 +00009652 // uncaptured touchpad should be a pointer device
9653 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009654}
9655
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009656// --- BluetoothMultiTouchInputMapperTest ---
9657
9658class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9659protected:
9660 void SetUp() override {
9661 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9662 }
9663};
9664
9665TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9666 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009667 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009668 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009669 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009670
9671 nsecs_t kernelEventTime = ARBITRARY_TIME;
9672 nsecs_t expectedEventTime = ARBITRARY_TIME;
9673 // Touch down.
9674 processId(mapper, FIRST_TRACKING_ID);
9675 processPosition(mapper, 100, 200);
9676 processPressure(mapper, RAW_PRESSURE_MAX);
9677 processSync(mapper, ARBITRARY_TIME);
9678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9679 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9680
9681 // Process several events that come in quick succession, according to their timestamps.
9682 for (int i = 0; i < 3; i++) {
9683 constexpr static nsecs_t delta = ms2ns(1);
9684 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9685 kernelEventTime += delta;
9686 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9687
9688 processPosition(mapper, 101 + i, 201 + i);
9689 processSync(mapper, kernelEventTime);
9690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9691 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9692 WithEventTime(expectedEventTime))));
9693 }
9694
9695 // Release the touch.
9696 processId(mapper, INVALID_TRACKING_ID);
9697 processPressure(mapper, RAW_PRESSURE_MIN);
9698 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9700 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9701 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9702}
9703
9704// --- MultiTouchPointerModeTest ---
9705
HQ Liue6983c72022-04-19 22:14:56 +00009706class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9707protected:
9708 float mPointerMovementScale;
9709 float mPointerXZoomScale;
9710 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9711 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009712 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009713
9714 prepareAxes(POSITION);
9715 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9716 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9717 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009718 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009719 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009720
9721 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9722 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9723 mPointerMovementScale =
9724 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9725 mPointerXZoomScale =
9726 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9727 }
9728
9729 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9730 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9731 /*flat*/ 0,
9732 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9733 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9734 /*flat*/ 0,
9735 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9736 }
9737};
9738
9739/**
9740 * Two fingers down on a pointer mode touch pad. The width
9741 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9742 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9743 * be greater than the both value to be freeform gesture, so that after two
9744 * fingers start to move downwards, the gesture should be swipe.
9745 */
9746TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9747 // The min freeform gesture width is 25units/mm x 30mm = 750
9748 // which is greater than fraction of the diagnal length of the touchpad (349).
9749 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009750 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009751 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009752 NotifyMotionArgs motionArgs;
9753
9754 // Two fingers down at once.
9755 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9756 // Pointer's initial position is used the [0,0] coordinate.
9757 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9758
9759 processId(mapper, FIRST_TRACKING_ID);
9760 processPosition(mapper, x1, y1);
9761 processMTSync(mapper);
9762 processId(mapper, SECOND_TRACKING_ID);
9763 processPosition(mapper, x2, y2);
9764 processMTSync(mapper);
9765 processSync(mapper);
9766
9767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009768 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009769 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009771 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009772 ASSERT_NO_FATAL_FAILURE(
9773 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9774
9775 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9776 // that there should be 1 pointer.
9777 int32_t movingDistance = 200;
9778 y1 += movingDistance;
9779 y2 += movingDistance;
9780
9781 processId(mapper, FIRST_TRACKING_ID);
9782 processPosition(mapper, x1, y1);
9783 processMTSync(mapper);
9784 processId(mapper, SECOND_TRACKING_ID);
9785 processPosition(mapper, x2, y2);
9786 processMTSync(mapper);
9787 processSync(mapper);
9788
9789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009790 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009791 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009792 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009793 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009794 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9795 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9796 0, 0, 0, 0));
9797}
9798
9799/**
9800 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9801 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9802 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9803 * value to be freeform gesture, so that after two fingers start to move downwards,
9804 * the gesture should be swipe.
9805 */
9806TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9807 // The min freeform gesture width is 5units/mm x 30mm = 150
9808 // which is greater than fraction of the diagnal length of the touchpad (349).
9809 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009810 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009811 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009812 NotifyMotionArgs motionArgs;
9813
9814 // Two fingers down at once.
9815 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9816 // Pointer's initial position is used the [0,0] coordinate.
9817 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9818
9819 processId(mapper, FIRST_TRACKING_ID);
9820 processPosition(mapper, x1, y1);
9821 processMTSync(mapper);
9822 processId(mapper, SECOND_TRACKING_ID);
9823 processPosition(mapper, x2, y2);
9824 processMTSync(mapper);
9825 processSync(mapper);
9826
9827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009828 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009829 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009830 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009831 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009832 ASSERT_NO_FATAL_FAILURE(
9833 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9834
9835 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9836 // and there should be 1 pointer.
9837 int32_t movingDistance = 200;
9838 y1 += movingDistance;
9839 y2 += movingDistance;
9840
9841 processId(mapper, FIRST_TRACKING_ID);
9842 processPosition(mapper, x1, y1);
9843 processMTSync(mapper);
9844 processId(mapper, SECOND_TRACKING_ID);
9845 processPosition(mapper, x2, y2);
9846 processMTSync(mapper);
9847 processSync(mapper);
9848
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009850 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009853 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009854 // New coordinate is the scaled relative coordinate from the initial coordinate.
9855 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9856 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9857 0, 0, 0, 0));
9858}
9859
9860/**
9861 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9862 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9863 * freeform gestures after two fingers start to move downwards.
9864 */
9865TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009866 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009867 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009868
9869 NotifyMotionArgs motionArgs;
9870
9871 // Two fingers down at once. Wider than the max swipe width.
9872 // The gesture is expected to be PRESS, then transformed to FREEFORM
9873 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9874
9875 processId(mapper, FIRST_TRACKING_ID);
9876 processPosition(mapper, x1, y1);
9877 processMTSync(mapper);
9878 processId(mapper, SECOND_TRACKING_ID);
9879 processPosition(mapper, x2, y2);
9880 processMTSync(mapper);
9881 processSync(mapper);
9882
9883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009884 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009885 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009886 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009887 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009888 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9889 ASSERT_NO_FATAL_FAILURE(
9890 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9891
9892 int32_t movingDistance = 200;
9893
9894 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9895 // then two down events for two pointers.
9896 y1 += movingDistance;
9897 y2 += movingDistance;
9898
9899 processId(mapper, FIRST_TRACKING_ID);
9900 processPosition(mapper, x1, y1);
9901 processMTSync(mapper);
9902 processId(mapper, SECOND_TRACKING_ID);
9903 processPosition(mapper, x2, y2);
9904 processMTSync(mapper);
9905 processSync(mapper);
9906
9907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9908 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009909 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009910 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009912 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009913 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009914 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009916 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009917 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009918 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009919 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009920 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009921 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009922 // Two pointers' scaled relative coordinates from their initial centroid.
9923 // Initial y coordinates are 0 as y1 and y2 have the same value.
9924 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9925 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9926 // When pointers move, the new coordinates equal to the initial coordinates plus
9927 // scaled moving distance.
9928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9929 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9930 0, 0, 0, 0));
9931 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9932 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9933 0, 0, 0, 0));
9934
9935 // Move two fingers down again, expect one MOVE motion event.
9936 y1 += movingDistance;
9937 y2 += movingDistance;
9938
9939 processId(mapper, FIRST_TRACKING_ID);
9940 processPosition(mapper, x1, y1);
9941 processMTSync(mapper);
9942 processId(mapper, SECOND_TRACKING_ID);
9943 processPosition(mapper, x2, y2);
9944 processMTSync(mapper);
9945 processSync(mapper);
9946
9947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009948 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009951 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009952 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9953 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9954 0, 0, 0, 0, 0));
9955 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9956 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9957 0, 0, 0, 0, 0));
9958}
9959
Harry Cutts39b7ca22022-10-05 15:55:48 +00009960TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +00009961 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009962 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +00009963 NotifyMotionArgs motionArgs;
9964
9965 // Place two fingers down.
9966 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9967
9968 processId(mapper, FIRST_TRACKING_ID);
9969 processPosition(mapper, x1, y1);
9970 processMTSync(mapper);
9971 processId(mapper, SECOND_TRACKING_ID);
9972 processPosition(mapper, x2, y2);
9973 processMTSync(mapper);
9974 processSync(mapper);
9975
9976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009977 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +00009978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9979 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
9980 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
9981 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
9982
9983 // Move the two fingers down and to the left.
9984 int32_t movingDistance = 200;
9985 x1 -= movingDistance;
9986 y1 += movingDistance;
9987 x2 -= movingDistance;
9988 y2 += movingDistance;
9989
9990 processId(mapper, FIRST_TRACKING_ID);
9991 processPosition(mapper, x1, y1);
9992 processMTSync(mapper);
9993 processId(mapper, SECOND_TRACKING_ID);
9994 processPosition(mapper, x2, y2);
9995 processMTSync(mapper);
9996 processSync(mapper);
9997
9998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009999 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010000 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10001 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10002 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10003 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10004}
10005
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010006TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010007 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010008 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010009 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10011
10012 // Start a stylus gesture.
10013 processKey(mapper, BTN_TOOL_PEN, 1);
10014 processId(mapper, FIRST_TRACKING_ID);
10015 processPosition(mapper, 100, 200);
10016 processSync(mapper);
10017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10018 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10019 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010020 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010021 // TODO(b/257078296): Pointer mode generates extra event.
10022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10023 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10024 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010025 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10027
10028 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10029 // gesture should be disabled.
10030 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10031 viewport->isActive = false;
10032 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010033 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10036 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010037 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010038 // TODO(b/257078296): Pointer mode generates extra event.
10039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10040 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10041 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010042 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10044}
10045
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010046// --- JoystickInputMapperTest ---
10047
10048class JoystickInputMapperTest : public InputMapperTest {
10049protected:
10050 static const int32_t RAW_X_MIN;
10051 static const int32_t RAW_X_MAX;
10052 static const int32_t RAW_Y_MIN;
10053 static const int32_t RAW_Y_MAX;
10054
10055 void SetUp() override {
10056 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10057 }
10058 void prepareAxes() {
10059 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10061 }
10062
10063 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10064 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10065 }
10066
10067 void processSync(JoystickInputMapper& mapper) {
10068 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10069 }
10070
Michael Wrighta9cf4192022-12-01 23:46:39 +000010071 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010072 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10073 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10074 NO_PORT, ViewportType::VIRTUAL);
10075 }
10076};
10077
10078const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10079const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10080const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10081const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10082
10083TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10084 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010085 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010086
10087 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10088
Michael Wrighta9cf4192022-12-01 23:46:39 +000010089 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010090
10091 // Send an axis event
10092 processAxis(mapper, ABS_X, 100);
10093 processSync(mapper);
10094
10095 NotifyMotionArgs args;
10096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10097 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10098
10099 // Send another axis event
10100 processAxis(mapper, ABS_Y, 100);
10101 processSync(mapper);
10102
10103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10104 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10105}
10106
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010107// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010108
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010109class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010110protected:
10111 static const char* DEVICE_NAME;
10112 static const char* DEVICE_LOCATION;
10113 static const int32_t DEVICE_ID;
10114 static const int32_t DEVICE_GENERATION;
10115 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010116 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010117 static const int32_t EVENTHUB_ID;
10118
10119 std::shared_ptr<FakeEventHub> mFakeEventHub;
10120 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010121 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010122 std::unique_ptr<InstrumentedInputReader> mReader;
10123 std::shared_ptr<InputDevice> mDevice;
10124
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010125 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010126 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010127 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010128 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010129 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010130 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010131 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10132 }
10133
10134 void SetUp() override { SetUp(DEVICE_CLASSES); }
10135
10136 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010137 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010138 mFakePolicy.clear();
10139 }
10140
Chris Yee2b1e5c2021-03-10 22:45:12 -080010141 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10142 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010143 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010144 InputDeviceIdentifier identifier;
10145 identifier.name = name;
10146 identifier.location = location;
10147 std::shared_ptr<InputDevice> device =
10148 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10149 identifier);
10150 mReader->pushNextDevice(device);
10151 mFakeEventHub->addDevice(eventHubId, name, classes);
10152 mReader->loopOnce();
10153 return device;
10154 }
10155
10156 template <class T, typename... Args>
10157 T& addControllerAndConfigure(Args... args) {
10158 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10159
10160 return controller;
10161 }
10162};
10163
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010164const char* PeripheralControllerTest::DEVICE_NAME = "device";
10165const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10166const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10167const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10168const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010169const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10170 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010171const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010172
10173// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010174class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010175protected:
10176 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010177 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010178 }
10179};
10180
10181TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010182 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010183
Harry Cuttsa5b71292022-11-28 12:56:17 +000010184 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10185 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10186 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010187}
10188
10189TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010190 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010191
Harry Cuttsa5b71292022-11-28 12:56:17 +000010192 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10193 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10194 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010195}
10196
10197// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010198class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010199protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010200 void SetUp() override {
10201 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10202 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010203};
10204
Chris Ye85758332021-05-16 23:05:17 -070010205TEST_F(LightControllerTest, MonoLight) {
10206 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010207 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010208 .maxBrightness = 255,
10209 .flags = InputLightClass::BRIGHTNESS,
10210 .path = ""};
10211 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010212
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010213 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010214 InputDeviceInfo info;
10215 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010216 std::vector<InputDeviceLightInfo> lights = info.getLights();
10217 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010218 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10219 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10220
10221 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10222 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10223}
10224
DingYong99f2c3c2023-12-20 15:46:06 +080010225TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10226 RawLightInfo infoMono = {.id = 1,
10227 .name = "mono_keyboard_mute",
10228 .maxBrightness = 255,
10229 .flags = InputLightClass::BRIGHTNESS |
10230 InputLightClass::KEYBOARD_MIC_MUTE,
10231 .path = ""};
10232 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10233
10234 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10235 std::list<NotifyArgs> unused =
10236 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10237 /*changes=*/{});
10238
10239 InputDeviceInfo info;
10240 controller.populateDeviceInfo(&info);
10241 std::vector<InputDeviceLightInfo> lights = info.getLights();
10242 ASSERT_EQ(1U, lights.size());
10243 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10244 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10245}
10246
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010247TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10248 RawLightInfo infoMono = {.id = 1,
10249 .name = "mono_keyboard_backlight",
10250 .maxBrightness = 255,
10251 .flags = InputLightClass::BRIGHTNESS |
10252 InputLightClass::KEYBOARD_BACKLIGHT,
10253 .path = ""};
10254 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10255
10256 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10257 InputDeviceInfo info;
10258 controller.populateDeviceInfo(&info);
10259 std::vector<InputDeviceLightInfo> lights = info.getLights();
10260 ASSERT_EQ(1U, lights.size());
10261 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10262 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010263
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010264 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10265 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010266}
10267
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010268TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10269 RawLightInfo infoMono = {.id = 1,
10270 .name = "mono_light",
10271 .maxBrightness = 255,
10272 .flags = InputLightClass::BRIGHTNESS,
10273 .path = ""};
10274 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10275 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10276 "0,100,200");
10277
10278 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10279 std::list<NotifyArgs> unused =
10280 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10281 /*changes=*/{});
10282
10283 InputDeviceInfo info;
10284 controller.populateDeviceInfo(&info);
10285 std::vector<InputDeviceLightInfo> lights = info.getLights();
10286 ASSERT_EQ(1U, lights.size());
10287 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10288}
10289
10290TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10291 RawLightInfo infoMono = {.id = 1,
10292 .name = "mono_keyboard_backlight",
10293 .maxBrightness = 255,
10294 .flags = InputLightClass::BRIGHTNESS |
10295 InputLightClass::KEYBOARD_BACKLIGHT,
10296 .path = ""};
10297 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10298
10299 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10300 std::list<NotifyArgs> unused =
10301 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10302 /*changes=*/{});
10303
10304 InputDeviceInfo info;
10305 controller.populateDeviceInfo(&info);
10306 std::vector<InputDeviceLightInfo> lights = info.getLights();
10307 ASSERT_EQ(1U, lights.size());
10308 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10309}
10310
10311TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10312 RawLightInfo infoMono = {.id = 1,
10313 .name = "mono_keyboard_backlight",
10314 .maxBrightness = 255,
10315 .flags = InputLightClass::BRIGHTNESS |
10316 InputLightClass::KEYBOARD_BACKLIGHT,
10317 .path = ""};
10318 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10319 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10320 "0,100,200");
10321
10322 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10323 std::list<NotifyArgs> unused =
10324 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10325 /*changes=*/{});
10326
10327 InputDeviceInfo info;
10328 controller.populateDeviceInfo(&info);
10329 std::vector<InputDeviceLightInfo> lights = info.getLights();
10330 ASSERT_EQ(1U, lights.size());
10331 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10332 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10333 ASSERT_EQ(BrightnessLevel(0), *it);
10334 std::advance(it, 1);
10335 ASSERT_EQ(BrightnessLevel(100), *it);
10336 std::advance(it, 1);
10337 ASSERT_EQ(BrightnessLevel(200), *it);
10338}
10339
10340TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10341 RawLightInfo infoMono = {.id = 1,
10342 .name = "mono_keyboard_backlight",
10343 .maxBrightness = 255,
10344 .flags = InputLightClass::BRIGHTNESS |
10345 InputLightClass::KEYBOARD_BACKLIGHT,
10346 .path = ""};
10347 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10348 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10349 "0,100,200,300,400,500");
10350
10351 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10352 std::list<NotifyArgs> unused =
10353 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10354 /*changes=*/{});
10355
10356 InputDeviceInfo info;
10357 controller.populateDeviceInfo(&info);
10358 std::vector<InputDeviceLightInfo> lights = info.getLights();
10359 ASSERT_EQ(1U, lights.size());
10360 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10361}
10362
Chris Yee2b1e5c2021-03-10 22:45:12 -080010363TEST_F(LightControllerTest, RGBLight) {
10364 RawLightInfo infoRed = {.id = 1,
10365 .name = "red",
10366 .maxBrightness = 255,
10367 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10368 .path = ""};
10369 RawLightInfo infoGreen = {.id = 2,
10370 .name = "green",
10371 .maxBrightness = 255,
10372 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10373 .path = ""};
10374 RawLightInfo infoBlue = {.id = 3,
10375 .name = "blue",
10376 .maxBrightness = 255,
10377 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10378 .path = ""};
10379 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10380 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10381 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10382
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010383 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010384 InputDeviceInfo info;
10385 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010386 std::vector<InputDeviceLightInfo> lights = info.getLights();
10387 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010388 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10389 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10390 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10391
10392 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10393 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10394}
10395
10396TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10397 RawLightInfo infoRed = {.id = 1,
10398 .name = "red_keyboard_backlight",
10399 .maxBrightness = 255,
10400 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10401 InputLightClass::KEYBOARD_BACKLIGHT,
10402 .path = ""};
10403 RawLightInfo infoGreen = {.id = 2,
10404 .name = "green_keyboard_backlight",
10405 .maxBrightness = 255,
10406 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10407 InputLightClass::KEYBOARD_BACKLIGHT,
10408 .path = ""};
10409 RawLightInfo infoBlue = {.id = 3,
10410 .name = "blue_keyboard_backlight",
10411 .maxBrightness = 255,
10412 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10413 InputLightClass::KEYBOARD_BACKLIGHT,
10414 .path = ""};
10415 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10416 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10417 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10418
10419 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10420 InputDeviceInfo info;
10421 controller.populateDeviceInfo(&info);
10422 std::vector<InputDeviceLightInfo> lights = info.getLights();
10423 ASSERT_EQ(1U, lights.size());
10424 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10425 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10426 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10427
10428 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10429 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10430}
10431
10432TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10433 RawLightInfo infoRed = {.id = 1,
10434 .name = "red",
10435 .maxBrightness = 255,
10436 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10437 .path = ""};
10438 RawLightInfo infoGreen = {.id = 2,
10439 .name = "green",
10440 .maxBrightness = 255,
10441 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10442 .path = ""};
10443 RawLightInfo infoBlue = {.id = 3,
10444 .name = "blue",
10445 .maxBrightness = 255,
10446 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10447 .path = ""};
10448 RawLightInfo infoGlobal = {.id = 3,
10449 .name = "global_keyboard_backlight",
10450 .maxBrightness = 255,
10451 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10452 InputLightClass::KEYBOARD_BACKLIGHT,
10453 .path = ""};
10454 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10455 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10456 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10457 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10458
10459 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10460 InputDeviceInfo info;
10461 controller.populateDeviceInfo(&info);
10462 std::vector<InputDeviceLightInfo> lights = info.getLights();
10463 ASSERT_EQ(1U, lights.size());
10464 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10465 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10466 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010467
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010468 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10469 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010470}
10471
10472TEST_F(LightControllerTest, MultiColorRGBLight) {
10473 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010474 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010475 .maxBrightness = 255,
10476 .flags = InputLightClass::BRIGHTNESS |
10477 InputLightClass::MULTI_INTENSITY |
10478 InputLightClass::MULTI_INDEX,
10479 .path = ""};
10480
10481 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10482
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010483 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010484 InputDeviceInfo info;
10485 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010486 std::vector<InputDeviceLightInfo> lights = info.getLights();
10487 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010488 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10489 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10490 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10491
10492 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10493 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10494}
10495
10496TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10497 RawLightInfo infoColor = {.id = 1,
10498 .name = "multi_color_keyboard_backlight",
10499 .maxBrightness = 255,
10500 .flags = InputLightClass::BRIGHTNESS |
10501 InputLightClass::MULTI_INTENSITY |
10502 InputLightClass::MULTI_INDEX |
10503 InputLightClass::KEYBOARD_BACKLIGHT,
10504 .path = ""};
10505
10506 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10507
10508 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10509 InputDeviceInfo info;
10510 controller.populateDeviceInfo(&info);
10511 std::vector<InputDeviceLightInfo> lights = info.getLights();
10512 ASSERT_EQ(1U, lights.size());
10513 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10514 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10515 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010516
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010517 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10518 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010519}
10520
10521TEST_F(LightControllerTest, PlayerIdLight) {
10522 RawLightInfo info1 = {.id = 1,
10523 .name = "player1",
10524 .maxBrightness = 255,
10525 .flags = InputLightClass::BRIGHTNESS,
10526 .path = ""};
10527 RawLightInfo info2 = {.id = 2,
10528 .name = "player2",
10529 .maxBrightness = 255,
10530 .flags = InputLightClass::BRIGHTNESS,
10531 .path = ""};
10532 RawLightInfo info3 = {.id = 3,
10533 .name = "player3",
10534 .maxBrightness = 255,
10535 .flags = InputLightClass::BRIGHTNESS,
10536 .path = ""};
10537 RawLightInfo info4 = {.id = 4,
10538 .name = "player4",
10539 .maxBrightness = 255,
10540 .flags = InputLightClass::BRIGHTNESS,
10541 .path = ""};
10542 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10543 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10544 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10545 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10546
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010547 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010548 InputDeviceInfo info;
10549 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010550 std::vector<InputDeviceLightInfo> lights = info.getLights();
10551 ASSERT_EQ(1U, lights.size());
10552 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010553 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10554 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010555
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010556 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10557 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10558 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010559}
10560
Michael Wrightd02c5b62014-02-10 15:10:22 -080010561} // namespace android