blob: fcc52a8f31ccc8be11f9639b31f9a9e71e2b6776 [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>
Michael Wrighta9cf4192022-12-01 23:46:39 +000043#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080044
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070045#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000046#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000047#include "FakeInputReaderPolicy.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000048#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000049#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000050#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000051#include "input/DisplayViewport.h"
52#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010053
Michael Wrightd02c5b62014-02-10 15:10:22 -080054namespace android {
55
Dominik Laskowski2f01d772022-03-23 16:01:29 -070056using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000057using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070058using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080059using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060
Michael Wrightd02c5b62014-02-10 15:10:22 -080061// Arbitrary display properties.
Linnan Li13bf76a2024-05-05 19:18:02 +080062static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::ADISPLAY_ID_DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000063static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080064static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
65 ui::LogicalDisplayId{DISPLAY_ID.val() + 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;
Linnan Li13bf76a2024-05-05 19:18:02 +080069static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080070static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +0000100
Byoungho Jungda10dd32023-10-06 17:03:45 +0900101namespace input_flags = com::android::input::flags;
102
Michael Wrightd02c5b62014-02-10 15:10:22 -0800103template<typename T>
104static inline T min(T a, T b) {
105 return a < b ? a : b;
106}
107
108static inline float avg(float x, float y) {
109 return (x + y) / 2;
110}
111
Chris Ye3fdbfef2021-01-06 18:45:18 -0800112// Mapping for light color name and the light color
113const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
114 {"green", LightColor::GREEN},
115 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800116
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700118 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119 case ui::ROTATION_90:
120 return ui::ROTATION_270;
121 case ui::ROTATION_270:
122 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700123 default:
124 return orientation;
125 }
126}
127
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800128static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
129 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000130 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800131
132 const InputDeviceInfo::MotionRange* motionRange =
133 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
134 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
135}
136
137static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
138 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000139 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800140
141 const InputDeviceInfo::MotionRange* motionRange =
142 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
143 ASSERT_EQ(nullptr, motionRange);
144}
145
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700146[[maybe_unused]] static void dumpReader(InputReader& reader) {
147 std::string dump;
148 reader.dump(dump);
149 std::istringstream iss(dump);
150 for (std::string line; std::getline(iss, line);) {
151 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800152 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700153 }
154}
155
Michael Wrightd02c5b62014-02-10 15:10:22 -0800156// --- FakeInputMapper ---
157
158class FakeInputMapper : public InputMapper {
159 uint32_t mSources;
160 int32_t mKeyboardType;
161 int32_t mMetaState;
162 KeyedVector<int32_t, int32_t> mKeyCodeStates;
163 KeyedVector<int32_t, int32_t> mScanCodeStates;
164 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100165 // fake mapping which would normally come from keyCharacterMap
166 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800167 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700168 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800169
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700170 std::mutex mLock;
171 std::condition_variable mStateChangedCondition;
172 bool mConfigureWasCalled GUARDED_BY(mLock);
173 bool mResetWasCalled GUARDED_BY(mLock);
174 bool mProcessWasCalled GUARDED_BY(mLock);
175 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176
Arthur Hungc23540e2018-11-29 20:42:11 +0800177 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000179 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
180 uint32_t sources)
181 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800182 mSources(sources),
183 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800184 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800185 mConfigureWasCalled(false),
186 mResetWasCalled(false),
187 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
Chris Yea52ade12020-08-27 16:49:20 -0700189 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
191 void setKeyboardType(int32_t keyboardType) {
192 mKeyboardType = keyboardType;
193 }
194
195 void setMetaState(int32_t metaState) {
196 mMetaState = metaState;
197 }
198
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700199 // Sets the return value for the `process` call.
200 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
201 mProcessResult.clear();
202 for (auto notifyArg : notifyArgs) {
203 mProcessResult.push_back(notifyArg);
204 }
205 }
206
Michael Wrightd02c5b62014-02-10 15:10:22 -0800207 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700208 std::unique_lock<std::mutex> lock(mLock);
209 base::ScopedLockAssertion assumeLocked(mLock);
210 const bool configureCalled =
211 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
212 return mConfigureWasCalled;
213 });
214 if (!configureCalled) {
215 FAIL() << "Expected configure() to have been called.";
216 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800217 mConfigureWasCalled = false;
218 }
219
220 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700221 std::unique_lock<std::mutex> lock(mLock);
222 base::ScopedLockAssertion assumeLocked(mLock);
223 const bool resetCalled =
224 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
225 return mResetWasCalled;
226 });
227 if (!resetCalled) {
228 FAIL() << "Expected reset() to have been called.";
229 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800230 mResetWasCalled = false;
231 }
232
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000233 void assertResetWasNotCalled() {
234 std::scoped_lock lock(mLock);
235 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
236 }
237
Yi Kong9b14ac62018-07-17 13:48:38 -0700238 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700239 std::unique_lock<std::mutex> lock(mLock);
240 base::ScopedLockAssertion assumeLocked(mLock);
241 const bool processCalled =
242 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
243 return mProcessWasCalled;
244 });
245 if (!processCalled) {
246 FAIL() << "Expected process() to have been called.";
247 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800248 if (outLastEvent) {
249 *outLastEvent = mLastEvent;
250 }
251 mProcessWasCalled = false;
252 }
253
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000254 void assertProcessWasNotCalled() {
255 std::scoped_lock lock(mLock);
256 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
257 }
258
Michael Wrightd02c5b62014-02-10 15:10:22 -0800259 void setKeyCodeState(int32_t keyCode, int32_t state) {
260 mKeyCodeStates.replaceValueFor(keyCode, state);
261 }
262
263 void setScanCodeState(int32_t scanCode, int32_t state) {
264 mScanCodeStates.replaceValueFor(scanCode, state);
265 }
266
267 void setSwitchState(int32_t switchCode, int32_t state) {
268 mSwitchStates.replaceValueFor(switchCode, state);
269 }
270
271 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800272 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800273 }
274
Philip Junker4af3b3d2021-12-14 10:36:55 +0100275 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
276 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
277 }
278
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100280 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281
Harry Cuttsd02ea102023-03-17 18:21:30 +0000282 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283 InputMapper::populateDeviceInfo(deviceInfo);
284
285 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000286 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800287 }
288 }
289
Arpit Singhed6c3de2023-04-05 19:24:37 +0000290 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000291 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700292 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800293 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800294
295 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800296 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000297 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000298 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800299 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700300
301 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700302 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800303 }
304
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700305 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800307 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800310 }
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700313 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800314 mLastEvent = *rawEvent;
315 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700316 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700317 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800318 }
319
Chris Yea52ade12020-08-27 16:49:20 -0700320 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800321 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
322 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
323 }
324
Philip Junker4af3b3d2021-12-14 10:36:55 +0100325 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
326 auto it = mKeyCodeMapping.find(locationKeyCode);
327 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
328 }
329
Chris Yea52ade12020-08-27 16:49:20 -0700330 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800331 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
332 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
333 }
334
Chris Yea52ade12020-08-27 16:49:20 -0700335 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800336 ssize_t index = mSwitchStates.indexOfKey(switchCode);
337 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
338 }
339
Chris Yea52ade12020-08-27 16:49:20 -0700340 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700342 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800344 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
345 if (keyCodes[i] == mSupportedKeyCodes[j]) {
346 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800347 }
348 }
349 }
Chris Yea52ade12020-08-27 16:49:20 -0700350 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800351 return result;
352 }
353
354 virtual int32_t getMetaState() {
355 return mMetaState;
356 }
357
358 virtual void fadePointer() {
359 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800360
Linnan Li13bf76a2024-05-05 19:18:02 +0800361 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800362 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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800420 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
421 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700422
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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800478 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
479 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700480
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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800523 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
524 ASSERT_NE(nonDefaultDisplayId, ui::ADISPLAY_ID_DEFAULT)
525 << "Test display ID should not be ui::ADISPLAY_ID_DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000526
527 // Add the default display first and ensure it gets returned.
528 mFakePolicy->clearViewports();
Linnan Li13bf76a2024-05-05 19:18:02 +0800529 mFakePolicy->addDisplayViewport(ui::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);
Linnan Li13bf76a2024-05-05 19:18:02 +0800539 ASSERT_EQ(ui::ADISPLAY_ID_DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000540 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);
Linnan Li13bf76a2024-05-05 19:18:02 +0800547 mFakePolicy->addDisplayViewport(ui::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);
Linnan Li13bf76a2024-05-05 19:18:02 +0800553 ASSERT_EQ(ui::ADISPLAY_ID_DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000554 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";
Linnan Li13bf76a2024-05-05 19:18:02 +0800564 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
565 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700566 const uint8_t hdmi1 = 0;
567 const uint8_t hdmi2 = 1;
568 const uint8_t hdmi3 = 2;
569
570 mFakePolicy->clearViewports();
571 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000572 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000573 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700574 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000575 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000576 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700577
578 // Check that correct display viewport was returned by comparing the display ports.
579 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
580 ASSERT_TRUE(hdmi1Viewport);
581 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
582 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
583
584 // Check that we can still get the same viewport using the uniqueId
585 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
586 ASSERT_TRUE(hdmi1Viewport);
587 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
588 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
589 ASSERT_EQ(type, hdmi1Viewport->type);
590
591 // Check that we cannot find a port with "HDMI2", because we never added one
592 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
593 ASSERT_FALSE(hdmi2Viewport);
594}
595
Michael Wrightd02c5b62014-02-10 15:10:22 -0800596// --- InputReaderTest ---
597
598class InputReaderTest : public testing::Test {
599protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700600 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800601 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700602 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000603 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800604
Chris Yea52ade12020-08-27 16:49:20 -0700605 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700606 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700607 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700608 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800609
Prabir Pradhan28efc192019-11-05 01:10:04 +0000610 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700611 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800612 }
613
Chris Yea52ade12020-08-27 16:49:20 -0700614 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700615 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800616 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800617 }
618
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700619 void addDevice(int32_t eventHubId, const std::string& name,
620 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800621 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800622
623 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800624 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800625 }
626 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000627 mReader->loopOnce();
628 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700629 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800632 }
633
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800634 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700635 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000636 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 }
638
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800639 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700640 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000641 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 }
643
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800644 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700645 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700646 ftl::Flags<InputDeviceClass> classes,
647 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800648 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800649 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000650 FakeInputMapper& mapper =
651 device->addMapper<FakeInputMapper>(eventHubId,
652 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800653 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800654 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800655 return mapper;
656 }
657};
658
Chris Ye98d3f532020-10-01 21:48:59 -0700659TEST_F(InputReaderTest, PolicyGetInputDevices) {
660 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700661 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700662 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663
664 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700665 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800667 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100668 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
670 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000671 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800672}
673
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000674TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
675 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
676 mFakeEventHub->setSysfsRootPath(1, "xyz");
677
678 // Should also have received a notification describing the new input device.
679 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
680 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
681 ASSERT_EQ(0U, inputDevice.getLights().size());
682
683 RawLightInfo infoMonolight = {.id = 123,
684 .name = "mono_keyboard_backlight",
685 .maxBrightness = 255,
686 .flags = InputLightClass::BRIGHTNESS,
687 .path = ""};
688 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
689 mReader->sysfsNodeChanged("xyz");
690 mReader->loopOnce();
691
692 // Should also have received a notification describing the new recreated input device.
693 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
694 inputDevice = mFakePolicy->getInputDevices()[0];
695 ASSERT_EQ(1U, inputDevice.getLights().size());
696}
697
Chris Yee7310032020-09-22 15:36:28 -0700698TEST_F(InputReaderTest, GetMergedInputDevices) {
699 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
700 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
701 // Add two subdevices to device
702 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
703 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000704 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
705 AINPUT_SOURCE_KEYBOARD);
706 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700708
709 // Push same device instance for next device to be added, so they'll have same identifier.
710 mReader->pushNextDevice(device);
711 mReader->pushNextDevice(device);
712 ASSERT_NO_FATAL_FAILURE(
713 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
716
717 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000718 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700719}
720
Chris Yee14523a2020-12-19 13:46:00 -0800721TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
722 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
723 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
724 // Add two subdevices to device
725 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
726 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000727 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
728 AINPUT_SOURCE_KEYBOARD);
729 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800731
732 // Push same device instance for next device to be added, so they'll have same identifier.
733 mReader->pushNextDevice(device);
734 mReader->pushNextDevice(device);
735 // Sensor device is initially disabled
736 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
737 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
738 nullptr));
739 // Device is disabled because the only sub device is a sensor device and disabled initially.
740 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
741 ASSERT_FALSE(device->isEnabled());
742 ASSERT_NO_FATAL_FAILURE(
743 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
744 // The merged device is enabled if any sub device is enabled
745 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
746 ASSERT_TRUE(device->isEnabled());
747}
748
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700749TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800750 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700751 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t eventHubId = 1;
753 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700754 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000755 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
756 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800757 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800758 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759
Yi Kong9b14ac62018-07-17 13:48:38 -0700760 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
762 NotifyDeviceResetArgs resetArgs;
763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764 ASSERT_EQ(deviceId, resetArgs.deviceId);
765
766 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800767 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000768 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700769
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771 ASSERT_EQ(deviceId, resetArgs.deviceId);
772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
777 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700778 ASSERT_EQ(device->isEnabled(), false);
779
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800780 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000781 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700783 ASSERT_EQ(deviceId, resetArgs.deviceId);
784 ASSERT_EQ(device->isEnabled(), true);
785}
786
Michael Wrightd02c5b62014-02-10 15:10:22 -0800787TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800788 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700789 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800791 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 AINPUT_SOURCE_KEYBOARD, nullptr);
794 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800795
796 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
797 AINPUT_SOURCE_ANY, AKEYCODE_A))
798 << "Should return unknown when the device id is >= 0 but unknown.";
799
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800800 ASSERT_EQ(AKEY_STATE_UNKNOWN,
801 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
802 << "Should return unknown when the device id is valid but the sources are not "
803 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800805 ASSERT_EQ(AKEY_STATE_DOWN,
806 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
807 AKEYCODE_A))
808 << "Should return value provided by mapper when device id is valid and the device "
809 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800810
811 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
812 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
813 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
814
815 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
816 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
817 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
818}
819
Philip Junker4af3b3d2021-12-14 10:36:55 +0100820TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
821 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
822 constexpr int32_t eventHubId = 1;
823 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
824 InputDeviceClass::KEYBOARD,
825 AINPUT_SOURCE_KEYBOARD, nullptr);
826 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
827
828 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
829 << "Should return unknown when the device with the specified id is not found.";
830
831 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
832 << "Should return correct mapping when device id is valid and mapping exists.";
833
834 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
835 << "Should return the location key code when device id is valid and there's no "
836 "mapping.";
837}
838
839TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
840 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
841 constexpr int32_t eventHubId = 1;
842 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
843 InputDeviceClass::JOYSTICK,
844 AINPUT_SOURCE_GAMEPAD, nullptr);
845 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
846
847 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
848 << "Should return unknown when the device id is valid but there is no keyboard mapper";
849}
850
Michael Wrightd02c5b62014-02-10 15:10:22 -0800851TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800852 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700853 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800855 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 AINPUT_SOURCE_KEYBOARD, nullptr);
858 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800859
860 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
861 AINPUT_SOURCE_ANY, KEY_A))
862 << "Should return unknown when the device id is >= 0 but unknown.";
863
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800864 ASSERT_EQ(AKEY_STATE_UNKNOWN,
865 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
866 << "Should return unknown when the device id is valid but the sources are not "
867 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800869 ASSERT_EQ(AKEY_STATE_DOWN,
870 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
871 KEY_A))
872 << "Should return value provided by mapper when device id is valid and the device "
873 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800874
875 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
876 AINPUT_SOURCE_TRACKBALL, KEY_A))
877 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
878
879 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
880 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
881 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
882}
883
884TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800885 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700886 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800888 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 AINPUT_SOURCE_KEYBOARD, nullptr);
891 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800892
893 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
894 AINPUT_SOURCE_ANY, SW_LID))
895 << "Should return unknown when the device id is >= 0 but unknown.";
896
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800897 ASSERT_EQ(AKEY_STATE_UNKNOWN,
898 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
899 << "Should return unknown when the device id is valid but the sources are not "
900 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800902 ASSERT_EQ(AKEY_STATE_DOWN,
903 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
904 SW_LID))
905 << "Should return value provided by mapper when device id is valid and the device "
906 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800907
908 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
909 AINPUT_SOURCE_TRACKBALL, SW_LID))
910 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
911
912 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
913 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
914 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
915}
916
917TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800918 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700919 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800921 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100924
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 mapper.addSupportedKeyCode(AKEYCODE_A);
926 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800927
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700928 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929 uint8_t flags[4] = { 0, 0, 0, 1 };
930
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700931 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800932 << "Should return false when device id is >= 0 but unknown.";
933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 << "Should return false when device id is valid but the sources are not supported by "
938 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800939 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
940
941 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700942 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800943 keyCodes, flags))
944 << "Should return value provided by mapper when device id is valid and the device "
945 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800946 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
947
948 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700949 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
950 << "Should return false when the device id is < 0 but the sources are not supported by "
951 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800952 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
953
954 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700955 ASSERT_TRUE(
956 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
957 << "Should return value provided by mapper when device id is < 0 and one of the "
958 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800959 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
960}
961
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000962TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700964 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
966 NotifyConfigurationChangedArgs args;
967
968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
969 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
970}
971
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000972TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800973 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700974 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000975 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800976 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800978 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800979 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800981
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000982 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000983 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800984 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
985
986 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800987 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000988 ASSERT_EQ(when, event.when);
989 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800990 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800991 ASSERT_EQ(EV_KEY, event.type);
992 ASSERT_EQ(KEY_A, event.code);
993 ASSERT_EQ(1, event.value);
994}
995
Garfield Tan1c7bc862020-01-28 13:24:04 -0800996TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800997 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700998 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t eventHubId = 1;
1000 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001001 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001002 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1003 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001004 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001005 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
1007 NotifyDeviceResetArgs resetArgs;
1008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001009 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001011 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001012 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001013 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001015 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001016
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001017 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001018 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001020 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001021 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001023 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001024 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001026 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001027 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001028}
1029
Garfield Tan1c7bc862020-01-28 13:24:04 -08001030TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1031 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001033 constexpr int32_t eventHubId = 1;
1034 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1035 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001036 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1037 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001038 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001039 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1040
1041 NotifyDeviceResetArgs resetArgs;
1042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1043 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1044}
1045
Arthur Hungc23540e2018-11-29 20:42:11 +08001046TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001047 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001048 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1052 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001053 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1054 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001055 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001056
1057 const uint8_t hdmi1 = 1;
1058
1059 // Associated touch screen with second display.
1060 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1061
1062 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001063 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001064 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001065 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001066 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001067 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001068 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001069 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001070 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001071
1072 // Add the device, and make sure all of the callbacks are triggered.
1073 // The device is added after the input port associations are processed since
1074 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001075 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001078 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001079
Arthur Hung2c9a3342019-07-23 14:18:59 +08001080 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001081 ASSERT_EQ(deviceId, device->getId());
1082 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1083 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001084
1085 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001086 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001087 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001088 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001089}
1090
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001091TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1092 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001093 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001094 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1095 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1096 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001097 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1098 AINPUT_SOURCE_KEYBOARD);
1099 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001101 mReader->pushNextDevice(device);
1102 mReader->pushNextDevice(device);
1103 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1104 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1105
1106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1107
1108 NotifyDeviceResetArgs resetArgs;
1109 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1110 ASSERT_EQ(deviceId, resetArgs.deviceId);
1111 ASSERT_TRUE(device->isEnabled());
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1113 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1114
1115 disableDevice(deviceId);
1116 mReader->loopOnce();
1117
1118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1119 ASSERT_EQ(deviceId, resetArgs.deviceId);
1120 ASSERT_FALSE(device->isEnabled());
1121 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1122 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1123
1124 enableDevice(deviceId);
1125 mReader->loopOnce();
1126
1127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1128 ASSERT_EQ(deviceId, resetArgs.deviceId);
1129 ASSERT_TRUE(device->isEnabled());
1130 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1131 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1132}
1133
1134TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1135 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001136 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001137 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1138 // Add two subdevices to device
1139 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1140 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001141 device->addMapper<FakeInputMapper>(eventHubIds[0],
1142 mFakePolicy->getReaderConfiguration(),
1143 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001144 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001145 device->addMapper<FakeInputMapper>(eventHubIds[1],
1146 mFakePolicy->getReaderConfiguration(),
1147 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001148 mReader->pushNextDevice(device);
1149 mReader->pushNextDevice(device);
1150 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1151 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1152
1153 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1154 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1155
1156 ASSERT_EQ(AKEY_STATE_DOWN,
1157 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1160 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1162}
1163
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1165 NotifyPointerCaptureChangedArgs args;
1166
Hiroki Sato25040232024-02-22 17:21:22 +09001167 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001168 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001169 mReader->loopOnce();
1170 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001171 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001172 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001173
Hiroki Sato25040232024-02-22 17:21:22 +09001174 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001175 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001176 mReader->loopOnce();
1177 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001178 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001179
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001182 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001184 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185}
1186
Chris Ye87143712020-11-10 05:05:58 +00001187class FakeVibratorInputMapper : public FakeInputMapper {
1188public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001189 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1190 const InputReaderConfiguration& readerConfig, uint32_t sources)
1191 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001192
1193 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1194};
1195
1196TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1197 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001198 ftl::Flags<InputDeviceClass> deviceClass =
1199 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001200 constexpr int32_t eventHubId = 1;
1201 const char* DEVICE_LOCATION = "BLUETOOTH";
1202 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1203 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001204 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1205 mFakePolicy->getReaderConfiguration(),
1206 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001207 mReader->pushNextDevice(device);
1208
1209 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1210 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1211
1212 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1213 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1214}
1215
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001216// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001217
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001219public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001220 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001222 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223
Andy Chenf9f1a022022-08-29 20:07:10 -04001224 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1225
Chris Yee2b1e5c2021-03-10 22:45:12 -08001226 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1227
1228 void dump(std::string& dump) override {}
1229
1230 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1231 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001232 }
1233
Chris Yee2b1e5c2021-03-10 22:45:12 -08001234 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1235 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001236 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001237
1238 bool setLightColor(int32_t lightId, int32_t color) override {
1239 getDeviceContext().setLightBrightness(lightId, color >> 24);
1240 return true;
1241 }
1242
1243 std::optional<int32_t> getLightColor(int32_t lightId) override {
1244 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1245 if (!result.has_value()) {
1246 return std::nullopt;
1247 }
1248 return result.value() << 24;
1249 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001250
1251 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1252
1253 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1254
1255private:
1256 InputDeviceContext& mDeviceContext;
1257 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1258 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001259 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001260};
1261
Chris Yee2b1e5c2021-03-10 22:45:12 -08001262TEST_F(InputReaderTest, BatteryGetCapacity) {
1263 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001264 ftl::Flags<InputDeviceClass> deviceClass =
1265 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001266 constexpr int32_t eventHubId = 1;
1267 const char* DEVICE_LOCATION = "BLUETOOTH";
1268 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001269 FakePeripheralController& controller =
1270 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001271 mReader->pushNextDevice(device);
1272
1273 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1274
Harry Cuttsa5b71292022-11-28 12:56:17 +00001275 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1276 FakeEventHub::BATTERY_CAPACITY);
1277 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278}
1279
1280TEST_F(InputReaderTest, BatteryGetStatus) {
1281 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001282 ftl::Flags<InputDeviceClass> deviceClass =
1283 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001284 constexpr int32_t eventHubId = 1;
1285 const char* DEVICE_LOCATION = "BLUETOOTH";
1286 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001287 FakePeripheralController& controller =
1288 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001289 mReader->pushNextDevice(device);
1290
1291 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1292
Harry Cuttsa5b71292022-11-28 12:56:17 +00001293 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1294 FakeEventHub::BATTERY_STATUS);
1295 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001296}
1297
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001298TEST_F(InputReaderTest, BatteryGetDevicePath) {
1299 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1300 ftl::Flags<InputDeviceClass> deviceClass =
1301 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1302 constexpr int32_t eventHubId = 1;
1303 const char* DEVICE_LOCATION = "BLUETOOTH";
1304 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1305 device->addController<FakePeripheralController>(eventHubId);
1306 mReader->pushNextDevice(device);
1307
1308 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1309
Harry Cuttsa5b71292022-11-28 12:56:17 +00001310 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001311}
1312
Chris Ye3fdbfef2021-01-06 18:45:18 -08001313TEST_F(InputReaderTest, LightGetColor) {
1314 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001315 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001316 constexpr int32_t eventHubId = 1;
1317 const char* DEVICE_LOCATION = "BLUETOOTH";
1318 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001319 FakePeripheralController& controller =
1320 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001321 mReader->pushNextDevice(device);
1322 RawLightInfo info = {.id = 1,
1323 .name = "Mono",
1324 .maxBrightness = 255,
1325 .flags = InputLightClass::BRIGHTNESS,
1326 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001327 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1328 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001329
1330 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001331
Harry Cutts33476232023-01-30 19:57:29 +00001332 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1333 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1334 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1335 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001336}
1337
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001338// --- InputReaderIntegrationTest ---
1339
1340// These tests create and interact with the InputReader only through its interface.
1341// The InputReader is started during SetUp(), which starts its processing in its own
1342// thread. The tests use linux uinput to emulate input devices.
1343// NOTE: Interacting with the physical device while these tests are running may cause
1344// the tests to fail.
1345class InputReaderIntegrationTest : public testing::Test {
1346protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001347 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001348 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001351 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1352 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1353
Chris Yea52ade12020-08-27 16:49:20 -07001354 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001355#if !defined(__ANDROID__)
1356 GTEST_SKIP();
1357#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001358 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001359
Arpit Singh440bf652023-08-09 09:23:43 +00001360 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001361 }
1362
Chris Yea52ade12020-08-27 16:49:20 -07001363 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001364#if !defined(__ANDROID__)
1365 return;
1366#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001367 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001368 mReader.reset();
1369 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001370 mFakePolicy.clear();
1371 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001372
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001373 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1374 std::chrono::time_point start = std::chrono::steady_clock::now();
1375 while (true) {
1376 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1377 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1378 [&deviceName](const InputDeviceInfo& info) {
1379 return info.getIdentifier().name == deviceName;
1380 });
1381 if (it != inputDevices.end()) {
1382 return std::make_optional(*it);
1383 }
1384 std::this_thread::sleep_for(1ms);
1385 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1386 if (elapsed > 5s) {
1387 return {};
1388 }
1389 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001390 }
Arpit Singh440bf652023-08-09 09:23:43 +00001391
1392 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001393 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1394 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001395
1396 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1397 *mTestListener);
1398 ASSERT_EQ(mReader->start(), OK);
1399
1400 // Since this test is run on a real device, all the input devices connected
1401 // to the test device will show up in mReader. We wait for those input devices to
1402 // show up before beginning the tests.
1403 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1404 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1405 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1406 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001407};
1408
1409TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1410 // An invalid input device that is only used for this test.
1411 class InvalidUinputDevice : public UinputDevice {
1412 public:
Harry Cutts33476232023-01-30 19:57:29 +00001413 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001414
1415 private:
1416 void configureDevice(int fd, uinput_user_dev* device) override {}
1417 };
1418
1419 const size_t numDevices = mFakePolicy->getInputDevices().size();
1420
1421 // UinputDevice does not set any event or key bits, so InputReader should not
1422 // consider it as a valid device.
1423 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1424 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1425 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1426 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1427
1428 invalidDevice.reset();
1429 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1430 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1431 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1432}
1433
1434TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1435 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1436
1437 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1438 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1439 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1440 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1441
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001442 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001443 ASSERT_TRUE(device.has_value());
1444 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1445 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1446 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001447
1448 keyboard.reset();
1449 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1451 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1452}
1453
1454TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1455 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1456 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1457
1458 NotifyConfigurationChangedArgs configChangedArgs;
1459 ASSERT_NO_FATAL_FAILURE(
1460 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001461 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001462 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1463
1464 NotifyKeyArgs keyArgs;
1465 keyboard->pressAndReleaseHomeKey();
1466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1467 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001468 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001469 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001470 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001471 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001472 prevTimestamp = keyArgs.eventTime;
1473
1474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1475 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001476 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001477 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001478 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001479}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001480
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001481TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1482 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1483 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1484
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001485 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001486 ASSERT_TRUE(device.has_value());
1487
Prabir Pradhana3621852022-10-14 18:57:23 +00001488 // An external stylus with buttons should also be recognized as a keyboard.
1489 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001490 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1491 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1492
1493 const auto DOWN =
1494 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1495 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1496
1497 stylus->pressAndReleaseKey(BTN_STYLUS);
1498 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1499 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1501 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1502
1503 stylus->pressAndReleaseKey(BTN_STYLUS2);
1504 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1505 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1507 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1508
1509 stylus->pressAndReleaseKey(BTN_STYLUS3);
1510 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1511 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1512 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1513 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1514}
1515
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001516TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1517 std::unique_ptr<UinputKeyboard> keyboard =
1518 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1519 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1520 KEY_R, KEY_T, KEY_Y,
1521 BTN_STYLUS, BTN_STYLUS2,
1522 BTN_STYLUS3});
1523 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1524
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001525 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001526 ASSERT_TRUE(device.has_value());
1527
1528 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1529 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1530 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1531 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1532}
1533
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001534TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1535 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1536 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1537 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1538 createUinputDevice<UinputKeyboardWithHidUsage>(
1539 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1540 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1541
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001542 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001543 ASSERT_TRUE(device.has_value());
1544
1545 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1546 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1547
1548 // If a device supports reporting HID usage codes, it shouldn't automatically support
1549 // stylus keys.
1550 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1551 uint8_t outFlags[] = {0};
1552 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1553 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1554}
1555
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001556/**
1557 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1558 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1559 * are passed to the listener.
1560 */
1561static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1562TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1563 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1564 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1565 NotifyKeyArgs keyArgs;
1566
1567 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1568 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1570 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1571
1572 controller->pressAndReleaseKey(BTN_GEAR_UP);
1573 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1574 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1575 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1576}
1577
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001578// --- TouchIntegrationTest ---
1579
Arpit Singh440bf652023-08-09 09:23:43 +00001580class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001581protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001582 const std::string UNIQUE_ID = "local:0";
1583
Chris Yea52ade12020-08-27 16:49:20 -07001584 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001585#if !defined(__ANDROID__)
1586 GTEST_SKIP();
1587#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001588 InputReaderIntegrationTest::SetUp();
1589 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001590 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1591 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001592
1593 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1594 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1595 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001596 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001597 ASSERT_TRUE(info);
1598 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001599 }
1600
Linnan Li13bf76a2024-05-05 19:18:02 +08001601 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001602 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001603 std::optional<uint8_t> physicalPort,
1604 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001605 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001606 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001607 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001608 }
1609
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001610 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1611 NotifyMotionArgs args;
1612 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1613 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001614 ASSERT_EQ(points.size(), args.getPointerCount());
1615 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001616 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1617 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1618 }
1619 }
1620
Arthur Hungaab25622020-01-16 11:22:11 +08001621 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001622 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001623};
1624
Arpit Singh440bf652023-08-09 09:23:43 +00001625enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1626
1627class TouchIntegrationTest : public BaseTouchIntegrationTest,
1628 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1629protected:
1630 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1631 const std::string INPUT_PORT = "uinput_touch/input0";
1632
1633 void SetUp() override {
1634#if !defined(__ANDROID__)
1635 GTEST_SKIP();
1636#endif
1637 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1638 BaseTouchIntegrationTest::SetUp();
1639 return;
1640 }
1641
1642 // setup policy with a input-port or UniqueId association to the display
1643 bool isInputPortAssociation =
1644 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1645
1646 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1647 if (isInputPortAssociation) {
1648 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1649 } else {
1650 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1651 }
Arpit Singh440bf652023-08-09 09:23:43 +00001652
1653 InputReaderIntegrationTest::setupInputReader();
1654
1655 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1656 INPUT_PORT);
1657 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1658
1659 // Add a display linked to a physical port or UniqueId.
1660 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1661 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1662 ViewportType::INTERNAL);
1663 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1664 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001665 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001666 ASSERT_TRUE(info);
1667 mDeviceInfo = *info;
1668 }
1669};
1670
1671TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001672 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1673 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1674 // presses).
1675 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1676 mDeviceInfo.getSources());
1677}
1678
Arpit Singh440bf652023-08-09 09:23:43 +00001679TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001680 NotifyMotionArgs args;
1681 const Point centerPoint = mDevice->getCenterPoint();
1682
1683 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001684 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001685 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001686 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001687 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1688 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1689
1690 // ACTION_MOVE
1691 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001692 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001693 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1694 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1695
1696 // ACTION_UP
1697 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001698 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001699 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1701}
1702
Arpit Singh440bf652023-08-09 09:23:43 +00001703TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001704 NotifyMotionArgs args;
1705 const Point centerPoint = mDevice->getCenterPoint();
1706
1707 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001708 mDevice->sendSlot(FIRST_SLOT);
1709 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001710 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001711 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001712 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1713 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1714
1715 // ACTION_POINTER_DOWN (Second slot)
1716 const Point secondPoint = centerPoint + Point(100, 100);
1717 mDevice->sendSlot(SECOND_SLOT);
1718 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001719 mDevice->sendDown(secondPoint);
1720 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001721 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001722 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001723
1724 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001725 mDevice->sendMove(secondPoint + Point(1, 1));
1726 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001727 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1728 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1729
1730 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001731 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001732 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001733 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001734 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001735
1736 // ACTION_UP
1737 mDevice->sendSlot(FIRST_SLOT);
1738 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001739 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001740 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1741 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1742}
1743
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001744/**
1745 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1746 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1747 * data?
1748 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1749 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1750 * for Pointer 0 only is generated after.
1751 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1752 * events, we will not miss any information.
1753 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1754 * event generated afterwards that contains the newest movement of pointer 0.
1755 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1756 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1757 * losing information about non-palm pointers.
1758 */
Arpit Singh440bf652023-08-09 09:23:43 +00001759TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001760 NotifyMotionArgs args;
1761 const Point centerPoint = mDevice->getCenterPoint();
1762
1763 // ACTION_DOWN
1764 mDevice->sendSlot(FIRST_SLOT);
1765 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1766 mDevice->sendDown(centerPoint);
1767 mDevice->sendSync();
1768 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1769
1770 // ACTION_POINTER_DOWN (Second slot)
1771 const Point secondPoint = centerPoint + Point(100, 100);
1772 mDevice->sendSlot(SECOND_SLOT);
1773 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1774 mDevice->sendDown(secondPoint);
1775 mDevice->sendSync();
1776 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1777
1778 // ACTION_MOVE (First slot)
1779 mDevice->sendSlot(FIRST_SLOT);
1780 mDevice->sendMove(centerPoint + Point(5, 5));
1781 // ACTION_POINTER_UP (Second slot)
1782 mDevice->sendSlot(SECOND_SLOT);
1783 mDevice->sendPointerUp();
1784 // Send a single sync for the above 2 pointer updates
1785 mDevice->sendSync();
1786
1787 // First, we should get POINTER_UP for the second pointer
1788 assertReceivedMotion(ACTION_POINTER_1_UP,
1789 {/*first pointer */ centerPoint + Point(5, 5),
1790 /*second pointer*/ secondPoint});
1791
1792 // Next, the MOVE event for the first pointer
1793 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1794}
1795
1796/**
1797 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1798 * move, and then it will go up, all in the same frame.
1799 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1800 * gets sent to the listener.
1801 */
Arpit Singh440bf652023-08-09 09:23:43 +00001802TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001803 NotifyMotionArgs args;
1804 const Point centerPoint = mDevice->getCenterPoint();
1805
1806 // ACTION_DOWN
1807 mDevice->sendSlot(FIRST_SLOT);
1808 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1809 mDevice->sendDown(centerPoint);
1810 mDevice->sendSync();
1811 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1812
1813 // ACTION_POINTER_DOWN (Second slot)
1814 const Point secondPoint = centerPoint + Point(100, 100);
1815 mDevice->sendSlot(SECOND_SLOT);
1816 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1817 mDevice->sendDown(secondPoint);
1818 mDevice->sendSync();
1819 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1820
1821 // ACTION_MOVE (First slot)
1822 mDevice->sendSlot(FIRST_SLOT);
1823 mDevice->sendMove(centerPoint + Point(5, 5));
1824 // ACTION_POINTER_UP (Second slot)
1825 mDevice->sendSlot(SECOND_SLOT);
1826 mDevice->sendMove(secondPoint + Point(6, 6));
1827 mDevice->sendPointerUp();
1828 // Send a single sync for the above 2 pointer updates
1829 mDevice->sendSync();
1830
1831 // First, we should get POINTER_UP for the second pointer
1832 // The movement of the second pointer during the liftoff frame is ignored.
1833 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1834 assertReceivedMotion(ACTION_POINTER_1_UP,
1835 {/*first pointer */ centerPoint + Point(5, 5),
1836 /*second pointer*/ secondPoint});
1837
1838 // Next, the MOVE event for the first pointer
1839 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1840}
1841
Arpit Singh440bf652023-08-09 09:23:43 +00001842TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001843 NotifyMotionArgs args;
1844 const Point centerPoint = mDevice->getCenterPoint();
1845
1846 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001847 mDevice->sendSlot(FIRST_SLOT);
1848 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001849 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001850 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001851 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1852 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1853
arthurhungcc7f9802020-04-30 17:55:40 +08001854 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001855 const Point secondPoint = centerPoint + Point(100, 100);
1856 mDevice->sendSlot(SECOND_SLOT);
1857 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1858 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001859 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001860 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001861 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001862
arthurhungcc7f9802020-04-30 17:55:40 +08001863 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001864 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001865 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001866 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1867 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1868
arthurhungcc7f9802020-04-30 17:55:40 +08001869 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1870 // a palm event.
1871 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001872 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001873 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001874 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001875 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001876 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001877
arthurhungcc7f9802020-04-30 17:55:40 +08001878 // Send up to second slot, expect first slot send moving.
1879 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001880 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001881 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1882 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001883
arthurhungcc7f9802020-04-30 17:55:40 +08001884 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001885 mDevice->sendSlot(FIRST_SLOT);
1886 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001887 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001888
arthurhungcc7f9802020-04-30 17:55:40 +08001889 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1890 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001891}
1892
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001893/**
1894 * Some drivers historically have reported axis values outside of the range specified in the
1895 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1896 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1897 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1898 * and no units for pressure (resolution) is specified by the evdev documentation.
1899 */
1900TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1901 const Point centerPoint = mDevice->getCenterPoint();
1902
1903 // Down with pressure outside the reported range
1904 mDevice->sendSlot(FIRST_SLOT);
1905 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1906 mDevice->sendDown(centerPoint);
1907 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1908 mDevice->sendSync();
1909 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1910 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1911
1912 // Move to a point outside the reported range
1913 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1914 mDevice->sendSync();
1915 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1916 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1917
1918 // Up
1919 mDevice->sendUp();
1920 mDevice->sendSync();
1921 ASSERT_NO_FATAL_FAILURE(
1922 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1923}
1924
Arpit Singh440bf652023-08-09 09:23:43 +00001925TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001926 const Point centerPoint = mDevice->getCenterPoint();
1927
1928 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1929 mDevice->sendSlot(FIRST_SLOT);
1930 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1931 mDevice->sendToolType(MT_TOOL_PEN);
1932 mDevice->sendDown(centerPoint);
1933 mDevice->sendSync();
1934 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1935 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001936 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001937
1938 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1939
1940 // Release the stylus touch.
1941 mDevice->sendUp();
1942 mDevice->sendSync();
1943 ASSERT_NO_FATAL_FAILURE(
1944 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1945
1946 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1947
1948 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1949 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1950 mDevice->sendToolType(MT_TOOL_FINGER);
1951 mDevice->sendDown(centerPoint);
1952 mDevice->sendSync();
1953 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1954 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001955 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001956
1957 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1958
1959 mDevice->sendUp();
1960 mDevice->sendSync();
1961 ASSERT_NO_FATAL_FAILURE(
1962 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1963
1964 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1965 // The policy should be notified of the stylus presence.
1966 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1967 mDevice->sendToolType(MT_TOOL_PEN);
1968 mDevice->sendMove(centerPoint);
1969 mDevice->sendSync();
1970 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1971 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001972 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001973
1974 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1975}
1976
Arpit Singh440bf652023-08-09 09:23:43 +00001977TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001978 const Point centerPoint = mDevice->getCenterPoint();
1979
1980 // Down
1981 mDevice->sendSlot(FIRST_SLOT);
1982 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1983 mDevice->sendDown(centerPoint);
1984 mDevice->sendSync();
1985 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1986 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1987
1988 // Move
1989 mDevice->sendMove(centerPoint + Point(1, 1));
1990 mDevice->sendSync();
1991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1992 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1993
1994 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1995 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1996 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1997 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001998 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001999 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002000
2001 // Move
2002 mDevice->sendMove(centerPoint + Point(2, 2));
2003 mDevice->sendSync();
2004 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2005 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2006
2007 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2008 externalStylus.reset();
2009 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2011 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2012
2013 // Up
2014 mDevice->sendUp();
2015 mDevice->sendSync();
2016 ASSERT_NO_FATAL_FAILURE(
2017 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2018
2019 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2020}
2021
Arpit Singh440bf652023-08-09 09:23:43 +00002022INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2023 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2024 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2025 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2026
Prabir Pradhan124ea442022-10-28 20:27:44 +00002027// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002028
Prabir Pradhan124ea442022-10-28 20:27:44 +00002029// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2030// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2031// stylus.
2032template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002033class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002034protected:
2035 void SetUp() override {
2036#if !defined(__ANDROID__)
2037 GTEST_SKIP();
2038#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002039 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002040 mTouchscreen = mDevice.get();
2041 mTouchscreenInfo = mDeviceInfo;
2042
2043 setUpStylusDevice();
2044 }
2045
2046 UinputStylusDevice* mStylus{nullptr};
2047 InputDeviceInfo mStylusInfo{};
2048
2049 UinputTouchScreen* mTouchscreen{nullptr};
2050 InputDeviceInfo mTouchscreenInfo{};
2051
2052private:
2053 // When we are attempting to test stylus button events that are sent from the touchscreen,
2054 // use the same Uinput device for the touchscreen and the stylus.
2055 template <typename T = UinputStylusDevice>
2056 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2057 mStylus = mDevice.get();
2058 mStylusInfo = mDeviceInfo;
2059 }
2060
2061 // When we are attempting to stylus buttons from an external stylus being merged with touches
2062 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2063 template <typename T = UinputStylusDevice>
2064 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2065 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2066 mStylus = mStylusDeviceLifecycleTracker.get();
2067 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2068 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002069 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002070 ASSERT_TRUE(info);
2071 mStylusInfo = *info;
2072 }
2073
2074 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2075
2076 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002077 using BaseTouchIntegrationTest::mDevice;
2078 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002079};
2080
2081using StylusButtonIntegrationTestTypes =
2082 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2083TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2084
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002085TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002086 const auto stylusId = TestFixture::mStylusInfo.getId();
2087
2088 TestFixture::mStylus->pressKey(BTN_STYLUS);
2089 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2090 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2091 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2092
2093 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2094 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002095 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002096 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002097}
2098
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002099TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002100 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2101 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2102 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002103
2104 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002105 TestFixture::mStylus->pressKey(BTN_STYLUS);
2106 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002107 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002108 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002109
2110 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002111 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2112 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2113 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2114 TestFixture::mTouchscreen->sendDown(centerPoint);
2115 TestFixture::mTouchscreen->sendSync();
2116 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002117 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002118 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002119 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2120 WithDeviceId(touchscreenId))));
2121 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002122 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002123 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2125 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002126
Prabir Pradhan124ea442022-10-28 20:27:44 +00002127 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2128 TestFixture::mTouchscreen->sendSync();
2129 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002131 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002132 WithDeviceId(touchscreenId))));
2133 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002134 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002135 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002136 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002137
2138 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002139 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2140 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002142 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002143}
2144
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002145TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002146 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2147 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2148 const auto stylusId = TestFixture::mStylusInfo.getId();
2149 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002150 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002151
2152 // Press the stylus button.
2153 TestFixture::mStylus->pressKey(BTN_STYLUS);
2154 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2155 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2156 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2157
2158 // Start hovering with the stylus.
2159 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2160 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2161 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2162 TestFixture::mTouchscreen->sendMove(centerPoint);
2163 TestFixture::mTouchscreen->sendSync();
2164 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2165 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2166 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2167 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2168 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2169 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2170 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2171 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2172 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2173
2174 // Touch down with the stylus.
2175 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2176 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2177 TestFixture::mTouchscreen->sendDown(centerPoint);
2178 TestFixture::mTouchscreen->sendSync();
2179 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2180 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2181 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2182
2183 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2184 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2185 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2186
2187 // Stop touching with the stylus, and start hovering.
2188 TestFixture::mTouchscreen->sendUp();
2189 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2190 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2191 TestFixture::mTouchscreen->sendMove(centerPoint);
2192 TestFixture::mTouchscreen->sendSync();
2193 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2194 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2195 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2196 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2197 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2198 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2199 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2200 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2201 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2202
2203 // Stop hovering.
2204 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2205 TestFixture::mTouchscreen->sendSync();
2206 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2207 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2208 WithButtonState(0))));
2209 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2210 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2211 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2212 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2213
2214 // Release the stylus button.
2215 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2216 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2217 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2218 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2219}
2220
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002221TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002222 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2223 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2224 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002225
2226 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002227 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2228 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2229 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2230 TestFixture::mTouchscreen->sendDown(centerPoint);
2231 TestFixture::mTouchscreen->sendSync();
2232 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002233 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002234 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002235 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002236
2237 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002238 TestFixture::mStylus->pressKey(BTN_STYLUS);
2239 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002240 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002241 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2242 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002243 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002244 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002245 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2246 WithDeviceId(touchscreenId))));
2247 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002248 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002249 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002250 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2251 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002252
Prabir Pradhan124ea442022-10-28 20:27:44 +00002253 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2254 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002255 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002256 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2257 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002258 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002259 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002260 WithDeviceId(touchscreenId))));
2261 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002262 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002263 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002264 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002265
2266 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002267 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2268 TestFixture::mTouchscreen->sendSync();
2269 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002270 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002271 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002272 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002273}
2274
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002275TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002276 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2277 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002278 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002279
2280 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2281 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2282 const auto stylusId = TestFixture::mStylusInfo.getId();
2283
2284 // Start a stylus gesture. By the time this event is processed, the configuration change that
2285 // was requested is guaranteed to be completed.
2286 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2287 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2288 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2289 TestFixture::mTouchscreen->sendDown(centerPoint);
2290 TestFixture::mTouchscreen->sendSync();
2291 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2292 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002293 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002294 WithDeviceId(touchscreenId))));
2295
2296 // Press and release a stylus button. Each change only generates a MOVE motion event.
2297 // Key events are unaffected.
2298 TestFixture::mStylus->pressKey(BTN_STYLUS);
2299 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2300 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2301 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2302 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2303 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002304 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002305 WithDeviceId(touchscreenId))));
2306
2307 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2308 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2309 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2310 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2311 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002313 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002314 WithDeviceId(touchscreenId))));
2315
2316 // Finish the stylus gesture.
2317 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2318 TestFixture::mTouchscreen->sendSync();
2319 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2320 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002321 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002322 WithDeviceId(touchscreenId))));
2323}
2324
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002325// --- ExternalStylusIntegrationTest ---
2326
2327// Verify the behavior of an external stylus. An external stylus can report pressure or button
2328// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2329// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002330using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002331
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002332TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2333 // Create an external stylus capable of reporting pressure data that
2334 // should be fused with a touch pointer.
2335 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2336 createUinputDevice<UinputExternalStylusWithPressure>();
2337 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2338 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002339 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002340 ASSERT_TRUE(stylusInfo);
2341
2342 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002343 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002344 ASSERT_TRUE(deviceInfo);
2345 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2346}
2347
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002348TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002349 const Point centerPoint = mDevice->getCenterPoint();
2350
2351 // Create an external stylus capable of reporting pressure data that
2352 // should be fused with a touch pointer.
2353 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2354 createUinputDevice<UinputExternalStylusWithPressure>();
2355 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2356 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002357 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002358 ASSERT_TRUE(stylusInfo);
2359
2360 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2361
2362 const auto touchscreenId = mDeviceInfo.getId();
2363
2364 // Set a pressure value on the stylus. It doesn't generate any events.
2365 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2366 stylus->setPressure(100);
2367 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2368
2369 // Start a finger gesture, and ensure it shows up as stylus gesture
2370 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002371 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002372 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002373 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002374 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002375 mDevice->sendSync();
2376 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002377 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2378 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2379 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002380
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002381 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2382 // event with the updated pressure.
2383 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002384 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002385 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2386 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2387 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002388
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002389 // The external stylus did not generate any events.
2390 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2391 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2392}
2393
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002394TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002395 const Point centerPoint = mDevice->getCenterPoint();
2396
2397 // Create an external stylus capable of reporting pressure data that
2398 // should be fused with a touch pointer.
2399 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2400 createUinputDevice<UinputExternalStylusWithPressure>();
2401 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2402 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002403 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002404 ASSERT_TRUE(stylusInfo);
2405
2406 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2407
2408 const auto touchscreenId = mDeviceInfo.getId();
2409
2410 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2411 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002412 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2413 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002414 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002415 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002416
2417 // Start a finger gesture. The touch device will withhold generating any touches for
2418 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2419 mDevice->sendSlot(FIRST_SLOT);
2420 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2421 mDevice->sendToolType(MT_TOOL_FINGER);
2422 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002423 const auto syncTime = std::chrono::system_clock::now();
2424 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2425 // is generated in that period, there will be a race condition between the event being generated
2426 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2427 // will reduce the liklihood of the race condition occurring.
2428 const auto waitUntilTimeForNoEvent =
2429 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002430 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002431 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002432
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002433 // Since the external stylus did not report a pressure value within the timeout,
2434 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002435 const auto waitUntilTimeForEvent = syncTime +
2436 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2437 ASSERT_NO_FATAL_FAILURE(
2438 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2439 AMOTION_EVENT_ACTION_DOWN),
2440 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2441 AINPUT_SOURCE_STYLUS),
2442 WithToolType(ToolType::FINGER),
2443 WithDeviceId(touchscreenId),
2444 WithPressure(1.f)),
2445 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002446
2447 // Change the pressure on the external stylus. Since the pressure was not present at the start
2448 // of the gesture, it is ignored for now.
2449 stylus->setPressure(200);
2450 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2451
2452 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002453 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2454 mDevice->sendSync();
2455 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2456 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002457 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002458 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002459
2460 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2461 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2462 mDevice->sendToolType(MT_TOOL_FINGER);
2463 mDevice->sendDown(centerPoint);
2464 mDevice->sendSync();
2465 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002466 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2467 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2468 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002469
2470 // The external stylus did not generate any events.
2471 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2472 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002473}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002474
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002475TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002476 const Point centerPoint = mDevice->getCenterPoint();
2477
2478 // Create an external stylus device that does not support pressure. It should not affect any
2479 // touch pointers.
2480 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2481 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002483 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002484 ASSERT_TRUE(stylusInfo);
2485
2486 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2487
2488 const auto touchscreenId = mDeviceInfo.getId();
2489
2490 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2491 // pressure data from the external stylus.
2492 mDevice->sendSlot(FIRST_SLOT);
2493 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2494 mDevice->sendToolType(MT_TOOL_FINGER);
2495 mDevice->sendDown(centerPoint);
2496 auto waitUntil = std::chrono::system_clock::now() +
2497 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2498 mDevice->sendSync();
2499 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002500 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2501 AMOTION_EVENT_ACTION_DOWN),
2502 WithToolType(ToolType::FINGER),
2503 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2504 AINPUT_SOURCE_STYLUS),
2505 WithButtonState(0),
2506 WithDeviceId(touchscreenId),
2507 WithPressure(1.f)),
2508 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002509
2510 // The external stylus did not generate any events.
2511 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2512 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2513}
2514
Michael Wrightd02c5b62014-02-10 15:10:22 -08002515// --- InputDeviceTest ---
2516class InputDeviceTest : public testing::Test {
2517protected:
2518 static const char* DEVICE_NAME;
2519 static const char* DEVICE_LOCATION;
2520 static const int32_t DEVICE_ID;
2521 static const int32_t DEVICE_GENERATION;
2522 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002523 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524 static const int32_t EVENTHUB_ID;
2525 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2526
2527 std::shared_ptr<FakeEventHub> mFakeEventHub;
2528 sp<FakeInputReaderPolicy> mFakePolicy;
2529 std::unique_ptr<TestInputListener> mFakeListener;
2530 std::unique_ptr<InstrumentedInputReader> mReader;
2531 std::shared_ptr<InputDevice> mDevice;
2532
2533 void SetUp() override {
2534 mFakeEventHub = std::make_unique<FakeEventHub>();
2535 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2536 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002537 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002539 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002540 identifier.name = DEVICE_NAME;
2541 identifier.location = DEVICE_LOCATION;
2542 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2543 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2544 identifier);
2545 mReader->pushNextDevice(mDevice);
2546 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002547 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002548 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002549
2550 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002551 mFakeListener.reset();
2552 mFakePolicy.clear();
2553 }
2554};
2555
2556const char* InputDeviceTest::DEVICE_NAME = "device";
2557const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2558const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2559const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002560const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002561const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2562 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002563const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002564const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2565
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002566TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002567 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002568 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2569 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002571
Michael Wrightd02c5b62014-02-10 15:10:22 -08002572TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2573 ASSERT_EQ(mDevice->isEnabled(), false);
2574}
2575
2576TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2577 // Configuration.
2578 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002579 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002580
2581 // Reset.
2582 unused += mDevice->reset(ARBITRARY_TIME);
2583
2584 NotifyDeviceResetArgs resetArgs;
2585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2586 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2587 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2588
2589 // Metadata.
2590 ASSERT_TRUE(mDevice->isIgnored());
2591 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2592
2593 InputDeviceInfo info = mDevice->getDeviceInfo();
2594 ASSERT_EQ(DEVICE_ID, info.getId());
2595 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2596 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2597 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2598
2599 // State queries.
2600 ASSERT_EQ(0, mDevice->getMetaState());
2601
2602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2603 << "Ignored device should return unknown key code state.";
2604 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2605 << "Ignored device should return unknown scan code state.";
2606 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2607 << "Ignored device should return unknown switch state.";
2608
2609 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2610 uint8_t flags[2] = { 0, 1 };
2611 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2612 << "Ignored device should never mark any key codes.";
2613 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2614 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2615}
2616
2617TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2618 // Configuration.
2619 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2620
2621 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002622 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2623 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002624 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2625 mapper1.setMetaState(AMETA_ALT_ON);
2626 mapper1.addSupportedKeyCode(AKEYCODE_A);
2627 mapper1.addSupportedKeyCode(AKEYCODE_B);
2628 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2629 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2630 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2631 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2632 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2633
2634 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002635 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2636 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002637 mapper2.setMetaState(AMETA_SHIFT_ON);
2638
2639 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002640 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002641
Harry Cuttsf13161a2023-03-08 14:15:49 +00002642 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2643 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002645 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002646
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002647 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2648 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002649
2650 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002651 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002652 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2653 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002654
2655 NotifyDeviceResetArgs resetArgs;
2656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2657 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2658 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2659
2660 // Metadata.
2661 ASSERT_FALSE(mDevice->isIgnored());
2662 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2663
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002664 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002666 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002667 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2668 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2669
2670 // State queries.
2671 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2672 << "Should query mappers and combine meta states.";
2673
2674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2675 << "Should return unknown key code state when source not supported.";
2676 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2677 << "Should return unknown scan code state when source not supported.";
2678 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2679 << "Should return unknown switch state when source not supported.";
2680
2681 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2682 << "Should query mapper when source is supported.";
2683 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2684 << "Should query mapper when source is supported.";
2685 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2686 << "Should query mapper when source is supported.";
2687
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002688 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002690 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691 << "Should do nothing when source is unsupported.";
2692 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2693 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2694 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2695 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2696
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002697 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002698 << "Should query mapper when source is supported.";
2699 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2700 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2701 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2702 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2703
2704 // Event handling.
2705 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002706 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002707 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002708
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002709 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2710 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002711}
2712
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002713TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2714 // Set some behavior to force the configuration to be update.
2715 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2716 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2717 AINPUT_SOURCE_KEYBOARD);
2718
2719 std::list<NotifyArgs> unused =
2720 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2721 /*changes=*/{});
2722
2723 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2724}
2725
2726TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2727 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2728 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2729 AINPUT_SOURCE_KEYBOARD);
2730
2731 std::list<NotifyArgs> unused =
2732 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2733 /*changes=*/{});
2734
2735 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2736}
2737
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002738TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2739 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2740 FakeInputMapper& mapper =
2741 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2742 AINPUT_SOURCE_KEYBOARD);
2743 NotifyMotionArgs args1;
2744 NotifySwitchArgs args2;
2745 NotifyKeyArgs args3;
2746 mapper.setProcessResult({args1, args2, args3});
2747
2748 InputReaderConfiguration config;
2749 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2750
2751 RawEvent event;
2752 event.deviceId = EVENTHUB_ID;
2753 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2754
2755 for (auto& arg : notifyArgs) {
2756 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2757 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2758 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2759 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2760 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2761 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2762 }
2763 }
2764}
2765
2766TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2767 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2768 FakeInputMapper& mapper =
2769 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2770 AINPUT_SOURCE_KEYBOARD);
2771 NotifyMotionArgs args;
2772 mapper.setProcessResult({args});
2773
2774 InputReaderConfiguration config;
2775 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2776
2777 RawEvent event;
2778 event.deviceId = EVENTHUB_ID;
2779 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2780
2781 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2782 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2783}
2784
2785TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2786 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2787 FakeInputMapper& mapper =
2788 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2789 AINPUT_SOURCE_KEYBOARD);
2790 NotifyMotionArgs args;
2791 args.policyFlags = POLICY_FLAG_WAKE;
2792 mapper.setProcessResult({args});
2793
2794 InputReaderConfiguration config;
2795 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2796
2797 RawEvent event;
2798 event.deviceId = EVENTHUB_ID;
2799 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2800
2801 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2802 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2803}
2804
Arthur Hung2c9a3342019-07-23 14:18:59 +08002805// A single input device is associated with a specific display. Check that:
2806// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002807// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002808TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002809 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2810 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002811
2812 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002813 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002814 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2815 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002816
2817 // Device should be enabled by default.
2818 ASSERT_TRUE(mDevice->isEnabled());
2819
2820 // Prepare associated info.
2821 constexpr uint8_t hdmi = 1;
2822 const std::string UNIQUE_ID = "local:1";
2823
2824 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002825 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002826 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002827 // Device should be disabled because it is associated with a specific display via
2828 // input port <-> display port association, but the corresponding display is not found
2829 ASSERT_FALSE(mDevice->isEnabled());
2830
2831 // Prepare displays.
2832 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002833 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002834 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002835 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002836 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002837 ASSERT_TRUE(mDevice->isEnabled());
2838
2839 // Device should be disabled after set disable.
2840 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002841 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002842 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002843 ASSERT_FALSE(mDevice->isEnabled());
2844
2845 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002846 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002847 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002848 ASSERT_FALSE(mDevice->isEnabled());
2849}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002850
Christine Franks1ba71cc2021-04-07 14:37:42 -07002851TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2852 // Device should be enabled by default.
2853 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002854 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2855 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002856 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002857 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2858 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002859 ASSERT_TRUE(mDevice->isEnabled());
2860
2861 // Device should be disabled because it is associated with a specific display, but the
2862 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002863 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002864 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002865 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002866 ASSERT_FALSE(mDevice->isEnabled());
2867
2868 // Device should be enabled when a display is found.
2869 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002870 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002871 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002872 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002873 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002874 ASSERT_TRUE(mDevice->isEnabled());
2875
2876 // Device should be disabled after set disable.
2877 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002878 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002879 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002880 ASSERT_FALSE(mDevice->isEnabled());
2881
2882 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002883 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002884 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002885 ASSERT_FALSE(mDevice->isEnabled());
2886}
2887
Christine Franks2a2293c2022-01-18 11:51:16 -08002888TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2889 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002890 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2891 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002892 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002893 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2894 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002895
Christine Franks2a2293c2022-01-18 11:51:16 -08002896 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2897 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002898 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002899 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002900 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002901 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002902 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002903 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002904 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2905 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002906}
2907
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002908/**
2909 * This test reproduces a crash caused by a dangling reference that remains after device is added
2910 * and removed. The reference is accessed in InputDevice::dump(..);
2911 */
2912TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2913 constexpr int32_t TEST_EVENTHUB_ID = 10;
2914 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2915
Harry Cutts33476232023-01-30 19:57:29 +00002916 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002917 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2918 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002919 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2920 std::string dumpStr, eventHubDevStr;
2921 device.dump(dumpStr, eventHubDevStr);
2922}
2923
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002924TEST_F(InputDeviceTest, GetBluetoothAddress) {
2925 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2926 ASSERT_TRUE(address);
2927 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2928}
2929
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002930TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2931 mFakePolicy->clearViewports();
2932 FakeInputMapper& mapper =
2933 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2934 AINPUT_SOURCE_KEYBOARD);
2935 std::list<NotifyArgs> unused =
2936 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2937 /*changes=*/{});
2938
2939 mapper.assertConfigureWasCalled();
2940 mapper.assertResetWasNotCalled();
2941
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002942 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002943 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002944 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002945 .type = EV_SYN,
2946 .code = SYN_REPORT,
2947 .value = 0};
2948
2949 // Events are processed normally.
2950 unused = mDevice->process(&event, /*count=*/1);
2951 mapper.assertProcessWasCalled();
2952
2953 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002954 event.type = EV_SYN;
2955 event.code = SYN_DROPPED;
2956 event.value = 0;
2957 unused = mDevice->process(&event, /*count=*/1);
2958 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002959
2960 // All events until the next SYN_REPORT should be dropped.
2961 event.type = EV_KEY;
2962 event.code = KEY_A;
2963 event.value = 1;
2964 unused = mDevice->process(&event, /*count=*/1);
2965 mapper.assertProcessWasNotCalled();
2966
2967 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00002968 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002969 event.type = EV_SYN;
2970 event.code = SYN_REPORT;
2971 event.value = 0;
2972 unused = mDevice->process(&event, /*count=*/1);
2973 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00002974 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002975
2976 // The mapper receives events normally now.
2977 event.type = EV_KEY;
2978 event.code = KEY_B;
2979 event.value = 1;
2980 unused = mDevice->process(&event, /*count=*/1);
2981 mapper.assertProcessWasCalled();
2982}
2983
Michael Wrightd02c5b62014-02-10 15:10:22 -08002984// --- SwitchInputMapperTest ---
2985
2986class SwitchInputMapperTest : public InputMapperTest {
2987protected:
2988};
2989
2990TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002991 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002993 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002994}
2995
2996TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002997 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002998
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002999 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003000 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003001
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003002 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003003 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003004}
3005
3006TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00003007 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003008 std::list<NotifyArgs> out;
3009 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
3010 ASSERT_TRUE(out.empty());
3011 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
3012 ASSERT_TRUE(out.empty());
3013 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
3014 ASSERT_TRUE(out.empty());
3015 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003016
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003017 ASSERT_EQ(1u, out.size());
3018 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003019 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08003020 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
3021 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08003022 args.switchMask);
3023 ASSERT_EQ(uint32_t(0), args.policyFlags);
3024}
3025
Chris Ye87143712020-11-10 05:05:58 +00003026// --- VibratorInputMapperTest ---
3027class VibratorInputMapperTest : public InputMapperTest {
3028protected:
3029 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3030};
3031
3032TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003033 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003034
3035 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3036}
3037
3038TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003039 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003040
3041 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3042}
3043
3044TEST_F(VibratorInputMapperTest, Vibrate) {
3045 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003046 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003047 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003048
3049 VibrationElement pattern(2);
3050 VibrationSequence sequence(2);
3051 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003052 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3053 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003054 sequence.addElement(pattern);
3055 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003056 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3057 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003058 sequence.addElement(pattern);
3059
3060 std::vector<int64_t> timings = {0, 1};
3061 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3062
3063 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003064 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003065 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003066 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003067 // Verify vibrator state listener was notified.
3068 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003069 ASSERT_EQ(1u, out.size());
3070 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3071 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3072 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003073 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003074 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003075 ASSERT_FALSE(mapper.isVibrating());
3076 // Verify vibrator state listener was notified.
3077 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003078 ASSERT_EQ(1u, out.size());
3079 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3080 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3081 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003082}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Chris Yef59a2f42020-10-16 12:55:26 -07003084// --- SensorInputMapperTest ---
3085
3086class SensorInputMapperTest : public InputMapperTest {
3087protected:
3088 static const int32_t ACCEL_RAW_MIN;
3089 static const int32_t ACCEL_RAW_MAX;
3090 static const int32_t ACCEL_RAW_FUZZ;
3091 static const int32_t ACCEL_RAW_FLAT;
3092 static const int32_t ACCEL_RAW_RESOLUTION;
3093
3094 static const int32_t GYRO_RAW_MIN;
3095 static const int32_t GYRO_RAW_MAX;
3096 static const int32_t GYRO_RAW_FUZZ;
3097 static const int32_t GYRO_RAW_FLAT;
3098 static const int32_t GYRO_RAW_RESOLUTION;
3099
3100 static const float GRAVITY_MS2_UNIT;
3101 static const float DEGREE_RADIAN_UNIT;
3102
3103 void prepareAccelAxes();
3104 void prepareGyroAxes();
3105 void setAccelProperties();
3106 void setGyroProperties();
3107 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3108};
3109
3110const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3111const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3112const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3113const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3114const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3115
3116const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3117const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3118const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3119const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3120const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3121
3122const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3123const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3124
3125void SensorInputMapperTest::prepareAccelAxes() {
3126 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3127 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3128 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3129 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3130 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3131 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3132}
3133
3134void SensorInputMapperTest::prepareGyroAxes() {
3135 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3136 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3137 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3138 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3139 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3140 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3141}
3142
3143void SensorInputMapperTest::setAccelProperties() {
3144 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3145 /* sensorDataIndex */ 0);
3146 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3147 /* sensorDataIndex */ 1);
3148 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3149 /* sensorDataIndex */ 2);
3150 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3151 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3152 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3153 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3154 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3155}
3156
3157void SensorInputMapperTest::setGyroProperties() {
3158 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3159 /* sensorDataIndex */ 0);
3160 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3161 /* sensorDataIndex */ 1);
3162 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3163 /* sensorDataIndex */ 2);
3164 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3165 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3166 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3167 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3168 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3169}
3170
3171TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003172 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003173
3174 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3175}
3176
3177TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3178 setAccelProperties();
3179 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003180 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003181
3182 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3183 std::chrono::microseconds(10000),
3184 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003185 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003191
3192 NotifySensorArgs args;
3193 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3194 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3195 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3196
3197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3198 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3199 ASSERT_EQ(args.deviceId, DEVICE_ID);
3200 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3201 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3202 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3203 ASSERT_EQ(args.values, values);
3204 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3205}
3206
3207TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3208 setGyroProperties();
3209 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003210 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003211
3212 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3213 std::chrono::microseconds(10000),
3214 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003215 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003216 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3218 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3219 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3220 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003221
3222 NotifySensorArgs args;
3223 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3224 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3225 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3226
3227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3228 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3229 ASSERT_EQ(args.deviceId, DEVICE_ID);
3230 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3231 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3232 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3233 ASSERT_EQ(args.values, values);
3234 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3235}
3236
Michael Wrightd02c5b62014-02-10 15:10:22 -08003237// --- KeyboardInputMapperTest ---
3238
3239class KeyboardInputMapperTest : public InputMapperTest {
3240protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003241 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003242 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003243 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003244
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003245 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003246 int32_t originalKeyCode, int32_t rotatedKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003247 ui::LogicalDisplayId displayId = ui::ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248};
3249
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003250/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3251 * orientation.
3252 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003253void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003254 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3255 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003256}
3257
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003258void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003259 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003260 int32_t rotatedKeyCode,
3261 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003262 NotifyKeyArgs args;
3263
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003264 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003265 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3266 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3267 ASSERT_EQ(originalScanCode, args.scanCode);
3268 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003269 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003271 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3273 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3274 ASSERT_EQ(originalScanCode, args.scanCode);
3275 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003276 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003277}
3278
Michael Wrightd02c5b62014-02-10 15:10:22 -08003279TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003280 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003281 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003282 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003284 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003285}
3286
3287TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3288 const int32_t USAGE_A = 0x070004;
3289 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003290 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3291 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003292 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3293 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3294 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003295
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003296 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003297 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003298 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003299 // Initial metastate is AMETA_NONE.
3300 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003301
3302 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003304 NotifyKeyArgs args;
3305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3306 ASSERT_EQ(DEVICE_ID, args.deviceId);
3307 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3308 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3309 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3310 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3311 ASSERT_EQ(KEY_HOME, args.scanCode);
3312 ASSERT_EQ(AMETA_NONE, args.metaState);
3313 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3314 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3315 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3316
3317 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003318 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3320 ASSERT_EQ(DEVICE_ID, args.deviceId);
3321 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3322 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3323 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3324 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3325 ASSERT_EQ(KEY_HOME, args.scanCode);
3326 ASSERT_EQ(AMETA_NONE, args.metaState);
3327 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3328 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3329 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3330
3331 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3333 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3335 ASSERT_EQ(DEVICE_ID, args.deviceId);
3336 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3337 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3338 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3339 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3340 ASSERT_EQ(0, args.scanCode);
3341 ASSERT_EQ(AMETA_NONE, args.metaState);
3342 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3343 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3344 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3345
3346 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003347 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3348 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3350 ASSERT_EQ(DEVICE_ID, args.deviceId);
3351 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3352 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3353 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3354 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3355 ASSERT_EQ(0, args.scanCode);
3356 ASSERT_EQ(AMETA_NONE, args.metaState);
3357 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3358 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3359 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3360
3361 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3365 ASSERT_EQ(DEVICE_ID, args.deviceId);
3366 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3367 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3368 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3369 ASSERT_EQ(0, args.keyCode);
3370 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3371 ASSERT_EQ(AMETA_NONE, args.metaState);
3372 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3373 ASSERT_EQ(0U, args.policyFlags);
3374 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3375
3376 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003377 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3378 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3380 ASSERT_EQ(DEVICE_ID, args.deviceId);
3381 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3382 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3383 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3384 ASSERT_EQ(0, args.keyCode);
3385 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3386 ASSERT_EQ(AMETA_NONE, args.metaState);
3387 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3388 ASSERT_EQ(0U, args.policyFlags);
3389 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3390}
3391
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003392TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3394 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3395 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3396
3397 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003398 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003399 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3400
3401 // Key down by scan code.
3402 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3403 NotifyKeyArgs args;
3404 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3405 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3406
3407 // Key up by scan code.
3408 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3410 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3411}
3412
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003413/**
3414 * Ensure that the readTime is set to the time when the EV_KEY is received.
3415 */
3416TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3417 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3418
3419 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003420 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003421 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3422 NotifyKeyArgs args;
3423
3424 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003425 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3427 ASSERT_EQ(12, args.readTime);
3428
3429 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003430 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3432 ASSERT_EQ(15, args.readTime);
3433}
3434
Michael Wrightd02c5b62014-02-10 15:10:22 -08003435TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003436 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3437 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003438 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3439 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3440 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003441
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003443 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003444 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445
Arthur Hung95f68612022-04-07 14:08:22 +08003446 // Initial metastate is AMETA_NONE.
3447 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
3449 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003450 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451 NotifyKeyArgs args;
3452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3453 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003455 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003456
3457 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003458 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3460 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003461 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462
3463 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003464 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003465 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3466 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003467 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003468
3469 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003470 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3472 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003473 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003474 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475}
3476
3477TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003478 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3479 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3480 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3481 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003482
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003483 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003484 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003485 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003486
Michael Wrighta9cf4192022-12-01 23:46:39 +00003487 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003488 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3489 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3490 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3491 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3492 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3493 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3495 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3496}
3497
3498TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003499 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3500 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3501 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3502 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003503
Michael Wrightd02c5b62014-02-10 15:10:22 -08003504 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003505 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003506 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003507 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003508
Michael Wrighta9cf4192022-12-01 23:46:39 +00003509 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003510 ASSERT_NO_FATAL_FAILURE(
3511 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3512 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3513 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3514 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3515 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3516 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3517 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003518
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003519 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003520 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003521 ASSERT_NO_FATAL_FAILURE(
3522 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3523 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3524 AKEYCODE_DPAD_UP, DISPLAY_ID));
3525 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3526 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3527 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3528 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003529
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003530 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003531 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003532 ASSERT_NO_FATAL_FAILURE(
3533 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3534 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3535 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3536 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3537 AKEYCODE_DPAD_UP, DISPLAY_ID));
3538 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3539 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003540
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003541 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003542 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003543 ASSERT_NO_FATAL_FAILURE(
3544 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3545 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3546 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3547 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3548 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3549 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3550 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003551
3552 // Special case: if orientation changes while key is down, we still emit the same keycode
3553 // in the key up as we did in the key down.
3554 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003555 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003556 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003557 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3559 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3560 ASSERT_EQ(KEY_UP, args.scanCode);
3561 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3562
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003563 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003564 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003565 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3567 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3568 ASSERT_EQ(KEY_UP, args.scanCode);
3569 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3570}
3571
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003572TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3573 // If the keyboard is not orientation aware,
3574 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003575 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003577 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003578 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003579 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003580 NotifyKeyArgs args;
3581
3582 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003585 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Linnan Li13bf76a2024-05-05 19:18:02 +08003587 ASSERT_EQ(ui::ADISPLAY_ID_NONE, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003588
Michael Wrighta9cf4192022-12-01 23:46:39 +00003589 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003590 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Linnan Li13bf76a2024-05-05 19:18:02 +08003594 ASSERT_EQ(ui::ADISPLAY_ID_NONE, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003595}
3596
3597TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3598 // If the keyboard is orientation aware,
3599 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003600 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003601
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003602 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003603 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003604 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003605 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003606 NotifyKeyArgs args;
3607
3608 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3609 // ^--- already checked by the previous test
3610
Michael Wrighta9cf4192022-12-01 23:46:39 +00003611 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003612 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003615 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3617 ASSERT_EQ(DISPLAY_ID, args.displayId);
3618
Linnan Li13bf76a2024-05-05 19:18:02 +08003619 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003620 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003621 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003622 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003623 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003625 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3627 ASSERT_EQ(newDisplayId, args.displayId);
3628}
3629
Michael Wrightd02c5b62014-02-10 15:10:22 -08003630TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003631 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003632 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003635 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003636 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003637
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003638 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640}
3641
Philip Junker4af3b3d2021-12-14 10:36:55 +01003642TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3643 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003644 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003645 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3646
3647 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3648 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3649 << "If a mapping is available, the result is equal to the mapping";
3650
3651 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3652 << "If no mapping is available, the result is the key location";
3653}
3654
Michael Wrightd02c5b62014-02-10 15:10:22 -08003655TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003656 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003657 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003658 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003659
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003660 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003661 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003663 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003664 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003665}
3666
3667TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003668 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003669 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003670 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003671
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003672 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003673
Michael Wrightd02c5b62014-02-10 15:10:22 -08003674 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003675 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003676 ASSERT_TRUE(flags[0]);
3677 ASSERT_FALSE(flags[1]);
3678}
3679
3680TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003681 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3682 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3683 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3684 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3685 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3686 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003687
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003688 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003689 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003690 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003691 // Initial metastate is AMETA_NONE.
3692 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003693
3694 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3696 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3697 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003698
3699 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003700 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003702 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3704 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003705 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003706
3707 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003708 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003710 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3711 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3712 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003713 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003714
3715 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003716 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3717 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003718 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3719 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3720 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003721 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003722
3723 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003724 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3725 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003726 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3727 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3728 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003729 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003730
3731 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003732 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3733 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003734 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3735 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3736 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003737 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003738
3739 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003740 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3741 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003742 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3743 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3744 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003745 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003746}
3747
Chris Yea52ade12020-08-27 16:49:20 -07003748TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3749 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3750 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3751 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3752 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3753
3754 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003755 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003756 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3757
Chris Yea52ade12020-08-27 16:49:20 -07003758 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003759 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003760 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3761 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3762 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3763 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3764
3765 NotifyKeyArgs args;
3766 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003767 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3769 ASSERT_EQ(AMETA_NONE, args.metaState);
3770 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3771 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3772 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3773
3774 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003775 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3777 ASSERT_EQ(AMETA_NONE, args.metaState);
3778 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3779 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3780 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3781}
3782
Arthur Hung2c9a3342019-07-23 14:18:59 +08003783TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3784 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003785 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3786 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3787 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3788 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003789
3790 // keyboard 2.
3791 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003792 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003793 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003794 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003795 std::shared_ptr<InputDevice> device2 =
3796 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003797 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003799 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3800 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3801 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3802 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003803
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003804 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003805 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003806 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003807
Arpit Singh67ca6842023-04-26 14:43:16 +00003808 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003809 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003810 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3811 mFakePolicy
3812 ->getReaderConfiguration(),
3813 AINPUT_SOURCE_KEYBOARD,
3814 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003815 std::list<NotifyArgs> unused =
3816 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003817 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003818 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003819
3820 // Prepared displays and associated info.
3821 constexpr uint8_t hdmi1 = 0;
3822 constexpr uint8_t hdmi2 = 1;
3823 const std::string SECONDARY_UNIQUE_ID = "local:1";
3824
3825 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3826 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3827
3828 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003829 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003830 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003831 ASSERT_FALSE(device2->isEnabled());
3832
3833 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003834 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003835 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003836 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003837 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003838 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003839 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003840 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003841 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003842
3843 // Device should be enabled after the associated display is found.
3844 ASSERT_TRUE(mDevice->isEnabled());
3845 ASSERT_TRUE(device2->isEnabled());
3846
3847 // Test pad key events
3848 ASSERT_NO_FATAL_FAILURE(
3849 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3850 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3851 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3852 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3853 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3854 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3855 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3856
3857 ASSERT_NO_FATAL_FAILURE(
3858 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3859 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3860 AKEYCODE_DPAD_RIGHT, newDisplayId));
3861 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3862 AKEYCODE_DPAD_DOWN, newDisplayId));
3863 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3864 AKEYCODE_DPAD_LEFT, newDisplayId));
3865}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003866
arthurhungc903df12020-08-11 15:08:42 +08003867TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3868 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3869 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3870 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3871 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3872 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3873 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3874
3875 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003876 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003877 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003878 // Initial metastate is AMETA_NONE.
3879 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003880
3881 // Initialization should have turned all of the lights off.
3882 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3883 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3884 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3885
3886 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003887 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3888 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003889 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3890 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3891
3892 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3894 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003895 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3896 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3897
3898 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003899 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3900 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003901 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3902 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3903
3904 mFakeEventHub->removeDevice(EVENTHUB_ID);
3905 mReader->loopOnce();
3906
3907 // keyboard 2 should default toggle keys.
3908 const std::string USB2 = "USB2";
3909 const std::string DEVICE_NAME2 = "KEYBOARD2";
3910 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3911 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3912 std::shared_ptr<InputDevice> device2 =
3913 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003914 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003915 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3916 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3917 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3918 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3919 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3920 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3921
Arpit Singh67ca6842023-04-26 14:43:16 +00003922 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003923 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003924 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3925 mFakePolicy
3926 ->getReaderConfiguration(),
3927 AINPUT_SOURCE_KEYBOARD,
3928 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003929 std::list<NotifyArgs> unused =
3930 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003931 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003932 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003933
3934 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3935 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3936 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003937 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3938 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003939}
3940
Arthur Hungcb40a002021-08-03 14:31:01 +00003941TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3942 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3943 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3944 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3945
3946 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003947 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003948 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3949 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003950 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003951 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003952 // Initial metastate is AMETA_NONE.
3953 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003954
3955 mReader->toggleCapsLockState(DEVICE_ID);
3956 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3957}
3958
Arthur Hungfb3cc112022-04-13 07:39:50 +00003959TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3960 // keyboard 1.
3961 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3962 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3963 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3964 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3965 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3966 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3967
3968 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003969 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003970 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3971
3972 // keyboard 2.
3973 const std::string USB2 = "USB2";
3974 const std::string DEVICE_NAME2 = "KEYBOARD2";
3975 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3976 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3977 std::shared_ptr<InputDevice> device2 =
3978 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3979 ftl::Flags<InputDeviceClass>(0));
3980 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3981 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3982 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3983 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3984 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3985 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3986
Arpit Singh67ca6842023-04-26 14:43:16 +00003987 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003988 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003989 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3990 mFakePolicy
3991 ->getReaderConfiguration(),
3992 AINPUT_SOURCE_KEYBOARD,
3993 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003994 std::list<NotifyArgs> unused =
3995 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003996 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003997 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003998
Arthur Hung95f68612022-04-07 14:08:22 +08003999 // Initial metastate is AMETA_NONE.
4000 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4001 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4002
4003 // Toggle num lock on and off.
4004 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4005 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00004006 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4007 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
4008 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
4009
4010 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
4011 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
4012 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
4013 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4014 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4015
4016 // Toggle caps lock on and off.
4017 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4018 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4019 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4020 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
4021 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
4022
4023 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
4024 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
4025 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
4026 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4027 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4028
4029 // Toggle scroll lock on and off.
4030 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4031 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4032 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4033 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4034 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4035
4036 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4037 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4038 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4039 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4040 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4041}
4042
Arthur Hung2141d542022-08-23 07:45:21 +00004043TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4044 const int32_t USAGE_A = 0x070004;
4045 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4046 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4047
4048 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004049 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004050 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4051 // Key down by scan code.
4052 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4053 NotifyKeyArgs args;
4054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4055 ASSERT_EQ(DEVICE_ID, args.deviceId);
4056 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4057 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4058 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4059 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4060 ASSERT_EQ(KEY_HOME, args.scanCode);
4061 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4062
4063 // Disable device, it should synthesize cancellation events for down events.
4064 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004065 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004066
4067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4068 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4069 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4070 ASSERT_EQ(KEY_HOME, args.scanCode);
4071 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4072}
4073
Zixuan Qufecb6062022-11-12 04:44:31 +00004074TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004075 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4076 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004077 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004078 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4079 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004080
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004081 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004082 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4083
4084 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004085 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004086
4087 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4088 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4089 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4090 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4091 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004092 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4093
4094 // Call change layout association with the same values: Generation shouldn't change
4095 generation = mReader->getContext()->getGeneration();
4096 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4097 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4098 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4099 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004100}
4101
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004102TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4103 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4104 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4105
4106 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004107 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004108 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4109 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004110 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004111
4112 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4113 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4114}
4115
Justin Chung71ddb432023-03-27 04:29:07 +00004116TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4117 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4118 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004119 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004120 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4121 NotifyKeyArgs args;
4122
4123 // Key down
4124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4126 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4127}
4128
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004129// --- KeyboardInputMapperTest_ExternalDevice ---
4130
4131class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4132protected:
Chris Yea52ade12020-08-27 16:49:20 -07004133 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004134};
4135
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004136TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004137 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4138 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004139
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004140 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4141 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4142 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4143 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004144
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004145 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004146 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004147 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004148
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004149 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150 NotifyKeyArgs args;
4151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4152 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4153
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004154 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4156 ASSERT_EQ(uint32_t(0), args.policyFlags);
4157
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004159 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004160 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004161
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004162 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4164 ASSERT_EQ(uint32_t(0), args.policyFlags);
4165
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004166 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4168 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4169
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004170 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4172 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4173}
4174
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004175TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior_NoneAlphabeticKeyboard) {
4176 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4177 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4178
4179 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4180 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4181 POLICY_FLAG_WAKE);
4182
4183 KeyboardInputMapper& mapper =
4184 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4185 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
4186
4187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4188 NotifyKeyArgs args;
4189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4190 ASSERT_EQ(uint32_t(0), args.policyFlags);
4191
4192 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4194 ASSERT_EQ(uint32_t(0), args.policyFlags);
4195
4196 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4198 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4199
4200 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4201 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4202 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4203}
4204
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004205TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004206 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004207
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004208 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4209 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4210 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004211
Powei Fengd041c5d2019-05-03 17:11:33 -07004212 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004213 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004214 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004215 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004216
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004217 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004218 NotifyKeyArgs args;
4219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4220 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4221
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004222 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4224 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4225
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004227 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4228 ASSERT_EQ(uint32_t(0), args.policyFlags);
4229
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004230 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4232 ASSERT_EQ(uint32_t(0), args.policyFlags);
4233
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004234 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4236 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4237
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004238 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4240 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4241}
4242
Michael Wrightd02c5b62014-02-10 15:10:22 -08004243// --- TouchInputMapperTest ---
4244
4245class TouchInputMapperTest : public InputMapperTest {
4246protected:
4247 static const int32_t RAW_X_MIN;
4248 static const int32_t RAW_X_MAX;
4249 static const int32_t RAW_Y_MIN;
4250 static const int32_t RAW_Y_MAX;
4251 static const int32_t RAW_TOUCH_MIN;
4252 static const int32_t RAW_TOUCH_MAX;
4253 static const int32_t RAW_TOOL_MIN;
4254 static const int32_t RAW_TOOL_MAX;
4255 static const int32_t RAW_PRESSURE_MIN;
4256 static const int32_t RAW_PRESSURE_MAX;
4257 static const int32_t RAW_ORIENTATION_MIN;
4258 static const int32_t RAW_ORIENTATION_MAX;
4259 static const int32_t RAW_DISTANCE_MIN;
4260 static const int32_t RAW_DISTANCE_MAX;
4261 static const int32_t RAW_TILT_MIN;
4262 static const int32_t RAW_TILT_MAX;
4263 static const int32_t RAW_ID_MIN;
4264 static const int32_t RAW_ID_MAX;
4265 static const int32_t RAW_SLOT_MIN;
4266 static const int32_t RAW_SLOT_MAX;
4267 static const float X_PRECISION;
4268 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004269 static const float X_PRECISION_VIRTUAL;
4270 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004271
4272 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004273 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004274
4275 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4276
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004277 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004278 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004279
Michael Wrightd02c5b62014-02-10 15:10:22 -08004280 enum Axes {
4281 POSITION = 1 << 0,
4282 TOUCH = 1 << 1,
4283 TOOL = 1 << 2,
4284 PRESSURE = 1 << 3,
4285 ORIENTATION = 1 << 4,
4286 MINOR = 1 << 5,
4287 ID = 1 << 6,
4288 DISTANCE = 1 << 7,
4289 TILT = 1 << 8,
4290 SLOT = 1 << 9,
4291 TOOL_TYPE = 1 << 10,
4292 };
4293
Michael Wrighta9cf4192022-12-01 23:46:39 +00004294 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004295 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004296 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004298 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004299 int32_t toRawX(float displayX);
4300 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004301 int32_t toRotatedRawX(float displayX);
4302 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004303 float toCookedX(float rawX, float rawY);
4304 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004306 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004307 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004308 float toDisplayY(int32_t rawY, int32_t displayHeight);
4309
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310};
4311
4312const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4313const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4314const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4315const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4316const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4317const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4318const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4319const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004320const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4321const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4323const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4324const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4325const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4326const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4327const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4328const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4329const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4330const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4331const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4332const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4333const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004334const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4335 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4336const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4337 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004338const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4339 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340
4341const float TouchInputMapperTest::GEOMETRIC_SCALE =
4342 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4343 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4344
4345const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4346 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4347 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4348};
4349
Michael Wrighta9cf4192022-12-01 23:46:39 +00004350void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004351 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4352 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004353}
4354
4355void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4356 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004357 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004358}
4359
Michael Wrighta9cf4192022-12-01 23:46:39 +00004360void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004361 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4362 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4363 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004364}
4365
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004367 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4368 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4370 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371}
4372
Jason Gerecke489fda82012-09-07 17:19:40 -07004373void TouchInputMapperTest::prepareLocationCalibration() {
4374 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4375}
4376
Michael Wrightd02c5b62014-02-10 15:10:22 -08004377int32_t TouchInputMapperTest::toRawX(float displayX) {
4378 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4379}
4380
4381int32_t TouchInputMapperTest::toRawY(float displayY) {
4382 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4383}
4384
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004385int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4386 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4387}
4388
4389int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4390 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4391}
4392
Jason Gerecke489fda82012-09-07 17:19:40 -07004393float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4394 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4395 return rawX;
4396}
4397
4398float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4399 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4400 return rawY;
4401}
4402
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004404 return toDisplayX(rawX, DISPLAY_WIDTH);
4405}
4406
4407float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4408 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004409}
4410
4411float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004412 return toDisplayY(rawY, DISPLAY_HEIGHT);
4413}
4414
4415float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4416 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004417}
4418
4419
4420// --- SingleTouchInputMapperTest ---
4421
4422class SingleTouchInputMapperTest : public TouchInputMapperTest {
4423protected:
4424 void prepareButtons();
4425 void prepareAxes(int axes);
4426
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004427 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4428 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4429 void processUp(SingleTouchInputMapper& mappery);
4430 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4431 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4432 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4433 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4434 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4435 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004436};
4437
4438void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004439 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004440}
4441
4442void SingleTouchInputMapperTest::prepareAxes(int axes) {
4443 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004444 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4445 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 }
4447 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004448 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4449 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004450 }
4451 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004452 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4453 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004454 }
4455 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004456 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4457 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004458 }
4459 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004460 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4461 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004462 }
4463}
4464
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004465void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004466 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4467 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::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004472 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4473 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004474}
4475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004476void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004477 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478}
4479
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004480void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004481 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482}
4483
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004484void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4485 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004486 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004487}
4488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004489void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004490 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491}
4492
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004493void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
4494 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497}
4498
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004499void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
4500 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004501 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502}
4503
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004504void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004505 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004506}
4507
Michael Wrightd02c5b62014-02-10 15:10:22 -08004508TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004509 prepareButtons();
4510 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004511 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004512
Josep del Río2d8c79a2023-01-23 19:33:50 +00004513 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004514}
4515
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004517 prepareButtons();
4518 prepareAxes(POSITION);
4519 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004520 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004521
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004522 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523}
4524
4525TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004526 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004527 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004528 prepareButtons();
4529 prepareAxes(POSITION);
4530 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004531 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004532
4533 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004534 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004535
4536 // Virtual key is down.
4537 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4538 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4539 processDown(mapper, x, y);
4540 processSync(mapper);
4541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4542
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004543 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004544
4545 // Virtual key is up.
4546 processUp(mapper);
4547 processSync(mapper);
4548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4549
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004550 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004551}
4552
4553TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004554 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004555 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004556 prepareButtons();
4557 prepareAxes(POSITION);
4558 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004559 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004560
4561 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004562 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004563
4564 // Virtual key is down.
4565 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4566 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4567 processDown(mapper, x, y);
4568 processSync(mapper);
4569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4570
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004571 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572
4573 // Virtual key is up.
4574 processUp(mapper);
4575 processSync(mapper);
4576 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4577
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004578 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579}
4580
4581TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004582 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004583 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584 prepareButtons();
4585 prepareAxes(POSITION);
4586 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004587 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588
Michael Wrightd02c5b62014-02-10 15:10:22 -08004589 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004590 ASSERT_TRUE(
4591 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004592 ASSERT_TRUE(flags[0]);
4593 ASSERT_FALSE(flags[1]);
4594}
4595
4596TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004597 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004598 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004599 prepareButtons();
4600 prepareAxes(POSITION);
4601 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004602 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004603
arthurhungdcef2dc2020-08-11 14:47:50 +08004604 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004605
4606 NotifyKeyArgs args;
4607
4608 // Press virtual key.
4609 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4610 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4611 processDown(mapper, x, y);
4612 processSync(mapper);
4613
4614 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4615 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4616 ASSERT_EQ(DEVICE_ID, args.deviceId);
4617 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4618 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4619 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4620 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4621 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4622 ASSERT_EQ(KEY_HOME, args.scanCode);
4623 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4624 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4625
4626 // Release virtual key.
4627 processUp(mapper);
4628 processSync(mapper);
4629
4630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4631 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4632 ASSERT_EQ(DEVICE_ID, args.deviceId);
4633 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4634 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4635 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4636 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4637 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4638 ASSERT_EQ(KEY_HOME, args.scanCode);
4639 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4640 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4641
4642 // Should not have sent any motions.
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4644}
4645
4646TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004647 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004648 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004649 prepareButtons();
4650 prepareAxes(POSITION);
4651 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004652 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004653
arthurhungdcef2dc2020-08-11 14:47:50 +08004654 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004655
4656 NotifyKeyArgs keyArgs;
4657
4658 // Press virtual key.
4659 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4660 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4661 processDown(mapper, x, y);
4662 processSync(mapper);
4663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4665 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4666 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4667 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4668 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4669 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4670 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4671 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4672 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4673 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4674 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4675
4676 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4677 // into the display area.
4678 y -= 100;
4679 processMove(mapper, x, y);
4680 processSync(mapper);
4681
4682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4683 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4684 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4685 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4686 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4687 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4688 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4689 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4690 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4691 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4692 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4693 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4694
4695 NotifyMotionArgs motionArgs;
4696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4697 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4698 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4699 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4700 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4702 ASSERT_EQ(0, motionArgs.flags);
4703 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4704 ASSERT_EQ(0, motionArgs.buttonState);
4705 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004706 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004707 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004708 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004709 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4710 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4711 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4712 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4713 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4714
4715 // Keep moving out of bounds. Should generate a pointer move.
4716 y -= 50;
4717 processMove(mapper, x, y);
4718 processSync(mapper);
4719
4720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4726 ASSERT_EQ(0, motionArgs.flags);
4727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4728 ASSERT_EQ(0, motionArgs.buttonState);
4729 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004730 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004732 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004733 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4734 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4735 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4736 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4737 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4738
4739 // Release out of bounds. Should generate a pointer up.
4740 processUp(mapper);
4741 processSync(mapper);
4742
4743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4748 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4749 ASSERT_EQ(0, motionArgs.flags);
4750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4751 ASSERT_EQ(0, motionArgs.buttonState);
4752 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004753 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4757 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4758 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4759 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4761
4762 // Should not have sent any more keys or motions.
4763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4764 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4765}
4766
4767TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004769 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004770 prepareButtons();
4771 prepareAxes(POSITION);
4772 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004773 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004774
arthurhungdcef2dc2020-08-11 14:47:50 +08004775 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004776
4777 NotifyMotionArgs motionArgs;
4778
4779 // Initially go down out of bounds.
4780 int32_t x = -10;
4781 int32_t y = -10;
4782 processDown(mapper, x, y);
4783 processSync(mapper);
4784
4785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4786
4787 // Move into the display area. Should generate a pointer down.
4788 x = 50;
4789 y = 75;
4790 processMove(mapper, x, y);
4791 processSync(mapper);
4792
4793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4794 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4795 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4796 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4797 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4798 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4799 ASSERT_EQ(0, motionArgs.flags);
4800 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4801 ASSERT_EQ(0, motionArgs.buttonState);
4802 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004803 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004804 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004805 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004806 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4807 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4808 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4809 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4810 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4811
4812 // Release. Should generate a pointer up.
4813 processUp(mapper);
4814 processSync(mapper);
4815
4816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4817 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4818 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4819 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4820 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4821 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4822 ASSERT_EQ(0, motionArgs.flags);
4823 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4824 ASSERT_EQ(0, motionArgs.buttonState);
4825 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004826 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004827 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004828 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004829 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4830 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4831 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4832 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4833 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4834
4835 // Should not have sent any more keys or motions.
4836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4838}
4839
Santos Cordonfa5cf462017-04-05 10:37:00 -07004840TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004841 addConfigurationProperty("touch.deviceType", "touchScreen");
4842 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4843
Michael Wrighta9cf4192022-12-01 23:46:39 +00004844 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004845 prepareButtons();
4846 prepareAxes(POSITION);
4847 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004848 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004849
arthurhungdcef2dc2020-08-11 14:47:50 +08004850 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004851
4852 NotifyMotionArgs motionArgs;
4853
4854 // Down.
4855 int32_t x = 100;
4856 int32_t y = 125;
4857 processDown(mapper, x, y);
4858 processSync(mapper);
4859
4860 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4861 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4862 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4863 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4864 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4865 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4866 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4867 ASSERT_EQ(0, motionArgs.flags);
4868 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4869 ASSERT_EQ(0, motionArgs.buttonState);
4870 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004871 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004872 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004873 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4875 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4876 1, 0, 0, 0, 0, 0, 0, 0));
4877 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4878 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4879 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4880
4881 // Move.
4882 x += 50;
4883 y += 75;
4884 processMove(mapper, x, y);
4885 processSync(mapper);
4886
4887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4888 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4889 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4890 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4891 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4892 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4894 ASSERT_EQ(0, motionArgs.flags);
4895 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4896 ASSERT_EQ(0, motionArgs.buttonState);
4897 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004898 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004899 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004900 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004901 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4902 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4903 1, 0, 0, 0, 0, 0, 0, 0));
4904 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4905 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4906 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4907
4908 // Up.
4909 processUp(mapper);
4910 processSync(mapper);
4911
4912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4913 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4914 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4915 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4916 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4917 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4918 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4919 ASSERT_EQ(0, motionArgs.flags);
4920 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4921 ASSERT_EQ(0, motionArgs.buttonState);
4922 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004923 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004924 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004925 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004926 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4927 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4928 1, 0, 0, 0, 0, 0, 0, 0));
4929 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4930 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4931 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4932
4933 // Should not have sent any more keys or motions.
4934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4936}
4937
Michael Wrightd02c5b62014-02-10 15:10:22 -08004938TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004940 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004941 prepareButtons();
4942 prepareAxes(POSITION);
4943 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004944 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945
arthurhungdcef2dc2020-08-11 14:47:50 +08004946 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004947
4948 NotifyMotionArgs motionArgs;
4949
4950 // Down.
4951 int32_t x = 100;
4952 int32_t y = 125;
4953 processDown(mapper, x, y);
4954 processSync(mapper);
4955
4956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4957 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4958 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4959 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4960 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4961 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4962 ASSERT_EQ(0, motionArgs.flags);
4963 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4964 ASSERT_EQ(0, motionArgs.buttonState);
4965 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004966 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004967 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004968 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004969 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4970 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4971 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4972 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4973 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4974
4975 // Move.
4976 x += 50;
4977 y += 75;
4978 processMove(mapper, x, y);
4979 processSync(mapper);
4980
4981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4982 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4983 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4984 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4985 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4986 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4987 ASSERT_EQ(0, motionArgs.flags);
4988 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4989 ASSERT_EQ(0, motionArgs.buttonState);
4990 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004991 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004992 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004993 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004994 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4995 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4996 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4997 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4998 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4999
5000 // Up.
5001 processUp(mapper);
5002 processSync(mapper);
5003
5004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5005 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
5006 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
5007 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
5008 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
5009 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5010 ASSERT_EQ(0, motionArgs.flags);
5011 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
5012 ASSERT_EQ(0, motionArgs.buttonState);
5013 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07005014 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005015 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005016 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005017 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5018 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
5019 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
5020 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
5021 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
5022
5023 // Should not have sent any more keys or motions.
5024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5026}
5027
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005028TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005029 addConfigurationProperty("touch.deviceType", "touchScreen");
5030 prepareButtons();
5031 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005032 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
5033 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00005034 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005035
5036 NotifyMotionArgs args;
5037
5038 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00005039 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005040 processDown(mapper, toRawX(50), toRawY(75));
5041 processSync(mapper);
5042
5043 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5044 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5045 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5046
5047 processUp(mapper);
5048 processSync(mapper);
5049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5050}
5051
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005052TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005053 addConfigurationProperty("touch.deviceType", "touchScreen");
5054 prepareButtons();
5055 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07005056 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5057 // orientation-aware are affected by display rotation.
5058 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00005059 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005060
5061 NotifyMotionArgs args;
5062
5063 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005064 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005065 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005066 processDown(mapper, toRawX(50), toRawY(75));
5067 processSync(mapper);
5068
5069 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5070 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5071 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5072
5073 processUp(mapper);
5074 processSync(mapper);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5076
5077 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005078 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005079 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005080 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005081 processSync(mapper);
5082
5083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5084 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5085 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5086
5087 processUp(mapper);
5088 processSync(mapper);
5089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5090
5091 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005092 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005093 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005094 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5095 processSync(mapper);
5096
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5098 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5099 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5100
5101 processUp(mapper);
5102 processSync(mapper);
5103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5104
5105 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005106 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005107 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005108 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005109 processSync(mapper);
5110
5111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5112 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5113 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5114
5115 processUp(mapper);
5116 processSync(mapper);
5117 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5118}
5119
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005120TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5121 addConfigurationProperty("touch.deviceType", "touchScreen");
5122 prepareButtons();
5123 prepareAxes(POSITION);
5124 addConfigurationProperty("touch.orientationAware", "1");
5125 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5126 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005127 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005128 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005129 NotifyMotionArgs args;
5130
5131 // Orientation 0.
5132 processDown(mapper, toRawX(50), toRawY(75));
5133 processSync(mapper);
5134
5135 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5136 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5137 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5138
5139 processUp(mapper);
5140 processSync(mapper);
5141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5142}
5143
5144TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5145 addConfigurationProperty("touch.deviceType", "touchScreen");
5146 prepareButtons();
5147 prepareAxes(POSITION);
5148 addConfigurationProperty("touch.orientationAware", "1");
5149 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5150 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005151 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005152 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005153 NotifyMotionArgs args;
5154
5155 // Orientation 90.
5156 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5157 processSync(mapper);
5158
5159 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5160 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5161 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5162
5163 processUp(mapper);
5164 processSync(mapper);
5165 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5166}
5167
5168TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5169 addConfigurationProperty("touch.deviceType", "touchScreen");
5170 prepareButtons();
5171 prepareAxes(POSITION);
5172 addConfigurationProperty("touch.orientationAware", "1");
5173 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5174 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005175 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005176 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005177 NotifyMotionArgs args;
5178
5179 // Orientation 180.
5180 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5181 processSync(mapper);
5182
5183 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5184 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5185 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5186
5187 processUp(mapper);
5188 processSync(mapper);
5189 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5190}
5191
5192TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5193 addConfigurationProperty("touch.deviceType", "touchScreen");
5194 prepareButtons();
5195 prepareAxes(POSITION);
5196 addConfigurationProperty("touch.orientationAware", "1");
5197 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5198 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005199 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005200 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005201 NotifyMotionArgs args;
5202
5203 // Orientation 270.
5204 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5205 processSync(mapper);
5206
5207 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5208 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5209 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5210
5211 processUp(mapper);
5212 processSync(mapper);
5213 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5214}
5215
5216TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5217 addConfigurationProperty("touch.deviceType", "touchScreen");
5218 prepareButtons();
5219 prepareAxes(POSITION);
5220 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5221 // orientation-aware are affected by display rotation.
5222 addConfigurationProperty("touch.orientationAware", "0");
5223 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005224 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005225
5226 NotifyMotionArgs args;
5227
5228 // Orientation 90, Rotation 0.
5229 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005230 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005231 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5232 processSync(mapper);
5233
5234 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5235 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5236 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5237
5238 processUp(mapper);
5239 processSync(mapper);
5240 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5241
5242 // Orientation 90, Rotation 90.
5243 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005244 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005245 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005246 processSync(mapper);
5247
5248 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5249 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5250 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5251
5252 processUp(mapper);
5253 processSync(mapper);
5254 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5255
5256 // Orientation 90, Rotation 180.
5257 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005258 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005259 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5260 processSync(mapper);
5261
5262 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5263 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5264 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5265
5266 processUp(mapper);
5267 processSync(mapper);
5268 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5269
5270 // Orientation 90, Rotation 270.
5271 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005272 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005273 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 -07005274 processSync(mapper);
5275
5276 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5277 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5278 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5279
5280 processUp(mapper);
5281 processSync(mapper);
5282 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5283}
5284
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005285TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5286 addConfigurationProperty("touch.deviceType", "touchScreen");
5287 prepareButtons();
5288 prepareAxes(POSITION);
5289 addConfigurationProperty("touch.orientationAware", "1");
5290 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005291 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005292
5293 // Set a physical frame in the display viewport.
5294 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5295 viewport->physicalLeft = 20;
5296 viewport->physicalTop = 600;
5297 viewport->physicalRight = 30;
5298 viewport->physicalBottom = 610;
5299 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005300 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005301
5302 // Start the touch.
5303 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5304 processSync(mapper);
5305
5306 // Expect all input starting outside the physical frame to be ignored.
5307 const std::array<Point, 6> outsidePoints = {
5308 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5309 for (const auto& p : outsidePoints) {
5310 processMove(mapper, toRawX(p.x), toRawY(p.y));
5311 processSync(mapper);
5312 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5313 }
5314
5315 // Move the touch into the physical frame.
5316 processMove(mapper, toRawX(25), toRawY(605));
5317 processSync(mapper);
5318 NotifyMotionArgs args;
5319 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5320 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5321 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5322 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5323
5324 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5325 for (const auto& p : outsidePoints) {
5326 processMove(mapper, toRawX(p.x), toRawY(p.y));
5327 processSync(mapper);
5328 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5329 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5330 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5331 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5332 }
5333
5334 processUp(mapper);
5335 processSync(mapper);
5336 EXPECT_NO_FATAL_FAILURE(
5337 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5338}
5339
Harry Cutts1db43992023-06-19 17:05:07 +00005340TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005341 addConfigurationProperty("touch.deviceType", "pointer");
5342 prepareAxes(POSITION);
5343 prepareDisplay(ui::ROTATION_0);
5344 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5345
5346 // Set a physical frame in the display viewport.
5347 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5348 viewport->physicalLeft = 20;
5349 viewport->physicalTop = 600;
5350 viewport->physicalRight = 30;
5351 viewport->physicalBottom = 610;
5352 mFakePolicy->updateViewport(*viewport);
5353 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5354
5355 // Start the touch.
5356 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5357 processSync(mapper);
5358
5359 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5360 // produced.
5361 const std::array<Point, 6> outsidePoints = {
5362 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5363 for (const auto& p : outsidePoints) {
5364 processMove(mapper, toRawX(p.x), toRawY(p.y));
5365 processSync(mapper);
5366 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5367 }
5368}
5369
Michael Wrightd02c5b62014-02-10 15:10:22 -08005370TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005371 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005372 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005373 prepareButtons();
5374 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005375 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005376
5377 // These calculations are based on the input device calibration documentation.
5378 int32_t rawX = 100;
5379 int32_t rawY = 200;
5380 int32_t rawPressure = 10;
5381 int32_t rawToolMajor = 12;
5382 int32_t rawDistance = 2;
5383 int32_t rawTiltX = 30;
5384 int32_t rawTiltY = 110;
5385
5386 float x = toDisplayX(rawX);
5387 float y = toDisplayY(rawY);
5388 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5389 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5390 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5391 float distance = float(rawDistance);
5392
5393 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5394 float tiltScale = M_PI / 180;
5395 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5396 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5397 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5398 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5399
5400 processDown(mapper, rawX, rawY);
5401 processPressure(mapper, rawPressure);
5402 processToolMajor(mapper, rawToolMajor);
5403 processDistance(mapper, rawDistance);
5404 processTilt(mapper, rawTiltX, rawTiltY);
5405 processSync(mapper);
5406
5407 NotifyMotionArgs args;
5408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5409 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5410 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5411 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
5412}
5413
Jason Gerecke489fda82012-09-07 17:19:40 -07005414TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005415 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005416 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005417 prepareLocationCalibration();
5418 prepareButtons();
5419 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005420 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005421
5422 int32_t rawX = 100;
5423 int32_t rawY = 200;
5424
5425 float x = toDisplayX(toCookedX(rawX, rawY));
5426 float y = toDisplayY(toCookedY(rawX, rawY));
5427
5428 processDown(mapper, rawX, rawY);
5429 processSync(mapper);
5430
5431 NotifyMotionArgs args;
5432 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5433 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5434 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5435}
5436
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005438 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005439 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005440 prepareButtons();
5441 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005442 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005443
5444 NotifyMotionArgs motionArgs;
5445 NotifyKeyArgs keyArgs;
5446
5447 processDown(mapper, 100, 200);
5448 processSync(mapper);
5449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5450 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5451 ASSERT_EQ(0, motionArgs.buttonState);
5452
5453 // press BTN_LEFT, release BTN_LEFT
5454 processKey(mapper, BTN_LEFT, 1);
5455 processSync(mapper);
5456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5458 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5459
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5461 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5462 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5463
Michael Wrightd02c5b62014-02-10 15:10:22 -08005464 processKey(mapper, BTN_LEFT, 0);
5465 processSync(mapper);
5466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005467 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005468 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005469
5470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005472 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005473
5474 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5475 processKey(mapper, BTN_RIGHT, 1);
5476 processKey(mapper, BTN_MIDDLE, 1);
5477 processSync(mapper);
5478 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5479 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5480 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5481 motionArgs.buttonState);
5482
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005483 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5484 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5485 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5486
5487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5488 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5489 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5490 motionArgs.buttonState);
5491
Michael Wrightd02c5b62014-02-10 15:10:22 -08005492 processKey(mapper, BTN_RIGHT, 0);
5493 processSync(mapper);
5494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005495 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005496 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005497
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005499 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005500 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005501
5502 processKey(mapper, BTN_MIDDLE, 0);
5503 processSync(mapper);
5504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005505 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005507
5508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005509 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005510 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005511
5512 // press BTN_BACK, release BTN_BACK
5513 processKey(mapper, BTN_BACK, 1);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5516 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5517 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005518
Michael Wrightd02c5b62014-02-10 15:10:22 -08005519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005521 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5522
5523 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5524 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5525 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005526
5527 processKey(mapper, BTN_BACK, 0);
5528 processSync(mapper);
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005531 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005532
5533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005534 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005535 ASSERT_EQ(0, motionArgs.buttonState);
5536
Michael Wrightd02c5b62014-02-10 15:10:22 -08005537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5538 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5539 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5540
5541 // press BTN_SIDE, release BTN_SIDE
5542 processKey(mapper, BTN_SIDE, 1);
5543 processSync(mapper);
5544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5545 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5546 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005547
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005549 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005550 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5551
5552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5553 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5554 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005555
5556 processKey(mapper, BTN_SIDE, 0);
5557 processSync(mapper);
5558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005559 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005560 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005561
5562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005564 ASSERT_EQ(0, motionArgs.buttonState);
5565
Michael Wrightd02c5b62014-02-10 15:10:22 -08005566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5567 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5568 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5569
5570 // press BTN_FORWARD, release BTN_FORWARD
5571 processKey(mapper, BTN_FORWARD, 1);
5572 processSync(mapper);
5573 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5574 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5575 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005576
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005578 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005579 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5580
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5583 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584
5585 processKey(mapper, BTN_FORWARD, 0);
5586 processSync(mapper);
5587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005588 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005589 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005590
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005593 ASSERT_EQ(0, motionArgs.buttonState);
5594
Michael Wrightd02c5b62014-02-10 15:10:22 -08005595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5596 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5597 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5598
5599 // press BTN_EXTRA, release BTN_EXTRA
5600 processKey(mapper, BTN_EXTRA, 1);
5601 processSync(mapper);
5602 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5603 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5604 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005605
Michael Wrightd02c5b62014-02-10 15:10:22 -08005606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005607 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005608 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5609
5610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5611 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5612 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005613
5614 processKey(mapper, BTN_EXTRA, 0);
5615 processSync(mapper);
5616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005617 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005618 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005619
5620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005621 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005622 ASSERT_EQ(0, motionArgs.buttonState);
5623
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5625 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5626 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5627
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5629
Michael Wrightd02c5b62014-02-10 15:10:22 -08005630 // press BTN_STYLUS, release BTN_STYLUS
5631 processKey(mapper, BTN_STYLUS, 1);
5632 processSync(mapper);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5634 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005635 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5636
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5639 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640
5641 processKey(mapper, BTN_STYLUS, 0);
5642 processSync(mapper);
5643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005644 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005645 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005646
5647 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005649 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005650
5651 // press BTN_STYLUS2, release BTN_STYLUS2
5652 processKey(mapper, BTN_STYLUS2, 1);
5653 processSync(mapper);
5654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5655 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005656 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5657
5658 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5659 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5660 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005661
5662 processKey(mapper, BTN_STYLUS2, 0);
5663 processSync(mapper);
5664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005665 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005666 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005667
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005670 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671
5672 // release touch
5673 processUp(mapper);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5677 ASSERT_EQ(0, motionArgs.buttonState);
5678}
5679
5680TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005681 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005682 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005683 prepareButtons();
5684 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005685 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005686
5687 NotifyMotionArgs motionArgs;
5688
5689 // default tool type is finger
5690 processDown(mapper, 100, 200);
5691 processSync(mapper);
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005694 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005695
5696 // eraser
5697 processKey(mapper, BTN_TOOL_RUBBER, 1);
5698 processSync(mapper);
5699 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5700 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005701 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005702
5703 // stylus
5704 processKey(mapper, BTN_TOOL_RUBBER, 0);
5705 processKey(mapper, BTN_TOOL_PEN, 1);
5706 processSync(mapper);
5707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5708 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005709 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005710
5711 // brush
5712 processKey(mapper, BTN_TOOL_PEN, 0);
5713 processKey(mapper, BTN_TOOL_BRUSH, 1);
5714 processSync(mapper);
5715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5716 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005717 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005718
5719 // pencil
5720 processKey(mapper, BTN_TOOL_BRUSH, 0);
5721 processKey(mapper, BTN_TOOL_PENCIL, 1);
5722 processSync(mapper);
5723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5724 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005725 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005726
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005727 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005728 processKey(mapper, BTN_TOOL_PENCIL, 0);
5729 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5730 processSync(mapper);
5731 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5732 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005733 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005734
5735 // mouse
5736 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5737 processKey(mapper, BTN_TOOL_MOUSE, 1);
5738 processSync(mapper);
5739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5740 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005741 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005742
5743 // lens
5744 processKey(mapper, BTN_TOOL_MOUSE, 0);
5745 processKey(mapper, BTN_TOOL_LENS, 1);
5746 processSync(mapper);
5747 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005749 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005750
5751 // double-tap
5752 processKey(mapper, BTN_TOOL_LENS, 0);
5753 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5754 processSync(mapper);
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005757 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005758
5759 // triple-tap
5760 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5761 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5762 processSync(mapper);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005765 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005766
5767 // quad-tap
5768 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5769 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5770 processSync(mapper);
5771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005773 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005774
5775 // finger
5776 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5777 processKey(mapper, BTN_TOOL_FINGER, 1);
5778 processSync(mapper);
5779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5780 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005781 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005782
5783 // stylus trumps finger
5784 processKey(mapper, BTN_TOOL_PEN, 1);
5785 processSync(mapper);
5786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5787 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005788 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005789
5790 // eraser trumps stylus
5791 processKey(mapper, BTN_TOOL_RUBBER, 1);
5792 processSync(mapper);
5793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5794 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005795 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796
5797 // mouse trumps eraser
5798 processKey(mapper, BTN_TOOL_MOUSE, 1);
5799 processSync(mapper);
5800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5801 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005802 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005803
5804 // back to default tool type
5805 processKey(mapper, BTN_TOOL_MOUSE, 0);
5806 processKey(mapper, BTN_TOOL_RUBBER, 0);
5807 processKey(mapper, BTN_TOOL_PEN, 0);
5808 processKey(mapper, BTN_TOOL_FINGER, 0);
5809 processSync(mapper);
5810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5811 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005812 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005813}
5814
5815TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005816 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005817 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005818 prepareButtons();
5819 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005820 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005821 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005822
5823 NotifyMotionArgs motionArgs;
5824
5825 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5826 processKey(mapper, BTN_TOOL_FINGER, 1);
5827 processMove(mapper, 100, 200);
5828 processSync(mapper);
5829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5830 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5831 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5832 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5833
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5838
5839 // move a little
5840 processMove(mapper, 150, 250);
5841 processSync(mapper);
5842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5843 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5844 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5845 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5846
5847 // down when BTN_TOUCH is pressed, pressure defaults to 1
5848 processKey(mapper, BTN_TOUCH, 1);
5849 processSync(mapper);
5850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5851 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5852 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5853 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5854
5855 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5856 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5858 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5859
5860 // up when BTN_TOUCH is released, hover restored
5861 processKey(mapper, BTN_TOUCH, 0);
5862 processSync(mapper);
5863 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5864 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5865 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5866 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5867
5868 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5869 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5870 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5871 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5872
5873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5874 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5875 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5876 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5877
5878 // exit hover when pointer goes away
5879 processKey(mapper, BTN_TOOL_FINGER, 0);
5880 processSync(mapper);
5881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5882 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5884 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5885}
5886
5887TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005888 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005889 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005890 prepareButtons();
5891 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005892 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005893
5894 NotifyMotionArgs motionArgs;
5895
5896 // initially hovering because pressure is 0
5897 processDown(mapper, 100, 200);
5898 processPressure(mapper, 0);
5899 processSync(mapper);
5900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5901 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5902 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5903 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5904
5905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5906 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5907 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5908 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5909
5910 // move a little
5911 processMove(mapper, 150, 250);
5912 processSync(mapper);
5913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5914 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5915 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5916 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5917
5918 // down when pressure is non-zero
5919 processPressure(mapper, RAW_PRESSURE_MAX);
5920 processSync(mapper);
5921 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5922 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5923 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5924 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5925
5926 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5927 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5928 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5929 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5930
5931 // up when pressure becomes 0, hover restored
5932 processPressure(mapper, 0);
5933 processSync(mapper);
5934 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5935 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5936 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5937 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5938
5939 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5940 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5941 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5942 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5943
5944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5945 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5946 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5947 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5948
5949 // exit hover when pointer goes away
5950 processUp(mapper);
5951 processSync(mapper);
5952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5953 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5954 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5955 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5956}
5957
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005958TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5959 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005960 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005961 prepareButtons();
5962 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005963 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005964
5965 // Touch down.
5966 processDown(mapper, 100, 200);
5967 processPressure(mapper, 1);
5968 processSync(mapper);
5969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5970 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5971
5972 // Reset the mapper. This should cancel the ongoing gesture.
5973 resetMapper(mapper, ARBITRARY_TIME);
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5975 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5976
5977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5978}
5979
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005980TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5981 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005982 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005983 prepareButtons();
5984 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005985 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005986
5987 // Set the initial state for the touch pointer.
5988 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5989 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5990 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5991 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5992
5993 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005994 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
5995 // does not generate any events.
5996 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005997
5998 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
5999 // the recreated touch state to generate a down event.
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6002 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
6003
6004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6005}
6006
lilinnan687e58f2022-07-19 16:00:50 +08006007TEST_F(SingleTouchInputMapperTest,
6008 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
6009 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006010 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08006011 prepareButtons();
6012 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006013 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08006014 NotifyMotionArgs motionArgs;
6015
6016 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00006017 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08006018 processSync(mapper);
6019
6020 // We should receive a down event
6021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6022 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6023
6024 // Change display id
6025 clearViewports();
6026 prepareSecondaryDisplay(ViewportType::INTERNAL);
6027
6028 // We should receive a cancel event
6029 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6030 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6031 // Then receive reset called
6032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6033}
6034
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006035TEST_F(SingleTouchInputMapperTest,
6036 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
6037 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006038 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006039 prepareButtons();
6040 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006041 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6043 NotifyMotionArgs motionArgs;
6044
6045 // Start a new gesture.
6046 processDown(mapper, 100, 200);
6047 processSync(mapper);
6048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6049 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6050
6051 // Make the viewport inactive. This will put the device in disabled mode.
6052 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6053 viewport->isActive = false;
6054 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006055 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006056
6057 // We should receive a cancel event for the ongoing gesture.
6058 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6059 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
6060 // Then we should be notified that the device was reset.
6061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6062
6063 // No events are generated while the viewport is inactive.
6064 processMove(mapper, 101, 201);
6065 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006066 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006067 processSync(mapper);
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6069
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006070 // Start a new gesture while the viewport is still inactive.
6071 processDown(mapper, 300, 400);
6072 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
6073 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
6074 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
6075 processSync(mapper);
6076
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006077 // Make the viewport active again. The device should resume processing events.
6078 viewport->isActive = true;
6079 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006080 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006081
6082 // The device is reset because it changes back to direct mode, without generating any events.
6083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6085
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006086 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006087 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00006088 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6089 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00006090
6091 // No more events.
6092 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
6094}
6095
Prabir Pradhan211ba622022-10-31 21:09:21 +00006096TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
6097 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006098 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00006099 prepareButtons();
6100 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006101 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00006102 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6103
6104 // Press a stylus button.
6105 processKey(mapper, BTN_STYLUS, 1);
6106 processSync(mapper);
6107
6108 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
6109 processDown(mapper, 100, 200);
6110 processSync(mapper);
6111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6112 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6113 WithCoords(toDisplayX(100), toDisplayY(200)),
6114 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6116 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6117 WithCoords(toDisplayX(100), toDisplayY(200)),
6118 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6119
6120 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6121 // the button has not actually been released, since there will be no pointers through which the
6122 // button state can be reported. The event is generated at the location of the pointer before
6123 // it went up.
6124 processUp(mapper);
6125 processSync(mapper);
6126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6127 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6128 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6130 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6131 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6132}
6133
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006134TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6135 addConfigurationProperty("touch.deviceType", "touchScreen");
6136 prepareDisplay(ui::ROTATION_0);
6137 prepareButtons();
6138 prepareAxes(POSITION);
6139
6140 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6141
Arpit Singha8c236b2023-04-25 13:56:05 +00006142 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6144
6145 // Press a stylus button.
6146 processKey(mapper, BTN_STYLUS, 1);
6147 processSync(mapper);
6148
6149 // Start a touch gesture and ensure that the stylus button is not reported.
6150 processDown(mapper, 100, 200);
6151 processSync(mapper);
6152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6153 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6154
6155 // Release and press the stylus button again.
6156 processKey(mapper, BTN_STYLUS, 0);
6157 processSync(mapper);
6158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6159 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6160 processKey(mapper, BTN_STYLUS, 1);
6161 processSync(mapper);
6162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6163 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6164
6165 // Release the touch gesture.
6166 processUp(mapper);
6167 processSync(mapper);
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6170
6171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6172}
6173
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006174TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6175 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6176 prepareDisplay(ui::ROTATION_0);
6177 prepareButtons();
6178 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006179 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6181
6182 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
6183}
6184
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006185TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6186 // Initialize the device without setting device source to touch navigation.
6187 addConfigurationProperty("touch.deviceType", "touchScreen");
6188 prepareDisplay(ui::ROTATION_0);
6189 prepareButtons();
6190 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006191 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006192
6193 // Ensure that the device is created as a touchscreen, not touch navigation.
6194 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6195
6196 // Add device type association after the device was created.
6197 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6198
6199 // Send update to the mapper.
6200 std::list<NotifyArgs> unused2 =
6201 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006202 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006203
6204 // Check whether device type update was successful.
6205 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
6206}
6207
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006208TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6209 // Initialize the device without setting device source to touch navigation.
6210 addConfigurationProperty("touch.deviceType", "touchScreen");
6211 prepareDisplay(ui::ROTATION_0);
6212 prepareButtons();
6213 prepareAxes(POSITION);
6214 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6215
6216 // Set a physical frame in the display viewport.
6217 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6218 viewport->physicalLeft = 0;
6219 viewport->physicalTop = 0;
6220 viewport->physicalRight = DISPLAY_WIDTH / 2;
6221 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6222 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006223 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006224
Arpit Singha8c236b2023-04-25 13:56:05 +00006225 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006226
6227 // Hovering inside the physical frame produces events.
6228 processKey(mapper, BTN_TOOL_PEN, 1);
6229 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6230 processSync(mapper);
6231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6232 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6233 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6234 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6235
6236 // Leaving the physical frame ends the hovering gesture.
6237 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6238 processSync(mapper);
6239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6240 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6241
6242 // Moving outside the physical frame does not produce events.
6243 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6244 processSync(mapper);
6245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6246
6247 // Re-entering the physical frame produces events.
6248 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6249 processSync(mapper);
6250 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6251 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6252 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6253 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6254}
6255
Prabir Pradhan5632d622021-09-06 07:57:20 -07006256// --- TouchDisplayProjectionTest ---
6257
6258class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6259public:
6260 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6261 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6262 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006263 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6264 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6265 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006266 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006267 auto rotatedWidth = naturalDisplayWidth;
6268 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006269 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006270 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006271 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006272 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006273 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006274 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006275 inverseRotationFlags = ui::Transform::ROT_180;
6276 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006277 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006278 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006279 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006280 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006281 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006282 inverseRotationFlags = ui::Transform::ROT_0;
6283 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006284 }
6285
Prabir Pradhana9df3162022-12-05 23:57:27 +00006286 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006287 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6288
6289 std::optional<DisplayViewport> internalViewport =
6290 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6291 DisplayViewport& v = *internalViewport;
6292 v.displayId = DISPLAY_ID;
6293 v.orientation = orientation;
6294
6295 v.logicalLeft = 0;
6296 v.logicalTop = 0;
6297 v.logicalRight = 100;
6298 v.logicalBottom = 100;
6299
6300 v.physicalLeft = rotatedPhysicalDisplay.left;
6301 v.physicalTop = rotatedPhysicalDisplay.top;
6302 v.physicalRight = rotatedPhysicalDisplay.right;
6303 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6304
Prabir Pradhana9df3162022-12-05 23:57:27 +00006305 v.deviceWidth = rotatedWidth;
6306 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006307
6308 v.isActive = true;
6309 v.uniqueId = UNIQUE_ID;
6310 v.type = ViewportType::INTERNAL;
6311 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006312 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006313 }
6314
6315 void assertReceivedMove(const Point& point) {
6316 NotifyMotionArgs motionArgs;
6317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006319 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6321 1, 0, 0, 0, 0, 0, 0, 0));
6322 }
6323};
6324
6325TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6326 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006327 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006328
6329 prepareButtons();
6330 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006331 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006332
6333 NotifyMotionArgs motionArgs;
6334
6335 // Configure the DisplayViewport such that the logical display maps to a subsection of
6336 // the display panel called the physical display. Here, the physical display is bounded by the
6337 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6338 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6339 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6340 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6341
Michael Wrighta9cf4192022-12-01 23:46:39 +00006342 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006343 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6344
6345 // Touches outside the physical display should be ignored, and should not generate any
6346 // events. Ensure touches at the following points that lie outside of the physical display
6347 // area do not generate any events.
6348 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6349 processDown(mapper, toRawX(point.x), toRawY(point.y));
6350 processSync(mapper);
6351 processUp(mapper);
6352 processSync(mapper);
6353 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6354 << "Unexpected event generated for touch outside physical display at point: "
6355 << point.x << ", " << point.y;
6356 }
6357 }
6358}
6359
6360TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6361 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006362 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006363
6364 prepareButtons();
6365 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006366 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006367
6368 NotifyMotionArgs motionArgs;
6369
6370 // Configure the DisplayViewport such that the logical display maps to a subsection of
6371 // the display panel called the physical display. Here, the physical display is bounded by the
6372 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6373 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6374
Michael Wrighta9cf4192022-12-01 23:46:39 +00006375 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006376 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6377
6378 // Touches that start outside the physical display should be ignored until it enters the
6379 // physical display bounds, at which point it should generate a down event. Start a touch at
6380 // the point (5, 100), which is outside the physical display bounds.
6381 static const Point kOutsidePoint{5, 100};
6382 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6383 processSync(mapper);
6384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6385
6386 // Move the touch into the physical display area. This should generate a pointer down.
6387 processMove(mapper, toRawX(11), toRawY(21));
6388 processSync(mapper);
6389 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6390 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006391 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006392 ASSERT_NO_FATAL_FAILURE(
6393 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6394
6395 // Move the touch inside the physical display area. This should generate a pointer move.
6396 processMove(mapper, toRawX(69), toRawY(159));
6397 processSync(mapper);
6398 assertReceivedMove({69, 159});
6399
6400 // Move outside the physical display area. Since the pointer is already down, this should
6401 // now continue generating events.
6402 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6403 processSync(mapper);
6404 assertReceivedMove(kOutsidePoint);
6405
6406 // Release. This should generate a pointer up.
6407 processUp(mapper);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6410 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6412 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6413
6414 // Ensure no more events were generated.
6415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6417 }
6418}
6419
Prabir Pradhana9df3162022-12-05 23:57:27 +00006420// --- TouchscreenPrecisionTests ---
6421
6422// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6423// in various orientations and with different display rotations. We configure the touchscreen to
6424// have a higher resolution than that of the display by an integer scale factor in each axis so that
6425// we can enforce that coordinates match precisely as expected.
6426class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6427 public ::testing::WithParamInterface<ui::Rotation> {
6428public:
6429 void SetUp() override {
6430 SingleTouchInputMapperTest::SetUp();
6431
6432 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6433 // four times the resolution of the display in the Y axis.
6434 prepareButtons();
6435 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006436 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6437 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006438 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006439 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6440 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006441 }
6442
6443 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6444 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6445 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6446 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6447
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006448 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6449 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6450
6451 static const int32_t PRECISION_RAW_X_FLAT = 16;
6452 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6453
6454 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6455 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6456
Prabir Pradhana9df3162022-12-05 23:57:27 +00006457 static const std::array<Point, 4> kRawCorners;
6458};
6459
6460const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6461 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6462 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6463 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6464 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6465}};
6466
6467// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6468// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6469// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6470TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6471 enum class Orientation {
6472 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6473 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6474 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6475 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6476 ftl_last = ORIENTATION_270,
6477 };
6478 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6479 Orientation::ORIENTATION_270;
6480 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6481 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6482 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6483 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6484 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6485 };
6486
6487 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6488
6489 // Configure the touchscreen as being installed in the one of the four different orientations
6490 // relative to the display.
6491 addConfigurationProperty("touch.deviceType", "touchScreen");
6492 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6493 prepareDisplay(ui::ROTATION_0);
6494
Arpit Singha8c236b2023-04-25 13:56:05 +00006495 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006496
6497 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6498 // orientations of either 90 or 270) this means the display's natural resolution will be
6499 // flipped.
6500 const bool displayRotated =
6501 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6502 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6503 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6504 const Rect physicalFrame{0, 0, width, height};
6505 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6506
6507 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6508 const float expectedPrecisionX = displayRotated ? 4 : 2;
6509 const float expectedPrecisionY = displayRotated ? 2 : 4;
6510
6511 // Test all four corners.
6512 for (int i = 0; i < 4; i++) {
6513 const auto& raw = kRawCorners[i];
6514 processDown(mapper, raw.x, raw.y);
6515 processSync(mapper);
6516 const auto& expected = expectedPoints[i];
6517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6518 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6519 WithCoords(expected.x, expected.y),
6520 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6521 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6522 << "with touchscreen orientation "
6523 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6524 << expected.x << ", " << expected.y << ").";
6525 processUp(mapper);
6526 processSync(mapper);
6527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6528 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6529 WithCoords(expected.x, expected.y))));
6530 }
6531}
6532
Prabir Pradhan82687402022-12-06 01:32:53 +00006533TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6534 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6535 kMappedCorners = {
6536 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6537 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6538 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6539 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6540 };
6541
6542 const ui::Rotation displayRotation = GetParam();
6543
6544 addConfigurationProperty("touch.deviceType", "touchScreen");
6545 prepareDisplay(displayRotation);
6546
Arpit Singha8c236b2023-04-25 13:56:05 +00006547 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006548
6549 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6550
6551 // Test all four corners.
6552 for (int i = 0; i < 4; i++) {
6553 const auto& expected = expectedPoints[i];
6554 const auto& raw = kRawCorners[i];
6555 processDown(mapper, raw.x, raw.y);
6556 processSync(mapper);
6557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6558 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6559 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6560 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6561 << "with display rotation " << ui::toCString(displayRotation)
6562 << ", expected point (" << expected.x << ", " << expected.y << ").";
6563 processUp(mapper);
6564 processSync(mapper);
6565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6566 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6567 WithCoords(expected.x, expected.y))));
6568 }
6569}
6570
Prabir Pradhan3e798762022-12-02 21:02:11 +00006571TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6572 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6573 kMappedCorners = {
6574 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6575 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6576 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6577 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6578 };
6579
6580 const ui::Rotation displayRotation = GetParam();
6581
6582 addConfigurationProperty("touch.deviceType", "touchScreen");
6583 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6584
Arpit Singha8c236b2023-04-25 13:56:05 +00006585 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006586
6587 // Ori 270, so width and height swapped
6588 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6589 prepareDisplay(displayRotation);
6590 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6591
6592 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6593
6594 // Test all four corners.
6595 for (int i = 0; i < 4; i++) {
6596 const auto& expected = expectedPoints[i];
6597 const auto& raw = kRawCorners[i];
6598 processDown(mapper, raw.x, raw.y);
6599 processSync(mapper);
6600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6601 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6602 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6603 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6604 << "with display rotation " << ui::toCString(displayRotation)
6605 << ", expected point (" << expected.x << ", " << expected.y << ").";
6606 processUp(mapper);
6607 processSync(mapper);
6608 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6609 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6610 WithCoords(expected.x, expected.y))));
6611 }
6612}
6613
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006614TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6615 const ui::Rotation displayRotation = GetParam();
6616
6617 addConfigurationProperty("touch.deviceType", "touchScreen");
6618 prepareDisplay(displayRotation);
6619
6620 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006621 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006622
6623 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6624 // MotionRanges use display pixels as their units
6625 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6626 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6627
6628 // The MotionRanges should be oriented in the rotated display's coordinate space
6629 const bool displayRotated =
6630 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6631
6632 constexpr float MAX_X = 479.5;
6633 constexpr float MAX_Y = 799.75;
6634 EXPECT_EQ(xRange->min, 0.f);
6635 EXPECT_EQ(yRange->min, 0.f);
6636 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6637 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6638
6639 EXPECT_EQ(xRange->flat, 8.f);
6640 EXPECT_EQ(yRange->flat, 8.f);
6641
6642 EXPECT_EQ(xRange->fuzz, 2.f);
6643 EXPECT_EQ(yRange->fuzz, 2.f);
6644
6645 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6646 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6647}
6648
Prabir Pradhana9df3162022-12-05 23:57:27 +00006649// Run the precision tests for all rotations.
6650INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6651 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6652 ui::ROTATION_270),
6653 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6654 return ftl::enum_string(testParamInfo.param);
6655 });
6656
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006657// --- ExternalStylusFusionTest ---
6658
6659class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6660public:
6661 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
6662 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006663 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006664 prepareButtons();
6665 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006666 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006667
6668 mStylusState.when = ARBITRARY_TIME;
6669 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006670 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006671 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006672 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006673 processExternalStylusState(mapper);
6674 return mapper;
6675 }
6676
6677 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6678 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6679 for (const NotifyArgs& args : generatedArgs) {
6680 mFakeListener->notify(args);
6681 }
6682 // Loop the reader to flush the input listener queue.
6683 mReader->loopOnce();
6684 return generatedArgs;
6685 }
6686
6687protected:
6688 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006689
6690 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6691 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006692 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006693
6694 // The first pointer is withheld.
6695 processDown(mapper, 100, 200);
6696 processSync(mapper);
6697 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6698 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6699 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6700
6701 // The external stylus reports pressure. The withheld finger pointer is released as a
6702 // stylus.
6703 mStylusState.pressure = 1.f;
6704 processExternalStylusState(mapper);
6705 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6706 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6707 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6708
6709 // Subsequent pointer events are not withheld.
6710 processMove(mapper, 101, 201);
6711 processSync(mapper);
6712 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6713 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6714
6715 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6717 }
6718
6719 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6720 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6721
6722 // Releasing the touch pointer ends the gesture.
6723 processUp(mapper);
6724 processSync(mapper);
6725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006726 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006727 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006728
6729 mStylusState.pressure = 0.f;
6730 processExternalStylusState(mapper);
6731 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6733 }
6734
6735 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006736 // When stylus fusion is not successful, events should be reported with the original source.
6737 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006738 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006739 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006740
6741 // The first pointer is withheld when an external stylus is connected,
6742 // and a timeout is requested.
6743 processDown(mapper, 100, 200);
6744 processSync(mapper);
6745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6746 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6747 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6748
6749 // If the timeout expires early, it is requested again.
6750 handleTimeout(mapper, ARBITRARY_TIME + 1);
6751 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6752 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6753
6754 // When the timeout expires, the withheld touch is released as a finger pointer.
6755 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6756 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6757 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6758
6759 // Subsequent pointer events are not withheld.
6760 processMove(mapper, 101, 201);
6761 processSync(mapper);
6762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6763 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6764 processUp(mapper);
6765 processSync(mapper);
6766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6767 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6768
6769 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6771 }
6772
6773private:
6774 InputDeviceInfo mExternalStylusDeviceInfo{};
6775};
6776
6777TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
6778 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006779 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006780}
6781
6782TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6783 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6784 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6785}
6786
6787TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6788 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6789 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6790}
6791
6792// Test a successful stylus fusion gesture where the pressure is reported by the external
6793// before the touch is reported by the touchscreen.
6794TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6795 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006796 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006797
6798 // The external stylus reports pressure first. It is ignored for now.
6799 mStylusState.pressure = 1.f;
6800 processExternalStylusState(mapper);
6801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6802 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6803
6804 // When the touch goes down afterwards, it is reported as a stylus pointer.
6805 processDown(mapper, 100, 200);
6806 processSync(mapper);
6807 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6808 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6809 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6810
6811 processMove(mapper, 101, 201);
6812 processSync(mapper);
6813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6814 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6815 processUp(mapper);
6816 processSync(mapper);
6817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6818 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6819
6820 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6822}
6823
6824TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6825 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6826
6827 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6828 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6829
6830 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6831 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6832 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6833 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6834}
6835
6836TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6837 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006838 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006839
6840 mStylusState.pressure = 0.8f;
6841 processExternalStylusState(mapper);
6842 processDown(mapper, 100, 200);
6843 processSync(mapper);
6844 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6845 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6846 WithPressure(0.8f))));
6847 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6848
6849 // The external stylus reports a pressure change. We wait for some time for a touch event.
6850 mStylusState.pressure = 0.6f;
6851 processExternalStylusState(mapper);
6852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6853 ASSERT_NO_FATAL_FAILURE(
6854 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6855
6856 // If a touch is reported within the timeout, it reports the updated pressure.
6857 processMove(mapper, 101, 201);
6858 processSync(mapper);
6859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6860 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6861 WithPressure(0.6f))));
6862 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6863
6864 // There is another pressure change.
6865 mStylusState.pressure = 0.5f;
6866 processExternalStylusState(mapper);
6867 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6868 ASSERT_NO_FATAL_FAILURE(
6869 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6870
6871 // If a touch is not reported within the timeout, a move event is generated to report
6872 // the new pressure.
6873 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6875 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6876 WithPressure(0.5f))));
6877
6878 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6879 // repeated indefinitely.
6880 mStylusState.pressure = 0.0f;
6881 processExternalStylusState(mapper);
6882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6883 ASSERT_NO_FATAL_FAILURE(
6884 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6885 processMove(mapper, 102, 202);
6886 processSync(mapper);
6887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6888 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6889 WithPressure(0.5f))));
6890 processMove(mapper, 103, 203);
6891 processSync(mapper);
6892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6894 WithPressure(0.5f))));
6895
6896 processUp(mapper);
6897 processSync(mapper);
6898 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006899 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006900 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006901
6902 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6904}
6905
6906TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6907 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006908 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006909
6910 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006911 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006912 processExternalStylusState(mapper);
6913 processDown(mapper, 100, 200);
6914 processSync(mapper);
6915 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6916 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006917 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006918 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6919
6920 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006921 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006922 processExternalStylusState(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6924 ASSERT_NO_FATAL_FAILURE(
6925 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6926
6927 // If a touch is reported within the timeout, it reports the updated pressure.
6928 processMove(mapper, 101, 201);
6929 processSync(mapper);
6930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6931 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006932 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006933 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6934
6935 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006936 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006937 processExternalStylusState(mapper);
6938 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6939 ASSERT_NO_FATAL_FAILURE(
6940 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6941
6942 // If a touch is not reported within the timeout, a move event is generated to report
6943 // the new tool type.
6944 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6946 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006947 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006948
6949 processUp(mapper);
6950 processSync(mapper);
6951 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6952 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006953 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006954
6955 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6957}
6958
6959TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6960 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006961 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006962
6963 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6964
6965 // The external stylus reports a button change. We wait for some time for a touch event.
6966 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6967 processExternalStylusState(mapper);
6968 ASSERT_NO_FATAL_FAILURE(
6969 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6970
6971 // If a touch is reported within the timeout, it reports the updated button state.
6972 processMove(mapper, 101, 201);
6973 processSync(mapper);
6974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6975 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6976 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6978 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6979 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6980 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6981
6982 // The button is now released.
6983 mStylusState.buttons = 0;
6984 processExternalStylusState(mapper);
6985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6986 ASSERT_NO_FATAL_FAILURE(
6987 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6988
6989 // If a touch is not reported within the timeout, a move event is generated to report
6990 // the new button state.
6991 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6993 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6994 WithButtonState(0))));
6995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00006996 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6997 WithButtonState(0))));
6998
6999 processUp(mapper);
7000 processSync(mapper);
7001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00007002 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7003
7004 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
7005 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7006}
7007
Michael Wrightd02c5b62014-02-10 15:10:22 -08007008// --- MultiTouchInputMapperTest ---
7009
7010class MultiTouchInputMapperTest : public TouchInputMapperTest {
7011protected:
7012 void prepareAxes(int axes);
7013
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007014 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
7015 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
7016 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
7017 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
7018 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
7019 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
7020 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
7021 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
7022 void processId(MultiTouchInputMapper& mapper, int32_t id);
7023 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
7024 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
7025 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007026 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007027 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007028 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
7029 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030};
7031
7032void MultiTouchInputMapperTest::prepareAxes(int axes) {
7033 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007034 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
7035 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 }
7037 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007038 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
7039 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007041 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
7042 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007043 }
7044 }
7045 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007046 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7047 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007049 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007050 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007051 }
7052 }
7053 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007054 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
7055 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007056 }
7057 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007058 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
7059 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007060 }
7061 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007062 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
7063 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007064 }
7065 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007066 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
7067 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007068 }
7069 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007070 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
7071 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007072 }
7073 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007074 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007075 }
7076}
7077
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007078void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
7079 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007080 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
7081 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007082}
7083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007084void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
7085 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007086 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007087}
7088
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007089void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
7090 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007091 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007092}
7093
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007094void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096}
7097
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007098void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007099 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100}
7101
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007102void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7103 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007104 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007105}
7106
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007107void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007108 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007109}
7110
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007111void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007112 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007113}
7114
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007115void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007116 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007117}
7118
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007119void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007120 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007121}
7122
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007123void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007124 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007125}
7126
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007127void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7128 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007129 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007130}
7131
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007132void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7133 int32_t value) {
7134 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7136}
7137
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007138void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007140}
7141
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007142void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7143 nsecs_t readTime) {
7144 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007145}
7146
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007148 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007149 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007150 prepareAxes(POSITION);
7151 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007152 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007153
arthurhungdcef2dc2020-08-11 14:47:50 +08007154 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155
7156 NotifyMotionArgs motionArgs;
7157
7158 // Two fingers down at once.
7159 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7160 processPosition(mapper, x1, y1);
7161 processMTSync(mapper);
7162 processPosition(mapper, x2, y2);
7163 processMTSync(mapper);
7164 processSync(mapper);
7165
7166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7167 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7168 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7169 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7170 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7171 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7172 ASSERT_EQ(0, motionArgs.flags);
7173 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7174 ASSERT_EQ(0, motionArgs.buttonState);
7175 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007176 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007177 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007178 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7180 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7181 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7182 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7183 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7184
7185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7186 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7187 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7188 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7189 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007190 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007191 ASSERT_EQ(0, motionArgs.flags);
7192 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7193 ASSERT_EQ(0, motionArgs.buttonState);
7194 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007195 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007196 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007197 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007198 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007199 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007200 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7201 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7203 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7204 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7205 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7206 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7207
7208 // Move.
7209 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7210 processPosition(mapper, x1, y1);
7211 processMTSync(mapper);
7212 processPosition(mapper, x2, y2);
7213 processMTSync(mapper);
7214 processSync(mapper);
7215
7216 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7217 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7218 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7219 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7220 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7221 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7222 ASSERT_EQ(0, motionArgs.flags);
7223 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7224 ASSERT_EQ(0, motionArgs.buttonState);
7225 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007226 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007227 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007228 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007229 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7232 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7233 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7234 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7235 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7236 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7237 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7238
7239 // First finger up.
7240 x2 += 15; y2 -= 20;
7241 processPosition(mapper, x2, y2);
7242 processMTSync(mapper);
7243 processSync(mapper);
7244
7245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7246 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7247 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7248 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7249 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007250 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251 ASSERT_EQ(0, motionArgs.flags);
7252 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7253 ASSERT_EQ(0, motionArgs.buttonState);
7254 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007255 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007256 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007257 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007258 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007259 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7263 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7264 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7265 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7266 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7267
7268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7269 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7270 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7271 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7272 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7273 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7274 ASSERT_EQ(0, motionArgs.flags);
7275 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7276 ASSERT_EQ(0, motionArgs.buttonState);
7277 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007278 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007279 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007280 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7283 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7284 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7285 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7286
7287 // Move.
7288 x2 += 20; y2 -= 25;
7289 processPosition(mapper, x2, y2);
7290 processMTSync(mapper);
7291 processSync(mapper);
7292
7293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7294 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7295 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7296 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7297 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7299 ASSERT_EQ(0, motionArgs.flags);
7300 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7301 ASSERT_EQ(0, motionArgs.buttonState);
7302 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007303 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007304 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007305 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7307 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7308 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7309 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7310 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7311
7312 // New finger down.
7313 int32_t x3 = 700, y3 = 300;
7314 processPosition(mapper, x2, y2);
7315 processMTSync(mapper);
7316 processPosition(mapper, x3, y3);
7317 processMTSync(mapper);
7318 processSync(mapper);
7319
7320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7321 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7322 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7323 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7324 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007325 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007326 ASSERT_EQ(0, motionArgs.flags);
7327 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7328 ASSERT_EQ(0, motionArgs.buttonState);
7329 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007330 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007331 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007332 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007333 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007334 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007335 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7336 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7337 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7338 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7339 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7340 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7341 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7342
7343 // Second finger up.
7344 x3 += 30; y3 -= 20;
7345 processPosition(mapper, x3, y3);
7346 processMTSync(mapper);
7347 processSync(mapper);
7348
7349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7350 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7351 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7352 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7353 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007354 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007355 ASSERT_EQ(0, motionArgs.flags);
7356 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7357 ASSERT_EQ(0, motionArgs.buttonState);
7358 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007359 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007360 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007361 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007362 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007363 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7365 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7366 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7367 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7368 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7369 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7370 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7371
7372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7373 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7374 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7375 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7376 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7377 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7378 ASSERT_EQ(0, motionArgs.flags);
7379 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7380 ASSERT_EQ(0, motionArgs.buttonState);
7381 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007382 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007383 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007384 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007385 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7386 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7387 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7388 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7389 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7390
7391 // Last finger up.
7392 processMTSync(mapper);
7393 processSync(mapper);
7394
7395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7396 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7397 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7398 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7399 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7400 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7401 ASSERT_EQ(0, motionArgs.flags);
7402 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7403 ASSERT_EQ(0, motionArgs.buttonState);
7404 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007405 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007407 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7409 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7410 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7411 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7412 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7413
7414 // Should not have sent any more keys or motions.
7415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7417}
7418
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007419TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7420 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007421 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007422
7423 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7424 /*fuzz*/ 0, /*resolution*/ 10);
7425 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7426 /*fuzz*/ 0, /*resolution*/ 11);
7427 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7428 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7429 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7430 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7431 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7432 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7433 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7434 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7435
Arpit Singha8c236b2023-04-25 13:56:05 +00007436 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007437
7438 // X and Y axes
7439 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7440 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7441 // Touch major and minor
7442 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7443 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7444 // Tool major and minor
7445 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7446 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7447}
7448
7449TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7450 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007451 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007452
7453 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7454 /*fuzz*/ 0, /*resolution*/ 10);
7455 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7456 /*fuzz*/ 0, /*resolution*/ 11);
7457
7458 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7459
Arpit Singha8c236b2023-04-25 13:56:05 +00007460 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007461
7462 // Touch major and minor
7463 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7464 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7465 // Tool major and minor
7466 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7467 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7468}
7469
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007471 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007472 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007473 prepareAxes(POSITION | ID);
7474 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007475 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007476
arthurhungdcef2dc2020-08-11 14:47:50 +08007477 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007478
7479 NotifyMotionArgs motionArgs;
7480
7481 // Two fingers down at once.
7482 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7483 processPosition(mapper, x1, y1);
7484 processId(mapper, 1);
7485 processMTSync(mapper);
7486 processPosition(mapper, x2, y2);
7487 processId(mapper, 2);
7488 processMTSync(mapper);
7489 processSync(mapper);
7490
7491 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7492 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007493 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007494 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007495 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007496 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7497 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7498
7499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007500 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007501 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007502 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007503 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007504 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007505 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007506 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7507 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7508 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7509 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7510
7511 // Move.
7512 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7513 processPosition(mapper, x1, y1);
7514 processId(mapper, 1);
7515 processMTSync(mapper);
7516 processPosition(mapper, x2, y2);
7517 processId(mapper, 2);
7518 processMTSync(mapper);
7519 processSync(mapper);
7520
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7522 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007523 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007524 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007525 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007526 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007527 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7529 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7530 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7531 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7532
7533 // First finger up.
7534 x2 += 15; y2 -= 20;
7535 processPosition(mapper, x2, y2);
7536 processId(mapper, 2);
7537 processMTSync(mapper);
7538 processSync(mapper);
7539
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007541 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007542 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007543 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007544 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007545 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007546 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7548 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7549 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7550 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7551
7552 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7553 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007554 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007555 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007556 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007557 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7558 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7559
7560 // Move.
7561 x2 += 20; y2 -= 25;
7562 processPosition(mapper, x2, y2);
7563 processId(mapper, 2);
7564 processMTSync(mapper);
7565 processSync(mapper);
7566
7567 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7568 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007569 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007570 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007571 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7573 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7574
7575 // New finger down.
7576 int32_t x3 = 700, y3 = 300;
7577 processPosition(mapper, x2, y2);
7578 processId(mapper, 2);
7579 processMTSync(mapper);
7580 processPosition(mapper, x3, y3);
7581 processId(mapper, 3);
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_DOWN, 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(x3), toDisplayY(y3), 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 // Second finger up.
7598 x3 += 30; y3 -= 20;
7599 processPosition(mapper, x3, y3);
7600 processId(mapper, 3);
7601 processMTSync(mapper);
7602 processSync(mapper);
7603
7604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007605 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007606 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007607 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007608 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007609 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007610 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007611 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7612 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7613 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7614 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7615
7616 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7617 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007618 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007619 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007620 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007621 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7622 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7623
7624 // Last finger up.
7625 processMTSync(mapper);
7626 processSync(mapper);
7627
7628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7629 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007630 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007631 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007633 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7634 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7635
7636 // Should not have sent any more keys or motions.
7637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7638 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7639}
7640
7641TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007642 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007643 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007644 prepareAxes(POSITION | ID | SLOT);
7645 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007646 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007647
arthurhungdcef2dc2020-08-11 14:47:50 +08007648 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007649
7650 NotifyMotionArgs motionArgs;
7651
7652 // Two fingers down at once.
7653 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7654 processPosition(mapper, x1, y1);
7655 processId(mapper, 1);
7656 processSlot(mapper, 1);
7657 processPosition(mapper, x2, y2);
7658 processId(mapper, 2);
7659 processSync(mapper);
7660
7661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7662 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, 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(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7668
7669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007670 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007671 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007672 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007673 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007674 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007675 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007676 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7677 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7678 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7679 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7680
7681 // Move.
7682 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7683 processSlot(mapper, 0);
7684 processPosition(mapper, x1, y1);
7685 processSlot(mapper, 1);
7686 processPosition(mapper, x2, y2);
7687 processSync(mapper);
7688
7689 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7690 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007691 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007692 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007693 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007694 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007695 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7697 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7698 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7699 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7700
7701 // First finger up.
7702 x2 += 15; y2 -= 20;
7703 processSlot(mapper, 0);
7704 processId(mapper, -1);
7705 processSlot(mapper, 1);
7706 processPosition(mapper, x2, y2);
7707 processSync(mapper);
7708
7709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007710 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007711 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007713 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007714 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007715 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007716 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7717 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7719 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7720
7721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7722 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007723 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007724 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007725 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7727 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7728
7729 // Move.
7730 x2 += 20; y2 -= 25;
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(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007737 ASSERT_EQ(1, 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_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7740 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7741
7742 // New finger down.
7743 int32_t x3 = 700, y3 = 300;
7744 processPosition(mapper, x2, y2);
7745 processSlot(mapper, 0);
7746 processId(mapper, 3);
7747 processPosition(mapper, x3, y3);
7748 processSync(mapper);
7749
7750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007751 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007752 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007753 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007754 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007756 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7758 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7759 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7760 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7761
7762 // Second finger up.
7763 x3 += 30; y3 -= 20;
7764 processSlot(mapper, 1);
7765 processId(mapper, -1);
7766 processSlot(mapper, 0);
7767 processPosition(mapper, x3, y3);
7768 processSync(mapper);
7769
7770 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007771 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007772 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007773 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007774 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007775 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007776 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007777 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7778 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7779 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7780 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7781
7782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7783 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007784 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007785 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007786 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007787 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7788 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7789
7790 // Last finger up.
7791 processId(mapper, -1);
7792 processSync(mapper);
7793
7794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7795 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007796 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007798 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007799 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7800 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7801
7802 // Should not have sent any more keys or motions.
7803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7804 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7805}
7806
7807TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007808 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007809 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007810 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007811 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007812
7813 // These calculations are based on the input device calibration documentation.
7814 int32_t rawX = 100;
7815 int32_t rawY = 200;
7816 int32_t rawTouchMajor = 7;
7817 int32_t rawTouchMinor = 6;
7818 int32_t rawToolMajor = 9;
7819 int32_t rawToolMinor = 8;
7820 int32_t rawPressure = 11;
7821 int32_t rawDistance = 0;
7822 int32_t rawOrientation = 3;
7823 int32_t id = 5;
7824
7825 float x = toDisplayX(rawX);
7826 float y = toDisplayY(rawY);
7827 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7828 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7829 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7830 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7831 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7832 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7833 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7834 float distance = float(rawDistance);
7835
7836 processPosition(mapper, rawX, rawY);
7837 processTouchMajor(mapper, rawTouchMajor);
7838 processTouchMinor(mapper, rawTouchMinor);
7839 processToolMajor(mapper, rawToolMajor);
7840 processToolMinor(mapper, rawToolMinor);
7841 processPressure(mapper, rawPressure);
7842 processOrientation(mapper, rawOrientation);
7843 processDistance(mapper, rawDistance);
7844 processId(mapper, id);
7845 processMTSync(mapper);
7846 processSync(mapper);
7847
7848 NotifyMotionArgs args;
7849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7850 ASSERT_EQ(0, args.pointerProperties[0].id);
7851 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7852 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7853 orientation, distance));
7854}
7855
7856TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007857 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007858 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007859 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7860 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007861 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007862
7863 // These calculations are based on the input device calibration documentation.
7864 int32_t rawX = 100;
7865 int32_t rawY = 200;
7866 int32_t rawTouchMajor = 140;
7867 int32_t rawTouchMinor = 120;
7868 int32_t rawToolMajor = 180;
7869 int32_t rawToolMinor = 160;
7870
7871 float x = toDisplayX(rawX);
7872 float y = toDisplayY(rawY);
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
7879 processPosition(mapper, rawX, rawY);
7880 processTouchMajor(mapper, rawTouchMajor);
7881 processTouchMinor(mapper, rawTouchMinor);
7882 processToolMajor(mapper, rawToolMajor);
7883 processToolMinor(mapper, rawToolMinor);
7884 processMTSync(mapper);
7885 processSync(mapper);
7886
7887 NotifyMotionArgs args;
7888 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7889 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7890 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7891}
7892
7893TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007894 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007895 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007896 prepareAxes(POSITION | TOUCH | TOOL);
7897 addConfigurationProperty("touch.size.calibration", "diameter");
7898 addConfigurationProperty("touch.size.scale", "10");
7899 addConfigurationProperty("touch.size.bias", "160");
7900 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007901 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007902
7903 // These calculations are based on the input device calibration documentation.
7904 // Note: We only provide a single common touch/tool value because the device is assumed
7905 // not to emit separate values for each pointer (isSummed = 1).
7906 int32_t rawX = 100;
7907 int32_t rawY = 200;
7908 int32_t rawX2 = 150;
7909 int32_t rawY2 = 250;
7910 int32_t rawTouchMajor = 5;
7911 int32_t rawToolMajor = 8;
7912
7913 float x = toDisplayX(rawX);
7914 float y = toDisplayY(rawY);
7915 float x2 = toDisplayX(rawX2);
7916 float y2 = toDisplayY(rawY2);
7917 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7918 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7919 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7920
7921 processPosition(mapper, rawX, rawY);
7922 processTouchMajor(mapper, rawTouchMajor);
7923 processToolMajor(mapper, rawToolMajor);
7924 processMTSync(mapper);
7925 processPosition(mapper, rawX2, rawY2);
7926 processTouchMajor(mapper, rawTouchMajor);
7927 processToolMajor(mapper, rawToolMajor);
7928 processMTSync(mapper);
7929 processSync(mapper);
7930
7931 NotifyMotionArgs args;
7932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7933 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7934
7935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007936 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007937 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007938 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7939 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7940 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7941 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7942}
7943
7944TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007945 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007946 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007947 prepareAxes(POSITION | TOUCH | TOOL);
7948 addConfigurationProperty("touch.size.calibration", "area");
7949 addConfigurationProperty("touch.size.scale", "43");
7950 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007951 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007952
7953 // These calculations are based on the input device calibration documentation.
7954 int32_t rawX = 100;
7955 int32_t rawY = 200;
7956 int32_t rawTouchMajor = 5;
7957 int32_t rawToolMajor = 8;
7958
7959 float x = toDisplayX(rawX);
7960 float y = toDisplayY(rawY);
7961 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7962 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7963 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7964
7965 processPosition(mapper, rawX, rawY);
7966 processTouchMajor(mapper, rawTouchMajor);
7967 processToolMajor(mapper, rawToolMajor);
7968 processMTSync(mapper);
7969 processSync(mapper);
7970
7971 NotifyMotionArgs args;
7972 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7973 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7974 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7975}
7976
7977TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007978 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007979 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007980 prepareAxes(POSITION | PRESSURE);
7981 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7982 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00007983 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984
Michael Wrightaa449c92017-12-13 21:21:43 +00007985 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00007986 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007987 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7988 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7989 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7990
Michael Wrightd02c5b62014-02-10 15:10:22 -08007991 // These calculations are based on the input device calibration documentation.
7992 int32_t rawX = 100;
7993 int32_t rawY = 200;
7994 int32_t rawPressure = 60;
7995
7996 float x = toDisplayX(rawX);
7997 float y = toDisplayY(rawY);
7998 float pressure = float(rawPressure) * 0.01f;
7999
8000 processPosition(mapper, rawX, rawY);
8001 processPressure(mapper, rawPressure);
8002 processMTSync(mapper);
8003 processSync(mapper);
8004
8005 NotifyMotionArgs args;
8006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8007 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
8008 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
8009}
8010
8011TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008013 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008014 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008015 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008016
8017 NotifyMotionArgs motionArgs;
8018 NotifyKeyArgs keyArgs;
8019
8020 processId(mapper, 1);
8021 processPosition(mapper, 100, 200);
8022 processSync(mapper);
8023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8024 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8025 ASSERT_EQ(0, motionArgs.buttonState);
8026
8027 // press BTN_LEFT, release BTN_LEFT
8028 processKey(mapper, BTN_LEFT, 1);
8029 processSync(mapper);
8030 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8031 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8032 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8033
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8035 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8036 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
8037
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 processKey(mapper, BTN_LEFT, 0);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008041 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008042 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008043
8044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008045 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008046 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008047
8048 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
8049 processKey(mapper, BTN_RIGHT, 1);
8050 processKey(mapper, BTN_MIDDLE, 1);
8051 processSync(mapper);
8052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8054 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8055 motionArgs.buttonState);
8056
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8058 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8059 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
8060
8061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8062 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8063 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
8064 motionArgs.buttonState);
8065
Michael Wrightd02c5b62014-02-10 15:10:22 -08008066 processKey(mapper, BTN_RIGHT, 0);
8067 processSync(mapper);
8068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008069 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008071
8072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008074 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008075
8076 processKey(mapper, BTN_MIDDLE, 0);
8077 processSync(mapper);
8078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008079 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008080 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008081
8082 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008083 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008084 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008085
8086 // press BTN_BACK, release BTN_BACK
8087 processKey(mapper, BTN_BACK, 1);
8088 processSync(mapper);
8089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8090 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8091 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008092
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008094 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008095 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8096
8097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8098 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8099 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008100
8101 processKey(mapper, BTN_BACK, 0);
8102 processSync(mapper);
8103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008104 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008106
8107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008108 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008109 ASSERT_EQ(0, motionArgs.buttonState);
8110
Michael Wrightd02c5b62014-02-10 15:10:22 -08008111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8113 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8114
8115 // press BTN_SIDE, release BTN_SIDE
8116 processKey(mapper, BTN_SIDE, 1);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8119 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8120 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121
Michael Wrightd02c5b62014-02-10 15:10:22 -08008122 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008123 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008124 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8125
8126 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8127 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8128 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008129
8130 processKey(mapper, BTN_SIDE, 0);
8131 processSync(mapper);
8132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008133 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008134 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008135
8136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008138 ASSERT_EQ(0, motionArgs.buttonState);
8139
Michael Wrightd02c5b62014-02-10 15:10:22 -08008140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8141 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8142 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8143
8144 // press BTN_FORWARD, release BTN_FORWARD
8145 processKey(mapper, BTN_FORWARD, 1);
8146 processSync(mapper);
8147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8148 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8149 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008150
Michael Wrightd02c5b62014-02-10 15:10:22 -08008151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008152 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008153 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8154
8155 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8156 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8157 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008158
8159 processKey(mapper, BTN_FORWARD, 0);
8160 processSync(mapper);
8161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008162 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008163 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008164
8165 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008166 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008167 ASSERT_EQ(0, motionArgs.buttonState);
8168
Michael Wrightd02c5b62014-02-10 15:10:22 -08008169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8170 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8171 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8172
8173 // press BTN_EXTRA, release BTN_EXTRA
8174 processKey(mapper, BTN_EXTRA, 1);
8175 processSync(mapper);
8176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8177 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8178 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008179
Michael Wrightd02c5b62014-02-10 15:10:22 -08008180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008181 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008182 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8183
8184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8185 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8186 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008187
8188 processKey(mapper, BTN_EXTRA, 0);
8189 processSync(mapper);
8190 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008191 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008192 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008193
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008195 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008196 ASSERT_EQ(0, motionArgs.buttonState);
8197
Michael Wrightd02c5b62014-02-10 15:10:22 -08008198 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8199 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8200 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8201
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8203
Michael Wrightd02c5b62014-02-10 15:10:22 -08008204 // press BTN_STYLUS, release BTN_STYLUS
8205 processKey(mapper, BTN_STYLUS, 1);
8206 processSync(mapper);
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8208 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008209 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8210
8211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8213 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008214
8215 processKey(mapper, BTN_STYLUS, 0);
8216 processSync(mapper);
8217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008218 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008219 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008220
8221 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008222 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008223 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008224
8225 // press BTN_STYLUS2, release BTN_STYLUS2
8226 processKey(mapper, BTN_STYLUS2, 1);
8227 processSync(mapper);
8228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008230 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8231
8232 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8233 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8234 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008235
8236 processKey(mapper, BTN_STYLUS2, 0);
8237 processSync(mapper);
8238 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008239 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008240 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008241
8242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008244 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008245
8246 // release touch
8247 processId(mapper, -1);
8248 processSync(mapper);
8249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8250 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8251 ASSERT_EQ(0, motionArgs.buttonState);
8252}
8253
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008254TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8255 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008256 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008257 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008258 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008259
8260 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8261 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8262
8263 // Touch down.
8264 processId(mapper, 1);
8265 processPosition(mapper, 100, 200);
8266 processSync(mapper);
8267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8269
8270 // Press and release button mapped to the primary stylus button.
8271 processKey(mapper, BTN_A, 1);
8272 processSync(mapper);
8273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8274 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8275 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8277 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8278 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8279
8280 processKey(mapper, BTN_A, 0);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8283 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8285 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8286
8287 // Press and release the HID usage mapped to the secondary stylus button.
8288 processHidUsage(mapper, 0xabcd, 1);
8289 processSync(mapper);
8290 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8291 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8292 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8293 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8294 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8295 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8296
8297 processHidUsage(mapper, 0xabcd, 0);
8298 processSync(mapper);
8299 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8300 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8301 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8303
8304 // Release touch.
8305 processId(mapper, -1);
8306 processSync(mapper);
8307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8308 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8309}
8310
Michael Wrightd02c5b62014-02-10 15:10:22 -08008311TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008312 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008313 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008315 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008316
8317 NotifyMotionArgs motionArgs;
8318
8319 // default tool type is finger
8320 processId(mapper, 1);
8321 processPosition(mapper, 100, 200);
8322 processSync(mapper);
8323 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8324 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008325 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008326
8327 // eraser
8328 processKey(mapper, BTN_TOOL_RUBBER, 1);
8329 processSync(mapper);
8330 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8331 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008332 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008333
8334 // stylus
8335 processKey(mapper, BTN_TOOL_RUBBER, 0);
8336 processKey(mapper, BTN_TOOL_PEN, 1);
8337 processSync(mapper);
8338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008340 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008341
8342 // brush
8343 processKey(mapper, BTN_TOOL_PEN, 0);
8344 processKey(mapper, BTN_TOOL_BRUSH, 1);
8345 processSync(mapper);
8346 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8347 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008348 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008349
8350 // pencil
8351 processKey(mapper, BTN_TOOL_BRUSH, 0);
8352 processKey(mapper, BTN_TOOL_PENCIL, 1);
8353 processSync(mapper);
8354 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8355 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008356 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008357
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008358 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008359 processKey(mapper, BTN_TOOL_PENCIL, 0);
8360 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8363 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008364 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365
8366 // mouse
8367 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8368 processKey(mapper, BTN_TOOL_MOUSE, 1);
8369 processSync(mapper);
8370 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8371 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008372 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008373
8374 // lens
8375 processKey(mapper, BTN_TOOL_MOUSE, 0);
8376 processKey(mapper, BTN_TOOL_LENS, 1);
8377 processSync(mapper);
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8379 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008380 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008381
8382 // double-tap
8383 processKey(mapper, BTN_TOOL_LENS, 0);
8384 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8385 processSync(mapper);
8386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8387 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008388 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008389
8390 // triple-tap
8391 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8392 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8393 processSync(mapper);
8394 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8395 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008396 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008397
8398 // quad-tap
8399 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8400 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8401 processSync(mapper);
8402 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8403 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008404 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008405
8406 // finger
8407 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8408 processKey(mapper, BTN_TOOL_FINGER, 1);
8409 processSync(mapper);
8410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008412 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008413
8414 // stylus trumps finger
8415 processKey(mapper, BTN_TOOL_PEN, 1);
8416 processSync(mapper);
8417 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8418 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008419 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008420
8421 // eraser trumps stylus
8422 processKey(mapper, BTN_TOOL_RUBBER, 1);
8423 processSync(mapper);
8424 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8425 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008426 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008427
8428 // mouse trumps eraser
8429 processKey(mapper, BTN_TOOL_MOUSE, 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::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434
8435 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8436 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8437 processSync(mapper);
8438 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8439 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008440 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008441
8442 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8443 processToolType(mapper, MT_TOOL_PEN);
8444 processSync(mapper);
8445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8446 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008447 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008448
8449 // back to default tool type
8450 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8451 processKey(mapper, BTN_TOOL_MOUSE, 0);
8452 processKey(mapper, BTN_TOOL_RUBBER, 0);
8453 processKey(mapper, BTN_TOOL_PEN, 0);
8454 processKey(mapper, BTN_TOOL_FINGER, 0);
8455 processSync(mapper);
8456 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8457 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008458 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008459}
8460
8461TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008462 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008463 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008464 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008465 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008466 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008467
8468 NotifyMotionArgs motionArgs;
8469
8470 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8471 processId(mapper, 1);
8472 processPosition(mapper, 100, 200);
8473 processSync(mapper);
8474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8475 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8476 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8477 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8478
8479 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8480 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8481 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8482 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8483
8484 // move a little
8485 processPosition(mapper, 150, 250);
8486 processSync(mapper);
8487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8488 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8489 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8490 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8491
8492 // down when BTN_TOUCH is pressed, pressure defaults to 1
8493 processKey(mapper, BTN_TOUCH, 1);
8494 processSync(mapper);
8495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8496 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8497 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8498 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8499
8500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8501 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8502 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8503 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8504
8505 // up when BTN_TOUCH is released, hover restored
8506 processKey(mapper, BTN_TOUCH, 0);
8507 processSync(mapper);
8508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8509 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8510 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8511 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8512
8513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8514 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8515 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8516 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8517
8518 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8519 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8520 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8521 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8522
8523 // exit hover when pointer goes away
8524 processId(mapper, -1);
8525 processSync(mapper);
8526 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8527 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8528 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8529 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8530}
8531
8532TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008533 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008534 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008536 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008537
8538 NotifyMotionArgs motionArgs;
8539
8540 // initially hovering because pressure is 0
8541 processId(mapper, 1);
8542 processPosition(mapper, 100, 200);
8543 processPressure(mapper, 0);
8544 processSync(mapper);
8545 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8546 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8547 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8548 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8549
8550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8551 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8552 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8553 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8554
8555 // move a little
8556 processPosition(mapper, 150, 250);
8557 processSync(mapper);
8558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8559 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, 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 // down when pressure becomes non-zero
8564 processPressure(mapper, RAW_PRESSURE_MAX);
8565 processSync(mapper);
8566 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8567 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8568 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8569 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8570
8571 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8572 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8573 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8574 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8575
8576 // up when pressure becomes 0, hover restored
8577 processPressure(mapper, 0);
8578 processSync(mapper);
8579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8580 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8581 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8582 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8583
8584 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8585 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8586 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8587 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8588
8589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8590 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8591 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8592 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8593
8594 // exit hover when pointer goes away
8595 processId(mapper, -1);
8596 processSync(mapper);
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8598 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8599 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8600 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8601}
8602
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008603/**
8604 * Set the input device port <--> display port associations, and check that the
8605 * events are routed to the display that matches the display port.
8606 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8607 */
8608TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008609 const std::string usb2 = "USB2";
8610 const uint8_t hdmi1 = 0;
8611 const uint8_t hdmi2 = 1;
8612 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008613 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008614
8615 addConfigurationProperty("touch.deviceType", "touchScreen");
8616 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008617 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008618
8619 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8620 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8621
8622 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8623 // for this input device is specified, and the matching viewport is not present,
8624 // the input device should be disabled (at the mapper level).
8625
8626 // Add viewport for display 2 on hdmi2
8627 prepareSecondaryDisplay(type, hdmi2);
8628 // Send a touch event
8629 processPosition(mapper, 100, 100);
8630 processSync(mapper);
8631 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8632
8633 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008634 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008635 // Send a touch event again
8636 processPosition(mapper, 100, 100);
8637 processSync(mapper);
8638
8639 NotifyMotionArgs args;
8640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8641 ASSERT_EQ(DISPLAY_ID, args.displayId);
8642}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008643
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008644TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8645 addConfigurationProperty("touch.deviceType", "touchScreen");
8646 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008647 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008648
8649 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8650
Michael Wrighta9cf4192022-12-01 23:46:39 +00008651 prepareDisplay(ui::ROTATION_0);
8652 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008653
8654 // Send a touch event
8655 processPosition(mapper, 100, 100);
8656 processSync(mapper);
8657
8658 NotifyMotionArgs args;
8659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8660 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8661}
8662
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008663TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008664 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008665
Michael Wrighta9cf4192022-12-01 23:46:39 +00008666 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008667 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008668 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008669
Josep del Río2d8c79a2023-01-23 19:33:50 +00008670 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008671
8672 NotifyMotionArgs motionArgs;
8673 processPosition(mapper, 100, 100);
8674 processSync(mapper);
8675
8676 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8677 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Linnan Li13bf76a2024-05-05 19:18:02 +08008678 ASSERT_EQ(ui::ADISPLAY_ID_NONE, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008679}
8680
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008681/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008682 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8683 */
8684TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8685 addConfigurationProperty("touch.deviceType", "touchScreen");
8686 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008687 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008688
Michael Wrighta9cf4192022-12-01 23:46:39 +00008689 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008690 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8691 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8692 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8693 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008694
8695 NotifyMotionArgs args;
8696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8697 ASSERT_EQ(26, args.readTime);
8698
Harry Cutts33476232023-01-30 19:57:29 +00008699 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8700 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8701 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008702
8703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8704 ASSERT_EQ(33, args.readTime);
8705}
8706
8707/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008708 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8709 * events should not be delivered to the listener.
8710 */
8711TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8712 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008713 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008714 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008715 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008716 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008717 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008718 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008719
8720 NotifyMotionArgs motionArgs;
8721 processPosition(mapper, 100, 100);
8722 processSync(mapper);
8723
8724 mFakeListener->assertNotifyMotionWasNotCalled();
8725}
8726
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008727/**
8728 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8729 * the touch mapper can process the events and the events can be delivered to the listener.
8730 */
8731TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8732 addConfigurationProperty("touch.deviceType", "touchScreen");
8733 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008734 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008735 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008736 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008737 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008738 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008739
8740 NotifyMotionArgs motionArgs;
8741 processPosition(mapper, 100, 100);
8742 processSync(mapper);
8743
8744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8745 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8746}
8747
Josh Thielene986aed2023-06-01 14:17:30 +00008748/**
8749 * When the viewport is deactivated (isActive transitions from true to false),
8750 * and touch.enableForInactiveViewport is false, touches prior to the transition
8751 * should be cancelled.
8752 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008753TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8754 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008755 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008756 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008757 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008758 std::optional<DisplayViewport> optionalDisplayViewport =
8759 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8760 ASSERT_TRUE(optionalDisplayViewport.has_value());
8761 DisplayViewport displayViewport = *optionalDisplayViewport;
8762
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008763 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008764 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008765 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008766
8767 // Finger down
8768 int32_t x = 100, y = 100;
8769 processPosition(mapper, x, y);
8770 processSync(mapper);
8771
8772 NotifyMotionArgs motionArgs;
8773 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8774 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8775
8776 // Deactivate display viewport
8777 displayViewport.isActive = false;
8778 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008779 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008780
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008781 // The ongoing touch should be canceled immediately
8782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8783 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8784
8785 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008786 x += 10, y += 10;
8787 processPosition(mapper, x, y);
8788 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008790
8791 // Reactivate display viewport
8792 displayViewport.isActive = true;
8793 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008794 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008795
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008796 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008797 x += 10, y += 10;
8798 processPosition(mapper, x, y);
8799 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008800 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8801 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008802}
8803
Josh Thielene986aed2023-06-01 14:17:30 +00008804/**
8805 * When the viewport is deactivated (isActive transitions from true to false),
8806 * and touch.enableForInactiveViewport is true, touches prior to the transition
8807 * should not be cancelled.
8808 */
8809TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8810 addConfigurationProperty("touch.deviceType", "touchScreen");
8811 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8812 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8813 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8814 std::optional<DisplayViewport> optionalDisplayViewport =
8815 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8816 ASSERT_TRUE(optionalDisplayViewport.has_value());
8817 DisplayViewport displayViewport = *optionalDisplayViewport;
8818
8819 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8820 prepareAxes(POSITION);
8821 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8822
8823 // Finger down
8824 int32_t x = 100, y = 100;
8825 processPosition(mapper, x, y);
8826 processSync(mapper);
8827 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8828 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8829
8830 // Deactivate display viewport
8831 displayViewport.isActive = false;
8832 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8833 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8834
8835 // The ongoing touch should not be canceled
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8837
8838 // Finger move is not ignored
8839 x += 10, y += 10;
8840 processPosition(mapper, x, y);
8841 processSync(mapper);
8842 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8843 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8844
8845 // Reactivate display viewport
8846 displayViewport.isActive = true;
8847 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8848 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8849
8850 // Finger move continues and does not start new gesture
8851 x += 10, y += 10;
8852 processPosition(mapper, x, y);
8853 processSync(mapper);
8854 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8855 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8856}
8857
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008858TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008859 prepareAxes(POSITION);
8860 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008861 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008862 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008863
8864 NotifyMotionArgs motionArgs;
8865 // Unrotated video frame
8866 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8867 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008868 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008869 processPosition(mapper, 100, 200);
8870 processSync(mapper);
8871 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8872 ASSERT_EQ(frames, motionArgs.videoFrames);
8873
8874 // Subsequent touch events should not have any videoframes
8875 // This is implemented separately in FakeEventHub,
8876 // but that should match the behaviour of TouchVideoDevice.
8877 processPosition(mapper, 200, 200);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8881}
8882
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008883TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008884 prepareAxes(POSITION);
8885 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008886 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008887 // Unrotated video frame
8888 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8889 NotifyMotionArgs motionArgs;
8890
8891 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008892 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008893 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008894 clearViewports();
8895 prepareDisplay(orientation);
8896 std::vector<TouchVideoFrame> frames{frame};
8897 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8898 processPosition(mapper, 100, 200);
8899 processSync(mapper);
8900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8901 ASSERT_EQ(frames, motionArgs.videoFrames);
8902 }
8903}
8904
8905TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8906 prepareAxes(POSITION);
8907 addConfigurationProperty("touch.deviceType", "touchScreen");
8908 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8909 // orientation-aware are affected by display rotation.
8910 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008911 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008912 // Unrotated video frame
8913 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8914 NotifyMotionArgs motionArgs;
8915
8916 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008917 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008918 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008919 clearViewports();
8920 prepareDisplay(orientation);
8921 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008922 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008923 processPosition(mapper, 100, 200);
8924 processSync(mapper);
8925 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008926 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8927 // compared to the display. This is so that when the window transform (which contains the
8928 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8929 // window's coordinate space.
8930 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008931 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008932
8933 // Release finger.
8934 processSync(mapper);
8935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008936 }
8937}
8938
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008939TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008940 prepareAxes(POSITION);
8941 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008942 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008943 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8944 // so mix these.
8945 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8946 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8947 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8948 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8949 NotifyMotionArgs motionArgs;
8950
Michael Wrighta9cf4192022-12-01 23:46:39 +00008951 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008952 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008953 processPosition(mapper, 100, 200);
8954 processSync(mapper);
8955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008956 ASSERT_EQ(frames, motionArgs.videoFrames);
8957}
8958
8959TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8960 prepareAxes(POSITION);
8961 addConfigurationProperty("touch.deviceType", "touchScreen");
8962 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8963 // orientation-aware are affected by display rotation.
8964 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008965 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008966 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8967 // so mix these.
8968 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8969 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8970 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8971 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8972 NotifyMotionArgs motionArgs;
8973
Michael Wrighta9cf4192022-12-01 23:46:39 +00008974 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008975 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8976 processPosition(mapper, 100, 200);
8977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8980 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8981 // compared to the display. This is so that when the window transform (which contains the
8982 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8983 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008984 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008985 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008986 ASSERT_EQ(frames, motionArgs.videoFrames);
8987}
8988
Arthur Hung9da14732019-09-02 16:16:58 +08008989/**
8990 * If we had defined port associations, but the viewport is not ready, the touch device would be
8991 * expected to be disabled, and it should be enabled after the viewport has found.
8992 */
8993TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008994 constexpr uint8_t hdmi2 = 1;
8995 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008996 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008997
8998 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8999
9000 addConfigurationProperty("touch.deviceType", "touchScreen");
9001 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00009002 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08009003
9004 ASSERT_EQ(mDevice->isEnabled(), false);
9005
9006 // Add display on hdmi2, the device should be enabled and can receive touch event.
9007 prepareSecondaryDisplay(type, hdmi2);
9008 ASSERT_EQ(mDevice->isEnabled(), true);
9009
9010 // Send a touch event.
9011 processPosition(mapper, 100, 100);
9012 processSync(mapper);
9013
9014 NotifyMotionArgs args;
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9016 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
9017}
9018
Arthur Hung421eb1c2020-01-16 00:09:42 +08009019TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009020 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009021 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009022 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009023 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009024
9025 NotifyMotionArgs motionArgs;
9026
9027 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9028 // finger down
9029 processId(mapper, 1);
9030 processPosition(mapper, x1, y1);
9031 processSync(mapper);
9032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9033 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009034 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009035
9036 // finger move
9037 processId(mapper, 1);
9038 processPosition(mapper, x2, y2);
9039 processSync(mapper);
9040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9041 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009042 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009043
9044 // finger up.
9045 processId(mapper, -1);
9046 processSync(mapper);
9047 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9048 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009049 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009050
9051 // new finger down
9052 processId(mapper, 1);
9053 processPosition(mapper, x3, y3);
9054 processSync(mapper);
9055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9056 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009057 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009058}
9059
9060/**
arthurhungcc7f9802020-04-30 17:55:40 +08009061 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
9062 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08009063 */
arthurhungcc7f9802020-04-30 17:55:40 +08009064TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08009065 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009066 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009067 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009068 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08009069
9070 NotifyMotionArgs motionArgs;
9071
9072 // default tool type is finger
9073 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08009074 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009075 processPosition(mapper, x1, y1);
9076 processSync(mapper);
9077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9078 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009079 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009080
9081 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
9082 processToolType(mapper, MT_TOOL_PALM);
9083 processSync(mapper);
9084 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9085 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9086
9087 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08009088 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009089 processPosition(mapper, x2, y2);
9090 processSync(mapper);
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9092
9093 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08009094 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009095 processSync(mapper);
9096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9097
9098 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009099 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009100 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009101 processPosition(mapper, x3, y3);
9102 processSync(mapper);
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009105 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009106}
9107
arthurhungbf89a482020-04-17 17:37:55 +08009108/**
arthurhungcc7f9802020-04-30 17:55:40 +08009109 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9110 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009111 */
arthurhungcc7f9802020-04-30 17:55:40 +08009112TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009113 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009114 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009115 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009116 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009117
9118 NotifyMotionArgs motionArgs;
9119
9120 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009121 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9122 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009123 processPosition(mapper, x1, y1);
9124 processSync(mapper);
9125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009127 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009128
9129 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009130 processSlot(mapper, SECOND_SLOT);
9131 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009132 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009133 processSync(mapper);
9134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009135 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009136 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009137
9138 // If the tool type of the first finger changes to MT_TOOL_PALM,
9139 // we expect to receive ACTION_POINTER_UP with cancel flag.
9140 processSlot(mapper, FIRST_SLOT);
9141 processId(mapper, FIRST_TRACKING_ID);
9142 processToolType(mapper, MT_TOOL_PALM);
9143 processSync(mapper);
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009145 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009146 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9147
9148 // The following MOVE events of second finger should be processed.
9149 processSlot(mapper, SECOND_SLOT);
9150 processId(mapper, SECOND_TRACKING_ID);
9151 processPosition(mapper, x2 + 1, y2 + 1);
9152 processSync(mapper);
9153 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9154 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009155 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009156
9157 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9158 // it. Second finger receive move.
9159 processSlot(mapper, FIRST_SLOT);
9160 processId(mapper, INVALID_TRACKING_ID);
9161 processSync(mapper);
9162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9163 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009164 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009165
9166 // Second finger keeps moving.
9167 processSlot(mapper, SECOND_SLOT);
9168 processId(mapper, SECOND_TRACKING_ID);
9169 processPosition(mapper, x2 + 2, y2 + 2);
9170 processSync(mapper);
9171 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9172 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009173 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009174
9175 // Second finger up.
9176 processId(mapper, INVALID_TRACKING_ID);
9177 processSync(mapper);
9178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9179 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9180 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9181}
9182
9183/**
9184 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9185 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9186 */
9187TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9188 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009189 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009190 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009191 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009192
9193 NotifyMotionArgs motionArgs;
9194
9195 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9196 // First finger down.
9197 processId(mapper, FIRST_TRACKING_ID);
9198 processPosition(mapper, x1, y1);
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);
arthurhungcc7f9802020-04-30 17:55:40 +08009203
9204 // Second finger down.
9205 processSlot(mapper, SECOND_SLOT);
9206 processId(mapper, SECOND_TRACKING_ID);
9207 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009208 processSync(mapper);
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009210 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009211 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009212
arthurhungcc7f9802020-04-30 17:55:40 +08009213 // If the tool type of the first finger changes to MT_TOOL_PALM,
9214 // we expect to receive ACTION_POINTER_UP with cancel flag.
9215 processSlot(mapper, FIRST_SLOT);
9216 processId(mapper, FIRST_TRACKING_ID);
9217 processToolType(mapper, MT_TOOL_PALM);
9218 processSync(mapper);
9219 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009220 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009221 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9222
9223 // Second finger keeps moving.
9224 processSlot(mapper, SECOND_SLOT);
9225 processId(mapper, SECOND_TRACKING_ID);
9226 processPosition(mapper, x2 + 1, y2 + 1);
9227 processSync(mapper);
9228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9230
9231 // second finger becomes palm, receive cancel due to only 1 finger is active.
9232 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009233 processToolType(mapper, MT_TOOL_PALM);
9234 processSync(mapper);
9235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9236 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9237
arthurhungcc7f9802020-04-30 17:55:40 +08009238 // third finger down.
9239 processSlot(mapper, THIRD_SLOT);
9240 processId(mapper, THIRD_TRACKING_ID);
9241 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009242 processPosition(mapper, x3, y3);
9243 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009244 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9245 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009246 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009247 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009248
9249 // third finger move
9250 processId(mapper, THIRD_TRACKING_ID);
9251 processPosition(mapper, x3 + 1, y3 + 1);
9252 processSync(mapper);
9253 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9254 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9255
9256 // first finger up, third finger receive move.
9257 processSlot(mapper, FIRST_SLOT);
9258 processId(mapper, INVALID_TRACKING_ID);
9259 processSync(mapper);
9260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9261 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009262 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009263
9264 // second finger up, third finger receive move.
9265 processSlot(mapper, SECOND_SLOT);
9266 processId(mapper, INVALID_TRACKING_ID);
9267 processSync(mapper);
9268 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9269 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009270 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009271
9272 // third finger up.
9273 processSlot(mapper, THIRD_SLOT);
9274 processId(mapper, INVALID_TRACKING_ID);
9275 processSync(mapper);
9276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9277 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9278 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9279}
9280
9281/**
9282 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9283 * and the active finger could still be allowed to receive the events
9284 */
9285TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9286 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009287 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009288 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009289 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009290
9291 NotifyMotionArgs motionArgs;
9292
9293 // default tool type is finger
9294 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9295 processId(mapper, FIRST_TRACKING_ID);
9296 processPosition(mapper, x1, y1);
9297 processSync(mapper);
9298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9299 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009300 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009301
9302 // Second finger down.
9303 processSlot(mapper, SECOND_SLOT);
9304 processId(mapper, SECOND_TRACKING_ID);
9305 processPosition(mapper, x2, y2);
9306 processSync(mapper);
9307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009308 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009309 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009310
9311 // If the tool type of the second finger changes to MT_TOOL_PALM,
9312 // we expect to receive ACTION_POINTER_UP with cancel flag.
9313 processId(mapper, SECOND_TRACKING_ID);
9314 processToolType(mapper, MT_TOOL_PALM);
9315 processSync(mapper);
9316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009317 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009318 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9319
9320 // The following MOVE event should be processed.
9321 processSlot(mapper, FIRST_SLOT);
9322 processId(mapper, FIRST_TRACKING_ID);
9323 processPosition(mapper, x1 + 1, y1 + 1);
9324 processSync(mapper);
9325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009327 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009328
9329 // second finger up.
9330 processSlot(mapper, SECOND_SLOT);
9331 processId(mapper, INVALID_TRACKING_ID);
9332 processSync(mapper);
9333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9334 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9335
9336 // first finger keep moving
9337 processSlot(mapper, FIRST_SLOT);
9338 processId(mapper, FIRST_TRACKING_ID);
9339 processPosition(mapper, x1 + 2, y1 + 2);
9340 processSync(mapper);
9341 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9342 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9343
9344 // first finger up.
9345 processId(mapper, INVALID_TRACKING_ID);
9346 processSync(mapper);
9347 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9348 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9349 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009350}
9351
Arthur Hung9ad18942021-06-19 02:04:46 +00009352/**
9353 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9354 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9355 * cause slot be valid again.
9356 */
9357TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9358 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009359 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009360 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009361 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009362
9363 NotifyMotionArgs motionArgs;
9364
9365 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9366 // First finger down.
9367 processId(mapper, FIRST_TRACKING_ID);
9368 processPosition(mapper, x1, y1);
9369 processPressure(mapper, RAW_PRESSURE_MAX);
9370 processSync(mapper);
9371 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9372 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009373 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009374
9375 // First finger move.
9376 processId(mapper, FIRST_TRACKING_ID);
9377 processPosition(mapper, x1 + 1, y1 + 1);
9378 processPressure(mapper, RAW_PRESSURE_MAX);
9379 processSync(mapper);
9380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009382 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009383
9384 // Second finger down.
9385 processSlot(mapper, SECOND_SLOT);
9386 processId(mapper, SECOND_TRACKING_ID);
9387 processPosition(mapper, x2, y2);
9388 processPressure(mapper, RAW_PRESSURE_MAX);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009391 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009392 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009393
9394 // second finger up with some unexpected data.
9395 processSlot(mapper, SECOND_SLOT);
9396 processId(mapper, INVALID_TRACKING_ID);
9397 processPosition(mapper, x2, y2);
9398 processSync(mapper);
9399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009400 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009401 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009402
9403 // first finger up with some unexpected data.
9404 processSlot(mapper, FIRST_SLOT);
9405 processId(mapper, INVALID_TRACKING_ID);
9406 processPosition(mapper, x2, y2);
9407 processPressure(mapper, RAW_PRESSURE_MAX);
9408 processSync(mapper);
9409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9410 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009411 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009412}
9413
Arpit Singh4b4a4572023-11-24 18:19:56 +00009414TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009415 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009416 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009417 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009418 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009419
9420 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009421 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009422 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009423 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009424 processPressure(mapper, RAW_PRESSURE_MAX);
9425 processSync(mapper);
9426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9427 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9428
9429 // Second finger down.
9430 processSlot(mapper, SECOND_SLOT);
9431 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009432 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009433 processPressure(mapper, RAW_PRESSURE_MAX);
9434 processSync(mapper);
9435 ASSERT_NO_FATAL_FAILURE(
9436 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9437
Arpit Singh4b4a4572023-11-24 18:19:56 +00009438 // Set MT Slot state to be repopulated for the required slots
9439 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9440 mtSlotValues[0] = FIRST_TRACKING_ID;
9441 mtSlotValues[1] = SECOND_TRACKING_ID;
9442 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9443
9444 mtSlotValues[0] = x1;
9445 mtSlotValues[1] = x2;
9446 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9447
9448 mtSlotValues[0] = y1;
9449 mtSlotValues[1] = y2;
9450 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9451
9452 mtSlotValues[0] = RAW_PRESSURE_MAX;
9453 mtSlotValues[1] = RAW_PRESSURE_MAX;
9454 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9455
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009456 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009457 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009458 resetMapper(mapper, ARBITRARY_TIME);
9459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9460 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009461
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009462 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9463 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009464 processPosition(mapper, 301, 302);
9465 processSync(mapper);
9466 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9467 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9469 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009470
9471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9472}
9473
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009474TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009475 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009476 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009477 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009478 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009479
9480 // First finger touches down and releases.
9481 processId(mapper, FIRST_TRACKING_ID);
9482 processPosition(mapper, 100, 200);
9483 processPressure(mapper, RAW_PRESSURE_MAX);
9484 processSync(mapper);
9485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9486 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9487 processId(mapper, INVALID_TRACKING_ID);
9488 processSync(mapper);
9489 ASSERT_NO_FATAL_FAILURE(
9490 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9491
9492 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9493 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009494 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009495 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9496
9497 // Send an empty sync frame. Since there are no pointers, no events are generated.
9498 processSync(mapper);
9499 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9500}
9501
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009502TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009503 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009504 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009505 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009506 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009507 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009508
9509 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9510 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9511 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9512 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9513 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9514
9515 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009516 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009517 processId(mapper, FIRST_TRACKING_ID);
9518 processToolType(mapper, MT_TOOL_PEN);
9519 processPosition(mapper, 100, 200);
9520 processPressure(mapper, RAW_PRESSURE_MAX);
9521 processSync(mapper);
9522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9523 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9524 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009525 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009526
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009527 // Now that we know the device supports styluses, ensure that the device is re-configured with
9528 // the stylus source.
9529 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9530 {
9531 const auto& devices = mReader->getInputDevices();
9532 auto deviceInfo =
9533 std::find_if(devices.begin(), devices.end(),
9534 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9535 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9536 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9537 }
9538
9539 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9541
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009542 processId(mapper, INVALID_TRACKING_ID);
9543 processSync(mapper);
9544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9545 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9546 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009547 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009548}
9549
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009550// --- MultiTouchInputMapperTest_ExternalDevice ---
9551
9552class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9553protected:
Chris Yea52ade12020-08-27 16:49:20 -07009554 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009555};
9556
9557/**
9558 * Expect fallback to internal viewport if device is external and external viewport is not present.
9559 */
9560TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9561 prepareAxes(POSITION);
9562 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009563 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009564 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009565
9566 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9567
9568 NotifyMotionArgs motionArgs;
9569
9570 // Expect the event to be sent to the internal viewport,
9571 // because an external viewport is not present.
9572 processPosition(mapper, 100, 100);
9573 processSync(mapper);
9574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Linnan Li13bf76a2024-05-05 19:18:02 +08009575 ASSERT_EQ(ui::ADISPLAY_ID_DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009576
9577 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009578 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009579 processPosition(mapper, 100, 100);
9580 processSync(mapper);
9581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9582 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9583}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009584
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009585// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9586// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9587// PointerChoreographer refactor.
9588TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009589 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009590 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009591 prepareAxes(POSITION | ID | SLOT);
9592 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9593 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009594 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009595 // run uncaptured pointer tests - pushes out generic events
9596 // FINGER 0 DOWN
9597 processId(mapper, 3);
9598 processPosition(mapper, 100, 100);
9599 processKey(mapper, BTN_TOUCH, 1);
9600 processSync(mapper);
9601
9602 // start at (100,100), cursor should be at (0,0) * scale
9603 NotifyMotionArgs args;
9604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9605 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9606 ASSERT_NO_FATAL_FAILURE(
9607 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9608
9609 // FINGER 0 MOVE
9610 processPosition(mapper, 200, 200);
9611 processSync(mapper);
9612
9613 // compute scaling to help with touch position checking
9614 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9615 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9616 float scale =
9617 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9618
9619 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9620 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9621 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9622 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9623 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009624
9625 // BUTTON DOWN
9626 processKey(mapper, BTN_LEFT, 1);
9627 processSync(mapper);
9628
9629 // touchinputmapper design sends a move before button press
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9633 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9634
9635 // BUTTON UP
9636 processKey(mapper, BTN_LEFT, 0);
9637 processSync(mapper);
9638
9639 // touchinputmapper design sends a move after button release
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9641 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9643 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009644}
9645
Harry Cutts8722be92024-04-05 14:46:05 +00009646TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009647 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009648 prepareAxes(POSITION | ID | SLOT);
9649 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009650 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009651 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009652
Josep del Río2d8c79a2023-01-23 19:33:50 +00009653 // uncaptured touchpad should be a pointer device
9654 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009655}
9656
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009657// --- BluetoothMultiTouchInputMapperTest ---
9658
9659class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9660protected:
9661 void SetUp() override {
9662 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9663 }
9664};
9665
9666TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9667 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009668 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009669 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009670 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009671
9672 nsecs_t kernelEventTime = ARBITRARY_TIME;
9673 nsecs_t expectedEventTime = ARBITRARY_TIME;
9674 // Touch down.
9675 processId(mapper, FIRST_TRACKING_ID);
9676 processPosition(mapper, 100, 200);
9677 processPressure(mapper, RAW_PRESSURE_MAX);
9678 processSync(mapper, ARBITRARY_TIME);
9679 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9680 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9681
9682 // Process several events that come in quick succession, according to their timestamps.
9683 for (int i = 0; i < 3; i++) {
9684 constexpr static nsecs_t delta = ms2ns(1);
9685 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9686 kernelEventTime += delta;
9687 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9688
9689 processPosition(mapper, 101 + i, 201 + i);
9690 processSync(mapper, kernelEventTime);
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9692 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9693 WithEventTime(expectedEventTime))));
9694 }
9695
9696 // Release the touch.
9697 processId(mapper, INVALID_TRACKING_ID);
9698 processPressure(mapper, RAW_PRESSURE_MIN);
9699 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9701 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9702 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9703}
9704
9705// --- MultiTouchPointerModeTest ---
9706
HQ Liue6983c72022-04-19 22:14:56 +00009707class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9708protected:
9709 float mPointerMovementScale;
9710 float mPointerXZoomScale;
9711 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9712 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009713 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009714
9715 prepareAxes(POSITION);
9716 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9717 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9718 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009719 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009720 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009721
9722 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9723 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9724 mPointerMovementScale =
9725 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9726 mPointerXZoomScale =
9727 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9728 }
9729
9730 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9731 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9732 /*flat*/ 0,
9733 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9734 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9735 /*flat*/ 0,
9736 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9737 }
9738};
9739
9740/**
9741 * Two fingers down on a pointer mode touch pad. The width
9742 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9743 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9744 * be greater than the both value to be freeform gesture, so that after two
9745 * fingers start to move downwards, the gesture should be swipe.
9746 */
9747TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9748 // The min freeform gesture width is 25units/mm x 30mm = 750
9749 // which is greater than fraction of the diagnal length of the touchpad (349).
9750 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009751 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009752 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009753 NotifyMotionArgs motionArgs;
9754
9755 // Two fingers down at once.
9756 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9757 // Pointer's initial position is used the [0,0] coordinate.
9758 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9759
9760 processId(mapper, FIRST_TRACKING_ID);
9761 processPosition(mapper, x1, y1);
9762 processMTSync(mapper);
9763 processId(mapper, SECOND_TRACKING_ID);
9764 processPosition(mapper, x2, y2);
9765 processMTSync(mapper);
9766 processSync(mapper);
9767
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009769 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009770 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009771 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009772 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009773 ASSERT_NO_FATAL_FAILURE(
9774 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9775
9776 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9777 // that there should be 1 pointer.
9778 int32_t movingDistance = 200;
9779 y1 += movingDistance;
9780 y2 += movingDistance;
9781
9782 processId(mapper, FIRST_TRACKING_ID);
9783 processPosition(mapper, x1, y1);
9784 processMTSync(mapper);
9785 processId(mapper, SECOND_TRACKING_ID);
9786 processPosition(mapper, x2, y2);
9787 processMTSync(mapper);
9788 processSync(mapper);
9789
9790 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009791 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009792 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009793 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009794 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9796 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9797 0, 0, 0, 0));
9798}
9799
9800/**
9801 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9802 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9803 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9804 * value to be freeform gesture, so that after two fingers start to move downwards,
9805 * the gesture should be swipe.
9806 */
9807TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9808 // The min freeform gesture width is 5units/mm x 30mm = 150
9809 // which is greater than fraction of the diagnal length of the touchpad (349).
9810 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009811 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009812 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009813 NotifyMotionArgs motionArgs;
9814
9815 // Two fingers down at once.
9816 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9817 // Pointer's initial position is used the [0,0] coordinate.
9818 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9819
9820 processId(mapper, FIRST_TRACKING_ID);
9821 processPosition(mapper, x1, y1);
9822 processMTSync(mapper);
9823 processId(mapper, SECOND_TRACKING_ID);
9824 processPosition(mapper, x2, y2);
9825 processMTSync(mapper);
9826 processSync(mapper);
9827
9828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009829 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009830 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009831 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009832 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009833 ASSERT_NO_FATAL_FAILURE(
9834 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9835
9836 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9837 // and there should be 1 pointer.
9838 int32_t movingDistance = 200;
9839 y1 += movingDistance;
9840 y2 += movingDistance;
9841
9842 processId(mapper, FIRST_TRACKING_ID);
9843 processPosition(mapper, x1, y1);
9844 processMTSync(mapper);
9845 processId(mapper, SECOND_TRACKING_ID);
9846 processPosition(mapper, x2, y2);
9847 processMTSync(mapper);
9848 processSync(mapper);
9849
9850 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009851 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009853 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009854 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009855 // New coordinate is the scaled relative coordinate from the initial coordinate.
9856 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9857 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9858 0, 0, 0, 0));
9859}
9860
9861/**
9862 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9863 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9864 * freeform gestures after two fingers start to move downwards.
9865 */
9866TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009867 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009868 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009869
9870 NotifyMotionArgs motionArgs;
9871
9872 // Two fingers down at once. Wider than the max swipe width.
9873 // The gesture is expected to be PRESS, then transformed to FREEFORM
9874 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9875
9876 processId(mapper, FIRST_TRACKING_ID);
9877 processPosition(mapper, x1, y1);
9878 processMTSync(mapper);
9879 processId(mapper, SECOND_TRACKING_ID);
9880 processPosition(mapper, x2, y2);
9881 processMTSync(mapper);
9882 processSync(mapper);
9883
9884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009885 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009886 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009888 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009889 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9890 ASSERT_NO_FATAL_FAILURE(
9891 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9892
9893 int32_t movingDistance = 200;
9894
9895 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9896 // then two down events for two pointers.
9897 y1 += movingDistance;
9898 y2 += movingDistance;
9899
9900 processId(mapper, FIRST_TRACKING_ID);
9901 processPosition(mapper, x1, y1);
9902 processMTSync(mapper);
9903 processId(mapper, SECOND_TRACKING_ID);
9904 processPosition(mapper, x2, y2);
9905 processMTSync(mapper);
9906 processSync(mapper);
9907
9908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9909 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009910 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009911 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009913 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009914 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009915 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009917 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009918 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009919 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009920 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009921 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009922 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009923 // Two pointers' scaled relative coordinates from their initial centroid.
9924 // Initial y coordinates are 0 as y1 and y2 have the same value.
9925 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9926 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9927 // When pointers move, the new coordinates equal to the initial coordinates plus
9928 // scaled moving distance.
9929 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9930 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9931 0, 0, 0, 0));
9932 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9933 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9934 0, 0, 0, 0));
9935
9936 // Move two fingers down again, expect one MOVE motion event.
9937 y1 += movingDistance;
9938 y2 += movingDistance;
9939
9940 processId(mapper, FIRST_TRACKING_ID);
9941 processPosition(mapper, x1, y1);
9942 processMTSync(mapper);
9943 processId(mapper, SECOND_TRACKING_ID);
9944 processPosition(mapper, x2, y2);
9945 processMTSync(mapper);
9946 processSync(mapper);
9947
9948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009949 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009950 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009951 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009952 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009953 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9954 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9955 0, 0, 0, 0, 0));
9956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9957 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9958 0, 0, 0, 0, 0));
9959}
9960
Harry Cutts39b7ca22022-10-05 15:55:48 +00009961TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +00009962 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009963 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +00009964 NotifyMotionArgs motionArgs;
9965
9966 // Place two fingers down.
9967 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9968
9969 processId(mapper, FIRST_TRACKING_ID);
9970 processPosition(mapper, x1, y1);
9971 processMTSync(mapper);
9972 processId(mapper, SECOND_TRACKING_ID);
9973 processPosition(mapper, x2, y2);
9974 processMTSync(mapper);
9975 processSync(mapper);
9976
9977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009978 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +00009979 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9980 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
9981 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
9982 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
9983
9984 // Move the two fingers down and to the left.
9985 int32_t movingDistance = 200;
9986 x1 -= movingDistance;
9987 y1 += movingDistance;
9988 x2 -= movingDistance;
9989 y2 += movingDistance;
9990
9991 processId(mapper, FIRST_TRACKING_ID);
9992 processPosition(mapper, x1, y1);
9993 processMTSync(mapper);
9994 processId(mapper, SECOND_TRACKING_ID);
9995 processPosition(mapper, x2, y2);
9996 processMTSync(mapper);
9997 processSync(mapper);
9998
9999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010000 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000010001 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10002 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
10003 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
10004 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
10005}
10006
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010007TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000010008 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010009 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010010 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
10012
10013 // Start a stylus gesture.
10014 processKey(mapper, BTN_TOOL_PEN, 1);
10015 processId(mapper, FIRST_TRACKING_ID);
10016 processPosition(mapper, 100, 200);
10017 processSync(mapper);
10018 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10019 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
10020 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010021 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010022 // TODO(b/257078296): Pointer mode generates extra event.
10023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10024 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
10025 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010026 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10028
10029 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
10030 // gesture should be disabled.
10031 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
10032 viewport->isActive = false;
10033 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010034 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10036 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10037 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010038 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010039 // TODO(b/257078296): Pointer mode generates extra event.
10040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
10042 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010043 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000010044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10045}
10046
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010047// --- JoystickInputMapperTest ---
10048
10049class JoystickInputMapperTest : public InputMapperTest {
10050protected:
10051 static const int32_t RAW_X_MIN;
10052 static const int32_t RAW_X_MAX;
10053 static const int32_t RAW_Y_MIN;
10054 static const int32_t RAW_Y_MAX;
10055
10056 void SetUp() override {
10057 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
10058 }
10059 void prepareAxes() {
10060 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
10061 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
10062 }
10063
10064 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
10065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
10066 }
10067
10068 void processSync(JoystickInputMapper& mapper) {
10069 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
10070 }
10071
Michael Wrighta9cf4192022-12-01 23:46:39 +000010072 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010073 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
10074 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
10075 NO_PORT, ViewportType::VIRTUAL);
10076 }
10077};
10078
10079const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
10080const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
10081const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
10082const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
10083
10084TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
10085 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000010086 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010087
10088 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10089
Michael Wrighta9cf4192022-12-01 23:46:39 +000010090 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010091
10092 // Send an axis event
10093 processAxis(mapper, ABS_X, 100);
10094 processSync(mapper);
10095
10096 NotifyMotionArgs args;
10097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10098 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10099
10100 // Send another axis event
10101 processAxis(mapper, ABS_Y, 100);
10102 processSync(mapper);
10103
10104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10105 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10106}
10107
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010108// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080010109
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010110class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010111protected:
10112 static const char* DEVICE_NAME;
10113 static const char* DEVICE_LOCATION;
10114 static const int32_t DEVICE_ID;
10115 static const int32_t DEVICE_GENERATION;
10116 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010117 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010118 static const int32_t EVENTHUB_ID;
10119
10120 std::shared_ptr<FakeEventHub> mFakeEventHub;
10121 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010122 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010123 std::unique_ptr<InstrumentedInputReader> mReader;
10124 std::shared_ptr<InputDevice> mDevice;
10125
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010126 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010127 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070010128 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010129 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010130 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010131 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010132 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
10133 }
10134
10135 void SetUp() override { SetUp(DEVICE_CLASSES); }
10136
10137 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070010138 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010139 mFakePolicy.clear();
10140 }
10141
Chris Yee2b1e5c2021-03-10 22:45:12 -080010142 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
10143 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010144 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010145 InputDeviceIdentifier identifier;
10146 identifier.name = name;
10147 identifier.location = location;
10148 std::shared_ptr<InputDevice> device =
10149 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
10150 identifier);
10151 mReader->pushNextDevice(device);
10152 mFakeEventHub->addDevice(eventHubId, name, classes);
10153 mReader->loopOnce();
10154 return device;
10155 }
10156
10157 template <class T, typename... Args>
10158 T& addControllerAndConfigure(Args... args) {
10159 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10160
10161 return controller;
10162 }
10163};
10164
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010165const char* PeripheralControllerTest::DEVICE_NAME = "device";
10166const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10167const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10168const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10169const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010170const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10171 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010172const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010173
10174// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010175class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010176protected:
10177 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010178 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010179 }
10180};
10181
10182TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010183 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010184
Harry Cuttsa5b71292022-11-28 12:56:17 +000010185 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10186 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10187 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010188}
10189
10190TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010191 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010192
Harry Cuttsa5b71292022-11-28 12:56:17 +000010193 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10194 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10195 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010196}
10197
10198// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010199class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010200protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010201 void SetUp() override {
10202 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10203 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010204};
10205
Chris Ye85758332021-05-16 23:05:17 -070010206TEST_F(LightControllerTest, MonoLight) {
10207 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010208 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010209 .maxBrightness = 255,
10210 .flags = InputLightClass::BRIGHTNESS,
10211 .path = ""};
10212 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010213
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010214 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010215 InputDeviceInfo info;
10216 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010217 std::vector<InputDeviceLightInfo> lights = info.getLights();
10218 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010219 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10220 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10221
10222 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10223 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10224}
10225
DingYong99f2c3c2023-12-20 15:46:06 +080010226TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10227 RawLightInfo infoMono = {.id = 1,
10228 .name = "mono_keyboard_mute",
10229 .maxBrightness = 255,
10230 .flags = InputLightClass::BRIGHTNESS |
10231 InputLightClass::KEYBOARD_MIC_MUTE,
10232 .path = ""};
10233 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10234
10235 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10236 std::list<NotifyArgs> unused =
10237 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10238 /*changes=*/{});
10239
10240 InputDeviceInfo info;
10241 controller.populateDeviceInfo(&info);
10242 std::vector<InputDeviceLightInfo> lights = info.getLights();
10243 ASSERT_EQ(1U, lights.size());
10244 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10245 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10246}
10247
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010248TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10249 RawLightInfo infoMono = {.id = 1,
10250 .name = "mono_keyboard_backlight",
10251 .maxBrightness = 255,
10252 .flags = InputLightClass::BRIGHTNESS |
10253 InputLightClass::KEYBOARD_BACKLIGHT,
10254 .path = ""};
10255 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10256
10257 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10258 InputDeviceInfo info;
10259 controller.populateDeviceInfo(&info);
10260 std::vector<InputDeviceLightInfo> lights = info.getLights();
10261 ASSERT_EQ(1U, lights.size());
10262 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10263 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010264
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010265 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10266 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010267}
10268
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010269TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10270 RawLightInfo infoMono = {.id = 1,
10271 .name = "mono_light",
10272 .maxBrightness = 255,
10273 .flags = InputLightClass::BRIGHTNESS,
10274 .path = ""};
10275 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10276 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10277 "0,100,200");
10278
10279 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10280 std::list<NotifyArgs> unused =
10281 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10282 /*changes=*/{});
10283
10284 InputDeviceInfo info;
10285 controller.populateDeviceInfo(&info);
10286 std::vector<InputDeviceLightInfo> lights = info.getLights();
10287 ASSERT_EQ(1U, lights.size());
10288 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10289}
10290
10291TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10292 RawLightInfo infoMono = {.id = 1,
10293 .name = "mono_keyboard_backlight",
10294 .maxBrightness = 255,
10295 .flags = InputLightClass::BRIGHTNESS |
10296 InputLightClass::KEYBOARD_BACKLIGHT,
10297 .path = ""};
10298 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10299
10300 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10301 std::list<NotifyArgs> unused =
10302 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10303 /*changes=*/{});
10304
10305 InputDeviceInfo info;
10306 controller.populateDeviceInfo(&info);
10307 std::vector<InputDeviceLightInfo> lights = info.getLights();
10308 ASSERT_EQ(1U, lights.size());
10309 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10310}
10311
10312TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10313 RawLightInfo infoMono = {.id = 1,
10314 .name = "mono_keyboard_backlight",
10315 .maxBrightness = 255,
10316 .flags = InputLightClass::BRIGHTNESS |
10317 InputLightClass::KEYBOARD_BACKLIGHT,
10318 .path = ""};
10319 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10320 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10321 "0,100,200");
10322
10323 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10324 std::list<NotifyArgs> unused =
10325 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10326 /*changes=*/{});
10327
10328 InputDeviceInfo info;
10329 controller.populateDeviceInfo(&info);
10330 std::vector<InputDeviceLightInfo> lights = info.getLights();
10331 ASSERT_EQ(1U, lights.size());
10332 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10333 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10334 ASSERT_EQ(BrightnessLevel(0), *it);
10335 std::advance(it, 1);
10336 ASSERT_EQ(BrightnessLevel(100), *it);
10337 std::advance(it, 1);
10338 ASSERT_EQ(BrightnessLevel(200), *it);
10339}
10340
10341TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10342 RawLightInfo infoMono = {.id = 1,
10343 .name = "mono_keyboard_backlight",
10344 .maxBrightness = 255,
10345 .flags = InputLightClass::BRIGHTNESS |
10346 InputLightClass::KEYBOARD_BACKLIGHT,
10347 .path = ""};
10348 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10349 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10350 "0,100,200,300,400,500");
10351
10352 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10353 std::list<NotifyArgs> unused =
10354 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10355 /*changes=*/{});
10356
10357 InputDeviceInfo info;
10358 controller.populateDeviceInfo(&info);
10359 std::vector<InputDeviceLightInfo> lights = info.getLights();
10360 ASSERT_EQ(1U, lights.size());
10361 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10362}
10363
Chris Yee2b1e5c2021-03-10 22:45:12 -080010364TEST_F(LightControllerTest, RGBLight) {
10365 RawLightInfo infoRed = {.id = 1,
10366 .name = "red",
10367 .maxBrightness = 255,
10368 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10369 .path = ""};
10370 RawLightInfo infoGreen = {.id = 2,
10371 .name = "green",
10372 .maxBrightness = 255,
10373 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10374 .path = ""};
10375 RawLightInfo infoBlue = {.id = 3,
10376 .name = "blue",
10377 .maxBrightness = 255,
10378 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10379 .path = ""};
10380 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10381 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10382 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10383
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010384 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010385 InputDeviceInfo info;
10386 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010387 std::vector<InputDeviceLightInfo> lights = info.getLights();
10388 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010389 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10390 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10391 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10392
10393 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10394 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10395}
10396
10397TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10398 RawLightInfo infoRed = {.id = 1,
10399 .name = "red_keyboard_backlight",
10400 .maxBrightness = 255,
10401 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10402 InputLightClass::KEYBOARD_BACKLIGHT,
10403 .path = ""};
10404 RawLightInfo infoGreen = {.id = 2,
10405 .name = "green_keyboard_backlight",
10406 .maxBrightness = 255,
10407 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10408 InputLightClass::KEYBOARD_BACKLIGHT,
10409 .path = ""};
10410 RawLightInfo infoBlue = {.id = 3,
10411 .name = "blue_keyboard_backlight",
10412 .maxBrightness = 255,
10413 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10414 InputLightClass::KEYBOARD_BACKLIGHT,
10415 .path = ""};
10416 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10417 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10418 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10419
10420 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10421 InputDeviceInfo info;
10422 controller.populateDeviceInfo(&info);
10423 std::vector<InputDeviceLightInfo> lights = info.getLights();
10424 ASSERT_EQ(1U, lights.size());
10425 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10426 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10427 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10428
10429 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10430 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10431}
10432
10433TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10434 RawLightInfo infoRed = {.id = 1,
10435 .name = "red",
10436 .maxBrightness = 255,
10437 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10438 .path = ""};
10439 RawLightInfo infoGreen = {.id = 2,
10440 .name = "green",
10441 .maxBrightness = 255,
10442 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10443 .path = ""};
10444 RawLightInfo infoBlue = {.id = 3,
10445 .name = "blue",
10446 .maxBrightness = 255,
10447 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10448 .path = ""};
10449 RawLightInfo infoGlobal = {.id = 3,
10450 .name = "global_keyboard_backlight",
10451 .maxBrightness = 255,
10452 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10453 InputLightClass::KEYBOARD_BACKLIGHT,
10454 .path = ""};
10455 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10456 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10457 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10458 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10459
10460 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10461 InputDeviceInfo info;
10462 controller.populateDeviceInfo(&info);
10463 std::vector<InputDeviceLightInfo> lights = info.getLights();
10464 ASSERT_EQ(1U, lights.size());
10465 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10466 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10467 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010468
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010469 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10470 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010471}
10472
10473TEST_F(LightControllerTest, MultiColorRGBLight) {
10474 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010475 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010476 .maxBrightness = 255,
10477 .flags = InputLightClass::BRIGHTNESS |
10478 InputLightClass::MULTI_INTENSITY |
10479 InputLightClass::MULTI_INDEX,
10480 .path = ""};
10481
10482 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10483
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010484 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010485 InputDeviceInfo info;
10486 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010487 std::vector<InputDeviceLightInfo> lights = info.getLights();
10488 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010489 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10490 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10491 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10492
10493 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10494 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10495}
10496
10497TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10498 RawLightInfo infoColor = {.id = 1,
10499 .name = "multi_color_keyboard_backlight",
10500 .maxBrightness = 255,
10501 .flags = InputLightClass::BRIGHTNESS |
10502 InputLightClass::MULTI_INTENSITY |
10503 InputLightClass::MULTI_INDEX |
10504 InputLightClass::KEYBOARD_BACKLIGHT,
10505 .path = ""};
10506
10507 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10508
10509 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10510 InputDeviceInfo info;
10511 controller.populateDeviceInfo(&info);
10512 std::vector<InputDeviceLightInfo> lights = info.getLights();
10513 ASSERT_EQ(1U, lights.size());
10514 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10515 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10516 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010517
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010518 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10519 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010520}
10521
10522TEST_F(LightControllerTest, PlayerIdLight) {
10523 RawLightInfo info1 = {.id = 1,
10524 .name = "player1",
10525 .maxBrightness = 255,
10526 .flags = InputLightClass::BRIGHTNESS,
10527 .path = ""};
10528 RawLightInfo info2 = {.id = 2,
10529 .name = "player2",
10530 .maxBrightness = 255,
10531 .flags = InputLightClass::BRIGHTNESS,
10532 .path = ""};
10533 RawLightInfo info3 = {.id = 3,
10534 .name = "player3",
10535 .maxBrightness = 255,
10536 .flags = InputLightClass::BRIGHTNESS,
10537 .path = ""};
10538 RawLightInfo info4 = {.id = 4,
10539 .name = "player4",
10540 .maxBrightness = 255,
10541 .flags = InputLightClass::BRIGHTNESS,
10542 .path = ""};
10543 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10544 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10545 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10546 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10547
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010548 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010549 InputDeviceInfo info;
10550 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010551 std::vector<InputDeviceLightInfo> lights = info.getLights();
10552 ASSERT_EQ(1U, lights.size());
10553 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010554 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10555 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010556
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010557 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10558 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10559 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010560}
10561
Michael Wrightd02c5b62014-02-10 15:10:22 -080010562} // namespace android