blob: 476f755bae4f73f797745734aa4804a18b6add8d [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
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());
152 std::this_thread::sleep_for(std::chrono::milliseconds(1));
153 }
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
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001167 auto request = mFakePolicy->setPointerCapture(true);
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);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001171 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
1174 mFakePolicy->setPointerCapture(false);
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);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001178 ASSERT_FALSE(args.request.enable) << "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 Pradhan2853b7a2021-08-23 14:08:51 +00001351 std::shared_ptr<FakePointerController> mFakePointerController;
1352
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001353 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1354 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1355
Chris Yea52ade12020-08-27 16:49:20 -07001356 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001357#if !defined(__ANDROID__)
1358 GTEST_SKIP();
1359#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001360 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001361 mFakePointerController = std::make_shared<FakePointerController>();
1362 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001363
Arpit Singh440bf652023-08-09 09:23:43 +00001364 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001365 }
1366
Chris Yea52ade12020-08-27 16:49:20 -07001367 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001368#if !defined(__ANDROID__)
1369 return;
1370#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001371 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001372 mReader.reset();
1373 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001374 mFakePolicy.clear();
1375 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001376
1377 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1378 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1379 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1380 [&name](const InputDeviceInfo& info) {
1381 return info.getIdentifier().name == name;
1382 });
1383 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1384 }
Arpit Singh440bf652023-08-09 09:23:43 +00001385
1386 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001387 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1388 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001389
1390 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1391 *mTestListener);
1392 ASSERT_EQ(mReader->start(), OK);
1393
1394 // Since this test is run on a real device, all the input devices connected
1395 // to the test device will show up in mReader. We wait for those input devices to
1396 // show up before beginning the tests.
1397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1399 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1400 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001401};
1402
1403TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1404 // An invalid input device that is only used for this test.
1405 class InvalidUinputDevice : public UinputDevice {
1406 public:
Harry Cutts33476232023-01-30 19:57:29 +00001407 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001408
1409 private:
1410 void configureDevice(int fd, uinput_user_dev* device) override {}
1411 };
1412
1413 const size_t numDevices = mFakePolicy->getInputDevices().size();
1414
1415 // UinputDevice does not set any event or key bits, so InputReader should not
1416 // consider it as a valid device.
1417 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1418 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1420 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1421
1422 invalidDevice.reset();
1423 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1424 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1425 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1426}
1427
1428TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1429 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1430
1431 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1432 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1433 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1434 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1435
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001436 const auto device = findDeviceByName(keyboard->getName());
1437 ASSERT_TRUE(device.has_value());
1438 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1439 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1440 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001441
1442 keyboard.reset();
1443 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1444 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1445 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1446}
1447
1448TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1449 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1450 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1451
1452 NotifyConfigurationChangedArgs configChangedArgs;
1453 ASSERT_NO_FATAL_FAILURE(
1454 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001455 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001456 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1457
1458 NotifyKeyArgs keyArgs;
1459 keyboard->pressAndReleaseHomeKey();
1460 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1461 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001462 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001463 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001464 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001465 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001466 prevTimestamp = keyArgs.eventTime;
1467
1468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1469 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001470 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001471 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001472 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001473}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001474
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001475TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1476 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1478
1479 const auto device = findDeviceByName(stylus->getName());
1480 ASSERT_TRUE(device.has_value());
1481
Prabir Pradhana3621852022-10-14 18:57:23 +00001482 // An external stylus with buttons should also be recognized as a keyboard.
1483 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001484 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1485 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1486
1487 const auto DOWN =
1488 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1489 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1490
1491 stylus->pressAndReleaseKey(BTN_STYLUS);
1492 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1493 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1495 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1496
1497 stylus->pressAndReleaseKey(BTN_STYLUS2);
1498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1502
1503 stylus->pressAndReleaseKey(BTN_STYLUS3);
1504 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1505 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1507 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1508}
1509
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001510TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1511 std::unique_ptr<UinputKeyboard> keyboard =
1512 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1513 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1514 KEY_R, KEY_T, KEY_Y,
1515 BTN_STYLUS, BTN_STYLUS2,
1516 BTN_STYLUS3});
1517 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1518
1519 const auto device = findDeviceByName(keyboard->getName());
1520 ASSERT_TRUE(device.has_value());
1521
1522 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1523 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1524 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1525 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1526}
1527
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001528TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1529 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1530 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1531 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1532 createUinputDevice<UinputKeyboardWithHidUsage>(
1533 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1534 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1535
1536 const auto device = findDeviceByName(keyboard->getName());
1537 ASSERT_TRUE(device.has_value());
1538
1539 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1540 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1541
1542 // If a device supports reporting HID usage codes, it shouldn't automatically support
1543 // stylus keys.
1544 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1545 uint8_t outFlags[] = {0};
1546 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1547 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1548}
1549
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001550/**
1551 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1552 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1553 * are passed to the listener.
1554 */
1555static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1556TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1557 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1558 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1559 NotifyKeyArgs keyArgs;
1560
1561 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1564 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1565
1566 controller->pressAndReleaseKey(BTN_GEAR_UP);
1567 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1569 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1570}
1571
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001572// --- TouchIntegrationTest ---
1573
Arpit Singh440bf652023-08-09 09:23:43 +00001574class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001575protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001576 const std::string UNIQUE_ID = "local:0";
1577
Chris Yea52ade12020-08-27 16:49:20 -07001578 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001579#if !defined(__ANDROID__)
1580 GTEST_SKIP();
1581#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001582 InputReaderIntegrationTest::SetUp();
1583 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001584 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1585 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001586
1587 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1588 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1589 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001590 const auto info = findDeviceByName(mDevice->getName());
1591 ASSERT_TRUE(info);
1592 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001593 }
1594
1595 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001596 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001597 std::optional<uint8_t> physicalPort,
1598 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001599 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001600 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001601 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001602 }
1603
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001604 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1605 NotifyMotionArgs args;
1606 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1607 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001608 ASSERT_EQ(points.size(), args.getPointerCount());
1609 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001610 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1611 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1612 }
1613 }
1614
Arthur Hungaab25622020-01-16 11:22:11 +08001615 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001616 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001617};
1618
Arpit Singh440bf652023-08-09 09:23:43 +00001619enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1620
1621class TouchIntegrationTest : public BaseTouchIntegrationTest,
1622 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1623protected:
1624 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1625 const std::string INPUT_PORT = "uinput_touch/input0";
1626
1627 void SetUp() override {
1628#if !defined(__ANDROID__)
1629 GTEST_SKIP();
1630#endif
1631 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1632 BaseTouchIntegrationTest::SetUp();
1633 return;
1634 }
1635
1636 // setup policy with a input-port or UniqueId association to the display
1637 bool isInputPortAssociation =
1638 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1639
1640 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1641 if (isInputPortAssociation) {
1642 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1643 } else {
1644 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1645 }
1646 mFakePointerController = std::make_shared<FakePointerController>();
1647 mFakePolicy->setPointerController(mFakePointerController);
1648
1649 InputReaderIntegrationTest::setupInputReader();
1650
1651 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1652 INPUT_PORT);
1653 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1654
1655 // Add a display linked to a physical port or UniqueId.
1656 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1657 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1658 ViewportType::INTERNAL);
1659 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1660 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1661 const auto info = findDeviceByName(mDevice->getName());
1662 ASSERT_TRUE(info);
1663 mDeviceInfo = *info;
1664 }
1665};
1666
1667TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001668 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1669 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1670 // presses).
1671 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1672 mDeviceInfo.getSources());
1673}
1674
Arpit Singh440bf652023-08-09 09:23:43 +00001675TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001676 NotifyMotionArgs args;
1677 const Point centerPoint = mDevice->getCenterPoint();
1678
1679 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001680 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001681 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001682 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001683 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1684 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1685
1686 // ACTION_MOVE
1687 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001688 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001689 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1691
1692 // ACTION_UP
1693 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001694 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001695 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1696 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1697}
1698
Arpit Singh440bf652023-08-09 09:23:43 +00001699TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001700 NotifyMotionArgs args;
1701 const Point centerPoint = mDevice->getCenterPoint();
1702
1703 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001704 mDevice->sendSlot(FIRST_SLOT);
1705 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001706 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001707 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001708 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1709 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1710
1711 // ACTION_POINTER_DOWN (Second slot)
1712 const Point secondPoint = centerPoint + Point(100, 100);
1713 mDevice->sendSlot(SECOND_SLOT);
1714 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001715 mDevice->sendDown(secondPoint);
1716 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001717 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001718 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001719
1720 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001721 mDevice->sendMove(secondPoint + Point(1, 1));
1722 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001723 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1725
1726 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001727 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001728 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001729 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001730 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001731
1732 // ACTION_UP
1733 mDevice->sendSlot(FIRST_SLOT);
1734 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001735 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001736 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1737 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1738}
1739
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001740/**
1741 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1742 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1743 * data?
1744 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1745 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1746 * for Pointer 0 only is generated after.
1747 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1748 * events, we will not miss any information.
1749 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1750 * event generated afterwards that contains the newest movement of pointer 0.
1751 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1752 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1753 * losing information about non-palm pointers.
1754 */
Arpit Singh440bf652023-08-09 09:23:43 +00001755TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001756 NotifyMotionArgs args;
1757 const Point centerPoint = mDevice->getCenterPoint();
1758
1759 // ACTION_DOWN
1760 mDevice->sendSlot(FIRST_SLOT);
1761 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1762 mDevice->sendDown(centerPoint);
1763 mDevice->sendSync();
1764 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1765
1766 // ACTION_POINTER_DOWN (Second slot)
1767 const Point secondPoint = centerPoint + Point(100, 100);
1768 mDevice->sendSlot(SECOND_SLOT);
1769 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1770 mDevice->sendDown(secondPoint);
1771 mDevice->sendSync();
1772 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1773
1774 // ACTION_MOVE (First slot)
1775 mDevice->sendSlot(FIRST_SLOT);
1776 mDevice->sendMove(centerPoint + Point(5, 5));
1777 // ACTION_POINTER_UP (Second slot)
1778 mDevice->sendSlot(SECOND_SLOT);
1779 mDevice->sendPointerUp();
1780 // Send a single sync for the above 2 pointer updates
1781 mDevice->sendSync();
1782
1783 // First, we should get POINTER_UP for the second pointer
1784 assertReceivedMotion(ACTION_POINTER_1_UP,
1785 {/*first pointer */ centerPoint + Point(5, 5),
1786 /*second pointer*/ secondPoint});
1787
1788 // Next, the MOVE event for the first pointer
1789 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1790}
1791
1792/**
1793 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1794 * move, and then it will go up, all in the same frame.
1795 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1796 * gets sent to the listener.
1797 */
Arpit Singh440bf652023-08-09 09:23:43 +00001798TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001799 NotifyMotionArgs args;
1800 const Point centerPoint = mDevice->getCenterPoint();
1801
1802 // ACTION_DOWN
1803 mDevice->sendSlot(FIRST_SLOT);
1804 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1805 mDevice->sendDown(centerPoint);
1806 mDevice->sendSync();
1807 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1808
1809 // ACTION_POINTER_DOWN (Second slot)
1810 const Point secondPoint = centerPoint + Point(100, 100);
1811 mDevice->sendSlot(SECOND_SLOT);
1812 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1813 mDevice->sendDown(secondPoint);
1814 mDevice->sendSync();
1815 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1816
1817 // ACTION_MOVE (First slot)
1818 mDevice->sendSlot(FIRST_SLOT);
1819 mDevice->sendMove(centerPoint + Point(5, 5));
1820 // ACTION_POINTER_UP (Second slot)
1821 mDevice->sendSlot(SECOND_SLOT);
1822 mDevice->sendMove(secondPoint + Point(6, 6));
1823 mDevice->sendPointerUp();
1824 // Send a single sync for the above 2 pointer updates
1825 mDevice->sendSync();
1826
1827 // First, we should get POINTER_UP for the second pointer
1828 // The movement of the second pointer during the liftoff frame is ignored.
1829 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1830 assertReceivedMotion(ACTION_POINTER_1_UP,
1831 {/*first pointer */ centerPoint + Point(5, 5),
1832 /*second pointer*/ secondPoint});
1833
1834 // Next, the MOVE event for the first pointer
1835 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1836}
1837
Arpit Singh440bf652023-08-09 09:23:43 +00001838TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001839 NotifyMotionArgs args;
1840 const Point centerPoint = mDevice->getCenterPoint();
1841
1842 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001843 mDevice->sendSlot(FIRST_SLOT);
1844 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001845 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001846 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001847 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1848 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1849
arthurhungcc7f9802020-04-30 17:55:40 +08001850 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001851 const Point secondPoint = centerPoint + Point(100, 100);
1852 mDevice->sendSlot(SECOND_SLOT);
1853 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1854 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001855 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001856 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001857 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001858
arthurhungcc7f9802020-04-30 17:55:40 +08001859 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001860 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001861 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001862 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1863 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1864
arthurhungcc7f9802020-04-30 17:55:40 +08001865 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1866 // a palm event.
1867 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001868 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001869 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001870 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001871 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001872 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001873
arthurhungcc7f9802020-04-30 17:55:40 +08001874 // Send up to second slot, expect first slot send moving.
1875 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001876 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001877 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001879
arthurhungcc7f9802020-04-30 17:55:40 +08001880 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001881 mDevice->sendSlot(FIRST_SLOT);
1882 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001883 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001884
arthurhungcc7f9802020-04-30 17:55:40 +08001885 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1886 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001887}
1888
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001889/**
1890 * Some drivers historically have reported axis values outside of the range specified in the
1891 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1892 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1893 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1894 * and no units for pressure (resolution) is specified by the evdev documentation.
1895 */
1896TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1897 const Point centerPoint = mDevice->getCenterPoint();
1898
1899 // Down with pressure outside the reported range
1900 mDevice->sendSlot(FIRST_SLOT);
1901 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1902 mDevice->sendDown(centerPoint);
1903 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1904 mDevice->sendSync();
1905 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1906 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1907
1908 // Move to a point outside the reported range
1909 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1910 mDevice->sendSync();
1911 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1912 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1913
1914 // Up
1915 mDevice->sendUp();
1916 mDevice->sendSync();
1917 ASSERT_NO_FATAL_FAILURE(
1918 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1919}
1920
Arpit Singh440bf652023-08-09 09:23:43 +00001921TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001922 const Point centerPoint = mDevice->getCenterPoint();
1923
1924 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1925 mDevice->sendSlot(FIRST_SLOT);
1926 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1927 mDevice->sendToolType(MT_TOOL_PEN);
1928 mDevice->sendDown(centerPoint);
1929 mDevice->sendSync();
1930 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1931 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001932 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001933
1934 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1935
1936 // Release the stylus touch.
1937 mDevice->sendUp();
1938 mDevice->sendSync();
1939 ASSERT_NO_FATAL_FAILURE(
1940 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1941
1942 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1943
1944 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1945 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1946 mDevice->sendToolType(MT_TOOL_FINGER);
1947 mDevice->sendDown(centerPoint);
1948 mDevice->sendSync();
1949 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001951 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001952
1953 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1954
1955 mDevice->sendUp();
1956 mDevice->sendSync();
1957 ASSERT_NO_FATAL_FAILURE(
1958 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1959
1960 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1961 // The policy should be notified of the stylus presence.
1962 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1963 mDevice->sendToolType(MT_TOOL_PEN);
1964 mDevice->sendMove(centerPoint);
1965 mDevice->sendSync();
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1967 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001968 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001969
1970 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1971}
1972
Arpit Singh440bf652023-08-09 09:23:43 +00001973TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001974 const Point centerPoint = mDevice->getCenterPoint();
1975
1976 // Down
1977 mDevice->sendSlot(FIRST_SLOT);
1978 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1979 mDevice->sendDown(centerPoint);
1980 mDevice->sendSync();
1981 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1982 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1983
1984 // Move
1985 mDevice->sendMove(centerPoint + Point(1, 1));
1986 mDevice->sendSync();
1987 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1988 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1989
1990 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1991 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1992 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1993 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1994 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1995 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001996
1997 // Move
1998 mDevice->sendMove(centerPoint + Point(2, 2));
1999 mDevice->sendSync();
2000 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2001 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2002
2003 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2004 externalStylus.reset();
2005 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2006 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2007 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2008
2009 // Up
2010 mDevice->sendUp();
2011 mDevice->sendSync();
2012 ASSERT_NO_FATAL_FAILURE(
2013 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2014
2015 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2016}
2017
Arpit Singh440bf652023-08-09 09:23:43 +00002018INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2019 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2020 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2021 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2022
Prabir Pradhan124ea442022-10-28 20:27:44 +00002023// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002024
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2026// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2027// stylus.
2028template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002029class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002030protected:
2031 void SetUp() override {
2032#if !defined(__ANDROID__)
2033 GTEST_SKIP();
2034#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002035 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002036 mTouchscreen = mDevice.get();
2037 mTouchscreenInfo = mDeviceInfo;
2038
2039 setUpStylusDevice();
2040 }
2041
2042 UinputStylusDevice* mStylus{nullptr};
2043 InputDeviceInfo mStylusInfo{};
2044
2045 UinputTouchScreen* mTouchscreen{nullptr};
2046 InputDeviceInfo mTouchscreenInfo{};
2047
2048private:
2049 // When we are attempting to test stylus button events that are sent from the touchscreen,
2050 // use the same Uinput device for the touchscreen and the stylus.
2051 template <typename T = UinputStylusDevice>
2052 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2053 mStylus = mDevice.get();
2054 mStylusInfo = mDeviceInfo;
2055 }
2056
2057 // When we are attempting to stylus buttons from an external stylus being merged with touches
2058 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2059 template <typename T = UinputStylusDevice>
2060 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2061 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2062 mStylus = mStylusDeviceLifecycleTracker.get();
2063 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2064 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2065 const auto info = findDeviceByName(mStylus->getName());
2066 ASSERT_TRUE(info);
2067 mStylusInfo = *info;
2068 }
2069
2070 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2071
2072 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002073 using BaseTouchIntegrationTest::mDevice;
2074 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002075};
2076
2077using StylusButtonIntegrationTestTypes =
2078 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2079TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2080
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002081TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002082 const auto stylusId = TestFixture::mStylusInfo.getId();
2083
2084 TestFixture::mStylus->pressKey(BTN_STYLUS);
2085 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2086 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2087 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2088
2089 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2090 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002091 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002092 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002093}
2094
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002095TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2097 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2098 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002099
2100 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002101 TestFixture::mStylus->pressKey(BTN_STYLUS);
2102 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002103 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002104 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105
2106 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002107 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2108 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2109 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2110 TestFixture::mTouchscreen->sendDown(centerPoint);
2111 TestFixture::mTouchscreen->sendSync();
2112 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002113 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002114 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002115 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2116 WithDeviceId(touchscreenId))));
2117 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002118 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002119 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002120 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2121 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122
Prabir Pradhan124ea442022-10-28 20:27:44 +00002123 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2124 TestFixture::mTouchscreen->sendSync();
2125 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002127 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002128 WithDeviceId(touchscreenId))));
2129 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002131 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002132 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002133
2134 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002135 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2136 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002138 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002139}
2140
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002141TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002142 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2143 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2144 const auto stylusId = TestFixture::mStylusInfo.getId();
2145 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002146 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002147
2148 // Press the stylus button.
2149 TestFixture::mStylus->pressKey(BTN_STYLUS);
2150 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2151 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2152 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2153
2154 // Start hovering with the stylus.
2155 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2156 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2157 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2158 TestFixture::mTouchscreen->sendMove(centerPoint);
2159 TestFixture::mTouchscreen->sendSync();
2160 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2161 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2162 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2163 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2164 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2165 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2166 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2167 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2168 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2169
2170 // Touch down with the stylus.
2171 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2172 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2173 TestFixture::mTouchscreen->sendDown(centerPoint);
2174 TestFixture::mTouchscreen->sendSync();
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2176 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2177 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2178
2179 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2180 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2181 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2182
2183 // Stop touching with the stylus, and start hovering.
2184 TestFixture::mTouchscreen->sendUp();
2185 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2186 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2187 TestFixture::mTouchscreen->sendMove(centerPoint);
2188 TestFixture::mTouchscreen->sendSync();
2189 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2190 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2191 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2192 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2193 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2194 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2195 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2196 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2197 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2198
2199 // Stop hovering.
2200 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2201 TestFixture::mTouchscreen->sendSync();
2202 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2203 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2204 WithButtonState(0))));
2205 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2208 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2209
2210 // Release the stylus button.
2211 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2212 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2213 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2214 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2215}
2216
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002217TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002218 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2219 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2220 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002221
2222 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002223 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2224 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2225 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2226 TestFixture::mTouchscreen->sendDown(centerPoint);
2227 TestFixture::mTouchscreen->sendSync();
2228 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002229 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002230 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002231 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002232
2233 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002234 TestFixture::mStylus->pressKey(BTN_STYLUS);
2235 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002236 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002237 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2238 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002239 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002240 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002241 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2242 WithDeviceId(touchscreenId))));
2243 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002244 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002245 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002246 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2247 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002248
Prabir Pradhan124ea442022-10-28 20:27:44 +00002249 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2250 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002251 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002252 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2253 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002254 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002255 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002256 WithDeviceId(touchscreenId))));
2257 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002259 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002261
2262 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002263 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2264 TestFixture::mTouchscreen->sendSync();
2265 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002266 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002267 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002268 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002269}
2270
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002271TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002272 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2273 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002274 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002275
2276 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2277 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2278 const auto stylusId = TestFixture::mStylusInfo.getId();
2279
2280 // Start a stylus gesture. By the time this event is processed, the configuration change that
2281 // was requested is guaranteed to be completed.
2282 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2283 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2284 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2285 TestFixture::mTouchscreen->sendDown(centerPoint);
2286 TestFixture::mTouchscreen->sendSync();
2287 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2288 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002289 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002290 WithDeviceId(touchscreenId))));
2291
2292 // Press and release a stylus button. Each change only generates a MOVE motion event.
2293 // Key events are unaffected.
2294 TestFixture::mStylus->pressKey(BTN_STYLUS);
2295 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2296 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2297 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2298 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2299 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002300 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002301 WithDeviceId(touchscreenId))));
2302
2303 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2304 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2305 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2306 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2307 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002309 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002310 WithDeviceId(touchscreenId))));
2311
2312 // Finish the stylus gesture.
2313 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2314 TestFixture::mTouchscreen->sendSync();
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2316 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002317 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002318 WithDeviceId(touchscreenId))));
2319}
2320
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002321// --- ExternalStylusIntegrationTest ---
2322
2323// Verify the behavior of an external stylus. An external stylus can report pressure or button
2324// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2325// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002326using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002327
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002328TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2329 // Create an external stylus capable of reporting pressure data that
2330 // should be fused with a touch pointer.
2331 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2332 createUinputDevice<UinputExternalStylusWithPressure>();
2333 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2334 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2335 const auto stylusInfo = findDeviceByName(stylus->getName());
2336 ASSERT_TRUE(stylusInfo);
2337
2338 // Connecting an external stylus changes the source of the touchscreen.
2339 const auto deviceInfo = findDeviceByName(mDevice->getName());
2340 ASSERT_TRUE(deviceInfo);
2341 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2342}
2343
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002344TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002345 const Point centerPoint = mDevice->getCenterPoint();
2346
2347 // Create an external stylus capable of reporting pressure data that
2348 // should be fused with a touch pointer.
2349 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2350 createUinputDevice<UinputExternalStylusWithPressure>();
2351 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2352 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2353 const auto stylusInfo = findDeviceByName(stylus->getName());
2354 ASSERT_TRUE(stylusInfo);
2355
2356 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2357
2358 const auto touchscreenId = mDeviceInfo.getId();
2359
2360 // Set a pressure value on the stylus. It doesn't generate any events.
2361 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2362 stylus->setPressure(100);
2363 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2364
2365 // Start a finger gesture, and ensure it shows up as stylus gesture
2366 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002367 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002368 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002369 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002370 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002371 mDevice->sendSync();
2372 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002373 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2374 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2375 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002376
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002377 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2378 // event with the updated pressure.
2379 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002380 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002381 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2382 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2383 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002384
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002385 // The external stylus did not generate any events.
2386 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2387 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2388}
2389
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002390TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002391 const Point centerPoint = mDevice->getCenterPoint();
2392
2393 // Create an external stylus capable of reporting pressure data that
2394 // should be fused with a touch pointer.
2395 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2396 createUinputDevice<UinputExternalStylusWithPressure>();
2397 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2398 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2399 const auto stylusInfo = findDeviceByName(stylus->getName());
2400 ASSERT_TRUE(stylusInfo);
2401
2402 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2403
2404 const auto touchscreenId = mDeviceInfo.getId();
2405
2406 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2407 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002408 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2409 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002410 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002411 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002412
2413 // Start a finger gesture. The touch device will withhold generating any touches for
2414 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2415 mDevice->sendSlot(FIRST_SLOT);
2416 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2417 mDevice->sendToolType(MT_TOOL_FINGER);
2418 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002419 const auto syncTime = std::chrono::system_clock::now();
2420 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2421 // is generated in that period, there will be a race condition between the event being generated
2422 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2423 // will reduce the liklihood of the race condition occurring.
2424 const auto waitUntilTimeForNoEvent =
2425 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002426 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002427 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002428
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002429 // Since the external stylus did not report a pressure value within the timeout,
2430 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002431 const auto waitUntilTimeForEvent = syncTime +
2432 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2433 ASSERT_NO_FATAL_FAILURE(
2434 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2435 AMOTION_EVENT_ACTION_DOWN),
2436 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2437 AINPUT_SOURCE_STYLUS),
2438 WithToolType(ToolType::FINGER),
2439 WithDeviceId(touchscreenId),
2440 WithPressure(1.f)),
2441 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002442
2443 // Change the pressure on the external stylus. Since the pressure was not present at the start
2444 // of the gesture, it is ignored for now.
2445 stylus->setPressure(200);
2446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2447
2448 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002449 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2450 mDevice->sendSync();
2451 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2452 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002453 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002454 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002455
2456 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2457 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2458 mDevice->sendToolType(MT_TOOL_FINGER);
2459 mDevice->sendDown(centerPoint);
2460 mDevice->sendSync();
2461 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2463 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2464 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002465
2466 // The external stylus did not generate any events.
2467 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2468 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002469}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002470
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002471TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002472 const Point centerPoint = mDevice->getCenterPoint();
2473
2474 // Create an external stylus device that does not support pressure. It should not affect any
2475 // touch pointers.
2476 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2477 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2478 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2479 const auto stylusInfo = findDeviceByName(stylus->getName());
2480 ASSERT_TRUE(stylusInfo);
2481
2482 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2483
2484 const auto touchscreenId = mDeviceInfo.getId();
2485
2486 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2487 // pressure data from the external stylus.
2488 mDevice->sendSlot(FIRST_SLOT);
2489 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2490 mDevice->sendToolType(MT_TOOL_FINGER);
2491 mDevice->sendDown(centerPoint);
2492 auto waitUntil = std::chrono::system_clock::now() +
2493 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2494 mDevice->sendSync();
2495 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002496 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2497 AMOTION_EVENT_ACTION_DOWN),
2498 WithToolType(ToolType::FINGER),
2499 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2500 AINPUT_SOURCE_STYLUS),
2501 WithButtonState(0),
2502 WithDeviceId(touchscreenId),
2503 WithPressure(1.f)),
2504 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002505
2506 // The external stylus did not generate any events.
2507 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2508 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2509}
2510
Michael Wrightd02c5b62014-02-10 15:10:22 -08002511// --- InputDeviceTest ---
2512class InputDeviceTest : public testing::Test {
2513protected:
2514 static const char* DEVICE_NAME;
2515 static const char* DEVICE_LOCATION;
2516 static const int32_t DEVICE_ID;
2517 static const int32_t DEVICE_GENERATION;
2518 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002519 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002520 static const int32_t EVENTHUB_ID;
2521 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2522
2523 std::shared_ptr<FakeEventHub> mFakeEventHub;
2524 sp<FakeInputReaderPolicy> mFakePolicy;
2525 std::unique_ptr<TestInputListener> mFakeListener;
2526 std::unique_ptr<InstrumentedInputReader> mReader;
2527 std::shared_ptr<InputDevice> mDevice;
2528
2529 void SetUp() override {
2530 mFakeEventHub = std::make_unique<FakeEventHub>();
2531 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2532 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002533 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002534 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002535 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536 identifier.name = DEVICE_NAME;
2537 identifier.location = DEVICE_LOCATION;
2538 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2539 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2540 identifier);
2541 mReader->pushNextDevice(mDevice);
2542 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002543 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002544 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002545
2546 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002547 mFakeListener.reset();
2548 mFakePolicy.clear();
2549 }
2550};
2551
2552const char* InputDeviceTest::DEVICE_NAME = "device";
2553const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2554const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2555const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002556const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002557const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2558 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002559const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2561
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002562TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002563 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002564 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2565 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002566}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002567
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2569 ASSERT_EQ(mDevice->isEnabled(), false);
2570}
2571
2572TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2573 // Configuration.
2574 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002575 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002576
2577 // Reset.
2578 unused += mDevice->reset(ARBITRARY_TIME);
2579
2580 NotifyDeviceResetArgs resetArgs;
2581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2582 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2583 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2584
2585 // Metadata.
2586 ASSERT_TRUE(mDevice->isIgnored());
2587 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2588
2589 InputDeviceInfo info = mDevice->getDeviceInfo();
2590 ASSERT_EQ(DEVICE_ID, info.getId());
2591 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2592 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2593 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2594
2595 // State queries.
2596 ASSERT_EQ(0, mDevice->getMetaState());
2597
2598 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2599 << "Ignored device should return unknown key code state.";
2600 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2601 << "Ignored device should return unknown scan code state.";
2602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2603 << "Ignored device should return unknown switch state.";
2604
2605 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2606 uint8_t flags[2] = { 0, 1 };
2607 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2608 << "Ignored device should never mark any key codes.";
2609 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2610 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2611}
2612
2613TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2614 // Configuration.
2615 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2616
2617 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002618 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2619 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2621 mapper1.setMetaState(AMETA_ALT_ON);
2622 mapper1.addSupportedKeyCode(AKEYCODE_A);
2623 mapper1.addSupportedKeyCode(AKEYCODE_B);
2624 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2625 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2626 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2627 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2628 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2629
2630 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002631 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2632 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002633 mapper2.setMetaState(AMETA_SHIFT_ON);
2634
2635 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002636 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637
Harry Cuttsf13161a2023-03-08 14:15:49 +00002638 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2639 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002640 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002641 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002643 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2644 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002645
2646 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002647 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002648 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2649 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002650
2651 NotifyDeviceResetArgs resetArgs;
2652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2653 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2654 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2655
2656 // Metadata.
2657 ASSERT_FALSE(mDevice->isIgnored());
2658 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2659
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002660 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002661 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002662 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2664 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2665
2666 // State queries.
2667 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2668 << "Should query mappers and combine meta states.";
2669
2670 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2671 << "Should return unknown key code state when source not supported.";
2672 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2673 << "Should return unknown scan code state when source not supported.";
2674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2675 << "Should return unknown switch state when source not supported.";
2676
2677 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2678 << "Should query mapper when source is supported.";
2679 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2680 << "Should query mapper when source is supported.";
2681 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2682 << "Should query mapper when source is supported.";
2683
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002684 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002685 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002686 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 << "Should do nothing when source is unsupported.";
2688 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2689 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2690 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2691 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2692
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002693 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 << "Should query mapper when source is supported.";
2695 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2696 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2697 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2698 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2699
2700 // Event handling.
2701 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002702 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002703 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002705 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2706 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002707}
2708
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002709TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2710 // Set some behavior to force the configuration to be update.
2711 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2712 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2713 AINPUT_SOURCE_KEYBOARD);
2714
2715 std::list<NotifyArgs> unused =
2716 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2717 /*changes=*/{});
2718
2719 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2720}
2721
2722TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2723 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2724 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2725 AINPUT_SOURCE_KEYBOARD);
2726
2727 std::list<NotifyArgs> unused =
2728 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2729 /*changes=*/{});
2730
2731 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2732}
2733
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002734TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2735 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2736 FakeInputMapper& mapper =
2737 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2738 AINPUT_SOURCE_KEYBOARD);
2739 NotifyMotionArgs args1;
2740 NotifySwitchArgs args2;
2741 NotifyKeyArgs args3;
2742 mapper.setProcessResult({args1, args2, args3});
2743
2744 InputReaderConfiguration config;
2745 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2746
2747 RawEvent event;
2748 event.deviceId = EVENTHUB_ID;
2749 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2750
2751 for (auto& arg : notifyArgs) {
2752 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2753 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2754 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2755 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2756 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2757 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2758 }
2759 }
2760}
2761
2762TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2763 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2764 FakeInputMapper& mapper =
2765 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2766 AINPUT_SOURCE_KEYBOARD);
2767 NotifyMotionArgs args;
2768 mapper.setProcessResult({args});
2769
2770 InputReaderConfiguration config;
2771 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2772
2773 RawEvent event;
2774 event.deviceId = EVENTHUB_ID;
2775 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2776
2777 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2778 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2779}
2780
2781TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2782 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2783 FakeInputMapper& mapper =
2784 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2785 AINPUT_SOURCE_KEYBOARD);
2786 NotifyMotionArgs args;
2787 args.policyFlags = POLICY_FLAG_WAKE;
2788 mapper.setProcessResult({args});
2789
2790 InputReaderConfiguration config;
2791 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2792
2793 RawEvent event;
2794 event.deviceId = EVENTHUB_ID;
2795 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2796
2797 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2798 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2799}
2800
Arthur Hung2c9a3342019-07-23 14:18:59 +08002801// A single input device is associated with a specific display. Check that:
2802// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002803// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002804TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002805 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2806 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002807
2808 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002809 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002810 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2811 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002812
2813 // Device should be enabled by default.
2814 ASSERT_TRUE(mDevice->isEnabled());
2815
2816 // Prepare associated info.
2817 constexpr uint8_t hdmi = 1;
2818 const std::string UNIQUE_ID = "local:1";
2819
2820 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002821 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002822 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002823 // Device should be disabled because it is associated with a specific display via
2824 // input port <-> display port association, but the corresponding display is not found
2825 ASSERT_FALSE(mDevice->isEnabled());
2826
2827 // Prepare displays.
2828 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002829 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002830 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002831 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002832 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002833 ASSERT_TRUE(mDevice->isEnabled());
2834
2835 // Device should be disabled after set disable.
2836 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002837 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002838 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002839 ASSERT_FALSE(mDevice->isEnabled());
2840
2841 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002842 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002843 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002844 ASSERT_FALSE(mDevice->isEnabled());
2845}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002846
Christine Franks1ba71cc2021-04-07 14:37:42 -07002847TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2848 // Device should be enabled by default.
2849 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002850 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2851 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002852 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002853 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2854 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002855 ASSERT_TRUE(mDevice->isEnabled());
2856
2857 // Device should be disabled because it is associated with a specific display, but the
2858 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002859 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002860 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002861 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002862 ASSERT_FALSE(mDevice->isEnabled());
2863
2864 // Device should be enabled when a display is found.
2865 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002866 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002867 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002868 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002869 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002870 ASSERT_TRUE(mDevice->isEnabled());
2871
2872 // Device should be disabled after set disable.
2873 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002874 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002875 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002876 ASSERT_FALSE(mDevice->isEnabled());
2877
2878 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002879 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002880 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002881 ASSERT_FALSE(mDevice->isEnabled());
2882}
2883
Christine Franks2a2293c2022-01-18 11:51:16 -08002884TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2885 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002886 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2887 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002888 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002889 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2890 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002891
Christine Franks2a2293c2022-01-18 11:51:16 -08002892 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2893 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002894 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002895 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002896 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002897 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002898 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002899 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002900 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2901 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002902}
2903
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002904/**
2905 * This test reproduces a crash caused by a dangling reference that remains after device is added
2906 * and removed. The reference is accessed in InputDevice::dump(..);
2907 */
2908TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2909 constexpr int32_t TEST_EVENTHUB_ID = 10;
2910 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2911
Harry Cutts33476232023-01-30 19:57:29 +00002912 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002913 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2914 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002915 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2916 std::string dumpStr, eventHubDevStr;
2917 device.dump(dumpStr, eventHubDevStr);
2918}
2919
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002920TEST_F(InputDeviceTest, GetBluetoothAddress) {
2921 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2922 ASSERT_TRUE(address);
2923 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2924}
2925
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002926TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2927 mFakePolicy->clearViewports();
2928 FakeInputMapper& mapper =
2929 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2930 AINPUT_SOURCE_KEYBOARD);
2931 std::list<NotifyArgs> unused =
2932 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2933 /*changes=*/{});
2934
2935 mapper.assertConfigureWasCalled();
2936 mapper.assertResetWasNotCalled();
2937
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002938 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002939 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002940 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002941 .type = EV_SYN,
2942 .code = SYN_REPORT,
2943 .value = 0};
2944
2945 // Events are processed normally.
2946 unused = mDevice->process(&event, /*count=*/1);
2947 mapper.assertProcessWasCalled();
2948
2949 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002950 event.type = EV_SYN;
2951 event.code = SYN_DROPPED;
2952 event.value = 0;
2953 unused = mDevice->process(&event, /*count=*/1);
2954 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002955
2956 // All events until the next SYN_REPORT should be dropped.
2957 event.type = EV_KEY;
2958 event.code = KEY_A;
2959 event.value = 1;
2960 unused = mDevice->process(&event, /*count=*/1);
2961 mapper.assertProcessWasNotCalled();
2962
2963 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002964 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002965 event.type = EV_SYN;
2966 event.code = SYN_REPORT;
2967 event.value = 0;
2968 unused = mDevice->process(&event, /*count=*/1);
2969 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002970 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002971
2972 // The mapper receives events normally now.
2973 event.type = EV_KEY;
2974 event.code = KEY_B;
2975 event.value = 1;
2976 unused = mDevice->process(&event, /*count=*/1);
2977 mapper.assertProcessWasCalled();
2978}
2979
Michael Wrightd02c5b62014-02-10 15:10:22 -08002980// --- SwitchInputMapperTest ---
2981
2982class SwitchInputMapperTest : public InputMapperTest {
2983protected:
2984};
2985
2986TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002987 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002989 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002990}
2991
2992TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002993 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002995 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002996 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002997
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002998 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002999 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003000}
3001
3002TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003003 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003004 std::list<NotifyArgs> out;
3005 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3006 ASSERT_TRUE(out.empty());
3007 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3008 ASSERT_TRUE(out.empty());
3009 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3010 ASSERT_TRUE(out.empty());
3011 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003012
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003013 ASSERT_EQ(1u, out.size());
3014 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003015 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003016 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3017 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003018 args.switchMask);
3019 ASSERT_EQ(uint32_t(0), args.policyFlags);
3020}
3021
Chris Ye87143712020-11-10 05:05:58 +00003022// --- VibratorInputMapperTest ---
3023class VibratorInputMapperTest : public InputMapperTest {
3024protected:
3025 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3026};
3027
3028TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003029 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003030
3031 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3032}
3033
3034TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003035 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003036
3037 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3038}
3039
3040TEST_F(VibratorInputMapperTest, Vibrate) {
3041 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003042 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003043 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003044
3045 VibrationElement pattern(2);
3046 VibrationSequence sequence(2);
3047 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003048 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3049 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003050 sequence.addElement(pattern);
3051 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003052 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3053 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003054 sequence.addElement(pattern);
3055
3056 std::vector<int64_t> timings = {0, 1};
3057 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3058
3059 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003060 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003061 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003062 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003063 // Verify vibrator state listener was notified.
3064 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003065 ASSERT_EQ(1u, out.size());
3066 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3067 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3068 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003069 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003070 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003071 ASSERT_FALSE(mapper.isVibrating());
3072 // Verify vibrator state listener was notified.
3073 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003074 ASSERT_EQ(1u, out.size());
3075 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3076 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3077 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003078}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003079
Chris Yef59a2f42020-10-16 12:55:26 -07003080// --- SensorInputMapperTest ---
3081
3082class SensorInputMapperTest : public InputMapperTest {
3083protected:
3084 static const int32_t ACCEL_RAW_MIN;
3085 static const int32_t ACCEL_RAW_MAX;
3086 static const int32_t ACCEL_RAW_FUZZ;
3087 static const int32_t ACCEL_RAW_FLAT;
3088 static const int32_t ACCEL_RAW_RESOLUTION;
3089
3090 static const int32_t GYRO_RAW_MIN;
3091 static const int32_t GYRO_RAW_MAX;
3092 static const int32_t GYRO_RAW_FUZZ;
3093 static const int32_t GYRO_RAW_FLAT;
3094 static const int32_t GYRO_RAW_RESOLUTION;
3095
3096 static const float GRAVITY_MS2_UNIT;
3097 static const float DEGREE_RADIAN_UNIT;
3098
3099 void prepareAccelAxes();
3100 void prepareGyroAxes();
3101 void setAccelProperties();
3102 void setGyroProperties();
3103 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3104};
3105
3106const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3107const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3108const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3109const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3110const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3111
3112const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3113const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3114const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3115const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3116const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3117
3118const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3119const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3120
3121void SensorInputMapperTest::prepareAccelAxes() {
3122 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3123 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3124 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3125 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3127 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3128}
3129
3130void SensorInputMapperTest::prepareGyroAxes() {
3131 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3132 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3133 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3134 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3136 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3137}
3138
3139void SensorInputMapperTest::setAccelProperties() {
3140 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3141 /* sensorDataIndex */ 0);
3142 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3143 /* sensorDataIndex */ 1);
3144 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3145 /* sensorDataIndex */ 2);
3146 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3147 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3148 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3149 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3150 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3151}
3152
3153void SensorInputMapperTest::setGyroProperties() {
3154 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3155 /* sensorDataIndex */ 0);
3156 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3157 /* sensorDataIndex */ 1);
3158 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3159 /* sensorDataIndex */ 2);
3160 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3161 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3162 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3163 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3164 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3165}
3166
3167TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003168 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003169
3170 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3171}
3172
3173TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3174 setAccelProperties();
3175 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003176 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003177
3178 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3179 std::chrono::microseconds(10000),
3180 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003181 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3183 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3185 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003187
3188 NotifySensorArgs args;
3189 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3190 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3191 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3192
3193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3194 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3195 ASSERT_EQ(args.deviceId, DEVICE_ID);
3196 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3197 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3198 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3199 ASSERT_EQ(args.values, values);
3200 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3201}
3202
3203TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3204 setGyroProperties();
3205 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003206 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003207
3208 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3209 std::chrono::microseconds(10000),
3210 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003211 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3213 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3214 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3215 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003217
3218 NotifySensorArgs args;
3219 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3220 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3221 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3222
3223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3224 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3225 ASSERT_EQ(args.deviceId, DEVICE_ID);
3226 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3227 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3228 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3229 ASSERT_EQ(args.values, values);
3230 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3231}
3232
Michael Wrightd02c5b62014-02-10 15:10:22 -08003233// --- KeyboardInputMapperTest ---
3234
3235class KeyboardInputMapperTest : public InputMapperTest {
3236protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003237 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003238 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003239 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003240
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003241 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003242 int32_t originalKeyCode, int32_t rotatedKeyCode,
3243 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244};
3245
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003246/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3247 * orientation.
3248 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003249void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003250 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3251 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003252}
3253
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003254void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003255 int32_t originalScanCode, int32_t originalKeyCode,
3256 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003257 NotifyKeyArgs args;
3258
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003259 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3261 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3262 ASSERT_EQ(originalScanCode, args.scanCode);
3263 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003264 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3269 ASSERT_EQ(originalScanCode, args.scanCode);
3270 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003271 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272}
3273
Michael Wrightd02c5b62014-02-10 15:10:22 -08003274TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003275 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003276 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003277 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003280}
3281
3282TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3283 const int32_t USAGE_A = 0x070004;
3284 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003285 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3286 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003287 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3288 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3289 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003290
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003291 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003292 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003293 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003294 // Initial metastate is AMETA_NONE.
3295 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003296
3297 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 NotifyKeyArgs args;
3300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3301 ASSERT_EQ(DEVICE_ID, args.deviceId);
3302 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3303 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3304 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3305 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3306 ASSERT_EQ(KEY_HOME, args.scanCode);
3307 ASSERT_EQ(AMETA_NONE, args.metaState);
3308 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3309 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3310 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3311
3312 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003313 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3315 ASSERT_EQ(DEVICE_ID, args.deviceId);
3316 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3317 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3318 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3319 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3320 ASSERT_EQ(KEY_HOME, args.scanCode);
3321 ASSERT_EQ(AMETA_NONE, args.metaState);
3322 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3323 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3324 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3325
3326 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003327 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3328 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3330 ASSERT_EQ(DEVICE_ID, args.deviceId);
3331 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3332 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3333 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3334 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3335 ASSERT_EQ(0, args.scanCode);
3336 ASSERT_EQ(AMETA_NONE, args.metaState);
3337 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3338 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3339 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3340
3341 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3343 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3345 ASSERT_EQ(DEVICE_ID, args.deviceId);
3346 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3347 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3348 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3349 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3350 ASSERT_EQ(0, args.scanCode);
3351 ASSERT_EQ(AMETA_NONE, args.metaState);
3352 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3353 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3354 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3355
3356 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003357 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3358 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3360 ASSERT_EQ(DEVICE_ID, args.deviceId);
3361 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3362 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3363 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3364 ASSERT_EQ(0, args.keyCode);
3365 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3366 ASSERT_EQ(AMETA_NONE, args.metaState);
3367 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3368 ASSERT_EQ(0U, args.policyFlags);
3369 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3370
3371 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003372 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3373 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003374 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3375 ASSERT_EQ(DEVICE_ID, args.deviceId);
3376 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3377 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3378 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3379 ASSERT_EQ(0, args.keyCode);
3380 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3381 ASSERT_EQ(AMETA_NONE, args.metaState);
3382 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3383 ASSERT_EQ(0U, args.policyFlags);
3384 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3385}
3386
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003387TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3388 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3389 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3390 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3391
3392 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003393 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003394 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3395
3396 // Key down by scan code.
3397 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3398 NotifyKeyArgs args;
3399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3400 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3401
3402 // Key up by scan code.
3403 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3406}
3407
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003408/**
3409 * Ensure that the readTime is set to the time when the EV_KEY is received.
3410 */
3411TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3412 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3413
3414 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003415 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003416 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3417 NotifyKeyArgs args;
3418
3419 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003420 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(12, args.readTime);
3423
3424 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003425 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(15, args.readTime);
3428}
3429
Michael Wrightd02c5b62014-02-10 15:10:22 -08003430TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003431 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3432 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003433 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3434 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3435 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003436
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003437 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003438 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003439 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440
Arthur Hung95f68612022-04-07 14:08:22 +08003441 // Initial metastate is AMETA_NONE.
3442 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003443
3444 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003445 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003446 NotifyKeyArgs args;
3447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3448 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003449 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003450 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
3452 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003453 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3455 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003456 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457
3458 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003459 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003462 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003463
3464 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003465 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3467 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003468 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003469 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003470}
3471
3472TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3474 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3475 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3476 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003478 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003479 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003480 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003481
Michael Wrighta9cf4192022-12-01 23:46:39 +00003482 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003483 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3484 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3485 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3486 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3487 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3488 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3489 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3490 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3491}
3492
3493TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003494 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3495 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3496 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3497 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
Michael Wrightd02c5b62014-02-10 15:10:22 -08003499 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003501 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003502 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Michael Wrighta9cf4192022-12-01 23:46:39 +00003504 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003505 ASSERT_NO_FATAL_FAILURE(
3506 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3507 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3508 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3509 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3510 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3511 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3512 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003513
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003514 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003515 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003516 ASSERT_NO_FATAL_FAILURE(
3517 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3518 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3519 AKEYCODE_DPAD_UP, DISPLAY_ID));
3520 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3521 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3522 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3523 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003524
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003525 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003526 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003527 ASSERT_NO_FATAL_FAILURE(
3528 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3529 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3530 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3531 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3532 AKEYCODE_DPAD_UP, DISPLAY_ID));
3533 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3534 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003536 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003537 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003538 ASSERT_NO_FATAL_FAILURE(
3539 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3540 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3541 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3542 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3543 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3544 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3545 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003546
3547 // Special case: if orientation changes while key is down, we still emit the same keycode
3548 // in the key up as we did in the key down.
3549 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003550 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003551 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3554 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3555 ASSERT_EQ(KEY_UP, args.scanCode);
3556 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3557
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003558 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003559 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003560 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3562 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3563 ASSERT_EQ(KEY_UP, args.scanCode);
3564 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3565}
3566
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003567TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3568 // If the keyboard is not orientation aware,
3569 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003570 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003571
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003573 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003575 NotifyKeyArgs args;
3576
3577 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003580 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3582 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3583
Michael Wrighta9cf4192022-12-01 23:46:39 +00003584 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003588 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3589 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3590}
3591
3592TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3593 // If the keyboard is orientation aware,
3594 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003595 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003596
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003597 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003598 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003599 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003600 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003601 NotifyKeyArgs args;
3602
3603 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3604 // ^--- already checked by the previous test
3605
Michael Wrighta9cf4192022-12-01 23:46:39 +00003606 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003607 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3612 ASSERT_EQ(DISPLAY_ID, args.displayId);
3613
3614 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003615 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003616 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003617 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003618 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3622 ASSERT_EQ(newDisplayId, args.displayId);
3623}
3624
Michael Wrightd02c5b62014-02-10 15:10:22 -08003625TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003626 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003627 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003628 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003629
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003630 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003632
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003633 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003634 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003635}
3636
Philip Junker4af3b3d2021-12-14 10:36:55 +01003637TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3638 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003639 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003640 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3641
3642 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3643 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3644 << "If a mapping is available, the result is equal to the mapping";
3645
3646 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3647 << "If no mapping is available, the result is the key location";
3648}
3649
Michael Wrightd02c5b62014-02-10 15:10:22 -08003650TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003651 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003652 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003653 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003654
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003655 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003656 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003657
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003658 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003659 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003660}
3661
3662TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003663 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003664 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003665 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003666
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003667 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003668
Michael Wrightd02c5b62014-02-10 15:10:22 -08003669 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003670 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671 ASSERT_TRUE(flags[0]);
3672 ASSERT_FALSE(flags[1]);
3673}
3674
3675TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003676 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3677 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3678 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3679 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3680 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3681 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003682
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003683 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003684 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003685 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003686 // Initial metastate is AMETA_NONE.
3687 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003688
3689 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003690 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3691 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3692 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
3694 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003695 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3696 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003697 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3698 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3699 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003700 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003701
3702 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003703 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3704 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003705 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3706 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3707 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003708 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003709
3710 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003711 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3712 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003713 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3714 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3715 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003716 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003717
3718 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003719 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3720 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003721 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3722 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3723 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003724 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003725
3726 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003729 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3730 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3731 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003732 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003733
3734 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003735 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003737 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3738 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3739 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003740 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003741}
3742
Chris Yea52ade12020-08-27 16:49:20 -07003743TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3744 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3745 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3746 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3747 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3748
3749 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003750 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003751 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3752
Chris Yea52ade12020-08-27 16:49:20 -07003753 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003754 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003755 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3756 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3757 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3758 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3759
3760 NotifyKeyArgs args;
3761 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003762 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3764 ASSERT_EQ(AMETA_NONE, args.metaState);
3765 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3766 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3767 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3768
3769 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003770 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3772 ASSERT_EQ(AMETA_NONE, args.metaState);
3773 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3774 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3775 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3776}
3777
Arthur Hung2c9a3342019-07-23 14:18:59 +08003778TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3779 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003780 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3781 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3782 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3783 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003784
3785 // keyboard 2.
3786 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003787 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003788 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003789 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003790 std::shared_ptr<InputDevice> device2 =
3791 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003792 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3795 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3796 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3797 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003798
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003799 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003800 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003801 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003802
Arpit Singh67ca6842023-04-26 14:43:16 +00003803 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003805 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3806 mFakePolicy
3807 ->getReaderConfiguration(),
3808 AINPUT_SOURCE_KEYBOARD,
3809 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003810 std::list<NotifyArgs> unused =
3811 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003812 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003813 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003814
3815 // Prepared displays and associated info.
3816 constexpr uint8_t hdmi1 = 0;
3817 constexpr uint8_t hdmi2 = 1;
3818 const std::string SECONDARY_UNIQUE_ID = "local:1";
3819
3820 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3821 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3822
3823 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003824 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003825 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003826 ASSERT_FALSE(device2->isEnabled());
3827
3828 // Prepare second display.
3829 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003830 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003831 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003832 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003833 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003834 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003835 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003836 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003837
3838 // Device should be enabled after the associated display is found.
3839 ASSERT_TRUE(mDevice->isEnabled());
3840 ASSERT_TRUE(device2->isEnabled());
3841
3842 // Test pad key events
3843 ASSERT_NO_FATAL_FAILURE(
3844 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3845 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3846 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3847 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3848 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3849 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3850 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3851
3852 ASSERT_NO_FATAL_FAILURE(
3853 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3854 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3855 AKEYCODE_DPAD_RIGHT, newDisplayId));
3856 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3857 AKEYCODE_DPAD_DOWN, newDisplayId));
3858 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3859 AKEYCODE_DPAD_LEFT, newDisplayId));
3860}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003861
arthurhungc903df12020-08-11 15:08:42 +08003862TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3863 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3864 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3865 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3866 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3867 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3868 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3869
3870 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003871 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003872 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003873 // Initial metastate is AMETA_NONE.
3874 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003875
3876 // Initialization should have turned all of the lights off.
3877 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3878 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3879 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3880
3881 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003882 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3883 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003884 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3885 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3886
3887 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3889 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003890 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3891 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3892
3893 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3895 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003896 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3897 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3898
3899 mFakeEventHub->removeDevice(EVENTHUB_ID);
3900 mReader->loopOnce();
3901
3902 // keyboard 2 should default toggle keys.
3903 const std::string USB2 = "USB2";
3904 const std::string DEVICE_NAME2 = "KEYBOARD2";
3905 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3906 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3907 std::shared_ptr<InputDevice> device2 =
3908 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003909 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003910 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3911 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3912 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3913 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3914 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3915 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3916
Arpit Singh67ca6842023-04-26 14:43:16 +00003917 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003918 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003919 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3920 mFakePolicy
3921 ->getReaderConfiguration(),
3922 AINPUT_SOURCE_KEYBOARD,
3923 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003924 std::list<NotifyArgs> unused =
3925 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003926 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003927 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003928
3929 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3930 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3931 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003932 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3933 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003934}
3935
Arthur Hungcb40a002021-08-03 14:31:01 +00003936TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3937 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3938 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3939 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3940
3941 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003942 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003943 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3944 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003945 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003946 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003947 // Initial metastate is AMETA_NONE.
3948 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003949
3950 mReader->toggleCapsLockState(DEVICE_ID);
3951 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3952}
3953
Arthur Hungfb3cc112022-04-13 07:39:50 +00003954TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3955 // keyboard 1.
3956 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3957 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3958 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3959 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3960 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3961 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3962
3963 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003964 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003965 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3966
3967 // keyboard 2.
3968 const std::string USB2 = "USB2";
3969 const std::string DEVICE_NAME2 = "KEYBOARD2";
3970 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3971 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3972 std::shared_ptr<InputDevice> device2 =
3973 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3974 ftl::Flags<InputDeviceClass>(0));
3975 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3976 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3977 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3978 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3979 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3980 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3981
Arpit Singh67ca6842023-04-26 14:43:16 +00003982 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003983 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003984 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3985 mFakePolicy
3986 ->getReaderConfiguration(),
3987 AINPUT_SOURCE_KEYBOARD,
3988 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003989 std::list<NotifyArgs> unused =
3990 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003991 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003992 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003993
Arthur Hung95f68612022-04-07 14:08:22 +08003994 // Initial metastate is AMETA_NONE.
3995 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3996 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3997
3998 // Toggle num lock on and off.
3999 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4000 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004001 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4002 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4003 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4004
4005 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4006 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4007 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4008 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4009 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4010
4011 // Toggle caps lock on and off.
4012 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4013 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4014 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4015 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4016 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4017
4018 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4019 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4020 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4021 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4022 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4023
4024 // Toggle scroll lock on and off.
4025 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4026 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4027 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4028 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4029 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4030
4031 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4032 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4033 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4034 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4035 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4036}
4037
Arthur Hung2141d542022-08-23 07:45:21 +00004038TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4039 const int32_t USAGE_A = 0x070004;
4040 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4041 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4042
4043 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004044 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4046 // Key down by scan code.
4047 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4048 NotifyKeyArgs args;
4049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4050 ASSERT_EQ(DEVICE_ID, args.deviceId);
4051 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4052 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4053 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4054 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4055 ASSERT_EQ(KEY_HOME, args.scanCode);
4056 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4057
4058 // Disable device, it should synthesize cancellation events for down events.
4059 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004060 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004061
4062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4063 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4064 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4065 ASSERT_EQ(KEY_HOME, args.scanCode);
4066 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4067}
4068
Zixuan Qufecb6062022-11-12 04:44:31 +00004069TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004070 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4071 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004072 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004073 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4074 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004075
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004076 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004077 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4078
4079 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004080 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004081
4082 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4083 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4084 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4085 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4086 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004087 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4088
4089 // Call change layout association with the same values: Generation shouldn't change
4090 generation = mReader->getContext()->getGeneration();
4091 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4092 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4093 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4094 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004095}
4096
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004097TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4098 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4099 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4100
4101 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004102 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004103 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4104 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004105 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004106
4107 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4108 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4109}
4110
Justin Chung71ddb432023-03-27 04:29:07 +00004111TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4112 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4113 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004114 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004115 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4116 NotifyKeyArgs args;
4117
4118 // Key down
4119 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4122}
4123
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004124// --- KeyboardInputMapperTest_ExternalDevice ---
4125
4126class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4127protected:
Chris Yea52ade12020-08-27 16:49:20 -07004128 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129};
4130
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004131TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004132 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4133 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004134
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004135 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4136 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4137 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4138 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004140 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004141 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004142 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004143
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 NotifyKeyArgs args;
4146 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4147 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4151 ASSERT_EQ(uint32_t(0), args.policyFlags);
4152
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004155 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004156
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004157 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(uint32_t(0), args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4168}
4169
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004170TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4171 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4172 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4173
4174 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4175 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4176 POLICY_FLAG_WAKE);
4177
4178 KeyboardInputMapper& mapper =
4179 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4180 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4181
4182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4183 NotifyKeyArgs args;
4184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4185 ASSERT_EQ(uint32_t(0), args.policyFlags);
4186
4187 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4189 ASSERT_EQ(uint32_t(0), args.policyFlags);
4190
4191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4193 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4194
4195 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4197 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4198}
4199
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004200TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004201 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004202
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4204 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4205 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004206
Powei Fengd041c5d2019-05-03 17:11:33 -07004207 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004208 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004209 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004210 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004211
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004212 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004213 NotifyKeyArgs args;
4214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4215 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 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, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4223 ASSERT_EQ(uint32_t(0), args.policyFlags);
4224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
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, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4231 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004233 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4235 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4236}
4237
Michael Wrightd02c5b62014-02-10 15:10:22 -08004238// --- TouchInputMapperTest ---
4239
4240class TouchInputMapperTest : public InputMapperTest {
4241protected:
4242 static const int32_t RAW_X_MIN;
4243 static const int32_t RAW_X_MAX;
4244 static const int32_t RAW_Y_MIN;
4245 static const int32_t RAW_Y_MAX;
4246 static const int32_t RAW_TOUCH_MIN;
4247 static const int32_t RAW_TOUCH_MAX;
4248 static const int32_t RAW_TOOL_MIN;
4249 static const int32_t RAW_TOOL_MAX;
4250 static const int32_t RAW_PRESSURE_MIN;
4251 static const int32_t RAW_PRESSURE_MAX;
4252 static const int32_t RAW_ORIENTATION_MIN;
4253 static const int32_t RAW_ORIENTATION_MAX;
4254 static const int32_t RAW_DISTANCE_MIN;
4255 static const int32_t RAW_DISTANCE_MAX;
4256 static const int32_t RAW_TILT_MIN;
4257 static const int32_t RAW_TILT_MAX;
4258 static const int32_t RAW_ID_MIN;
4259 static const int32_t RAW_ID_MAX;
4260 static const int32_t RAW_SLOT_MIN;
4261 static const int32_t RAW_SLOT_MAX;
4262 static const float X_PRECISION;
4263 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004264 static const float X_PRECISION_VIRTUAL;
4265 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004268 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269
4270 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4271
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004272 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004273 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004274
Michael Wrightd02c5b62014-02-10 15:10:22 -08004275 enum Axes {
4276 POSITION = 1 << 0,
4277 TOUCH = 1 << 1,
4278 TOOL = 1 << 2,
4279 PRESSURE = 1 << 3,
4280 ORIENTATION = 1 << 4,
4281 MINOR = 1 << 5,
4282 ID = 1 << 6,
4283 DISTANCE = 1 << 7,
4284 TILT = 1 << 8,
4285 SLOT = 1 << 9,
4286 TOOL_TYPE = 1 << 10,
4287 };
4288
Michael Wrighta9cf4192022-12-01 23:46:39 +00004289 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004290 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004291 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004293 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294 int32_t toRawX(float displayX);
4295 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004296 int32_t toRotatedRawX(float displayX);
4297 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004298 float toCookedX(float rawX, float rawY);
4299 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004300 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004301 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004302 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004303 float toDisplayY(int32_t rawY, int32_t displayHeight);
4304
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305};
4306
4307const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4308const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4309const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4310const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4311const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4312const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4313const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4314const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004315const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4316const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4318const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4319const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4320const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4321const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4322const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4323const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4324const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4325const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4326const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4327const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4328const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004329const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4330 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4331const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4332 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004333const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4334 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335
4336const float TouchInputMapperTest::GEOMETRIC_SCALE =
4337 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4338 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4339
4340const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4341 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4342 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4343};
4344
Michael Wrighta9cf4192022-12-01 23:46:39 +00004345void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004346 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4347 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004348}
4349
4350void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4351 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004352 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004353}
4354
Michael Wrighta9cf4192022-12-01 23:46:39 +00004355void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004356 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4357 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4358 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004359}
4360
Michael Wrightd02c5b62014-02-10 15:10:22 -08004361void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004362 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4363 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4364 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4365 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366}
4367
Jason Gerecke489fda82012-09-07 17:19:40 -07004368void TouchInputMapperTest::prepareLocationCalibration() {
4369 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4370}
4371
Michael Wrightd02c5b62014-02-10 15:10:22 -08004372int32_t TouchInputMapperTest::toRawX(float displayX) {
4373 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4374}
4375
4376int32_t TouchInputMapperTest::toRawY(float displayY) {
4377 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4378}
4379
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004380int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4381 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4382}
4383
4384int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4385 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4386}
4387
Jason Gerecke489fda82012-09-07 17:19:40 -07004388float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4389 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4390 return rawX;
4391}
4392
4393float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4394 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4395 return rawY;
4396}
4397
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004399 return toDisplayX(rawX, DISPLAY_WIDTH);
4400}
4401
4402float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4403 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004404}
4405
4406float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004407 return toDisplayY(rawY, DISPLAY_HEIGHT);
4408}
4409
4410float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4411 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004412}
4413
4414
4415// --- SingleTouchInputMapperTest ---
4416
4417class SingleTouchInputMapperTest : public TouchInputMapperTest {
4418protected:
4419 void prepareButtons();
4420 void prepareAxes(int axes);
4421
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004422 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4423 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4424 void processUp(SingleTouchInputMapper& mappery);
4425 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4426 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4427 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4428 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4429 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4430 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431};
4432
4433void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004434 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435}
4436
4437void SingleTouchInputMapperTest::prepareAxes(int axes) {
4438 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004439 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4440 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 }
4442 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004443 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4444 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004445 }
4446 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004447 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4448 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004449 }
4450 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004451 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4452 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453 }
4454 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4456 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004457 }
4458}
4459
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004460void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004461 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4462 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4463 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004464}
4465
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004466void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004467 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4468 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004469}
4470
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004471void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473}
4474
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004475void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477}
4478
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004479void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4480 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482}
4483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004484void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486}
4487
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004488void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4489 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4491 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004492}
4493
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004494void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4495 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497}
4498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004500 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004501}
4502
Michael Wrightd02c5b62014-02-10 15:10:22 -08004503TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 prepareButtons();
4505 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004506 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004507
Josep del Río2d8c79a2023-01-23 19:33:50 +00004508 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509}
4510
Michael Wrightd02c5b62014-02-10 15:10:22 -08004511TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512 prepareButtons();
4513 prepareAxes(POSITION);
4514 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004515 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004517 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004518}
4519
4520TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004522 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 prepareButtons();
4524 prepareAxes(POSITION);
4525 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004526 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004527
4528 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004529 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004530
4531 // Virtual key is down.
4532 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4533 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4534 processDown(mapper, x, y);
4535 processSync(mapper);
4536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4537
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004538 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004539
4540 // Virtual key is up.
4541 processUp(mapper);
4542 processSync(mapper);
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4544
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004545 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004546}
4547
4548TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004549 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004550 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551 prepareButtons();
4552 prepareAxes(POSITION);
4553 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004554 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004555
4556 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004557 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004558
4559 // Virtual key is down.
4560 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4561 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4562 processDown(mapper, x, y);
4563 processSync(mapper);
4564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4565
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004566 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567
4568 // Virtual key is up.
4569 processUp(mapper);
4570 processSync(mapper);
4571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004573 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004574}
4575
4576TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004577 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004578 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 prepareButtons();
4580 prepareAxes(POSITION);
4581 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004582 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004583
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004585 ASSERT_TRUE(
4586 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004587 ASSERT_TRUE(flags[0]);
4588 ASSERT_FALSE(flags[1]);
4589}
4590
4591TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004593 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004594 prepareButtons();
4595 prepareAxes(POSITION);
4596 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004597 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598
arthurhungdcef2dc2020-08-11 14:47:50 +08004599 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004600
4601 NotifyKeyArgs args;
4602
4603 // Press virtual key.
4604 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4605 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4606 processDown(mapper, x, y);
4607 processSync(mapper);
4608
4609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4610 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4611 ASSERT_EQ(DEVICE_ID, args.deviceId);
4612 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4613 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4614 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4615 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4616 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4617 ASSERT_EQ(KEY_HOME, args.scanCode);
4618 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4619 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4620
4621 // Release virtual key.
4622 processUp(mapper);
4623 processSync(mapper);
4624
4625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4626 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4627 ASSERT_EQ(DEVICE_ID, args.deviceId);
4628 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4629 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4630 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4631 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4632 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4633 ASSERT_EQ(KEY_HOME, args.scanCode);
4634 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4635 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4636
4637 // Should not have sent any motions.
4638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4639}
4640
4641TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004642 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004643 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 prepareButtons();
4645 prepareAxes(POSITION);
4646 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004647 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
arthurhungdcef2dc2020-08-11 14:47:50 +08004649 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004650
4651 NotifyKeyArgs keyArgs;
4652
4653 // Press virtual key.
4654 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4655 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4656 processDown(mapper, x, y);
4657 processSync(mapper);
4658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4660 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4661 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4662 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4663 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4664 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4665 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4666 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4667 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4668 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4669 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4670
4671 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4672 // into the display area.
4673 y -= 100;
4674 processMove(mapper, x, y);
4675 processSync(mapper);
4676
4677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4678 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4679 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4680 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4681 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4682 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4683 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4684 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4685 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4686 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4687 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4688 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4689
4690 NotifyMotionArgs motionArgs;
4691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4696 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4697 ASSERT_EQ(0, motionArgs.flags);
4698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4699 ASSERT_EQ(0, motionArgs.buttonState);
4700 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004701 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004702 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004703 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4705 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4706 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4707 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4708 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4709
4710 // Keep moving out of bounds. Should generate a pointer move.
4711 y -= 50;
4712 processMove(mapper, x, y);
4713 processSync(mapper);
4714
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4716 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4717 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4718 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4719 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4720 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4721 ASSERT_EQ(0, motionArgs.flags);
4722 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4723 ASSERT_EQ(0, motionArgs.buttonState);
4724 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004725 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004727 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4729 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4730 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4731 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4732 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4733
4734 // Release out of bounds. Should generate a pointer up.
4735 processUp(mapper);
4736 processSync(mapper);
4737
4738 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4739 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4740 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4741 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4742 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4743 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4744 ASSERT_EQ(0, motionArgs.flags);
4745 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4746 ASSERT_EQ(0, motionArgs.buttonState);
4747 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004748 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004749 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004750 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4752 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4753 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4754 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4755 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4756
4757 // Should not have sent any more keys or motions.
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4760}
4761
4762TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004764 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004765 prepareButtons();
4766 prepareAxes(POSITION);
4767 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004768 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004769
arthurhungdcef2dc2020-08-11 14:47:50 +08004770 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004771
4772 NotifyMotionArgs motionArgs;
4773
4774 // Initially go down out of bounds.
4775 int32_t x = -10;
4776 int32_t y = -10;
4777 processDown(mapper, x, y);
4778 processSync(mapper);
4779
4780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4781
4782 // Move into the display area. Should generate a pointer down.
4783 x = 50;
4784 y = 75;
4785 processMove(mapper, x, y);
4786 processSync(mapper);
4787
4788 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4789 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4790 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4791 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4792 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4793 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4794 ASSERT_EQ(0, motionArgs.flags);
4795 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4796 ASSERT_EQ(0, motionArgs.buttonState);
4797 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004798 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004799 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004800 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004801 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4802 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4803 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4804 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4805 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4806
4807 // Release. Should generate a pointer up.
4808 processUp(mapper);
4809 processSync(mapper);
4810
4811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4812 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4813 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4814 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4815 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4816 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4817 ASSERT_EQ(0, motionArgs.flags);
4818 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4819 ASSERT_EQ(0, motionArgs.buttonState);
4820 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004821 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004822 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004823 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4825 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4826 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4827 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4828 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4829
4830 // Should not have sent any more keys or motions.
4831 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4833}
4834
Santos Cordonfa5cf462017-04-05 10:37:00 -07004835TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004836 addConfigurationProperty("touch.deviceType", "touchScreen");
4837 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4838
Michael Wrighta9cf4192022-12-01 23:46:39 +00004839 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004840 prepareButtons();
4841 prepareAxes(POSITION);
4842 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004843 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004844
arthurhungdcef2dc2020-08-11 14:47:50 +08004845 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004846
4847 NotifyMotionArgs motionArgs;
4848
4849 // Down.
4850 int32_t x = 100;
4851 int32_t y = 125;
4852 processDown(mapper, x, y);
4853 processSync(mapper);
4854
4855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4856 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4857 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4858 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4859 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4860 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4861 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4862 ASSERT_EQ(0, motionArgs.flags);
4863 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4864 ASSERT_EQ(0, motionArgs.buttonState);
4865 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004866 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004867 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004868 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004869 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4870 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4871 1, 0, 0, 0, 0, 0, 0, 0));
4872 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4873 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4874 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4875
4876 // Move.
4877 x += 50;
4878 y += 75;
4879 processMove(mapper, x, y);
4880 processSync(mapper);
4881
4882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4883 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4884 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4885 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4886 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4887 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4889 ASSERT_EQ(0, motionArgs.flags);
4890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4891 ASSERT_EQ(0, motionArgs.buttonState);
4892 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004893 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004895 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4897 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4898 1, 0, 0, 0, 0, 0, 0, 0));
4899 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4900 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4901 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4902
4903 // Up.
4904 processUp(mapper);
4905 processSync(mapper);
4906
4907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4908 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4909 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4910 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4911 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4912 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4913 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4914 ASSERT_EQ(0, motionArgs.flags);
4915 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4916 ASSERT_EQ(0, motionArgs.buttonState);
4917 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004918 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004919 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004920 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004921 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4922 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4923 1, 0, 0, 0, 0, 0, 0, 0));
4924 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4925 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4926 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4927
4928 // Should not have sent any more keys or motions.
4929 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4931}
4932
Michael Wrightd02c5b62014-02-10 15:10:22 -08004933TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004934 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004935 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004936 prepareButtons();
4937 prepareAxes(POSITION);
4938 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004939 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004940
arthurhungdcef2dc2020-08-11 14:47:50 +08004941 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004942
4943 NotifyMotionArgs motionArgs;
4944
4945 // Down.
4946 int32_t x = 100;
4947 int32_t y = 125;
4948 processDown(mapper, x, y);
4949 processSync(mapper);
4950
4951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4952 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4953 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4954 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4955 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4956 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4957 ASSERT_EQ(0, motionArgs.flags);
4958 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4959 ASSERT_EQ(0, motionArgs.buttonState);
4960 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004961 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004962 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004963 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4965 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4966 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4967 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4968 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4969
4970 // Move.
4971 x += 50;
4972 y += 75;
4973 processMove(mapper, x, y);
4974 processSync(mapper);
4975
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4977 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4978 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4979 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4980 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4981 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4982 ASSERT_EQ(0, motionArgs.flags);
4983 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4984 ASSERT_EQ(0, motionArgs.buttonState);
4985 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004986 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004987 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004988 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004989 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4990 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4991 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4992 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4993 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4994
4995 // Up.
4996 processUp(mapper);
4997 processSync(mapper);
4998
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5000 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5001 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5002 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5003 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5004 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5005 ASSERT_EQ(0, motionArgs.flags);
5006 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5007 ASSERT_EQ(0, motionArgs.buttonState);
5008 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005009 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005010 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005011 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005012 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5013 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5014 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5015 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5016 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5017
5018 // Should not have sent any more keys or motions.
5019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5021}
5022
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005023TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005024 addConfigurationProperty("touch.deviceType", "touchScreen");
5025 prepareButtons();
5026 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005027 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5028 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005029 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005030
5031 NotifyMotionArgs args;
5032
5033 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005034 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035 processDown(mapper, toRawX(50), toRawY(75));
5036 processSync(mapper);
5037
5038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5039 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5040 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5041
5042 processUp(mapper);
5043 processSync(mapper);
5044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5045}
5046
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005047TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005048 addConfigurationProperty("touch.deviceType", "touchScreen");
5049 prepareButtons();
5050 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005051 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5052 // orientation-aware are affected by display rotation.
5053 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005054 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005055
5056 NotifyMotionArgs args;
5057
5058 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005059 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005060 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005061 processDown(mapper, toRawX(50), toRawY(75));
5062 processSync(mapper);
5063
5064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5065 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5066 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5067
5068 processUp(mapper);
5069 processSync(mapper);
5070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5071
5072 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005073 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005074 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005075 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005076 processSync(mapper);
5077
5078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5079 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5080 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5081
5082 processUp(mapper);
5083 processSync(mapper);
5084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5085
5086 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005087 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005088 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005089 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5090 processSync(mapper);
5091
5092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5093 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5094 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5095
5096 processUp(mapper);
5097 processSync(mapper);
5098 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5099
5100 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005101 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005102 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005103 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005104 processSync(mapper);
5105
5106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5107 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5108 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5109
5110 processUp(mapper);
5111 processSync(mapper);
5112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5113}
5114
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005115TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5116 addConfigurationProperty("touch.deviceType", "touchScreen");
5117 prepareButtons();
5118 prepareAxes(POSITION);
5119 addConfigurationProperty("touch.orientationAware", "1");
5120 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5121 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005122 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005123 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005124 NotifyMotionArgs args;
5125
5126 // Orientation 0.
5127 processDown(mapper, toRawX(50), toRawY(75));
5128 processSync(mapper);
5129
5130 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5131 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5132 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5133
5134 processUp(mapper);
5135 processSync(mapper);
5136 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5137}
5138
5139TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5140 addConfigurationProperty("touch.deviceType", "touchScreen");
5141 prepareButtons();
5142 prepareAxes(POSITION);
5143 addConfigurationProperty("touch.orientationAware", "1");
5144 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5145 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005146 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005147 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005148 NotifyMotionArgs args;
5149
5150 // Orientation 90.
5151 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5152 processSync(mapper);
5153
5154 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5155 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5156 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5157
5158 processUp(mapper);
5159 processSync(mapper);
5160 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5161}
5162
5163TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5164 addConfigurationProperty("touch.deviceType", "touchScreen");
5165 prepareButtons();
5166 prepareAxes(POSITION);
5167 addConfigurationProperty("touch.orientationAware", "1");
5168 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5169 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005170 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005171 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005172 NotifyMotionArgs args;
5173
5174 // Orientation 180.
5175 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5176 processSync(mapper);
5177
5178 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5179 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5180 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5181
5182 processUp(mapper);
5183 processSync(mapper);
5184 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5185}
5186
5187TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5188 addConfigurationProperty("touch.deviceType", "touchScreen");
5189 prepareButtons();
5190 prepareAxes(POSITION);
5191 addConfigurationProperty("touch.orientationAware", "1");
5192 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5193 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005194 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005195 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005196 NotifyMotionArgs args;
5197
5198 // Orientation 270.
5199 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5200 processSync(mapper);
5201
5202 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5203 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5204 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5205
5206 processUp(mapper);
5207 processSync(mapper);
5208 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5209}
5210
5211TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5212 addConfigurationProperty("touch.deviceType", "touchScreen");
5213 prepareButtons();
5214 prepareAxes(POSITION);
5215 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5216 // orientation-aware are affected by display rotation.
5217 addConfigurationProperty("touch.orientationAware", "0");
5218 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005219 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005220
5221 NotifyMotionArgs args;
5222
5223 // Orientation 90, Rotation 0.
5224 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005225 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005226 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5227 processSync(mapper);
5228
5229 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5230 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5231 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5232
5233 processUp(mapper);
5234 processSync(mapper);
5235 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5236
5237 // Orientation 90, Rotation 90.
5238 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005239 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005240 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005241 processSync(mapper);
5242
5243 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5244 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5245 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5246
5247 processUp(mapper);
5248 processSync(mapper);
5249 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5250
5251 // Orientation 90, Rotation 180.
5252 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005253 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005254 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5255 processSync(mapper);
5256
5257 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5258 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5259 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5260
5261 processUp(mapper);
5262 processSync(mapper);
5263 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5264
5265 // Orientation 90, Rotation 270.
5266 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005267 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005268 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 -07005269 processSync(mapper);
5270
5271 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5272 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5273 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5274
5275 processUp(mapper);
5276 processSync(mapper);
5277 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5278}
5279
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005280TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5281 addConfigurationProperty("touch.deviceType", "touchScreen");
5282 prepareButtons();
5283 prepareAxes(POSITION);
5284 addConfigurationProperty("touch.orientationAware", "1");
5285 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005286 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005287
5288 // Set a physical frame in the display viewport.
5289 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5290 viewport->physicalLeft = 20;
5291 viewport->physicalTop = 600;
5292 viewport->physicalRight = 30;
5293 viewport->physicalBottom = 610;
5294 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005295 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005296
5297 // Start the touch.
5298 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5299 processSync(mapper);
5300
5301 // Expect all input starting outside the physical frame to be ignored.
5302 const std::array<Point, 6> outsidePoints = {
5303 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5304 for (const auto& p : outsidePoints) {
5305 processMove(mapper, toRawX(p.x), toRawY(p.y));
5306 processSync(mapper);
5307 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5308 }
5309
5310 // Move the touch into the physical frame.
5311 processMove(mapper, toRawX(25), toRawY(605));
5312 processSync(mapper);
5313 NotifyMotionArgs args;
5314 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5315 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5316 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5317 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5318
5319 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5320 for (const auto& p : outsidePoints) {
5321 processMove(mapper, toRawX(p.x), toRawY(p.y));
5322 processSync(mapper);
5323 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5324 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5325 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5326 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5327 }
5328
5329 processUp(mapper);
5330 processSync(mapper);
5331 EXPECT_NO_FATAL_FAILURE(
5332 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5333}
5334
Harry Cutts1db43992023-06-19 17:05:07 +00005335TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
5336 std::shared_ptr<FakePointerController> fakePointerController =
5337 std::make_shared<FakePointerController>();
5338 mFakePolicy->setPointerController(fakePointerController);
5339
5340 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
Seunghwan Choi356026c2023-02-01 14:37:25 +09006184TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
6185 std::shared_ptr<FakePointerController> fakePointerController =
6186 std::make_shared<FakePointerController>();
6187 addConfigurationProperty("touch.deviceType", "touchScreen");
6188 prepareDisplay(ui::ROTATION_0);
6189 prepareButtons();
6190 prepareAxes(POSITION);
6191 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6192 mFakePolicy->setPointerController(fakePointerController);
6193 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00006194 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006195
6196 processKey(mapper, BTN_TOOL_PEN, 1);
6197 processMove(mapper, 100, 200);
6198 processSync(mapper);
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6200 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006201 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006202 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6203 ASSERT_TRUE(fakePointerController->isPointerShown());
6204 ASSERT_NO_FATAL_FAILURE(
6205 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
6206}
6207
6208TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
6209 std::shared_ptr<FakePointerController> fakePointerController =
6210 std::make_shared<FakePointerController>();
6211 addConfigurationProperty("touch.deviceType", "touchScreen");
6212 prepareDisplay(ui::ROTATION_0);
6213 prepareButtons();
6214 prepareAxes(POSITION);
6215 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6216 mFakePolicy->setPointerController(fakePointerController);
6217 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00006218 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09006219
6220 processKey(mapper, BTN_TOOL_PEN, 1);
6221 processMove(mapper, 100, 200);
6222 processSync(mapper);
6223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6224 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006225 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09006226 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
6227 ASSERT_FALSE(fakePointerController->isPointerShown());
6228}
6229
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006230TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6231 // Initialize the device without setting device source to touch navigation.
6232 addConfigurationProperty("touch.deviceType", "touchScreen");
6233 prepareDisplay(ui::ROTATION_0);
6234 prepareButtons();
6235 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006236 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006237
6238 // Ensure that the device is created as a touchscreen, not touch navigation.
6239 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6240
6241 // Add device type association after the device was created.
6242 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6243
6244 // Send update to the mapper.
6245 std::list<NotifyArgs> unused2 =
6246 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006247 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006248
6249 // Check whether device type update was successful.
6250 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6251}
6252
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006253TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6254 // Initialize the device without setting device source to touch navigation.
6255 addConfigurationProperty("touch.deviceType", "touchScreen");
6256 prepareDisplay(ui::ROTATION_0);
6257 prepareButtons();
6258 prepareAxes(POSITION);
6259 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6260
6261 // Set a physical frame in the display viewport.
6262 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6263 viewport->physicalLeft = 0;
6264 viewport->physicalTop = 0;
6265 viewport->physicalRight = DISPLAY_WIDTH / 2;
6266 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6267 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006268 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006269
Arpit Singha8c236b2023-04-25 13:56:05 +00006270 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006271
6272 // Hovering inside the physical frame produces events.
6273 processKey(mapper, BTN_TOOL_PEN, 1);
6274 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6275 processSync(mapper);
6276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6277 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6279 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6280
6281 // Leaving the physical frame ends the hovering gesture.
6282 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6283 processSync(mapper);
6284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6285 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6286
6287 // Moving outside the physical frame does not produce events.
6288 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6289 processSync(mapper);
6290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6291
6292 // Re-entering the physical frame produces events.
6293 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6294 processSync(mapper);
6295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6296 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6298 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6299}
6300
Prabir Pradhan5632d622021-09-06 07:57:20 -07006301// --- TouchDisplayProjectionTest ---
6302
6303class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6304public:
6305 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6306 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6307 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006308 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6309 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6310 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006311 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006312 auto rotatedWidth = naturalDisplayWidth;
6313 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006314 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006315 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006316 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006317 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006318 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006319 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006320 inverseRotationFlags = ui::Transform::ROT_180;
6321 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006322 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006323 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006324 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006325 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006326 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006327 inverseRotationFlags = ui::Transform::ROT_0;
6328 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006329 }
6330
Prabir Pradhana9df3162022-12-05 23:57:27 +00006331 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006332 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6333
6334 std::optional<DisplayViewport> internalViewport =
6335 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6336 DisplayViewport& v = *internalViewport;
6337 v.displayId = DISPLAY_ID;
6338 v.orientation = orientation;
6339
6340 v.logicalLeft = 0;
6341 v.logicalTop = 0;
6342 v.logicalRight = 100;
6343 v.logicalBottom = 100;
6344
6345 v.physicalLeft = rotatedPhysicalDisplay.left;
6346 v.physicalTop = rotatedPhysicalDisplay.top;
6347 v.physicalRight = rotatedPhysicalDisplay.right;
6348 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6349
Prabir Pradhana9df3162022-12-05 23:57:27 +00006350 v.deviceWidth = rotatedWidth;
6351 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006352
6353 v.isActive = true;
6354 v.uniqueId = UNIQUE_ID;
6355 v.type = ViewportType::INTERNAL;
6356 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006357 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006358 }
6359
6360 void assertReceivedMove(const Point& point) {
6361 NotifyMotionArgs motionArgs;
6362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006364 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6366 1, 0, 0, 0, 0, 0, 0, 0));
6367 }
6368};
6369
6370TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6371 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006372 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006373
6374 prepareButtons();
6375 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006376 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006377
6378 NotifyMotionArgs motionArgs;
6379
6380 // Configure the DisplayViewport such that the logical display maps to a subsection of
6381 // the display panel called the physical display. Here, the physical display is bounded by the
6382 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6383 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6384 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6385 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6386
Michael Wrighta9cf4192022-12-01 23:46:39 +00006387 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006388 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6389
6390 // Touches outside the physical display should be ignored, and should not generate any
6391 // events. Ensure touches at the following points that lie outside of the physical display
6392 // area do not generate any events.
6393 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6394 processDown(mapper, toRawX(point.x), toRawY(point.y));
6395 processSync(mapper);
6396 processUp(mapper);
6397 processSync(mapper);
6398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6399 << "Unexpected event generated for touch outside physical display at point: "
6400 << point.x << ", " << point.y;
6401 }
6402 }
6403}
6404
6405TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6406 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006407 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006408
6409 prepareButtons();
6410 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006411 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006412
6413 NotifyMotionArgs motionArgs;
6414
6415 // Configure the DisplayViewport such that the logical display maps to a subsection of
6416 // the display panel called the physical display. Here, the physical display is bounded by the
6417 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6418 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6419
Michael Wrighta9cf4192022-12-01 23:46:39 +00006420 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006421 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6422
6423 // Touches that start outside the physical display should be ignored until it enters the
6424 // physical display bounds, at which point it should generate a down event. Start a touch at
6425 // the point (5, 100), which is outside the physical display bounds.
6426 static const Point kOutsidePoint{5, 100};
6427 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6428 processSync(mapper);
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6430
6431 // Move the touch into the physical display area. This should generate a pointer down.
6432 processMove(mapper, toRawX(11), toRawY(21));
6433 processSync(mapper);
6434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6435 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006436 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006437 ASSERT_NO_FATAL_FAILURE(
6438 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6439
6440 // Move the touch inside the physical display area. This should generate a pointer move.
6441 processMove(mapper, toRawX(69), toRawY(159));
6442 processSync(mapper);
6443 assertReceivedMove({69, 159});
6444
6445 // Move outside the physical display area. Since the pointer is already down, this should
6446 // now continue generating events.
6447 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6448 processSync(mapper);
6449 assertReceivedMove(kOutsidePoint);
6450
6451 // Release. This should generate a pointer up.
6452 processUp(mapper);
6453 processSync(mapper);
6454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6455 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6456 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6457 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6458
6459 // Ensure no more events were generated.
6460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6462 }
6463}
6464
Prabir Pradhana9df3162022-12-05 23:57:27 +00006465// --- TouchscreenPrecisionTests ---
6466
6467// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6468// in various orientations and with different display rotations. We configure the touchscreen to
6469// have a higher resolution than that of the display by an integer scale factor in each axis so that
6470// we can enforce that coordinates match precisely as expected.
6471class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6472 public ::testing::WithParamInterface<ui::Rotation> {
6473public:
6474 void SetUp() override {
6475 SingleTouchInputMapperTest::SetUp();
6476
6477 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6478 // four times the resolution of the display in the Y axis.
6479 prepareButtons();
6480 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006481 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6482 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006483 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006484 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6485 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006486 }
6487
6488 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6489 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6490 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6491 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6492
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006493 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6494 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6495
6496 static const int32_t PRECISION_RAW_X_FLAT = 16;
6497 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6498
6499 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6500 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6501
Prabir Pradhana9df3162022-12-05 23:57:27 +00006502 static const std::array<Point, 4> kRawCorners;
6503};
6504
6505const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6506 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6507 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6508 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6509 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6510}};
6511
6512// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6513// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6514// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6515TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6516 enum class Orientation {
6517 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6518 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6519 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6520 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6521 ftl_last = ORIENTATION_270,
6522 };
6523 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6524 Orientation::ORIENTATION_270;
6525 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6526 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6527 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6528 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6529 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6530 };
6531
6532 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6533
6534 // Configure the touchscreen as being installed in the one of the four different orientations
6535 // relative to the display.
6536 addConfigurationProperty("touch.deviceType", "touchScreen");
6537 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6538 prepareDisplay(ui::ROTATION_0);
6539
Arpit Singha8c236b2023-04-25 13:56:05 +00006540 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006541
6542 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6543 // orientations of either 90 or 270) this means the display's natural resolution will be
6544 // flipped.
6545 const bool displayRotated =
6546 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6547 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6548 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6549 const Rect physicalFrame{0, 0, width, height};
6550 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6551
6552 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6553 const float expectedPrecisionX = displayRotated ? 4 : 2;
6554 const float expectedPrecisionY = displayRotated ? 2 : 4;
6555
6556 // Test all four corners.
6557 for (int i = 0; i < 4; i++) {
6558 const auto& raw = kRawCorners[i];
6559 processDown(mapper, raw.x, raw.y);
6560 processSync(mapper);
6561 const auto& expected = expectedPoints[i];
6562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6563 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6564 WithCoords(expected.x, expected.y),
6565 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6566 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6567 << "with touchscreen orientation "
6568 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6569 << expected.x << ", " << expected.y << ").";
6570 processUp(mapper);
6571 processSync(mapper);
6572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6573 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6574 WithCoords(expected.x, expected.y))));
6575 }
6576}
6577
Prabir Pradhan82687402022-12-06 01:32:53 +00006578TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6579 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6580 kMappedCorners = {
6581 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6582 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6583 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6584 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6585 };
6586
6587 const ui::Rotation displayRotation = GetParam();
6588
6589 addConfigurationProperty("touch.deviceType", "touchScreen");
6590 prepareDisplay(displayRotation);
6591
Arpit Singha8c236b2023-04-25 13:56:05 +00006592 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006593
6594 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6595
6596 // Test all four corners.
6597 for (int i = 0; i < 4; i++) {
6598 const auto& expected = expectedPoints[i];
6599 const auto& raw = kRawCorners[i];
6600 processDown(mapper, raw.x, raw.y);
6601 processSync(mapper);
6602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6603 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6604 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6605 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6606 << "with display rotation " << ui::toCString(displayRotation)
6607 << ", expected point (" << expected.x << ", " << expected.y << ").";
6608 processUp(mapper);
6609 processSync(mapper);
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6611 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6612 WithCoords(expected.x, expected.y))));
6613 }
6614}
6615
Prabir Pradhan3e798762022-12-02 21:02:11 +00006616TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6617 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6618 kMappedCorners = {
6619 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6620 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6621 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6622 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6623 };
6624
6625 const ui::Rotation displayRotation = GetParam();
6626
6627 addConfigurationProperty("touch.deviceType", "touchScreen");
6628 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6629
Arpit Singha8c236b2023-04-25 13:56:05 +00006630 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006631
6632 // Ori 270, so width and height swapped
6633 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6634 prepareDisplay(displayRotation);
6635 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6636
6637 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6638
6639 // Test all four corners.
6640 for (int i = 0; i < 4; i++) {
6641 const auto& expected = expectedPoints[i];
6642 const auto& raw = kRawCorners[i];
6643 processDown(mapper, raw.x, raw.y);
6644 processSync(mapper);
6645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6646 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6647 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6648 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6649 << "with display rotation " << ui::toCString(displayRotation)
6650 << ", expected point (" << expected.x << ", " << expected.y << ").";
6651 processUp(mapper);
6652 processSync(mapper);
6653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6654 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6655 WithCoords(expected.x, expected.y))));
6656 }
6657}
6658
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006659TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6660 const ui::Rotation displayRotation = GetParam();
6661
6662 addConfigurationProperty("touch.deviceType", "touchScreen");
6663 prepareDisplay(displayRotation);
6664
6665 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006666 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006667
6668 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6669 // MotionRanges use display pixels as their units
6670 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6671 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6672
6673 // The MotionRanges should be oriented in the rotated display's coordinate space
6674 const bool displayRotated =
6675 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6676
6677 constexpr float MAX_X = 479.5;
6678 constexpr float MAX_Y = 799.75;
6679 EXPECT_EQ(xRange->min, 0.f);
6680 EXPECT_EQ(yRange->min, 0.f);
6681 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6682 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6683
6684 EXPECT_EQ(xRange->flat, 8.f);
6685 EXPECT_EQ(yRange->flat, 8.f);
6686
6687 EXPECT_EQ(xRange->fuzz, 2.f);
6688 EXPECT_EQ(yRange->fuzz, 2.f);
6689
6690 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6691 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6692}
6693
Prabir Pradhana9df3162022-12-05 23:57:27 +00006694// Run the precision tests for all rotations.
6695INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6696 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6697 ui::ROTATION_270),
6698 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6699 return ftl::enum_string(testParamInfo.param);
6700 });
6701
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006702// --- ExternalStylusFusionTest ---
6703
6704class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6705public:
6706 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6707 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006708 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006709 prepareButtons();
6710 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006711 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006712
6713 mStylusState.when = ARBITRARY_TIME;
6714 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006715 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006716 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006717 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006718 processExternalStylusState(mapper);
6719 return mapper;
6720 }
6721
6722 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6723 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6724 for (const NotifyArgs& args : generatedArgs) {
6725 mFakeListener->notify(args);
6726 }
6727 // Loop the reader to flush the input listener queue.
6728 mReader->loopOnce();
6729 return generatedArgs;
6730 }
6731
6732protected:
6733 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006734
6735 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6736 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006737 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006738
6739 // The first pointer is withheld.
6740 processDown(mapper, 100, 200);
6741 processSync(mapper);
6742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6743 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6744 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6745
6746 // The external stylus reports pressure. The withheld finger pointer is released as a
6747 // stylus.
6748 mStylusState.pressure = 1.f;
6749 processExternalStylusState(mapper);
6750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6751 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6752 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6753
6754 // Subsequent pointer events are not withheld.
6755 processMove(mapper, 101, 201);
6756 processSync(mapper);
6757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6758 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6759
6760 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6761 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6762 }
6763
6764 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6765 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6766
6767 // Releasing the touch pointer ends the gesture.
6768 processUp(mapper);
6769 processSync(mapper);
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006771 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006772 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006773
6774 mStylusState.pressure = 0.f;
6775 processExternalStylusState(mapper);
6776 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6778 }
6779
6780 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006781 // When stylus fusion is not successful, events should be reported with the original source.
6782 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006783 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006784 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006785
6786 // The first pointer is withheld when an external stylus is connected,
6787 // and a timeout is requested.
6788 processDown(mapper, 100, 200);
6789 processSync(mapper);
6790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6791 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6792 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6793
6794 // If the timeout expires early, it is requested again.
6795 handleTimeout(mapper, ARBITRARY_TIME + 1);
6796 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6797 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6798
6799 // When the timeout expires, the withheld touch is released as a finger pointer.
6800 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6802 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6803
6804 // Subsequent pointer events are not withheld.
6805 processMove(mapper, 101, 201);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6808 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6809 processUp(mapper);
6810 processSync(mapper);
6811 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6812 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6813
6814 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6816 }
6817
6818private:
6819 InputDeviceInfo mExternalStylusDeviceInfo{};
6820};
6821
6822TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6823 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006824 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006825}
6826
6827TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6828 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6829 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6830}
6831
6832TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6833 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6834 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6835}
6836
6837// Test a successful stylus fusion gesture where the pressure is reported by the external
6838// before the touch is reported by the touchscreen.
6839TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6840 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006841 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006842
6843 // The external stylus reports pressure first. It is ignored for now.
6844 mStylusState.pressure = 1.f;
6845 processExternalStylusState(mapper);
6846 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6847 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6848
6849 // When the touch goes down afterwards, it is reported as a stylus pointer.
6850 processDown(mapper, 100, 200);
6851 processSync(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6853 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6854 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6855
6856 processMove(mapper, 101, 201);
6857 processSync(mapper);
6858 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6859 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6860 processUp(mapper);
6861 processSync(mapper);
6862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6863 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6864
6865 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6866 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6867}
6868
6869TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6870 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6871
6872 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6873 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6874
6875 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6876 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6877 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6878 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6879}
6880
6881TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6882 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006883 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006884
6885 mStylusState.pressure = 0.8f;
6886 processExternalStylusState(mapper);
6887 processDown(mapper, 100, 200);
6888 processSync(mapper);
6889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6890 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6891 WithPressure(0.8f))));
6892 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6893
6894 // The external stylus reports a pressure change. We wait for some time for a touch event.
6895 mStylusState.pressure = 0.6f;
6896 processExternalStylusState(mapper);
6897 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6898 ASSERT_NO_FATAL_FAILURE(
6899 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6900
6901 // If a touch is reported within the timeout, it reports the updated pressure.
6902 processMove(mapper, 101, 201);
6903 processSync(mapper);
6904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6905 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6906 WithPressure(0.6f))));
6907 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6908
6909 // There is another pressure change.
6910 mStylusState.pressure = 0.5f;
6911 processExternalStylusState(mapper);
6912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6913 ASSERT_NO_FATAL_FAILURE(
6914 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6915
6916 // If a touch is not reported within the timeout, a move event is generated to report
6917 // the new pressure.
6918 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6920 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6921 WithPressure(0.5f))));
6922
6923 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6924 // repeated indefinitely.
6925 mStylusState.pressure = 0.0f;
6926 processExternalStylusState(mapper);
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6928 ASSERT_NO_FATAL_FAILURE(
6929 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6930 processMove(mapper, 102, 202);
6931 processSync(mapper);
6932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6933 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6934 WithPressure(0.5f))));
6935 processMove(mapper, 103, 203);
6936 processSync(mapper);
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6938 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6939 WithPressure(0.5f))));
6940
6941 processUp(mapper);
6942 processSync(mapper);
6943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006944 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006945 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006946
6947 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6949}
6950
6951TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6952 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006953 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006954
6955 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006956 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006957 processExternalStylusState(mapper);
6958 processDown(mapper, 100, 200);
6959 processSync(mapper);
6960 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6961 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006962 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006963 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6964
6965 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006966 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006967 processExternalStylusState(mapper);
6968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6969 ASSERT_NO_FATAL_FAILURE(
6970 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6971
6972 // If a touch is reported within the timeout, it reports the updated pressure.
6973 processMove(mapper, 101, 201);
6974 processSync(mapper);
6975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6976 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006977 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006978 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6979
6980 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006981 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006982 processExternalStylusState(mapper);
6983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6984 ASSERT_NO_FATAL_FAILURE(
6985 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6986
6987 // If a touch is not reported within the timeout, a move event is generated to report
6988 // the new tool type.
6989 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6991 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006992 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006993
6994 processUp(mapper);
6995 processSync(mapper);
6996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6997 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006998 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006999
7000 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7002}
7003
7004TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
7005 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00007006 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007007
7008 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
7009
7010 // The external stylus reports a button change. We wait for some time for a touch event.
7011 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
7012 processExternalStylusState(mapper);
7013 ASSERT_NO_FATAL_FAILURE(
7014 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7015
7016 // If a touch is reported within the timeout, it reports the updated button state.
7017 processMove(mapper, 101, 201);
7018 processSync(mapper);
7019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7020 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7021 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7023 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7024 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7025 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7026
7027 // The button is now released.
7028 mStylusState.buttons = 0;
7029 processExternalStylusState(mapper);
7030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7031 ASSERT_NO_FATAL_FAILURE(
7032 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
7033
7034 // If a touch is not reported within the timeout, a move event is generated to report
7035 // the new button state.
7036 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7038 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7039 WithButtonState(0))));
7040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00007041 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
7042 WithButtonState(0))));
7043
7044 processUp(mapper);
7045 processSync(mapper);
7046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007047 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7048
7049 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7051}
7052
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053// --- MultiTouchInputMapperTest ---
7054
7055class MultiTouchInputMapperTest : public TouchInputMapperTest {
7056protected:
7057 void prepareAxes(int axes);
7058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007059 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7060 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7061 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7062 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7063 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7064 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7065 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7066 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7067 void processId(MultiTouchInputMapper& mapper, int32_t id);
7068 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7069 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7070 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007071 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007072 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007073 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7074 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075};
7076
7077void MultiTouchInputMapperTest::prepareAxes(int axes) {
7078 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007079 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7080 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007081 }
7082 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007083 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7084 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007085 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007086 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7087 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007088 }
7089 }
7090 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007091 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7092 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007093 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007094 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007095 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 }
7097 }
7098 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007099 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7100 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007101 }
7102 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007103 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7104 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105 }
7106 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7108 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109 }
7110 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007111 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7112 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113 }
7114 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007115 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7116 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117 }
7118 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007119 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007120 }
7121}
7122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007123void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7124 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007125 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7126 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127}
7128
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007129void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7130 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007132}
7133
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007134void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7135 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007136 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007137}
7138
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007139void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007140 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007141}
7142
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007143void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007144 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007147void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7148 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150}
7151
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007152void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007153 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007154}
7155
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007156void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158}
7159
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007160void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007162}
7163
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007164void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007166}
7167
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007168void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007169 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007170}
7171
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007172void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7173 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007174 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007175}
7176
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007177void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7178 int32_t value) {
7179 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7180 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7181}
7182
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007183void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007184 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007185}
7186
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007187void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7188 nsecs_t readTime) {
7189 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007190}
7191
Michael Wrightd02c5b62014-02-10 15:10:22 -08007192TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007194 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195 prepareAxes(POSITION);
7196 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007197 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198
arthurhungdcef2dc2020-08-11 14:47:50 +08007199 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200
7201 NotifyMotionArgs motionArgs;
7202
7203 // Two fingers down at once.
7204 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7205 processPosition(mapper, x1, y1);
7206 processMTSync(mapper);
7207 processPosition(mapper, x2, y2);
7208 processMTSync(mapper);
7209 processSync(mapper);
7210
7211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7212 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7213 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7214 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7215 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7217 ASSERT_EQ(0, motionArgs.flags);
7218 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7219 ASSERT_EQ(0, motionArgs.buttonState);
7220 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007221 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007222 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007223 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007224 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7225 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7226 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7227 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7228 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7229
7230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7231 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7232 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7233 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7234 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007235 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007236 ASSERT_EQ(0, motionArgs.flags);
7237 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7238 ASSERT_EQ(0, motionArgs.buttonState);
7239 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007240 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007241 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007242 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007243 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007244 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7246 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7247 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7248 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7249 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7250 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7251 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7252
7253 // Move.
7254 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7255 processPosition(mapper, x1, y1);
7256 processMTSync(mapper);
7257 processPosition(mapper, x2, y2);
7258 processMTSync(mapper);
7259 processSync(mapper);
7260
7261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7262 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7263 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7264 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7265 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7266 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7267 ASSERT_EQ(0, motionArgs.flags);
7268 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7269 ASSERT_EQ(0, motionArgs.buttonState);
7270 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007271 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007274 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007275 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007276 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7277 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7278 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7279 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7280 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7281 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7282 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7283
7284 // First finger up.
7285 x2 += 15; y2 -= 20;
7286 processPosition(mapper, x2, y2);
7287 processMTSync(mapper);
7288 processSync(mapper);
7289
7290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7291 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7292 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7293 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7294 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007295 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007296 ASSERT_EQ(0, motionArgs.flags);
7297 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7298 ASSERT_EQ(0, motionArgs.buttonState);
7299 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007300 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007301 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007302 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007303 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007304 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007305 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7306 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7307 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7308 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7309 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7310 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7311 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7312
7313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7314 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7315 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7319 ASSERT_EQ(0, motionArgs.flags);
7320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7321 ASSERT_EQ(0, motionArgs.buttonState);
7322 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007323 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007324 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007325 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7327 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7328 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7329 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7330 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7331
7332 // Move.
7333 x2 += 20; y2 -= 25;
7334 processPosition(mapper, x2, y2);
7335 processMTSync(mapper);
7336 processSync(mapper);
7337
7338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7339 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7340 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7344 ASSERT_EQ(0, motionArgs.flags);
7345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7346 ASSERT_EQ(0, motionArgs.buttonState);
7347 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007348 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007349 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007350 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7352 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7353 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7354 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7355 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7356
7357 // New finger down.
7358 int32_t x3 = 700, y3 = 300;
7359 processPosition(mapper, x2, y2);
7360 processMTSync(mapper);
7361 processPosition(mapper, x3, y3);
7362 processMTSync(mapper);
7363 processSync(mapper);
7364
7365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7366 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7367 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7368 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7369 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007370 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 ASSERT_EQ(0, motionArgs.flags);
7372 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7373 ASSERT_EQ(0, motionArgs.buttonState);
7374 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007375 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007376 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007377 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007379 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007380 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7381 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7383 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7384 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7385 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7386 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7387
7388 // Second finger up.
7389 x3 += 30; y3 -= 20;
7390 processPosition(mapper, x3, y3);
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);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007399 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007400 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(2), 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007408 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7410 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7412 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7413 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7414 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7415 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7416
7417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7418 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7419 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7420 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7421 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7423 ASSERT_EQ(0, motionArgs.flags);
7424 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7425 ASSERT_EQ(0, motionArgs.buttonState);
7426 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007427 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007428 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007429 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7431 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7432 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7433 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7434 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7435
7436 // Last finger up.
7437 processMTSync(mapper);
7438 processSync(mapper);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7441 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7442 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7443 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7444 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7445 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7446 ASSERT_EQ(0, motionArgs.flags);
7447 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7448 ASSERT_EQ(0, motionArgs.buttonState);
7449 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007450 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007451 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007452 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7454 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7455 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7456 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7457 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7458
7459 // Should not have sent any more keys or motions.
7460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7462}
7463
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007464TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7465 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007466 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007467
7468 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7469 /*fuzz*/ 0, /*resolution*/ 10);
7470 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7471 /*fuzz*/ 0, /*resolution*/ 11);
7472 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7473 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7474 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7475 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7476 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7477 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7478 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7479 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7480
Arpit Singha8c236b2023-04-25 13:56:05 +00007481 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007482
7483 // X and Y axes
7484 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7485 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7486 // Touch major and minor
7487 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7488 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7489 // Tool major and minor
7490 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7491 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7492}
7493
7494TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7495 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007496 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007497
7498 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7499 /*fuzz*/ 0, /*resolution*/ 10);
7500 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7501 /*fuzz*/ 0, /*resolution*/ 11);
7502
7503 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7504
Arpit Singha8c236b2023-04-25 13:56:05 +00007505 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007506
7507 // Touch major and minor
7508 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7509 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7510 // Tool major and minor
7511 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7512 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7513}
7514
Michael Wrightd02c5b62014-02-10 15:10:22 -08007515TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007516 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007517 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007518 prepareAxes(POSITION | ID);
7519 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007520 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521
arthurhungdcef2dc2020-08-11 14:47:50 +08007522 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007523
7524 NotifyMotionArgs motionArgs;
7525
7526 // Two fingers down at once.
7527 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7528 processPosition(mapper, x1, y1);
7529 processId(mapper, 1);
7530 processMTSync(mapper);
7531 processPosition(mapper, x2, y2);
7532 processId(mapper, 2);
7533 processMTSync(mapper);
7534 processSync(mapper);
7535
7536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7537 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007538 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007539 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007540 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007541 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7542 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7543
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007545 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007546 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007548 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007550 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007551 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7552 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7553 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7554 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7555
7556 // Move.
7557 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7558 processPosition(mapper, x1, y1);
7559 processId(mapper, 1);
7560 processMTSync(mapper);
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(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007569 ASSERT_EQ(0, 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_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007572 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7574 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7575 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7576 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7577
7578 // First finger up.
7579 x2 += 15; y2 -= 20;
7580 processPosition(mapper, x2, y2);
7581 processId(mapper, 2);
7582 processMTSync(mapper);
7583 processSync(mapper);
7584
7585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007586 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007587 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007588 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007589 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007590 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007591 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7593 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7594 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7595 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7596
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007599 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007600 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007601 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007602 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7603 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7604
7605 // Move.
7606 x2 += 20; y2 -= 25;
7607 processPosition(mapper, x2, y2);
7608 processId(mapper, 2);
7609 processMTSync(mapper);
7610 processSync(mapper);
7611
7612 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7613 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007614 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007615 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007616 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007617 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7618 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7619
7620 // New finger down.
7621 int32_t x3 = 700, y3 = 300;
7622 processPosition(mapper, x2, y2);
7623 processId(mapper, 2);
7624 processMTSync(mapper);
7625 processPosition(mapper, x3, y3);
7626 processId(mapper, 3);
7627 processMTSync(mapper);
7628 processSync(mapper);
7629
7630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007631 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007632 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007634 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007635 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007636 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7638 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 // Second finger up.
7643 x3 += 30; y3 -= 20;
7644 processPosition(mapper, x3, y3);
7645 processId(mapper, 3);
7646 processMTSync(mapper);
7647 processSync(mapper);
7648
7649 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007650 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007651 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007654 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007655 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007656 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7657 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7658 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7659 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007663 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007664 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007665 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007666 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7667 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 // Last finger up.
7670 processMTSync(mapper);
7671 processSync(mapper);
7672
7673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7674 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007675 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007677 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7679 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // Should not have sent any more keys or motions.
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7684}
7685
7686TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007688 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007689 prepareAxes(POSITION | ID | SLOT);
7690 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007691 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692
arthurhungdcef2dc2020-08-11 14:47:50 +08007693 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694
7695 NotifyMotionArgs motionArgs;
7696
7697 // Two fingers down at once.
7698 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7699 processPosition(mapper, x1, y1);
7700 processId(mapper, 1);
7701 processSlot(mapper, 1);
7702 processPosition(mapper, x2, y2);
7703 processId(mapper, 2);
7704 processSync(mapper);
7705
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7707 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007708 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007709 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007710 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007711 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7712 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7713
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007715 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007716 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007717 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007718 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007719 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007720 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007721 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7722 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7723 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7724 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7725
7726 // Move.
7727 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7728 processSlot(mapper, 0);
7729 processPosition(mapper, x1, y1);
7730 processSlot(mapper, 1);
7731 processPosition(mapper, x2, y2);
7732 processSync(mapper);
7733
7734 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7735 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007736 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007738 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007739 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007740 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007741 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7742 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7743 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7744 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7745
7746 // First finger up.
7747 x2 += 15; y2 -= 20;
7748 processSlot(mapper, 0);
7749 processId(mapper, -1);
7750 processSlot(mapper, 1);
7751 processPosition(mapper, x2, y2);
7752 processSync(mapper);
7753
7754 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007755 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007756 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007758 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007759 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007760 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007761 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7762 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7763 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7764 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7765
7766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7767 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007768 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007769 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007770 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007771 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7772 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7773
7774 // Move.
7775 x2 += 20; y2 -= 25;
7776 processPosition(mapper, x2, y2);
7777 processSync(mapper);
7778
7779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007781 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007782 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007783 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7785 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7786
7787 // New finger down.
7788 int32_t x3 = 700, y3 = 300;
7789 processPosition(mapper, x2, y2);
7790 processSlot(mapper, 0);
7791 processId(mapper, 3);
7792 processPosition(mapper, x3, y3);
7793 processSync(mapper);
7794
7795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007796 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007797 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007798 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007799 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007800 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007801 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007802 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7803 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7804 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7805 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7806
7807 // Second finger up.
7808 x3 += 30; y3 -= 20;
7809 processSlot(mapper, 1);
7810 processId(mapper, -1);
7811 processSlot(mapper, 0);
7812 processPosition(mapper, x3, y3);
7813 processSync(mapper);
7814
7815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007816 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007817 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007818 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007819 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007820 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007821 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007822 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7823 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7824 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7825 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7826
7827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7828 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007829 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007830 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007832 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7833 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7834
7835 // Last finger up.
7836 processId(mapper, -1);
7837 processSync(mapper);
7838
7839 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7840 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007841 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007842 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007843 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7845 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7846
7847 // Should not have sent any more keys or motions.
7848 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7850}
7851
7852TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007854 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007855 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007856 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857
7858 // These calculations are based on the input device calibration documentation.
7859 int32_t rawX = 100;
7860 int32_t rawY = 200;
7861 int32_t rawTouchMajor = 7;
7862 int32_t rawTouchMinor = 6;
7863 int32_t rawToolMajor = 9;
7864 int32_t rawToolMinor = 8;
7865 int32_t rawPressure = 11;
7866 int32_t rawDistance = 0;
7867 int32_t rawOrientation = 3;
7868 int32_t id = 5;
7869
7870 float x = toDisplayX(rawX);
7871 float y = toDisplayY(rawY);
7872 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7873 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7874 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7875 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7876 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7877 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7878 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7879 float distance = float(rawDistance);
7880
7881 processPosition(mapper, rawX, rawY);
7882 processTouchMajor(mapper, rawTouchMajor);
7883 processTouchMinor(mapper, rawTouchMinor);
7884 processToolMajor(mapper, rawToolMajor);
7885 processToolMinor(mapper, rawToolMinor);
7886 processPressure(mapper, rawPressure);
7887 processOrientation(mapper, rawOrientation);
7888 processDistance(mapper, rawDistance);
7889 processId(mapper, id);
7890 processMTSync(mapper);
7891 processSync(mapper);
7892
7893 NotifyMotionArgs args;
7894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7895 ASSERT_EQ(0, args.pointerProperties[0].id);
7896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7897 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7898 orientation, distance));
7899}
7900
7901TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007903 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007904 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7905 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007906 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007907
7908 // These calculations are based on the input device calibration documentation.
7909 int32_t rawX = 100;
7910 int32_t rawY = 200;
7911 int32_t rawTouchMajor = 140;
7912 int32_t rawTouchMinor = 120;
7913 int32_t rawToolMajor = 180;
7914 int32_t rawToolMinor = 160;
7915
7916 float x = toDisplayX(rawX);
7917 float y = toDisplayY(rawY);
7918 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7919 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7920 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7921 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7922 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7923
7924 processPosition(mapper, rawX, rawY);
7925 processTouchMajor(mapper, rawTouchMajor);
7926 processTouchMinor(mapper, rawTouchMinor);
7927 processToolMajor(mapper, rawToolMajor);
7928 processToolMinor(mapper, rawToolMinor);
7929 processMTSync(mapper);
7930 processSync(mapper);
7931
7932 NotifyMotionArgs args;
7933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7934 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7935 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7936}
7937
7938TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007940 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007941 prepareAxes(POSITION | TOUCH | TOOL);
7942 addConfigurationProperty("touch.size.calibration", "diameter");
7943 addConfigurationProperty("touch.size.scale", "10");
7944 addConfigurationProperty("touch.size.bias", "160");
7945 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007946 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947
7948 // These calculations are based on the input device calibration documentation.
7949 // Note: We only provide a single common touch/tool value because the device is assumed
7950 // not to emit separate values for each pointer (isSummed = 1).
7951 int32_t rawX = 100;
7952 int32_t rawY = 200;
7953 int32_t rawX2 = 150;
7954 int32_t rawY2 = 250;
7955 int32_t rawTouchMajor = 5;
7956 int32_t rawToolMajor = 8;
7957
7958 float x = toDisplayX(rawX);
7959 float y = toDisplayY(rawY);
7960 float x2 = toDisplayX(rawX2);
7961 float y2 = toDisplayY(rawY2);
7962 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7963 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7964 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7965
7966 processPosition(mapper, rawX, rawY);
7967 processTouchMajor(mapper, rawTouchMajor);
7968 processToolMajor(mapper, rawToolMajor);
7969 processMTSync(mapper);
7970 processPosition(mapper, rawX2, rawY2);
7971 processTouchMajor(mapper, rawTouchMajor);
7972 processToolMajor(mapper, rawToolMajor);
7973 processMTSync(mapper);
7974 processSync(mapper);
7975
7976 NotifyMotionArgs args;
7977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7978 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7979
7980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007981 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007982 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7984 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7986 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7987}
7988
7989TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007990 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007991 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007992 prepareAxes(POSITION | TOUCH | TOOL);
7993 addConfigurationProperty("touch.size.calibration", "area");
7994 addConfigurationProperty("touch.size.scale", "43");
7995 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007996 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007997
7998 // These calculations are based on the input device calibration documentation.
7999 int32_t rawX = 100;
8000 int32_t rawY = 200;
8001 int32_t rawTouchMajor = 5;
8002 int32_t rawToolMajor = 8;
8003
8004 float x = toDisplayX(rawX);
8005 float y = toDisplayY(rawY);
8006 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
8007 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
8008 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
8009
8010 processPosition(mapper, rawX, rawY);
8011 processTouchMajor(mapper, rawTouchMajor);
8012 processToolMajor(mapper, rawToolMajor);
8013 processMTSync(mapper);
8014 processSync(mapper);
8015
8016 NotifyMotionArgs args;
8017 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8018 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8019 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
8020}
8021
8022TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008024 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008025 prepareAxes(POSITION | PRESSURE);
8026 addConfigurationProperty("touch.pressure.calibration", "amplitude");
8027 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00008028 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008029
Michael Wrightaa449c92017-12-13 21:21:43 +00008030 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00008031 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00008032 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
8033 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
8034 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
8035
Michael Wrightd02c5b62014-02-10 15:10:22 -08008036 // These calculations are based on the input device calibration documentation.
8037 int32_t rawX = 100;
8038 int32_t rawY = 200;
8039 int32_t rawPressure = 60;
8040
8041 float x = toDisplayX(rawX);
8042 float y = toDisplayY(rawY);
8043 float pressure = float(rawPressure) * 0.01f;
8044
8045 processPosition(mapper, rawX, rawY);
8046 processPressure(mapper, rawPressure);
8047 processMTSync(mapper);
8048 processSync(mapper);
8049
8050 NotifyMotionArgs args;
8051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8052 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8053 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8054}
8055
8056TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008057 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008058 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008060 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008061
8062 NotifyMotionArgs motionArgs;
8063 NotifyKeyArgs keyArgs;
8064
8065 processId(mapper, 1);
8066 processPosition(mapper, 100, 200);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8069 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8070 ASSERT_EQ(0, motionArgs.buttonState);
8071
8072 // press BTN_LEFT, release BTN_LEFT
8073 processKey(mapper, BTN_LEFT, 1);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8076 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8077 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8078
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8080 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8081 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8082
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 processKey(mapper, BTN_LEFT, 0);
8084 processSync(mapper);
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008087 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008088
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008090 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008091 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008092
8093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8094 processKey(mapper, BTN_RIGHT, 1);
8095 processKey(mapper, BTN_MIDDLE, 1);
8096 processSync(mapper);
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8100 motionArgs.buttonState);
8101
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8103 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8104 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8105
8106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8107 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8108 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8109 motionArgs.buttonState);
8110
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 processKey(mapper, BTN_RIGHT, 0);
8112 processSync(mapper);
8113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008114 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008116
8117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008118 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120
8121 processKey(mapper, BTN_MIDDLE, 0);
8122 processSync(mapper);
8123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008126
8127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008128 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008129 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008130
8131 // press BTN_BACK, release BTN_BACK
8132 processKey(mapper, BTN_BACK, 1);
8133 processSync(mapper);
8134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8135 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8136 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008137
Michael Wrightd02c5b62014-02-10 15:10:22 -08008138 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008139 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8141
8142 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8143 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8144 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008145
8146 processKey(mapper, BTN_BACK, 0);
8147 processSync(mapper);
8148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008149 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008150 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008151
8152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008154 ASSERT_EQ(0, motionArgs.buttonState);
8155
Michael Wrightd02c5b62014-02-10 15:10:22 -08008156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8157 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8158 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8159
8160 // press BTN_SIDE, release BTN_SIDE
8161 processKey(mapper, BTN_SIDE, 1);
8162 processSync(mapper);
8163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8164 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8165 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008166
Michael Wrightd02c5b62014-02-10 15:10:22 -08008167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008168 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008169 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8170
8171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8172 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8173 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008174
8175 processKey(mapper, BTN_SIDE, 0);
8176 processSync(mapper);
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008178 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008179 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008180
8181 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008182 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008183 ASSERT_EQ(0, motionArgs.buttonState);
8184
Michael Wrightd02c5b62014-02-10 15:10:22 -08008185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8186 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8187 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8188
8189 // press BTN_FORWARD, release BTN_FORWARD
8190 processKey(mapper, BTN_FORWARD, 1);
8191 processSync(mapper);
8192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8193 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8194 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008195
Michael Wrightd02c5b62014-02-10 15:10:22 -08008196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008198 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8199
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8201 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8202 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008203
8204 processKey(mapper, BTN_FORWARD, 0);
8205 processSync(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008207 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008208 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209
8210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008211 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008212 ASSERT_EQ(0, motionArgs.buttonState);
8213
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8215 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8216 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8217
8218 // press BTN_EXTRA, release BTN_EXTRA
8219 processKey(mapper, BTN_EXTRA, 1);
8220 processSync(mapper);
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8222 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8223 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008224
Michael Wrightd02c5b62014-02-10 15:10:22 -08008225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008226 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008227 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8228
8229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8230 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8231 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008232
8233 processKey(mapper, BTN_EXTRA, 0);
8234 processSync(mapper);
8235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008236 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008237 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008238
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241 ASSERT_EQ(0, motionArgs.buttonState);
8242
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8244 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8245 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8246
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8248
Michael Wrightd02c5b62014-02-10 15:10:22 -08008249 // press BTN_STYLUS, release BTN_STYLUS
8250 processKey(mapper, BTN_STYLUS, 1);
8251 processSync(mapper);
8252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8253 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008254 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8255
8256 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8257 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8258 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008259
8260 processKey(mapper, BTN_STYLUS, 0);
8261 processSync(mapper);
8262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008263 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008264 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008265
8266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008267 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008268 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008269
8270 // press BTN_STYLUS2, release BTN_STYLUS2
8271 processKey(mapper, BTN_STYLUS2, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8274 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008275 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8276
8277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8279 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008280
8281 processKey(mapper, BTN_STYLUS2, 0);
8282 processSync(mapper);
8283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008284 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008285 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008286
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008289 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 // release touch
8292 processId(mapper, -1);
8293 processSync(mapper);
8294 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8295 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8296 ASSERT_EQ(0, motionArgs.buttonState);
8297}
8298
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008299TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8300 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008301 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008302 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008303 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008304
8305 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8306 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8307
8308 // Touch down.
8309 processId(mapper, 1);
8310 processPosition(mapper, 100, 200);
8311 processSync(mapper);
8312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8313 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8314
8315 // Press and release button mapped to the primary stylus button.
8316 processKey(mapper, BTN_A, 1);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8319 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8320 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8322 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8323 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8324
8325 processKey(mapper, BTN_A, 0);
8326 processSync(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8328 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8330 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8331
8332 // Press and release the HID usage mapped to the secondary stylus button.
8333 processHidUsage(mapper, 0xabcd, 1);
8334 processSync(mapper);
8335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8336 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8337 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8339 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8340 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8341
8342 processHidUsage(mapper, 0xabcd, 0);
8343 processSync(mapper);
8344 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8345 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8347 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8348
8349 // Release touch.
8350 processId(mapper, -1);
8351 processSync(mapper);
8352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8353 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8354}
8355
Michael Wrightd02c5b62014-02-10 15:10:22 -08008356TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008358 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008360 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008361
8362 NotifyMotionArgs motionArgs;
8363
8364 // default tool type is finger
8365 processId(mapper, 1);
8366 processPosition(mapper, 100, 200);
8367 processSync(mapper);
8368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8369 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008370 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008371
8372 // eraser
8373 processKey(mapper, BTN_TOOL_RUBBER, 1);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008377 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008378
8379 // stylus
8380 processKey(mapper, BTN_TOOL_RUBBER, 0);
8381 processKey(mapper, BTN_TOOL_PEN, 1);
8382 processSync(mapper);
8383 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8384 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008385 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008386
8387 // brush
8388 processKey(mapper, BTN_TOOL_PEN, 0);
8389 processKey(mapper, BTN_TOOL_BRUSH, 1);
8390 processSync(mapper);
8391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8392 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008393 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008394
8395 // pencil
8396 processKey(mapper, BTN_TOOL_BRUSH, 0);
8397 processKey(mapper, BTN_TOOL_PENCIL, 1);
8398 processSync(mapper);
8399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8400 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008401 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008402
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008403 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008404 processKey(mapper, BTN_TOOL_PENCIL, 0);
8405 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8406 processSync(mapper);
8407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8408 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008409 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008410
8411 // mouse
8412 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8413 processKey(mapper, BTN_TOOL_MOUSE, 1);
8414 processSync(mapper);
8415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008417 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008418
8419 // lens
8420 processKey(mapper, BTN_TOOL_MOUSE, 0);
8421 processKey(mapper, BTN_TOOL_LENS, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008426
8427 // double-tap
8428 processKey(mapper, BTN_TOOL_LENS, 0);
8429 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8430 processSync(mapper);
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8432 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008433 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434
8435 // triple-tap
8436 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8437 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8438 processSync(mapper);
8439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8440 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008441 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008442
8443 // quad-tap
8444 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8445 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8446 processSync(mapper);
8447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8448 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008449 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008450
8451 // finger
8452 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8453 processKey(mapper, BTN_TOOL_FINGER, 1);
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 // stylus trumps finger
8460 processKey(mapper, BTN_TOOL_PEN, 1);
8461 processSync(mapper);
8462 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8463 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008464 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008465
8466 // eraser trumps stylus
8467 processKey(mapper, BTN_TOOL_RUBBER, 1);
8468 processSync(mapper);
8469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8470 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008471 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008472
8473 // mouse trumps eraser
8474 processKey(mapper, BTN_TOOL_MOUSE, 1);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8477 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008478 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008479
8480 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8481 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8482 processSync(mapper);
8483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8484 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008485 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008486
8487 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8488 processToolType(mapper, MT_TOOL_PEN);
8489 processSync(mapper);
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008492 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008493
8494 // back to default tool type
8495 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8496 processKey(mapper, BTN_TOOL_MOUSE, 0);
8497 processKey(mapper, BTN_TOOL_RUBBER, 0);
8498 processKey(mapper, BTN_TOOL_PEN, 0);
8499 processKey(mapper, BTN_TOOL_FINGER, 0);
8500 processSync(mapper);
8501 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8502 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008503 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008504}
8505
8506TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008507 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008508 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008509 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008510 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008511 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008512
8513 NotifyMotionArgs motionArgs;
8514
8515 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8516 processId(mapper, 1);
8517 processPosition(mapper, 100, 200);
8518 processSync(mapper);
8519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8520 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8522 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8523
8524 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8525 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8526 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8527 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8528
8529 // move a little
8530 processPosition(mapper, 150, 250);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8533 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8534 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8535 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8536
8537 // down when BTN_TOUCH is pressed, pressure defaults to 1
8538 processKey(mapper, BTN_TOUCH, 1);
8539 processSync(mapper);
8540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8541 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8542 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8543 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8544
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8549
8550 // up when BTN_TOUCH is released, hover restored
8551 processKey(mapper, BTN_TOUCH, 0);
8552 processSync(mapper);
8553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8554 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8555 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8556 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8557
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8560 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8561 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8562
8563 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8564 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8565 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8566 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8567
8568 // exit hover when pointer goes away
8569 processId(mapper, -1);
8570 processSync(mapper);
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8575}
8576
8577TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008578 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008579 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008581 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008582
8583 NotifyMotionArgs motionArgs;
8584
8585 // initially hovering because pressure is 0
8586 processId(mapper, 1);
8587 processPosition(mapper, 100, 200);
8588 processPressure(mapper, 0);
8589 processSync(mapper);
8590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8591 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8592 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8593 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8594
8595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8596 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8597 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8598 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8599
8600 // move a little
8601 processPosition(mapper, 150, 250);
8602 processSync(mapper);
8603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8604 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8605 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8606 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8607
8608 // down when pressure becomes non-zero
8609 processPressure(mapper, RAW_PRESSURE_MAX);
8610 processSync(mapper);
8611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8612 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8614 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8615
8616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8617 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8619 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8620
8621 // up when pressure becomes 0, hover restored
8622 processPressure(mapper, 0);
8623 processSync(mapper);
8624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8625 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8627 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8628
8629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8630 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8632 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8633
8634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8635 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8636 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8637 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8638
8639 // exit hover when pointer goes away
8640 processId(mapper, -1);
8641 processSync(mapper);
8642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8643 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8644 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8645 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8646}
8647
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008648/**
8649 * Set the input device port <--> display port associations, and check that the
8650 * events are routed to the display that matches the display port.
8651 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8652 */
8653TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008654 const std::string usb2 = "USB2";
8655 const uint8_t hdmi1 = 0;
8656 const uint8_t hdmi2 = 1;
8657 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008658 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008659
8660 addConfigurationProperty("touch.deviceType", "touchScreen");
8661 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008662 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008663
8664 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8665 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8666
8667 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8668 // for this input device is specified, and the matching viewport is not present,
8669 // the input device should be disabled (at the mapper level).
8670
8671 // Add viewport for display 2 on hdmi2
8672 prepareSecondaryDisplay(type, hdmi2);
8673 // Send a touch event
8674 processPosition(mapper, 100, 100);
8675 processSync(mapper);
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8677
8678 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008679 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008680 // Send a touch event again
8681 processPosition(mapper, 100, 100);
8682 processSync(mapper);
8683
8684 NotifyMotionArgs args;
8685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8686 ASSERT_EQ(DISPLAY_ID, args.displayId);
8687}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008688
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008689TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8690 addConfigurationProperty("touch.deviceType", "touchScreen");
8691 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008692 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008693
8694 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8695
Michael Wrighta9cf4192022-12-01 23:46:39 +00008696 prepareDisplay(ui::ROTATION_0);
8697 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008698
8699 // Send a touch event
8700 processPosition(mapper, 100, 100);
8701 processSync(mapper);
8702
8703 NotifyMotionArgs args;
8704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8705 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8706}
8707
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008708TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -08008709 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +01008710 std::shared_ptr<FakePointerController> fakePointerController =
8711 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -08008712 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008713 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008714 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008715
Garfield Tan888a6a42020-01-09 11:39:16 -08008716 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008717 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008718
Michael Wrighta9cf4192022-12-01 23:46:39 +00008719 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008720 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008721 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008722
Josep del Río2d8c79a2023-01-23 19:33:50 +00008723 // Check source is mouse that would obtain the PointerController.
8724 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008725
8726 NotifyMotionArgs motionArgs;
8727 processPosition(mapper, 100, 100);
8728 processSync(mapper);
8729
8730 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8731 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8732 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
8733}
8734
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008735/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008736 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8737 */
8738TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8739 addConfigurationProperty("touch.deviceType", "touchScreen");
8740 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008741 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008742
Michael Wrighta9cf4192022-12-01 23:46:39 +00008743 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008744 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8745 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8746 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8747 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008748
8749 NotifyMotionArgs args;
8750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8751 ASSERT_EQ(26, args.readTime);
8752
Harry Cutts33476232023-01-30 19:57:29 +00008753 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8754 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8755 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008756
8757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8758 ASSERT_EQ(33, args.readTime);
8759}
8760
8761/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008762 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8763 * events should not be delivered to the listener.
8764 */
8765TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8766 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008767 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008768 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008769 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008770 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008771 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008772 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008773
8774 NotifyMotionArgs motionArgs;
8775 processPosition(mapper, 100, 100);
8776 processSync(mapper);
8777
8778 mFakeListener->assertNotifyMotionWasNotCalled();
8779}
8780
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008781/**
8782 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8783 * the touch mapper can process the events and the events can be delivered to the listener.
8784 */
8785TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8786 addConfigurationProperty("touch.deviceType", "touchScreen");
8787 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008788 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008789 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008790 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008791 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008792 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008793
8794 NotifyMotionArgs motionArgs;
8795 processPosition(mapper, 100, 100);
8796 processSync(mapper);
8797
8798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8799 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8800}
8801
Josh Thielene986aed2023-06-01 14:17:30 +00008802/**
8803 * When the viewport is deactivated (isActive transitions from true to false),
8804 * and touch.enableForInactiveViewport is false, touches prior to the transition
8805 * should be cancelled.
8806 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008807TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8808 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008809 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008810 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008811 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008812 std::optional<DisplayViewport> optionalDisplayViewport =
8813 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8814 ASSERT_TRUE(optionalDisplayViewport.has_value());
8815 DisplayViewport displayViewport = *optionalDisplayViewport;
8816
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008817 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008818 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008819 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008820
8821 // Finger down
8822 int32_t x = 100, y = 100;
8823 processPosition(mapper, x, y);
8824 processSync(mapper);
8825
8826 NotifyMotionArgs motionArgs;
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8828 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8829
8830 // Deactivate display viewport
8831 displayViewport.isActive = false;
8832 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008833 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008834
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008835 // The ongoing touch should be canceled immediately
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8837 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8838
8839 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008840 x += 10, y += 10;
8841 processPosition(mapper, x, y);
8842 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008844
8845 // Reactivate display viewport
8846 displayViewport.isActive = true;
8847 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008848 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008849
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008850 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008851 x += 10, y += 10;
8852 processPosition(mapper, x, y);
8853 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8855 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008856}
8857
Josh Thielene986aed2023-06-01 14:17:30 +00008858/**
8859 * When the viewport is deactivated (isActive transitions from true to false),
8860 * and touch.enableForInactiveViewport is true, touches prior to the transition
8861 * should not be cancelled.
8862 */
8863TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8864 addConfigurationProperty("touch.deviceType", "touchScreen");
8865 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8866 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8867 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8868 std::optional<DisplayViewport> optionalDisplayViewport =
8869 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8870 ASSERT_TRUE(optionalDisplayViewport.has_value());
8871 DisplayViewport displayViewport = *optionalDisplayViewport;
8872
8873 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8874 prepareAxes(POSITION);
8875 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8876
8877 // Finger down
8878 int32_t x = 100, y = 100;
8879 processPosition(mapper, x, y);
8880 processSync(mapper);
8881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8882 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8883
8884 // Deactivate display viewport
8885 displayViewport.isActive = false;
8886 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8887 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8888
8889 // The ongoing touch should not be canceled
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8891
8892 // Finger move is not ignored
8893 x += 10, y += 10;
8894 processPosition(mapper, x, y);
8895 processSync(mapper);
8896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8897 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8898
8899 // Reactivate display viewport
8900 displayViewport.isActive = true;
8901 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8902 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8903
8904 // Finger move continues and does not start new gesture
8905 x += 10, y += 10;
8906 processPosition(mapper, x, y);
8907 processSync(mapper);
8908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8909 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8910}
8911
Arthur Hung7c645402019-01-25 17:45:42 +08008912TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
8913 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +08008914 prepareAxes(POSITION | ID | SLOT);
8915 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008916 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +08008917
8918 // Create the second touch screen device, and enable multi fingers.
8919 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08008920 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08008921 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008922 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08008923 std::shared_ptr<InputDevice> device2 =
8924 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07008925 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08008926
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008927 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008928 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008929 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008930 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008931 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008932 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008933 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +00008934 /*flat=*/0, /*fuzz=*/0);
8935 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008936 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
8937 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +08008938
8939 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +00008940 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
8941 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
8942 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008943 std::list<NotifyArgs> unused =
8944 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008945 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008946 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +08008947
8948 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +01008949 std::shared_ptr<FakePointerController> fakePointerController =
8950 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00008951 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +08008952
8953 // Setup policy for associated displays and show touches.
8954 const uint8_t hdmi1 = 0;
8955 const uint8_t hdmi2 = 1;
8956 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8957 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
8958 mFakePolicy->setShowTouches(true);
8959
8960 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008961 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008962 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +08008963
8964 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008965 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008966 InputReaderConfiguration::Change::DISPLAY_INFO |
8967 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +08008968
8969 // Two fingers down at default display.
8970 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8971 processPosition(mapper, x1, y1);
8972 processId(mapper, 1);
8973 processSlot(mapper, 1);
8974 processPosition(mapper, x2, y2);
8975 processId(mapper, 2);
8976 processSync(mapper);
8977
8978 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
8979 fakePointerController->getSpots().find(DISPLAY_ID);
8980 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8981 ASSERT_EQ(size_t(2), iter->second.size());
8982
8983 // Two fingers down at second display.
8984 processPosition(mapper2, x1, y1);
8985 processId(mapper2, 1);
8986 processSlot(mapper2, 1);
8987 processPosition(mapper2, x2, y2);
8988 processId(mapper2, 2);
8989 processSync(mapper2);
8990
8991 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
8992 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
8993 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +00008994
8995 // Disable the show touches configuration and ensure the spots are cleared.
8996 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07008997 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008998 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +00008999
9000 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +08009001}
9002
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009003TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009004 prepareAxes(POSITION);
9005 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009006 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009007 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009008
9009 NotifyMotionArgs motionArgs;
9010 // Unrotated video frame
9011 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9012 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009013 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06009014 processPosition(mapper, 100, 200);
9015 processSync(mapper);
9016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9017 ASSERT_EQ(frames, motionArgs.videoFrames);
9018
9019 // Subsequent touch events should not have any videoframes
9020 // This is implemented separately in FakeEventHub,
9021 // but that should match the behaviour of TouchVideoDevice.
9022 processPosition(mapper, 200, 200);
9023 processSync(mapper);
9024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9025 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
9026}
9027
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009028TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009029 prepareAxes(POSITION);
9030 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009031 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009032 // Unrotated video frame
9033 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9034 NotifyMotionArgs motionArgs;
9035
9036 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009037 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009038 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9039 clearViewports();
9040 prepareDisplay(orientation);
9041 std::vector<TouchVideoFrame> frames{frame};
9042 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9043 processPosition(mapper, 100, 200);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9046 ASSERT_EQ(frames, motionArgs.videoFrames);
9047 }
9048}
9049
9050TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
9051 prepareAxes(POSITION);
9052 addConfigurationProperty("touch.deviceType", "touchScreen");
9053 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9054 // orientation-aware are affected by display rotation.
9055 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009056 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009057 // Unrotated video frame
9058 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9059 NotifyMotionArgs motionArgs;
9060
9061 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00009062 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009063 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
9064 clearViewports();
9065 prepareDisplay(orientation);
9066 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009067 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009068 processPosition(mapper, 100, 200);
9069 processSync(mapper);
9070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009071 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9072 // compared to the display. This is so that when the window transform (which contains the
9073 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9074 // window's coordinate space.
9075 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009076 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08009077
9078 // Release finger.
9079 processSync(mapper);
9080 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009081 }
9082}
9083
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009084TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009085 prepareAxes(POSITION);
9086 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00009087 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009088 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9089 // so mix these.
9090 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9091 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9092 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9093 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9094 NotifyMotionArgs motionArgs;
9095
Michael Wrighta9cf4192022-12-01 23:46:39 +00009096 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009097 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009098 processPosition(mapper, 100, 200);
9099 processSync(mapper);
9100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009101 ASSERT_EQ(frames, motionArgs.videoFrames);
9102}
9103
9104TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
9105 prepareAxes(POSITION);
9106 addConfigurationProperty("touch.deviceType", "touchScreen");
9107 // Since InputReader works in the un-rotated coordinate space, only devices that are not
9108 // orientation-aware are affected by display rotation.
9109 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00009110 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009111 // Unrotated video frames. There's no rule that they must all have the same dimensions,
9112 // so mix these.
9113 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
9114 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
9115 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
9116 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
9117 NotifyMotionArgs motionArgs;
9118
Michael Wrighta9cf4192022-12-01 23:46:39 +00009119 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009120 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
9121 processPosition(mapper, 100, 200);
9122 processSync(mapper);
9123 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9124 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
9125 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
9126 // compared to the display. This is so that when the window transform (which contains the
9127 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
9128 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00009129 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07009130 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06009131 ASSERT_EQ(frames, motionArgs.videoFrames);
9132}
9133
Arthur Hung9da14732019-09-02 16:16:58 +08009134/**
9135 * If we had defined port associations, but the viewport is not ready, the touch device would be
9136 * expected to be disabled, and it should be enabled after the viewport has found.
9137 */
9138TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08009139 constexpr uint8_t hdmi2 = 1;
9140 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009141 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08009142
9143 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
9144
9145 addConfigurationProperty("touch.deviceType", "touchScreen");
9146 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009147 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009148
9149 ASSERT_EQ(mDevice->isEnabled(), false);
9150
9151 // Add display on hdmi2, the device should be enabled and can receive touch event.
9152 prepareSecondaryDisplay(type, hdmi2);
9153 ASSERT_EQ(mDevice->isEnabled(), true);
9154
9155 // Send a touch event.
9156 processPosition(mapper, 100, 100);
9157 processSync(mapper);
9158
9159 NotifyMotionArgs args;
9160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9161 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9162}
9163
Arthur Hung421eb1c2020-01-16 00:09:42 +08009164TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009165 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009166 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009167 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009168 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009169
9170 NotifyMotionArgs motionArgs;
9171
9172 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9173 // finger down
9174 processId(mapper, 1);
9175 processPosition(mapper, x1, y1);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009179 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009180
9181 // finger move
9182 processId(mapper, 1);
9183 processPosition(mapper, x2, y2);
9184 processSync(mapper);
9185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9186 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009187 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009188
9189 // finger up.
9190 processId(mapper, -1);
9191 processSync(mapper);
9192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9193 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009194 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009195
9196 // new finger down
9197 processId(mapper, 1);
9198 processPosition(mapper, x3, y3);
9199 processSync(mapper);
9200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9201 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009202 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009203}
9204
9205/**
arthurhungcc7f9802020-04-30 17:55:40 +08009206 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9207 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009208 */
arthurhungcc7f9802020-04-30 17:55:40 +08009209TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009210 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009211 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009212 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009213 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009214
9215 NotifyMotionArgs motionArgs;
9216
9217 // default tool type is finger
9218 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009219 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009220 processPosition(mapper, x1, y1);
9221 processSync(mapper);
9222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009224 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009225
9226 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9227 processToolType(mapper, MT_TOOL_PALM);
9228 processSync(mapper);
9229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9230 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9231
9232 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009233 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009234 processPosition(mapper, x2, y2);
9235 processSync(mapper);
9236 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9237
9238 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009239 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009240 processSync(mapper);
9241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9242
9243 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009244 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009245 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009246 processPosition(mapper, x3, y3);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009250 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009251}
9252
arthurhungbf89a482020-04-17 17:37:55 +08009253/**
arthurhungcc7f9802020-04-30 17:55:40 +08009254 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9255 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009256 */
arthurhungcc7f9802020-04-30 17:55:40 +08009257TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009258 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009259 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009260 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009261 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009262
9263 NotifyMotionArgs motionArgs;
9264
9265 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009266 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9267 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009268 processPosition(mapper, x1, y1);
9269 processSync(mapper);
9270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9271 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009272 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009273
9274 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009275 processSlot(mapper, SECOND_SLOT);
9276 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009277 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009278 processSync(mapper);
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009280 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009281 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009282
9283 // If the tool type of the first finger changes to MT_TOOL_PALM,
9284 // we expect to receive ACTION_POINTER_UP with cancel flag.
9285 processSlot(mapper, FIRST_SLOT);
9286 processId(mapper, FIRST_TRACKING_ID);
9287 processToolType(mapper, MT_TOOL_PALM);
9288 processSync(mapper);
9289 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009290 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009291 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9292
9293 // The following MOVE events of second finger should be processed.
9294 processSlot(mapper, SECOND_SLOT);
9295 processId(mapper, SECOND_TRACKING_ID);
9296 processPosition(mapper, x2 + 1, y2 + 1);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009300 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009301
9302 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9303 // it. Second finger receive move.
9304 processSlot(mapper, FIRST_SLOT);
9305 processId(mapper, INVALID_TRACKING_ID);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009309 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009310
9311 // Second finger keeps moving.
9312 processSlot(mapper, SECOND_SLOT);
9313 processId(mapper, SECOND_TRACKING_ID);
9314 processPosition(mapper, x2 + 2, y2 + 2);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9317 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009318 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009319
9320 // Second finger up.
9321 processId(mapper, INVALID_TRACKING_ID);
9322 processSync(mapper);
9323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9324 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9325 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9326}
9327
9328/**
9329 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9330 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9331 */
9332TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9333 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009334 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009335 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009336 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009337
9338 NotifyMotionArgs motionArgs;
9339
9340 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9341 // First finger down.
9342 processId(mapper, FIRST_TRACKING_ID);
9343 processPosition(mapper, x1, y1);
9344 processSync(mapper);
9345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9346 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009347 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009348
9349 // Second finger down.
9350 processSlot(mapper, SECOND_SLOT);
9351 processId(mapper, SECOND_TRACKING_ID);
9352 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009353 processSync(mapper);
9354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009355 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009356 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009357
arthurhungcc7f9802020-04-30 17:55:40 +08009358 // If the tool type of the first finger changes to MT_TOOL_PALM,
9359 // we expect to receive ACTION_POINTER_UP with cancel flag.
9360 processSlot(mapper, FIRST_SLOT);
9361 processId(mapper, FIRST_TRACKING_ID);
9362 processToolType(mapper, MT_TOOL_PALM);
9363 processSync(mapper);
9364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009365 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009366 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9367
9368 // Second finger keeps moving.
9369 processSlot(mapper, SECOND_SLOT);
9370 processId(mapper, SECOND_TRACKING_ID);
9371 processPosition(mapper, x2 + 1, y2 + 1);
9372 processSync(mapper);
9373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9374 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9375
9376 // second finger becomes palm, receive cancel due to only 1 finger is active.
9377 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009378 processToolType(mapper, MT_TOOL_PALM);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9382
arthurhungcc7f9802020-04-30 17:55:40 +08009383 // third finger down.
9384 processSlot(mapper, THIRD_SLOT);
9385 processId(mapper, THIRD_TRACKING_ID);
9386 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009387 processPosition(mapper, x3, y3);
9388 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009391 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009392 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009393
9394 // third finger move
9395 processId(mapper, THIRD_TRACKING_ID);
9396 processPosition(mapper, x3 + 1, y3 + 1);
9397 processSync(mapper);
9398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9399 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9400
9401 // first finger up, third finger receive move.
9402 processSlot(mapper, FIRST_SLOT);
9403 processId(mapper, INVALID_TRACKING_ID);
9404 processSync(mapper);
9405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9406 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009407 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009408
9409 // second finger up, third finger receive move.
9410 processSlot(mapper, SECOND_SLOT);
9411 processId(mapper, INVALID_TRACKING_ID);
9412 processSync(mapper);
9413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9414 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009415 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009416
9417 // third finger up.
9418 processSlot(mapper, THIRD_SLOT);
9419 processId(mapper, INVALID_TRACKING_ID);
9420 processSync(mapper);
9421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9422 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9423 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9424}
9425
9426/**
9427 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9428 * and the active finger could still be allowed to receive the events
9429 */
9430TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9431 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009432 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009433 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009434 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009435
9436 NotifyMotionArgs motionArgs;
9437
9438 // default tool type is finger
9439 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9440 processId(mapper, FIRST_TRACKING_ID);
9441 processPosition(mapper, x1, y1);
9442 processSync(mapper);
9443 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9444 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009445 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009446
9447 // Second finger down.
9448 processSlot(mapper, SECOND_SLOT);
9449 processId(mapper, SECOND_TRACKING_ID);
9450 processPosition(mapper, x2, y2);
9451 processSync(mapper);
9452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009453 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009454 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009455
9456 // If the tool type of the second finger changes to MT_TOOL_PALM,
9457 // we expect to receive ACTION_POINTER_UP with cancel flag.
9458 processId(mapper, SECOND_TRACKING_ID);
9459 processToolType(mapper, MT_TOOL_PALM);
9460 processSync(mapper);
9461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009462 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009463 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9464
9465 // The following MOVE event should be processed.
9466 processSlot(mapper, FIRST_SLOT);
9467 processId(mapper, FIRST_TRACKING_ID);
9468 processPosition(mapper, x1 + 1, y1 + 1);
9469 processSync(mapper);
9470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009472 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009473
9474 // second finger up.
9475 processSlot(mapper, SECOND_SLOT);
9476 processId(mapper, INVALID_TRACKING_ID);
9477 processSync(mapper);
9478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9480
9481 // first finger keep moving
9482 processSlot(mapper, FIRST_SLOT);
9483 processId(mapper, FIRST_TRACKING_ID);
9484 processPosition(mapper, x1 + 2, y1 + 2);
9485 processSync(mapper);
9486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9487 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9488
9489 // first finger up.
9490 processId(mapper, INVALID_TRACKING_ID);
9491 processSync(mapper);
9492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9493 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9494 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009495}
9496
Arthur Hung9ad18942021-06-19 02:04:46 +00009497/**
9498 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9499 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9500 * cause slot be valid again.
9501 */
9502TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9503 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009504 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009505 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009506 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009507
9508 NotifyMotionArgs motionArgs;
9509
9510 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9511 // First finger down.
9512 processId(mapper, FIRST_TRACKING_ID);
9513 processPosition(mapper, x1, y1);
9514 processPressure(mapper, RAW_PRESSURE_MAX);
9515 processSync(mapper);
9516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9517 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009518 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009519
9520 // First finger move.
9521 processId(mapper, FIRST_TRACKING_ID);
9522 processPosition(mapper, x1 + 1, y1 + 1);
9523 processPressure(mapper, RAW_PRESSURE_MAX);
9524 processSync(mapper);
9525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9526 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009527 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009528
9529 // Second finger down.
9530 processSlot(mapper, SECOND_SLOT);
9531 processId(mapper, SECOND_TRACKING_ID);
9532 processPosition(mapper, x2, y2);
9533 processPressure(mapper, RAW_PRESSURE_MAX);
9534 processSync(mapper);
9535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009536 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009537 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009538
9539 // second finger up with some unexpected data.
9540 processSlot(mapper, SECOND_SLOT);
9541 processId(mapper, INVALID_TRACKING_ID);
9542 processPosition(mapper, x2, y2);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009545 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009546 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009547
9548 // first finger up with some unexpected data.
9549 processSlot(mapper, FIRST_SLOT);
9550 processId(mapper, INVALID_TRACKING_ID);
9551 processPosition(mapper, x2, y2);
9552 processPressure(mapper, RAW_PRESSURE_MAX);
9553 processSync(mapper);
9554 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9555 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009556 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009557}
9558
Arpit Singh4b4a4572023-11-24 18:19:56 +00009559TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009560 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009561 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009562 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009563 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009564
9565 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009566 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009567 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009568 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009569 processPressure(mapper, RAW_PRESSURE_MAX);
9570 processSync(mapper);
9571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9572 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9573
9574 // Second finger down.
9575 processSlot(mapper, SECOND_SLOT);
9576 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009577 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009578 processPressure(mapper, RAW_PRESSURE_MAX);
9579 processSync(mapper);
9580 ASSERT_NO_FATAL_FAILURE(
9581 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9582
Arpit Singh4b4a4572023-11-24 18:19:56 +00009583 // Set MT Slot state to be repopulated for the required slots
9584 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9585 mtSlotValues[0] = FIRST_TRACKING_ID;
9586 mtSlotValues[1] = SECOND_TRACKING_ID;
9587 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9588
9589 mtSlotValues[0] = x1;
9590 mtSlotValues[1] = x2;
9591 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9592
9593 mtSlotValues[0] = y1;
9594 mtSlotValues[1] = y2;
9595 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9596
9597 mtSlotValues[0] = RAW_PRESSURE_MAX;
9598 mtSlotValues[1] = RAW_PRESSURE_MAX;
9599 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9600
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009601 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009602 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009603 resetMapper(mapper, ARBITRARY_TIME);
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9605 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009606
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009607 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9608 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009609 processPosition(mapper, 301, 302);
9610 processSync(mapper);
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9612 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9614 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009615
9616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9617}
9618
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009619TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009620 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009621 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009622 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009623 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009624
9625 // First finger touches down and releases.
9626 processId(mapper, FIRST_TRACKING_ID);
9627 processPosition(mapper, 100, 200);
9628 processPressure(mapper, RAW_PRESSURE_MAX);
9629 processSync(mapper);
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9631 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9632 processId(mapper, INVALID_TRACKING_ID);
9633 processSync(mapper);
9634 ASSERT_NO_FATAL_FAILURE(
9635 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9636
9637 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9638 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009639 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9641
9642 // Send an empty sync frame. Since there are no pointers, no events are generated.
9643 processSync(mapper);
9644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9645}
9646
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009647TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009648 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009649 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009650 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009651 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009653
9654 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9655 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9656 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9657 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9658 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9659
9660 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009661 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009662 processId(mapper, FIRST_TRACKING_ID);
9663 processToolType(mapper, MT_TOOL_PEN);
9664 processPosition(mapper, 100, 200);
9665 processPressure(mapper, RAW_PRESSURE_MAX);
9666 processSync(mapper);
9667 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9668 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9669 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009670 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009671
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009672 // Now that we know the device supports styluses, ensure that the device is re-configured with
9673 // the stylus source.
9674 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9675 {
9676 const auto& devices = mReader->getInputDevices();
9677 auto deviceInfo =
9678 std::find_if(devices.begin(), devices.end(),
9679 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9680 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9681 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9682 }
9683
9684 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9685 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9686
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009687 processId(mapper, INVALID_TRACKING_ID);
9688 processSync(mapper);
9689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9690 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9691 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009692 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009693}
9694
Seunghwan Choi356026c2023-02-01 14:37:25 +09009695TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
9696 addConfigurationProperty("touch.deviceType", "touchScreen");
9697 prepareDisplay(ui::ROTATION_0);
9698 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9699 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9700 // indicate stylus presence dynamically.
9701 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9702 std::shared_ptr<FakePointerController> fakePointerController =
9703 std::make_shared<FakePointerController>();
9704 mFakePolicy->setPointerController(fakePointerController);
9705 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00009706 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009707
9708 processId(mapper, FIRST_TRACKING_ID);
9709 processPressure(mapper, RAW_PRESSURE_MIN);
9710 processPosition(mapper, 100, 200);
9711 processToolType(mapper, MT_TOOL_PEN);
9712 processSync(mapper);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9714 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009715 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009716 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9717 ASSERT_TRUE(fakePointerController->isPointerShown());
9718 ASSERT_NO_FATAL_FAILURE(
9719 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
9720}
9721
9722TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
9723 addConfigurationProperty("touch.deviceType", "touchScreen");
9724 prepareDisplay(ui::ROTATION_0);
9725 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
9726 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
9727 // indicate stylus presence dynamically.
9728 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
9729 std::shared_ptr<FakePointerController> fakePointerController =
9730 std::make_shared<FakePointerController>();
9731 mFakePolicy->setPointerController(fakePointerController);
9732 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00009733 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09009734
9735 processId(mapper, FIRST_TRACKING_ID);
9736 processPressure(mapper, RAW_PRESSURE_MIN);
9737 processPosition(mapper, 100, 200);
9738 processToolType(mapper, MT_TOOL_PEN);
9739 processSync(mapper);
9740 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9741 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009742 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09009743 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
9744 ASSERT_FALSE(fakePointerController->isPointerShown());
9745}
9746
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009747// --- MultiTouchInputMapperTest_ExternalDevice ---
9748
9749class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9750protected:
Chris Yea52ade12020-08-27 16:49:20 -07009751 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009752};
9753
9754/**
9755 * Expect fallback to internal viewport if device is external and external viewport is not present.
9756 */
9757TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9758 prepareAxes(POSITION);
9759 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009760 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009761 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009762
9763 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9764
9765 NotifyMotionArgs motionArgs;
9766
9767 // Expect the event to be sent to the internal viewport,
9768 // because an external viewport is not present.
9769 processPosition(mapper, 100, 100);
9770 processSync(mapper);
9771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9772 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
9773
9774 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009775 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009776 processPosition(mapper, 100, 100);
9777 processSync(mapper);
9778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9779 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9780}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009781
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009782TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
9783 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
9784 std::shared_ptr<FakePointerController> fakePointerController =
9785 std::make_shared<FakePointerController>();
9786 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9787 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009788
9789 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009790 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009791 prepareAxes(POSITION | ID | SLOT);
9792 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9793 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
9794 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009795 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009796 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009797
9798 // captured touchpad should be a touchpad source
9799 NotifyDeviceResetArgs resetArgs;
9800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
9801 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
9802
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00009803 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -07009804
9805 const InputDeviceInfo::MotionRange* relRangeX =
9806 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
9807 ASSERT_NE(relRangeX, nullptr);
9808 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
9809 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
9810 const InputDeviceInfo::MotionRange* relRangeY =
9811 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
9812 ASSERT_NE(relRangeY, nullptr);
9813 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
9814 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
9815
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009816 // run captured pointer tests - note that this is unscaled, so input listener events should be
9817 // identical to what the hardware sends (accounting for any
9818 // calibration).
9819 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -07009820 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009821 processId(mapper, 1);
9822 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
9823 processKey(mapper, BTN_TOUCH, 1);
9824 processSync(mapper);
9825
9826 // expect coord[0] to contain initial location of touch 0
9827 NotifyMotionArgs args;
9828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9829 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009830 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009831 ASSERT_EQ(0, args.pointerProperties[0].id);
9832 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
9833 ASSERT_NO_FATAL_FAILURE(
9834 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9835
9836 // FINGER 1 DOWN
9837 processSlot(mapper, 1);
9838 processId(mapper, 2);
9839 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
9840 processSync(mapper);
9841
9842 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009844 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009845 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009846 ASSERT_EQ(0, args.pointerProperties[0].id);
9847 ASSERT_EQ(1, args.pointerProperties[1].id);
9848 ASSERT_NO_FATAL_FAILURE(
9849 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9850 ASSERT_NO_FATAL_FAILURE(
9851 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
9852
9853 // FINGER 1 MOVE
9854 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
9855 processSync(mapper);
9856
9857 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
9858 // from move
9859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9860 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9861 ASSERT_NO_FATAL_FAILURE(
9862 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
9863 ASSERT_NO_FATAL_FAILURE(
9864 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9865
9866 // FINGER 0 MOVE
9867 processSlot(mapper, 0);
9868 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
9869 processSync(mapper);
9870
9871 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
9872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9873 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9874 ASSERT_NO_FATAL_FAILURE(
9875 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
9876 ASSERT_NO_FATAL_FAILURE(
9877 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
9878
9879 // BUTTON DOWN
9880 processKey(mapper, BTN_LEFT, 1);
9881 processSync(mapper);
9882
9883 // touchinputmapper design sends a move before button press
9884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9885 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9887 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9888
9889 // BUTTON UP
9890 processKey(mapper, BTN_LEFT, 0);
9891 processSync(mapper);
9892
9893 // touchinputmapper design sends a move after button release
9894 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9895 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9896 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9897 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
9898
9899 // FINGER 0 UP
9900 processId(mapper, -1);
9901 processSync(mapper);
9902 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9903 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
9904
9905 // FINGER 1 MOVE
9906 processSlot(mapper, 1);
9907 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
9908 processSync(mapper);
9909
9910 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
9911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9912 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009913 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009914 ASSERT_EQ(1, args.pointerProperties[0].id);
9915 ASSERT_NO_FATAL_FAILURE(
9916 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
9917
9918 // FINGER 1 UP
9919 processId(mapper, -1);
9920 processKey(mapper, BTN_TOUCH, 0);
9921 processSync(mapper);
9922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
9924
Josep del Río2d8c79a2023-01-23 19:33:50 +00009925 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009926 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009927 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +00009929 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009930}
9931
9932TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
9933 std::shared_ptr<FakePointerController> fakePointerController =
9934 std::make_shared<FakePointerController>();
9935 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
9936 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009937
9938 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +00009939 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009940 prepareAxes(POSITION | ID | SLOT);
9941 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9942 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00009943 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +00009944 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009945 // run uncaptured pointer tests - pushes out generic events
9946 // FINGER 0 DOWN
9947 processId(mapper, 3);
9948 processPosition(mapper, 100, 100);
9949 processKey(mapper, BTN_TOUCH, 1);
9950 processSync(mapper);
9951
9952 // start at (100,100), cursor should be at (0,0) * scale
9953 NotifyMotionArgs args;
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9956 ASSERT_NO_FATAL_FAILURE(
9957 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9958
9959 // FINGER 0 MOVE
9960 processPosition(mapper, 200, 200);
9961 processSync(mapper);
9962
9963 // compute scaling to help with touch position checking
9964 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9965 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9966 float scale =
9967 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9968
9969 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9973 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009974
9975 // BUTTON DOWN
9976 processKey(mapper, BTN_LEFT, 1);
9977 processSync(mapper);
9978
9979 // touchinputmapper design sends a move before button press
9980 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9981 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9983 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9984
9985 // BUTTON UP
9986 processKey(mapper, BTN_LEFT, 0);
9987 processSync(mapper);
9988
9989 // touchinputmapper design sends a move after button release
9990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9991 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9993 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009994}
9995
9996TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
9997 std::shared_ptr<FakePointerController> fakePointerController =
9998 std::make_shared<FakePointerController>();
9999
Michael Wrighta9cf4192022-12-01 23:46:39 +000010000 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010001 prepareAxes(POSITION | ID | SLOT);
10002 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010003 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010004 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000010005 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010006
Josep del Río2d8c79a2023-01-23 19:33:50 +000010007 // uncaptured touchpad should be a pointer device
10008 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010009
Josep del Río2d8c79a2023-01-23 19:33:50 +000010010 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010011 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010012 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080010013 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
10014}
10015
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010016// --- BluetoothMultiTouchInputMapperTest ---
10017
10018class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
10019protected:
10020 void SetUp() override {
10021 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
10022 }
10023};
10024
10025TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
10026 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010027 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010028 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010029 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000010030
10031 nsecs_t kernelEventTime = ARBITRARY_TIME;
10032 nsecs_t expectedEventTime = ARBITRARY_TIME;
10033 // Touch down.
10034 processId(mapper, FIRST_TRACKING_ID);
10035 processPosition(mapper, 100, 200);
10036 processPressure(mapper, RAW_PRESSURE_MAX);
10037 processSync(mapper, ARBITRARY_TIME);
10038 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10039 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
10040
10041 // Process several events that come in quick succession, according to their timestamps.
10042 for (int i = 0; i < 3; i++) {
10043 constexpr static nsecs_t delta = ms2ns(1);
10044 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
10045 kernelEventTime += delta;
10046 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
10047
10048 processPosition(mapper, 101 + i, 201 + i);
10049 processSync(mapper, kernelEventTime);
10050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10052 WithEventTime(expectedEventTime))));
10053 }
10054
10055 // Release the touch.
10056 processId(mapper, INVALID_TRACKING_ID);
10057 processPressure(mapper, RAW_PRESSURE_MIN);
10058 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10060 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
10061 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
10062}
10063
10064// --- MultiTouchPointerModeTest ---
10065
HQ Liue6983c72022-04-19 22:14:56 +000010066class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
10067protected:
10068 float mPointerMovementScale;
10069 float mPointerXZoomScale;
10070 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
10071 addConfigurationProperty("touch.deviceType", "pointer");
10072 std::shared_ptr<FakePointerController> fakePointerController =
10073 std::make_shared<FakePointerController>();
10074 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
10075 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000010076 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000010077
10078 prepareAxes(POSITION);
10079 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
10080 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
10081 // needs to be disabled, and the pointer gesture needs to be enabled.
10082 mFakePolicy->setPointerCapture(false);
10083 mFakePolicy->setPointerGestureEnabled(true);
10084 mFakePolicy->setPointerController(fakePointerController);
10085
10086 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
10087 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
10088 mPointerMovementScale =
10089 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
10090 mPointerXZoomScale =
10091 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
10092 }
10093
10094 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
10095 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
10096 /*flat*/ 0,
10097 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
10098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
10099 /*flat*/ 0,
10100 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
10101 }
10102};
10103
10104/**
10105 * Two fingers down on a pointer mode touch pad. The width
10106 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
10107 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
10108 * be greater than the both value to be freeform gesture, so that after two
10109 * fingers start to move downwards, the gesture should be swipe.
10110 */
10111TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
10112 // The min freeform gesture width is 25units/mm x 30mm = 750
10113 // which is greater than fraction of the diagnal length of the touchpad (349).
10114 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000010115 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010116 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010117 NotifyMotionArgs motionArgs;
10118
10119 // Two fingers down at once.
10120 // The two fingers are 450 units apart, expects the current gesture to be PRESS
10121 // Pointer's initial position is used the [0,0] coordinate.
10122 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10123
10124 processId(mapper, FIRST_TRACKING_ID);
10125 processPosition(mapper, x1, y1);
10126 processMTSync(mapper);
10127 processId(mapper, SECOND_TRACKING_ID);
10128 processPosition(mapper, x2, y2);
10129 processMTSync(mapper);
10130 processSync(mapper);
10131
10132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010133 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010134 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010135 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010136 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010137 ASSERT_NO_FATAL_FAILURE(
10138 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10139
10140 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10141 // that there should be 1 pointer.
10142 int32_t movingDistance = 200;
10143 y1 += movingDistance;
10144 y2 += movingDistance;
10145
10146 processId(mapper, FIRST_TRACKING_ID);
10147 processPosition(mapper, x1, y1);
10148 processMTSync(mapper);
10149 processId(mapper, SECOND_TRACKING_ID);
10150 processPosition(mapper, x2, y2);
10151 processMTSync(mapper);
10152 processSync(mapper);
10153
10154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010155 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010156 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010157 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010158 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010159 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10160 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10161 0, 0, 0, 0));
10162}
10163
10164/**
10165 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
10166 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
10167 * the touch pack diagnal length. Two fingers' distance must be greater than the both
10168 * value to be freeform gesture, so that after two fingers start to move downwards,
10169 * the gesture should be swipe.
10170 */
10171TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
10172 // The min freeform gesture width is 5units/mm x 30mm = 150
10173 // which is greater than fraction of the diagnal length of the touchpad (349).
10174 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000010175 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000010176 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010177 NotifyMotionArgs motionArgs;
10178
10179 // Two fingers down at once.
10180 // The two fingers are 250 units apart, expects the current gesture to be PRESS
10181 // Pointer's initial position is used the [0,0] coordinate.
10182 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
10183
10184 processId(mapper, FIRST_TRACKING_ID);
10185 processPosition(mapper, x1, y1);
10186 processMTSync(mapper);
10187 processId(mapper, SECOND_TRACKING_ID);
10188 processPosition(mapper, x2, y2);
10189 processMTSync(mapper);
10190 processSync(mapper);
10191
10192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010193 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010194 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010195 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010196 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010197 ASSERT_NO_FATAL_FAILURE(
10198 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10199
10200 // It should be recognized as a SWIPE gesture when two fingers start to move down,
10201 // and there should be 1 pointer.
10202 int32_t movingDistance = 200;
10203 y1 += movingDistance;
10204 y2 += movingDistance;
10205
10206 processId(mapper, FIRST_TRACKING_ID);
10207 processPosition(mapper, x1, y1);
10208 processMTSync(mapper);
10209 processId(mapper, SECOND_TRACKING_ID);
10210 processPosition(mapper, x2, y2);
10211 processMTSync(mapper);
10212 processSync(mapper);
10213
10214 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010215 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010217 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010218 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010219 // New coordinate is the scaled relative coordinate from the initial coordinate.
10220 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
10221 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10222 0, 0, 0, 0));
10223}
10224
10225/**
10226 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
10227 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
10228 * freeform gestures after two fingers start to move downwards.
10229 */
10230TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000010231 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010232 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000010233
10234 NotifyMotionArgs motionArgs;
10235
10236 // Two fingers down at once. Wider than the max swipe width.
10237 // The gesture is expected to be PRESS, then transformed to FREEFORM
10238 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
10239
10240 processId(mapper, FIRST_TRACKING_ID);
10241 processPosition(mapper, x1, y1);
10242 processMTSync(mapper);
10243 processId(mapper, SECOND_TRACKING_ID);
10244 processPosition(mapper, x2, y2);
10245 processMTSync(mapper);
10246 processSync(mapper);
10247
10248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010249 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010250 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010252 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010253 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
10254 ASSERT_NO_FATAL_FAILURE(
10255 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
10256
10257 int32_t movingDistance = 200;
10258
10259 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
10260 // then two down events for two pointers.
10261 y1 += movingDistance;
10262 y2 += movingDistance;
10263
10264 processId(mapper, FIRST_TRACKING_ID);
10265 processPosition(mapper, x1, y1);
10266 processMTSync(mapper);
10267 processId(mapper, SECOND_TRACKING_ID);
10268 processPosition(mapper, x2, y2);
10269 processMTSync(mapper);
10270 processSync(mapper);
10271
10272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10273 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010274 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010275 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010277 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010278 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010279 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10280 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010281 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010282 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010283 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010284 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010285 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010286 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010287 // Two pointers' scaled relative coordinates from their initial centroid.
10288 // Initial y coordinates are 0 as y1 and y2 have the same value.
10289 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
10290 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
10291 // When pointers move, the new coordinates equal to the initial coordinates plus
10292 // scaled moving distance.
10293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10294 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10295 0, 0, 0, 0));
10296 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10297 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
10298 0, 0, 0, 0));
10299
10300 // Move two fingers down again, expect one MOVE motion event.
10301 y1 += movingDistance;
10302 y2 += movingDistance;
10303
10304 processId(mapper, FIRST_TRACKING_ID);
10305 processPosition(mapper, x1, y1);
10306 processMTSync(mapper);
10307 processId(mapper, SECOND_TRACKING_ID);
10308 processPosition(mapper, x2, y2);
10309 processMTSync(mapper);
10310 processSync(mapper);
10311
10312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010313 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000010314 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010315 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000010316 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000010317 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
10318 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10319 0, 0, 0, 0, 0));
10320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
10321 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
10322 0, 0, 0, 0, 0));
10323}
10324
Harry Cutts39b7ca22022-10-05 15:55:48 +000010325TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000010326 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000010327 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000010328 NotifyMotionArgs motionArgs;
10329
10330 // Place two fingers down.
10331 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
10332
10333 processId(mapper, FIRST_TRACKING_ID);
10334 processPosition(mapper, x1, y1);
10335 processMTSync(mapper);
10336 processId(mapper, SECOND_TRACKING_ID);
10337 processPosition(mapper, x2, y2);
10338 processMTSync(mapper);
10339 processSync(mapper);
10340
10341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010342 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010343 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10344 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
10345 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
10346 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
10347
10348 // Move the two fingers down and to the left.
10349 int32_t movingDistance = 200;
10350 x1 -= movingDistance;
10351 y1 += movingDistance;
10352 x2 -= movingDistance;
10353 y2 += movingDistance;
10354
10355 processId(mapper, FIRST_TRACKING_ID);
10356 processPosition(mapper, x1, y1);
10357 processMTSync(mapper);
10358 processId(mapper, SECOND_TRACKING_ID);
10359 processPosition(mapper, x2, y2);
10360 processMTSync(mapper);
10361 processSync(mapper);
10362
10363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010364 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010365 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10366 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10367 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10368 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10369}
10370
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010371TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010372 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010373 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010374 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10376
10377 // Start a stylus gesture.
10378 processKey(mapper, BTN_TOOL_PEN, 1);
10379 processId(mapper, FIRST_TRACKING_ID);
10380 processPosition(mapper, 100, 200);
10381 processSync(mapper);
10382 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10384 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010385 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010386 // TODO(b/257078296): Pointer mode generates extra event.
10387 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10388 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10389 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010390 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10392
10393 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10394 // gesture should be disabled.
10395 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10396 viewport->isActive = false;
10397 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010398 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10400 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10401 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010402 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010403 // TODO(b/257078296): Pointer mode generates extra event.
10404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10405 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10406 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010407 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10409}
10410
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010411// --- JoystickInputMapperTest ---
10412
10413class JoystickInputMapperTest : public InputMapperTest {
10414protected:
10415 static const int32_t RAW_X_MIN;
10416 static const int32_t RAW_X_MAX;
10417 static const int32_t RAW_Y_MIN;
10418 static const int32_t RAW_Y_MAX;
10419
10420 void SetUp() override {
10421 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10422 }
10423 void prepareAxes() {
10424 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10426 }
10427
10428 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10430 }
10431
10432 void processSync(JoystickInputMapper& mapper) {
10433 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10434 }
10435
Michael Wrighta9cf4192022-12-01 23:46:39 +000010436 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010437 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10438 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10439 NO_PORT, ViewportType::VIRTUAL);
10440 }
10441};
10442
10443const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10444const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10445const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10446const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10447
10448TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10449 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010450 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010451
10452 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10453
Michael Wrighta9cf4192022-12-01 23:46:39 +000010454 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010455
10456 // Send an axis event
10457 processAxis(mapper, ABS_X, 100);
10458 processSync(mapper);
10459
10460 NotifyMotionArgs args;
10461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10462 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10463
10464 // Send another axis event
10465 processAxis(mapper, ABS_Y, 100);
10466 processSync(mapper);
10467
10468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10469 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10470}
10471
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010472// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010473
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010474class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010475protected:
10476 static const char* DEVICE_NAME;
10477 static const char* DEVICE_LOCATION;
10478 static const int32_t DEVICE_ID;
10479 static const int32_t DEVICE_GENERATION;
10480 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010481 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010482 static const int32_t EVENTHUB_ID;
10483
10484 std::shared_ptr<FakeEventHub> mFakeEventHub;
10485 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010486 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010487 std::unique_ptr<InstrumentedInputReader> mReader;
10488 std::shared_ptr<InputDevice> mDevice;
10489
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010490 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010491 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010492 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010493 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010494 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010495 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010496 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10497 }
10498
10499 void SetUp() override { SetUp(DEVICE_CLASSES); }
10500
10501 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010502 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010503 mFakePolicy.clear();
10504 }
10505
Chris Yee2b1e5c2021-03-10 22:45:12 -080010506 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10507 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010508 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010509 InputDeviceIdentifier identifier;
10510 identifier.name = name;
10511 identifier.location = location;
10512 std::shared_ptr<InputDevice> device =
10513 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10514 identifier);
10515 mReader->pushNextDevice(device);
10516 mFakeEventHub->addDevice(eventHubId, name, classes);
10517 mReader->loopOnce();
10518 return device;
10519 }
10520
10521 template <class T, typename... Args>
10522 T& addControllerAndConfigure(Args... args) {
10523 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10524
10525 return controller;
10526 }
10527};
10528
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010529const char* PeripheralControllerTest::DEVICE_NAME = "device";
10530const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10531const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10532const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10533const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010534const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10535 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010536const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010537
10538// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010539class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010540protected:
10541 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010542 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010543 }
10544};
10545
10546TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010547 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010548
Harry Cuttsa5b71292022-11-28 12:56:17 +000010549 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10550 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10551 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010552}
10553
10554TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010555 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556
Harry Cuttsa5b71292022-11-28 12:56:17 +000010557 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10558 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10559 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010560}
10561
10562// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010563class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010564protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010565 void SetUp() override {
10566 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10567 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010568};
10569
Chris Ye85758332021-05-16 23:05:17 -070010570TEST_F(LightControllerTest, MonoLight) {
10571 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010572 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010573 .maxBrightness = 255,
10574 .flags = InputLightClass::BRIGHTNESS,
10575 .path = ""};
10576 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010577
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010578 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010579 InputDeviceInfo info;
10580 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010581 std::vector<InputDeviceLightInfo> lights = info.getLights();
10582 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010583 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10584 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10585
10586 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10587 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10588}
10589
10590TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10591 RawLightInfo infoMono = {.id = 1,
10592 .name = "mono_keyboard_backlight",
10593 .maxBrightness = 255,
10594 .flags = InputLightClass::BRIGHTNESS |
10595 InputLightClass::KEYBOARD_BACKLIGHT,
10596 .path = ""};
10597 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10598
10599 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10600 InputDeviceInfo info;
10601 controller.populateDeviceInfo(&info);
10602 std::vector<InputDeviceLightInfo> lights = info.getLights();
10603 ASSERT_EQ(1U, lights.size());
10604 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10605 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010606
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010607 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10608 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010609}
10610
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010611TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10612 RawLightInfo infoMono = {.id = 1,
10613 .name = "mono_light",
10614 .maxBrightness = 255,
10615 .flags = InputLightClass::BRIGHTNESS,
10616 .path = ""};
10617 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10618 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10619 "0,100,200");
10620
10621 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10622 std::list<NotifyArgs> unused =
10623 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10624 /*changes=*/{});
10625
10626 InputDeviceInfo info;
10627 controller.populateDeviceInfo(&info);
10628 std::vector<InputDeviceLightInfo> lights = info.getLights();
10629 ASSERT_EQ(1U, lights.size());
10630 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10631}
10632
10633TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10634 RawLightInfo infoMono = {.id = 1,
10635 .name = "mono_keyboard_backlight",
10636 .maxBrightness = 255,
10637 .flags = InputLightClass::BRIGHTNESS |
10638 InputLightClass::KEYBOARD_BACKLIGHT,
10639 .path = ""};
10640 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10641
10642 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10643 std::list<NotifyArgs> unused =
10644 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10645 /*changes=*/{});
10646
10647 InputDeviceInfo info;
10648 controller.populateDeviceInfo(&info);
10649 std::vector<InputDeviceLightInfo> lights = info.getLights();
10650 ASSERT_EQ(1U, lights.size());
10651 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10652}
10653
10654TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10655 RawLightInfo infoMono = {.id = 1,
10656 .name = "mono_keyboard_backlight",
10657 .maxBrightness = 255,
10658 .flags = InputLightClass::BRIGHTNESS |
10659 InputLightClass::KEYBOARD_BACKLIGHT,
10660 .path = ""};
10661 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10662 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10663 "0,100,200");
10664
10665 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10666 std::list<NotifyArgs> unused =
10667 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10668 /*changes=*/{});
10669
10670 InputDeviceInfo info;
10671 controller.populateDeviceInfo(&info);
10672 std::vector<InputDeviceLightInfo> lights = info.getLights();
10673 ASSERT_EQ(1U, lights.size());
10674 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10675 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10676 ASSERT_EQ(BrightnessLevel(0), *it);
10677 std::advance(it, 1);
10678 ASSERT_EQ(BrightnessLevel(100), *it);
10679 std::advance(it, 1);
10680 ASSERT_EQ(BrightnessLevel(200), *it);
10681}
10682
10683TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10684 RawLightInfo infoMono = {.id = 1,
10685 .name = "mono_keyboard_backlight",
10686 .maxBrightness = 255,
10687 .flags = InputLightClass::BRIGHTNESS |
10688 InputLightClass::KEYBOARD_BACKLIGHT,
10689 .path = ""};
10690 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10691 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10692 "0,100,200,300,400,500");
10693
10694 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10695 std::list<NotifyArgs> unused =
10696 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10697 /*changes=*/{});
10698
10699 InputDeviceInfo info;
10700 controller.populateDeviceInfo(&info);
10701 std::vector<InputDeviceLightInfo> lights = info.getLights();
10702 ASSERT_EQ(1U, lights.size());
10703 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10704}
10705
Chris Yee2b1e5c2021-03-10 22:45:12 -080010706TEST_F(LightControllerTest, RGBLight) {
10707 RawLightInfo infoRed = {.id = 1,
10708 .name = "red",
10709 .maxBrightness = 255,
10710 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10711 .path = ""};
10712 RawLightInfo infoGreen = {.id = 2,
10713 .name = "green",
10714 .maxBrightness = 255,
10715 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10716 .path = ""};
10717 RawLightInfo infoBlue = {.id = 3,
10718 .name = "blue",
10719 .maxBrightness = 255,
10720 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10721 .path = ""};
10722 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10723 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10724 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10725
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010726 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010727 InputDeviceInfo info;
10728 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010729 std::vector<InputDeviceLightInfo> lights = info.getLights();
10730 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010731 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10732 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10733 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10734
10735 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10736 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10737}
10738
10739TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10740 RawLightInfo infoRed = {.id = 1,
10741 .name = "red_keyboard_backlight",
10742 .maxBrightness = 255,
10743 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10744 InputLightClass::KEYBOARD_BACKLIGHT,
10745 .path = ""};
10746 RawLightInfo infoGreen = {.id = 2,
10747 .name = "green_keyboard_backlight",
10748 .maxBrightness = 255,
10749 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10750 InputLightClass::KEYBOARD_BACKLIGHT,
10751 .path = ""};
10752 RawLightInfo infoBlue = {.id = 3,
10753 .name = "blue_keyboard_backlight",
10754 .maxBrightness = 255,
10755 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10756 InputLightClass::KEYBOARD_BACKLIGHT,
10757 .path = ""};
10758 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10759 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10760 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10761
10762 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10763 InputDeviceInfo info;
10764 controller.populateDeviceInfo(&info);
10765 std::vector<InputDeviceLightInfo> lights = info.getLights();
10766 ASSERT_EQ(1U, lights.size());
10767 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10768 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10769 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10770
10771 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10772 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10773}
10774
10775TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10776 RawLightInfo infoRed = {.id = 1,
10777 .name = "red",
10778 .maxBrightness = 255,
10779 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10780 .path = ""};
10781 RawLightInfo infoGreen = {.id = 2,
10782 .name = "green",
10783 .maxBrightness = 255,
10784 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10785 .path = ""};
10786 RawLightInfo infoBlue = {.id = 3,
10787 .name = "blue",
10788 .maxBrightness = 255,
10789 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10790 .path = ""};
10791 RawLightInfo infoGlobal = {.id = 3,
10792 .name = "global_keyboard_backlight",
10793 .maxBrightness = 255,
10794 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10795 InputLightClass::KEYBOARD_BACKLIGHT,
10796 .path = ""};
10797 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10798 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10799 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10800 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10801
10802 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10803 InputDeviceInfo info;
10804 controller.populateDeviceInfo(&info);
10805 std::vector<InputDeviceLightInfo> lights = info.getLights();
10806 ASSERT_EQ(1U, lights.size());
10807 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10808 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10809 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010810
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010811 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10812 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010813}
10814
10815TEST_F(LightControllerTest, MultiColorRGBLight) {
10816 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010817 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010818 .maxBrightness = 255,
10819 .flags = InputLightClass::BRIGHTNESS |
10820 InputLightClass::MULTI_INTENSITY |
10821 InputLightClass::MULTI_INDEX,
10822 .path = ""};
10823
10824 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10825
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010826 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010827 InputDeviceInfo info;
10828 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010829 std::vector<InputDeviceLightInfo> lights = info.getLights();
10830 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010831 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10832 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10833 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10834
10835 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10836 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10837}
10838
10839TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10840 RawLightInfo infoColor = {.id = 1,
10841 .name = "multi_color_keyboard_backlight",
10842 .maxBrightness = 255,
10843 .flags = InputLightClass::BRIGHTNESS |
10844 InputLightClass::MULTI_INTENSITY |
10845 InputLightClass::MULTI_INDEX |
10846 InputLightClass::KEYBOARD_BACKLIGHT,
10847 .path = ""};
10848
10849 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10850
10851 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10852 InputDeviceInfo info;
10853 controller.populateDeviceInfo(&info);
10854 std::vector<InputDeviceLightInfo> lights = info.getLights();
10855 ASSERT_EQ(1U, lights.size());
10856 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10857 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10858 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010859
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010860 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10861 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010862}
10863
10864TEST_F(LightControllerTest, PlayerIdLight) {
10865 RawLightInfo info1 = {.id = 1,
10866 .name = "player1",
10867 .maxBrightness = 255,
10868 .flags = InputLightClass::BRIGHTNESS,
10869 .path = ""};
10870 RawLightInfo info2 = {.id = 2,
10871 .name = "player2",
10872 .maxBrightness = 255,
10873 .flags = InputLightClass::BRIGHTNESS,
10874 .path = ""};
10875 RawLightInfo info3 = {.id = 3,
10876 .name = "player3",
10877 .maxBrightness = 255,
10878 .flags = InputLightClass::BRIGHTNESS,
10879 .path = ""};
10880 RawLightInfo info4 = {.id = 4,
10881 .name = "player4",
10882 .maxBrightness = 255,
10883 .flags = InputLightClass::BRIGHTNESS,
10884 .path = ""};
10885 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10886 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10887 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10888 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10889
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010890 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010891 InputDeviceInfo info;
10892 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010893 std::vector<InputDeviceLightInfo> lights = info.getLights();
10894 ASSERT_EQ(1U, lights.size());
10895 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010896 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10897 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010898
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010899 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10900 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10901 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010902}
10903
Michael Wrightd02c5b62014-02-10 15:10:22 -080010904} // namespace android