blob: ee3b2a24d7efab3e659c87f4079f10c4cdbab444 [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>
Prabir Pradhan2770d242019-09-02 18:07:11 -070027#include <KeyboardInputMapper.h>
28#include <MultiTouchInputMapper.h>
Prabir Pradhan018faea2024-05-08 21:52:54 +000029#include <NotifyArgsBuilders.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070031#include <SingleTouchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000032#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070033#include <TestInputListener.h>
34#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080035#include <UinputDevice.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070036#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090037#include <com_android_input_flags.h>
Henry Barnora7ed6372024-10-21 14:33:43 -070038#include <flag_macros.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000039#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080040#include <gtest/gtest.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070043#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000044#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000045#include "FakeInputReaderPolicy.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000046#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000047#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000048#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000049#include "input/DisplayViewport.h"
50#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010051
Michael Wrightd02c5b62014-02-10 15:10:22 -080052namespace android {
53
Dominik Laskowski2f01d772022-03-23 16:01:29 -070054using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000055using testing::AllOf;
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +000056using testing::VariantWith;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070057using std::chrono_literals::operator""ms;
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -080058using std::chrono_literals::operator""s;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070059
Michael Wrightd02c5b62014-02-10 15:10:22 -080060// Arbitrary display properties.
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -070061static constexpr ui::LogicalDisplayId DISPLAY_ID = ui::LogicalDisplayId::DEFAULT;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000062static const std::string DISPLAY_UNIQUE_ID = "local:1";
Linnan Li13bf76a2024-05-05 19:18:02 +080063static constexpr ui::LogicalDisplayId SECONDARY_DISPLAY_ID =
64 ui::LogicalDisplayId{DISPLAY_ID.val() + 1};
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t DISPLAY_WIDTH = 480;
66static constexpr int32_t DISPLAY_HEIGHT = 800;
Linnan Li13bf76a2024-05-05 19:18:02 +080067static constexpr ui::LogicalDisplayId VIRTUAL_DISPLAY_ID = ui::LogicalDisplayId{1};
arthurhungcc7f9802020-04-30 17:55:40 +080068static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
69static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070070static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070071static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080072
arthurhungcc7f9802020-04-30 17:55:40 +080073static constexpr int32_t FIRST_SLOT = 0;
74static constexpr int32_t SECOND_SLOT = 1;
75static constexpr int32_t THIRD_SLOT = 2;
76static constexpr int32_t INVALID_TRACKING_ID = -1;
77static constexpr int32_t FIRST_TRACKING_ID = 0;
78static constexpr int32_t SECOND_TRACKING_ID = 1;
79static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080080static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
81static constexpr int32_t LIGHT_COLOR = 0x7F448866;
82static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080083
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080084static constexpr int32_t ACTION_POINTER_0_DOWN =
85 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
86static constexpr int32_t ACTION_POINTER_0_UP =
87 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_1_DOWN =
89 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_UP =
91 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000093static constexpr uint32_t STYLUS_FUSION_SOURCE =
94 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
95
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000096// Minimum timestamp separation between subsequent input events from a Bluetooth device.
97static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098
Byoungho Jungda10dd32023-10-06 17:03:45 +090099namespace input_flags = com::android::input::flags;
100
Michael Wrightd02c5b62014-02-10 15:10:22 -0800101template<typename T>
102static inline T min(T a, T b) {
103 return a < b ? a : b;
104}
105
106static inline float avg(float x, float y) {
107 return (x + y) / 2;
108}
109
Chris Ye3fdbfef2021-01-06 18:45:18 -0800110// Mapping for light color name and the light color
111const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
112 {"green", LightColor::GREEN},
113 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800114
Michael Wrighta9cf4192022-12-01 23:46:39 +0000115static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700116 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000117 case ui::ROTATION_90:
118 return ui::ROTATION_270;
119 case ui::ROTATION_270:
120 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700121 default:
122 return orientation;
123 }
124}
125
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800126static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
127 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000128 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800129
130 const InputDeviceInfo::MotionRange* motionRange =
131 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
132 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
133}
134
135static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
136 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000137 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800138
139 const InputDeviceInfo::MotionRange* motionRange =
140 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
141 ASSERT_EQ(nullptr, motionRange);
142}
143
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700144[[maybe_unused]] static void dumpReader(InputReader& reader) {
145 std::string dump;
146 reader.dump(dump);
147 std::istringstream iss(dump);
148 for (std::string line; std::getline(iss, line);) {
149 ALOGE("%s", line.c_str());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -0800150 std::this_thread::sleep_for(1ms);
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700151 }
152}
153
Michael Wrightd02c5b62014-02-10 15:10:22 -0800154// --- FakeInputMapper ---
155
156class FakeInputMapper : public InputMapper {
157 uint32_t mSources;
158 int32_t mKeyboardType;
159 int32_t mMetaState;
160 KeyedVector<int32_t, int32_t> mKeyCodeStates;
161 KeyedVector<int32_t, int32_t> mScanCodeStates;
162 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100163 // fake mapping which would normally come from keyCharacterMap
164 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800165 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700166 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800167
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700168 std::mutex mLock;
169 std::condition_variable mStateChangedCondition;
170 bool mConfigureWasCalled GUARDED_BY(mLock);
171 bool mResetWasCalled GUARDED_BY(mLock);
172 bool mProcessWasCalled GUARDED_BY(mLock);
173 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800174
Arthur Hungc23540e2018-11-29 20:42:11 +0800175 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800176public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000177 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
178 uint32_t sources)
179 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800180 mSources(sources),
181 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800182 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800183 mConfigureWasCalled(false),
184 mResetWasCalled(false),
185 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186
Chris Yea52ade12020-08-27 16:49:20 -0700187 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800188
189 void setKeyboardType(int32_t keyboardType) {
190 mKeyboardType = keyboardType;
191 }
192
193 void setMetaState(int32_t metaState) {
194 mMetaState = metaState;
195 }
196
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700197 // Sets the return value for the `process` call.
198 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
199 mProcessResult.clear();
200 for (auto notifyArg : notifyArgs) {
201 mProcessResult.push_back(notifyArg);
202 }
203 }
204
Michael Wrightd02c5b62014-02-10 15:10:22 -0800205 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700206 std::unique_lock<std::mutex> lock(mLock);
207 base::ScopedLockAssertion assumeLocked(mLock);
208 const bool configureCalled =
209 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
210 return mConfigureWasCalled;
211 });
212 if (!configureCalled) {
213 FAIL() << "Expected configure() to have been called.";
214 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800215 mConfigureWasCalled = false;
216 }
217
218 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700219 std::unique_lock<std::mutex> lock(mLock);
220 base::ScopedLockAssertion assumeLocked(mLock);
221 const bool resetCalled =
222 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
223 return mResetWasCalled;
224 });
225 if (!resetCalled) {
226 FAIL() << "Expected reset() to have been called.";
227 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800228 mResetWasCalled = false;
229 }
230
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000231 void assertResetWasNotCalled() {
232 std::scoped_lock lock(mLock);
233 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
234 }
235
Yi Kong9b14ac62018-07-17 13:48:38 -0700236 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700237 std::unique_lock<std::mutex> lock(mLock);
238 base::ScopedLockAssertion assumeLocked(mLock);
239 const bool processCalled =
240 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
241 return mProcessWasCalled;
242 });
243 if (!processCalled) {
244 FAIL() << "Expected process() to have been called.";
245 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800246 if (outLastEvent) {
247 *outLastEvent = mLastEvent;
248 }
249 mProcessWasCalled = false;
250 }
251
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000252 void assertProcessWasNotCalled() {
253 std::scoped_lock lock(mLock);
254 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
255 }
256
Michael Wrightd02c5b62014-02-10 15:10:22 -0800257 void setKeyCodeState(int32_t keyCode, int32_t state) {
258 mKeyCodeStates.replaceValueFor(keyCode, state);
259 }
260
261 void setScanCodeState(int32_t scanCode, int32_t state) {
262 mScanCodeStates.replaceValueFor(scanCode, state);
263 }
264
265 void setSwitchState(int32_t switchCode, int32_t state) {
266 mSwitchStates.replaceValueFor(switchCode, state);
267 }
268
269 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800270 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800271 }
272
Philip Junker4af3b3d2021-12-14 10:36:55 +0100273 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
274 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
275 }
276
Michael Wrightd02c5b62014-02-10 15:10:22 -0800277private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100278 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800279
Harry Cuttsd02ea102023-03-17 18:21:30 +0000280 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281 InputMapper::populateDeviceInfo(deviceInfo);
282
283 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000284 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 }
286 }
287
Arpit Singhed6c3de2023-04-05 19:24:37 +0000288 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000289 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700290 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800291 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800292
293 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800294 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000295 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000296 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800297 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700298
299 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700300 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800301 }
302
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700303 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700304 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700306 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700307 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800308 }
309
Harry Cuttsa32a1192024-06-04 15:10:31 +0000310 std::list<NotifyArgs> process(const RawEvent& rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700311 std::scoped_lock<std::mutex> lock(mLock);
Harry Cuttsa32a1192024-06-04 15:10:31 +0000312 mLastEvent = rawEvent;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800313 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700314 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700315 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 }
317
Chris Yea52ade12020-08-27 16:49:20 -0700318 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800319 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
320 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
321 }
322
Philip Junker4af3b3d2021-12-14 10:36:55 +0100323 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
324 auto it = mKeyCodeMapping.find(locationKeyCode);
325 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
326 }
327
Chris Yea52ade12020-08-27 16:49:20 -0700328 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800329 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
330 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
331 }
332
Chris Yea52ade12020-08-27 16:49:20 -0700333 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800334 ssize_t index = mSwitchStates.indexOfKey(switchCode);
335 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
336 }
337
Chris Yea52ade12020-08-27 16:49:20 -0700338 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700339 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700340 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700341 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800342 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
343 if (keyCodes[i] == mSupportedKeyCodes[j]) {
344 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800345 }
346 }
347 }
Chris Yea52ade12020-08-27 16:49:20 -0700348 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 return result;
350 }
351
352 virtual int32_t getMetaState() {
353 return mMetaState;
354 }
355
356 virtual void fadePointer() {
357 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800358
Linnan Li13bf76a2024-05-05 19:18:02 +0800359 virtual std::optional<ui::LogicalDisplayId> getAssociatedDisplay() {
Arthur Hungc23540e2018-11-29 20:42:11 +0800360 if (mViewport) {
361 return std::make_optional(mViewport->displayId);
362 }
363 return std::nullopt;
364 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800365};
366
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700367// --- InputReaderPolicyTest ---
368class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700369protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700370 sp<FakeInputReaderPolicy> mFakePolicy;
371
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700372 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700373 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374};
375
376/**
377 * Check that empty set of viewports is an acceptable configuration.
378 * Also try to get internal viewport two different ways - by type and by uniqueId.
379 *
380 * There will be confusion if two viewports with empty uniqueId and identical type are present.
381 * Such configuration is not currently allowed.
382 */
383TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700384 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700385
386 // We didn't add any viewports yet, so there shouldn't be any.
387 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100388 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389 ASSERT_FALSE(internalViewport);
390
391 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000392 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000393 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700394
395 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700396 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700397 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100398 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700399
400 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100401 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700402 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700403 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700404
405 mFakePolicy->clearViewports();
406 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700407 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100409 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700410 ASSERT_FALSE(internalViewport);
411}
412
413TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
414 const std::string internalUniqueId = "local:0";
415 const std::string externalUniqueId = "local:1";
416 const std::string virtualUniqueId1 = "virtual:2";
417 const std::string virtualUniqueId2 = "virtual:3";
Linnan Li13bf76a2024-05-05 19:18:02 +0800418 constexpr ui::LogicalDisplayId virtualDisplayId1 = ui::LogicalDisplayId{2};
419 constexpr ui::LogicalDisplayId virtualDisplayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700420
421 // Add an internal viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000422 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000423 /*isActive=*/true, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000424 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700425 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000426 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000427 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700429 // Add an virtual viewport
430 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000431 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000432 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 // Add another virtual viewport
434 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000435 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000436 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700437
438 // Check matching by type for internal
439 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100440 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441 ASSERT_TRUE(internalViewport);
442 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
443
444 // Check matching by type for external
445 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100446 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700447 ASSERT_TRUE(externalViewport);
448 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
449
450 // Check matching by uniqueId for virtual viewport #1
451 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700452 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700453 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100454 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700455 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
456 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
457
458 // Check matching by uniqueId for virtual viewport #2
459 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700460 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700461 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100462 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700463 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
464 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
465}
466
467
468/**
469 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
470 * that lookup works by checking display id.
471 * Check that 2 viewports of each kind is possible, for all existing viewport types.
472 */
473TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
474 const std::string uniqueId1 = "uniqueId1";
475 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800476 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{2};
477 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{3};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700478
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100479 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
480 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700481 for (const ViewportType& type : types) {
482 mFakePolicy->clearViewports();
483 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000484 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000485 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700486 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000487 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000488 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700489
490 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700491 std::optional<DisplayViewport> viewport1 =
492 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700493 ASSERT_TRUE(viewport1);
494 ASSERT_EQ(displayId1, viewport1->displayId);
495 ASSERT_EQ(type, viewport1->type);
496
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700497 std::optional<DisplayViewport> viewport2 =
498 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700499 ASSERT_TRUE(viewport2);
500 ASSERT_EQ(displayId2, viewport2->displayId);
501 ASSERT_EQ(type, viewport2->type);
502
503 // When there are multiple viewports of the same kind, and uniqueId is not specified
504 // in the call to getDisplayViewport, then that situation is not supported.
505 // The viewports can be stored in any order, so we cannot rely on the order, since that
506 // is just implementation detail.
507 // However, we can check that it still returns *a* viewport, we just cannot assert
508 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700509 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700510 ASSERT_TRUE(someViewport);
511 }
512}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800513
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700514/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000515 * When we have multiple internal displays make sure we always return the default display when
516 * querying by type.
517 */
518TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
519 const std::string uniqueId1 = "uniqueId1";
520 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800521 constexpr ui::LogicalDisplayId nonDefaultDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700522 ASSERT_NE(nonDefaultDisplayId, ui::LogicalDisplayId::DEFAULT)
523 << "Test display ID should not be ui::LogicalDisplayId::DEFAULT ";
Michael Wrightdde67b82020-10-27 16:09:22 +0000524
525 // Add the default display first and ensure it gets returned.
526 mFakePolicy->clearViewports();
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700527 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000528 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000529 ViewportType::INTERNAL);
530 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000531 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000532 ViewportType::INTERNAL);
533
534 std::optional<DisplayViewport> viewport =
535 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
536 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700537 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000538 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
539
540 // Add the default display second to make sure order doesn't matter.
541 mFakePolicy->clearViewports();
542 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000543 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000544 ViewportType::INTERNAL);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700545 mFakePolicy->addDisplayViewport(ui::LogicalDisplayId::DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000546 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000547 ViewportType::INTERNAL);
548
549 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
550 ASSERT_TRUE(viewport);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -0700551 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, viewport->displayId);
Michael Wrightdde67b82020-10-27 16:09:22 +0000552 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
553}
554
555/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700556 * Check getDisplayViewportByPort
557 */
558TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100559 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700560 const std::string uniqueId1 = "uniqueId1";
561 const std::string uniqueId2 = "uniqueId2";
Linnan Li13bf76a2024-05-05 19:18:02 +0800562 constexpr ui::LogicalDisplayId displayId1 = ui::LogicalDisplayId{1};
563 constexpr ui::LogicalDisplayId displayId2 = ui::LogicalDisplayId{2};
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700564 const uint8_t hdmi1 = 0;
565 const uint8_t hdmi2 = 1;
566 const uint8_t hdmi3 = 2;
567
568 mFakePolicy->clearViewports();
569 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000570 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000571 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700572 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000573 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000574 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700575
576 // Check that correct display viewport was returned by comparing the display ports.
577 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
578 ASSERT_TRUE(hdmi1Viewport);
579 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
580 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
581
582 // Check that we can still get the same viewport using the uniqueId
583 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
584 ASSERT_TRUE(hdmi1Viewport);
585 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
586 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
587 ASSERT_EQ(type, hdmi1Viewport->type);
588
589 // Check that we cannot find a port with "HDMI2", because we never added one
590 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
591 ASSERT_FALSE(hdmi2Viewport);
592}
593
Michael Wrightd02c5b62014-02-10 15:10:22 -0800594// --- InputReaderTest ---
595
596class InputReaderTest : public testing::Test {
597protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700598 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800599 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700600 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000601 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800602
Chris Yea52ade12020-08-27 16:49:20 -0700603 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700604 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700605 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700606 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800607
Prabir Pradhan28efc192019-11-05 01:10:04 +0000608 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700609 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800610 }
611
Chris Yea52ade12020-08-27 16:49:20 -0700612 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700613 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800615 }
616
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700617 void addDevice(int32_t eventHubId, const std::string& name,
618 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800619 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800620
621 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800622 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800623 }
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000624 mReader->loopOnce();
625 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700626 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000627 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700628 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800629 }
630
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800631 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700632 mFakePolicy->addDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000633 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700634 }
635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800636 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000638 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700639 }
640
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800641 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700642 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700643 ftl::Flags<InputDeviceClass> classes,
644 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800645 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800646 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000647 FakeInputMapper& mapper =
648 device->addMapper<FakeInputMapper>(eventHubId,
649 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800650 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800651 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800652 return mapper;
653 }
654};
655
Chris Ye98d3f532020-10-01 21:48:59 -0700656TEST_F(InputReaderTest, PolicyGetInputDevices) {
657 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700658 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700659 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800660
661 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700662 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800663 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800664 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100665 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800666 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
667 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000668 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800669}
670
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000671TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
672 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
673 mFakeEventHub->setSysfsRootPath(1, "xyz");
674
675 // Should also have received a notification describing the new input device.
676 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
677 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
678 ASSERT_EQ(0U, inputDevice.getLights().size());
679
680 RawLightInfo infoMonolight = {.id = 123,
681 .name = "mono_keyboard_backlight",
682 .maxBrightness = 255,
683 .flags = InputLightClass::BRIGHTNESS,
684 .path = ""};
685 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
686 mReader->sysfsNodeChanged("xyz");
687 mReader->loopOnce();
688
689 // Should also have received a notification describing the new recreated input device.
690 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
691 inputDevice = mFakePolicy->getInputDevices()[0];
692 ASSERT_EQ(1U, inputDevice.getLights().size());
693}
694
Chris Yee7310032020-09-22 15:36:28 -0700695TEST_F(InputReaderTest, GetMergedInputDevices) {
696 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
697 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
698 // Add two subdevices to device
699 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
700 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000701 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
702 AINPUT_SOURCE_KEYBOARD);
703 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
704 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700705
706 // Push same device instance for next device to be added, so they'll have same identifier.
707 mReader->pushNextDevice(device);
708 mReader->pushNextDevice(device);
709 ASSERT_NO_FATAL_FAILURE(
710 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
711 ASSERT_NO_FATAL_FAILURE(
712 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
713
714 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000715 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700716}
717
Chris Yee14523a2020-12-19 13:46:00 -0800718TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
719 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
720 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
721 // Add two subdevices to device
722 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
723 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000724 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
725 AINPUT_SOURCE_KEYBOARD);
726 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
727 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800728
729 // Push same device instance for next device to be added, so they'll have same identifier.
730 mReader->pushNextDevice(device);
731 mReader->pushNextDevice(device);
732 // Sensor device is initially disabled
733 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
734 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
735 nullptr));
736 // Device is disabled because the only sub device is a sensor device and disabled initially.
737 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
738 ASSERT_FALSE(device->isEnabled());
739 ASSERT_NO_FATAL_FAILURE(
740 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
741 // The merged device is enabled if any sub device is enabled
742 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
743 ASSERT_TRUE(device->isEnabled());
744}
745
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700746TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800747 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700748 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800749 constexpr int32_t eventHubId = 1;
750 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700751 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000752 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
753 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800754 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800755 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700756
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700757 NotifyDeviceResetArgs resetArgs;
758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700759 ASSERT_EQ(deviceId, resetArgs.deviceId);
760
761 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800762 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000763 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700764
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700766 ASSERT_EQ(deviceId, resetArgs.deviceId);
767 ASSERT_EQ(device->isEnabled(), false);
768
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800769 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000770 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700771 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700772 ASSERT_EQ(device->isEnabled(), false);
773
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800774 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000775 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700777 ASSERT_EQ(deviceId, resetArgs.deviceId);
778 ASSERT_EQ(device->isEnabled(), true);
779}
780
Michael Wrightd02c5b62014-02-10 15:10:22 -0800781TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800782 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700783 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800784 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800785 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800786 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800787 AINPUT_SOURCE_KEYBOARD, nullptr);
788 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789
790 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
791 AINPUT_SOURCE_ANY, AKEYCODE_A))
792 << "Should return unknown when the device id is >= 0 but unknown.";
793
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800794 ASSERT_EQ(AKEY_STATE_UNKNOWN,
795 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
796 << "Should return unknown when the device id is valid but the sources are not "
797 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800798
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800799 ASSERT_EQ(AKEY_STATE_DOWN,
800 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
801 AKEYCODE_A))
802 << "Should return value provided by mapper when device id is valid and the device "
803 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800804
805 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
806 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
807 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
808
809 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
810 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
811 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
812}
813
Philip Junker4af3b3d2021-12-14 10:36:55 +0100814TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
815 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
816 constexpr int32_t eventHubId = 1;
817 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
818 InputDeviceClass::KEYBOARD,
819 AINPUT_SOURCE_KEYBOARD, nullptr);
820 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
821
822 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
823 << "Should return unknown when the device with the specified id is not found.";
824
825 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
826 << "Should return correct mapping when device id is valid and mapping exists.";
827
828 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
829 << "Should return the location key code when device id is valid and there's no "
830 "mapping.";
831}
832
833TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
834 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
835 constexpr int32_t eventHubId = 1;
836 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
837 InputDeviceClass::JOYSTICK,
838 AINPUT_SOURCE_GAMEPAD, nullptr);
839 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
840
841 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
842 << "Should return unknown when the device id is valid but there is no keyboard mapper";
843}
844
Michael Wrightd02c5b62014-02-10 15:10:22 -0800845TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800846 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700847 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800848 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800849 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800850 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800851 AINPUT_SOURCE_KEYBOARD, nullptr);
852 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853
854 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
855 AINPUT_SOURCE_ANY, KEY_A))
856 << "Should return unknown when the device id is >= 0 but unknown.";
857
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 ASSERT_EQ(AKEY_STATE_UNKNOWN,
859 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
860 << "Should return unknown when the device id is valid but the sources are not "
861 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800862
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800863 ASSERT_EQ(AKEY_STATE_DOWN,
864 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
865 KEY_A))
866 << "Should return value provided by mapper when device id is valid and the device "
867 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800868
869 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
870 AINPUT_SOURCE_TRACKBALL, KEY_A))
871 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
872
873 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
874 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
875 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
876}
877
878TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800879 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700880 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800881 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800882 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800883 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800884 AINPUT_SOURCE_KEYBOARD, nullptr);
885 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800886
887 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
888 AINPUT_SOURCE_ANY, SW_LID))
889 << "Should return unknown when the device id is >= 0 but unknown.";
890
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800891 ASSERT_EQ(AKEY_STATE_UNKNOWN,
892 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
893 << "Should return unknown when the device id is valid but the sources are not "
894 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800895
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800896 ASSERT_EQ(AKEY_STATE_DOWN,
897 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
898 SW_LID))
899 << "Should return value provided by mapper when device id is valid and the device "
900 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800901
902 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
903 AINPUT_SOURCE_TRACKBALL, SW_LID))
904 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
905
906 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
907 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
908 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
909}
910
911TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800912 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700913 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800914 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800915 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800916 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800917 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100918
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800919 mapper.addSupportedKeyCode(AKEYCODE_A);
920 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800921
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700922 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800923 uint8_t flags[4] = { 0, 0, 0, 1 };
924
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700925 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800926 << "Should return false when device id is >= 0 but unknown.";
927 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
928
929 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700930 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800931 << "Should return false when device id is valid but the sources are not supported by "
932 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800933 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
934
935 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700936 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800937 keyCodes, flags))
938 << "Should return value provided by mapper when device id is valid and the device "
939 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800940 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
941
942 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700943 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
944 << "Should return false when the device id is < 0 but the sources are not supported by "
945 "any device.";
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_TRUE(
950 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
951 << "Should return value provided by mapper when device id is < 0 and one of the "
952 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800953 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
954}
955
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000956TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800957 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700958 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000959 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800960 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000961 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800962 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800963 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800964 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800965
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000966 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000967 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800968 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
969
970 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800971 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000972 ASSERT_EQ(when, event.when);
973 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800974 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800975 ASSERT_EQ(EV_KEY, event.type);
976 ASSERT_EQ(KEY_A, event.code);
977 ASSERT_EQ(1, event.value);
978}
979
Garfield Tan1c7bc862020-01-28 13:24:04 -0800980TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800981 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700982 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800983 constexpr int32_t eventHubId = 1;
984 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -0800985 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000986 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
987 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800988 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800989 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -0800990
991 NotifyDeviceResetArgs resetArgs;
992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800993 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -0800994
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800995 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000996 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -0800998 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -0800999 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001000
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001001 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001002 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001003 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001004 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001005 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001007 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001008 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001009 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001010 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001011 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001012}
1013
Garfield Tan1c7bc862020-01-28 13:24:04 -08001014TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1015 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001016 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001017 constexpr int32_t eventHubId = 1;
1018 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1019 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001020 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1021 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001022 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001023 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1024
1025 NotifyDeviceResetArgs resetArgs;
1026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1027 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1028}
1029
Arthur Hungc23540e2018-11-29 20:42:11 +08001030TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001031 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001032 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001033 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001034 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001035 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1036 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001037 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1038 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001039 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001040
1041 const uint8_t hdmi1 = 1;
1042
1043 // Associated touch screen with second display.
1044 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1045
1046 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001047 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001048 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001049 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001050 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001051 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001052 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001053 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001054 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001055
1056 // Add the device, and make sure all of the callbacks are triggered.
1057 // The device is added after the input port associations are processed since
1058 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001059 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan28efc192019-11-05 01:10:04 +00001060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001061 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001062
Arthur Hung2c9a3342019-07-23 14:18:59 +08001063 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001064 ASSERT_EQ(deviceId, device->getId());
1065 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1066 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001067
1068 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001069 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001070 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001071 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001072}
1073
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001074TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1075 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001076 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001077 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1078 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1079 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001080 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1081 AINPUT_SOURCE_KEYBOARD);
1082 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1083 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001084 mReader->pushNextDevice(device);
1085 mReader->pushNextDevice(device);
1086 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1087 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1088
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001089 NotifyDeviceResetArgs resetArgs;
1090 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1091 ASSERT_EQ(deviceId, resetArgs.deviceId);
1092 ASSERT_TRUE(device->isEnabled());
1093 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1094 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1095
1096 disableDevice(deviceId);
1097 mReader->loopOnce();
1098
1099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1100 ASSERT_EQ(deviceId, resetArgs.deviceId);
1101 ASSERT_FALSE(device->isEnabled());
1102 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1103 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1104
1105 enableDevice(deviceId);
1106 mReader->loopOnce();
1107
1108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1109 ASSERT_EQ(deviceId, resetArgs.deviceId);
1110 ASSERT_TRUE(device->isEnabled());
1111 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1112 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1113}
1114
1115TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1116 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001117 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001118 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1119 // Add two subdevices to device
1120 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1121 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001122 device->addMapper<FakeInputMapper>(eventHubIds[0],
1123 mFakePolicy->getReaderConfiguration(),
1124 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001125 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001126 device->addMapper<FakeInputMapper>(eventHubIds[1],
1127 mFakePolicy->getReaderConfiguration(),
1128 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001129 mReader->pushNextDevice(device);
1130 mReader->pushNextDevice(device);
1131 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1132 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1133
1134 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1135 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1136
1137 ASSERT_EQ(AKEY_STATE_DOWN,
1138 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1139 ASSERT_EQ(AKEY_STATE_DOWN,
1140 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1141 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1142 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1143}
1144
Prabir Pradhan7e186182020-11-10 13:56:45 -08001145TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1146 NotifyPointerCaptureChangedArgs args;
1147
Hiroki Sato25040232024-02-22 17:21:22 +09001148 auto request = mFakePolicy->setPointerCapture(/*window=*/sp<BBinder>::make());
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001149 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001150 mReader->loopOnce();
1151 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001152 ASSERT_TRUE(args.request.isEnable()) << "Pointer Capture should be enabled.";
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001153 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001154
Hiroki Sato25040232024-02-22 17:21:22 +09001155 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001156 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001157 mReader->loopOnce();
1158 mFakeListener->assertNotifyCaptureWasCalled(&args);
Hiroki Sato25040232024-02-22 17:21:22 +09001159 ASSERT_FALSE(args.request.isEnable()) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001160
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001161 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001162 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001163 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001164 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001165 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001166}
1167
Prabir Pradhan018faea2024-05-08 21:52:54 +00001168TEST_F(InputReaderTest, GetLastUsedInputDeviceId) {
1169 constexpr int32_t FIRST_DEVICE_ID = END_RESERVED_ID + 1000;
1170 constexpr int32_t SECOND_DEVICE_ID = FIRST_DEVICE_ID + 1;
1171 FakeInputMapper& firstMapper =
1172 addDeviceWithFakeInputMapper(FIRST_DEVICE_ID, FIRST_DEVICE_ID, "first",
1173 InputDeviceClass::KEYBOARD, AINPUT_SOURCE_KEYBOARD,
1174 /*configuration=*/nullptr);
1175 FakeInputMapper& secondMapper =
1176 addDeviceWithFakeInputMapper(SECOND_DEVICE_ID, SECOND_DEVICE_ID, "second",
1177 InputDeviceClass::TOUCH_MT, AINPUT_SOURCE_STYLUS,
1178 /*configuration=*/nullptr);
1179
1180 ASSERT_EQ(ReservedInputDeviceId::INVALID_INPUT_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1181
1182 // Start a new key gesture from the first device
1183 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1184 .deviceId(FIRST_DEVICE_ID)
1185 .build()});
1186 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1187 mReader->loopOnce();
1188 ASSERT_EQ(firstMapper.getDeviceId(), mReader->getLastUsedInputDeviceId());
1189
1190 // Start a new touch gesture from the second device
1191 secondMapper.setProcessResult(
1192 {MotionArgsBuilder(AMOTION_EVENT_ACTION_DOWN, AINPUT_SOURCE_STYLUS)
1193 .deviceId(SECOND_DEVICE_ID)
1194 .pointer(PointerBuilder(/*id=*/0, ToolType::FINGER))
1195 .build()});
1196 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1197 mReader->loopOnce();
1198 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1199
1200 // Releasing the key is not a new gesture, so it does not update the last used device
1201 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_UP, AINPUT_SOURCE_KEYBOARD)
1202 .deviceId(FIRST_DEVICE_ID)
1203 .build()});
1204 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1205 mReader->loopOnce();
1206 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1207
1208 // But pressing a new key does start a new gesture
1209 firstMapper.setProcessResult({KeyArgsBuilder(AKEY_EVENT_ACTION_DOWN, AINPUT_SOURCE_KEYBOARD)
1210 .deviceId(FIRST_DEVICE_ID)
1211 .build()});
1212 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, FIRST_DEVICE_ID, 0, 0, 0);
1213 mReader->loopOnce();
1214 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1215
1216 // Moving or ending a touch gesture does not update the last used device
1217 secondMapper.setProcessResult(
1218 {MotionArgsBuilder(AMOTION_EVENT_ACTION_MOVE, AINPUT_SOURCE_STYLUS)
1219 .deviceId(SECOND_DEVICE_ID)
1220 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1221 .build()});
1222 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1223 mReader->loopOnce();
1224 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1225 secondMapper.setProcessResult({MotionArgsBuilder(AMOTION_EVENT_ACTION_UP, AINPUT_SOURCE_STYLUS)
1226 .deviceId(SECOND_DEVICE_ID)
1227 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1228 .build()});
1229 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1230 mReader->loopOnce();
1231 ASSERT_EQ(FIRST_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1232
1233 // Starting a new hover gesture updates the last used device
1234 secondMapper.setProcessResult(
1235 {MotionArgsBuilder(AMOTION_EVENT_ACTION_HOVER_ENTER, AINPUT_SOURCE_STYLUS)
1236 .deviceId(SECOND_DEVICE_ID)
1237 .pointer(PointerBuilder(/*id=*/0, ToolType::STYLUS))
1238 .build()});
1239 mFakeEventHub->enqueueEvent(ARBITRARY_TIME, ARBITRARY_TIME, SECOND_DEVICE_ID, 0, 0, 0);
1240 mReader->loopOnce();
1241 ASSERT_EQ(SECOND_DEVICE_ID, mReader->getLastUsedInputDeviceId());
1242}
1243
Chris Ye87143712020-11-10 05:05:58 +00001244class FakeVibratorInputMapper : public FakeInputMapper {
1245public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001246 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1247 const InputReaderConfiguration& readerConfig, uint32_t sources)
1248 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001249
1250 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1251};
1252
1253TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1254 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001255 ftl::Flags<InputDeviceClass> deviceClass =
1256 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001257 constexpr int32_t eventHubId = 1;
1258 const char* DEVICE_LOCATION = "BLUETOOTH";
1259 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1260 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001261 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1262 mFakePolicy->getReaderConfiguration(),
1263 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001264 mReader->pushNextDevice(device);
1265
1266 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1267 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1268
1269 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1270 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1271}
1272
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001273// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001274
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001275class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001276public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001277 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001278
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001279 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001280
Andy Chenf9f1a022022-08-29 20:07:10 -04001281 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1282
Chris Yee2b1e5c2021-03-10 22:45:12 -08001283 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1284
1285 void dump(std::string& dump) override {}
1286
1287 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1288 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001289 }
1290
Chris Yee2b1e5c2021-03-10 22:45:12 -08001291 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1292 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001293 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001294
1295 bool setLightColor(int32_t lightId, int32_t color) override {
1296 getDeviceContext().setLightBrightness(lightId, color >> 24);
1297 return true;
1298 }
1299
1300 std::optional<int32_t> getLightColor(int32_t lightId) override {
1301 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1302 if (!result.has_value()) {
1303 return std::nullopt;
1304 }
1305 return result.value() << 24;
1306 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001307
1308 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1309
1310 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1311
1312private:
1313 InputDeviceContext& mDeviceContext;
1314 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1315 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001316 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001317};
1318
Chris Yee2b1e5c2021-03-10 22:45:12 -08001319TEST_F(InputReaderTest, BatteryGetCapacity) {
1320 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001321 ftl::Flags<InputDeviceClass> deviceClass =
1322 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001323 constexpr int32_t eventHubId = 1;
1324 const char* DEVICE_LOCATION = "BLUETOOTH";
1325 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001326 FakePeripheralController& controller =
1327 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001328 mReader->pushNextDevice(device);
1329
1330 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1331
Harry Cuttsa5b71292022-11-28 12:56:17 +00001332 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1333 FakeEventHub::BATTERY_CAPACITY);
1334 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001335}
1336
1337TEST_F(InputReaderTest, BatteryGetStatus) {
1338 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001339 ftl::Flags<InputDeviceClass> deviceClass =
1340 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001341 constexpr int32_t eventHubId = 1;
1342 const char* DEVICE_LOCATION = "BLUETOOTH";
1343 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001344 FakePeripheralController& controller =
1345 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001346 mReader->pushNextDevice(device);
1347
1348 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1349
Harry Cuttsa5b71292022-11-28 12:56:17 +00001350 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1351 FakeEventHub::BATTERY_STATUS);
1352 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001353}
1354
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001355TEST_F(InputReaderTest, BatteryGetDevicePath) {
1356 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1357 ftl::Flags<InputDeviceClass> deviceClass =
1358 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1359 constexpr int32_t eventHubId = 1;
1360 const char* DEVICE_LOCATION = "BLUETOOTH";
1361 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1362 device->addController<FakePeripheralController>(eventHubId);
1363 mReader->pushNextDevice(device);
1364
1365 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1366
Harry Cuttsa5b71292022-11-28 12:56:17 +00001367 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001368}
1369
Chris Ye3fdbfef2021-01-06 18:45:18 -08001370TEST_F(InputReaderTest, LightGetColor) {
1371 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001372 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001373 constexpr int32_t eventHubId = 1;
1374 const char* DEVICE_LOCATION = "BLUETOOTH";
1375 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001376 FakePeripheralController& controller =
1377 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001378 mReader->pushNextDevice(device);
1379 RawLightInfo info = {.id = 1,
1380 .name = "Mono",
1381 .maxBrightness = 255,
1382 .flags = InputLightClass::BRIGHTNESS,
1383 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001384 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1385 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001386
1387 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001388
Harry Cutts33476232023-01-30 19:57:29 +00001389 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1390 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1391 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1392 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001393}
1394
Yanye Li81a590c2024-10-22 19:25:54 +00001395TEST_F(InputReaderTest, SetPowerWakeUp) {
1396 ASSERT_NO_FATAL_FAILURE(addDevice(1, "1st", InputDeviceClass::KEYBOARD, nullptr));
1397 ASSERT_NO_FATAL_FAILURE(addDevice(2, "2nd", InputDeviceClass::KEYBOARD, nullptr));
1398 ASSERT_NO_FATAL_FAILURE(addDevice(3, "3rd", InputDeviceClass::KEYBOARD, nullptr));
1399
1400 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(1), false);
1401
1402 ASSERT_TRUE(mFakeEventHub->setKernelWakeEnabled(2, true));
1403 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(2), true);
1404
1405 ASSERT_TRUE(mFakeEventHub->setKernelWakeEnabled(3, false));
1406 ASSERT_EQ(mFakeEventHub->fakeReadKernelWakeup(3), false);
1407}
1408
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001409// --- InputReaderIntegrationTest ---
1410
1411// These tests create and interact with the InputReader only through its interface.
1412// The InputReader is started during SetUp(), which starts its processing in its own
1413// thread. The tests use linux uinput to emulate input devices.
1414// NOTE: Interacting with the physical device while these tests are running may cause
1415// the tests to fail.
1416class InputReaderIntegrationTest : public testing::Test {
1417protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001418 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001419 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001420 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001421
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001422 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1423 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1424
Chris Yea52ade12020-08-27 16:49:20 -07001425 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001426#if !defined(__ANDROID__)
1427 GTEST_SKIP();
1428#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001429 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001430
Arpit Singh440bf652023-08-09 09:23:43 +00001431 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001432 }
1433
Chris Yea52ade12020-08-27 16:49:20 -07001434 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001435#if !defined(__ANDROID__)
1436 return;
1437#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001438 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001439 mReader.reset();
1440 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001441 mFakePolicy.clear();
1442 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001443
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001444 std::optional<InputDeviceInfo> waitForDevice(const std::string& deviceName) {
1445 std::chrono::time_point start = std::chrono::steady_clock::now();
1446 while (true) {
1447 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1448 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1449 [&deviceName](const InputDeviceInfo& info) {
1450 return info.getIdentifier().name == deviceName;
1451 });
1452 if (it != inputDevices.end()) {
1453 return std::make_optional(*it);
1454 }
1455 std::this_thread::sleep_for(1ms);
1456 std::chrono::duration elapsed = std::chrono::steady_clock::now() - start;
1457 if (elapsed > 5s) {
1458 return {};
1459 }
1460 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001461 }
Arpit Singh440bf652023-08-09 09:23:43 +00001462
1463 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001464 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1465 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001466
1467 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1468 *mTestListener);
1469 ASSERT_EQ(mReader->start(), OK);
1470
1471 // Since this test is run on a real device, all the input devices connected
1472 // to the test device will show up in mReader. We wait for those input devices to
1473 // show up before beginning the tests.
Arpit Singh440bf652023-08-09 09:23:43 +00001474 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
Vaibhav Devmuraric109d812024-07-10 14:21:27 +00001475 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Arpit Singh440bf652023-08-09 09:23:43 +00001476 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001477};
1478
1479TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1480 // An invalid input device that is only used for this test.
1481 class InvalidUinputDevice : public UinputDevice {
1482 public:
Harry Cutts33476232023-01-30 19:57:29 +00001483 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001484
1485 private:
1486 void configureDevice(int fd, uinput_user_dev* device) override {}
1487 };
1488
1489 const size_t numDevices = mFakePolicy->getInputDevices().size();
1490
1491 // UinputDevice does not set any event or key bits, so InputReader should not
1492 // consider it as a valid device.
1493 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1494 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001495 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1496
1497 invalidDevice.reset();
1498 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001499 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1500}
1501
1502TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1503 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1504
1505 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1506 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001507 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1508
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001509 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001510 ASSERT_TRUE(device.has_value());
1511 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1512 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1513 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001514
1515 keyboard.reset();
1516 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001517 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1518}
1519
1520TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1521 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1522 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1523
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001524 NotifyKeyArgs keyArgs;
1525 keyboard->pressAndReleaseHomeKey();
1526 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1527 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001528 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Liana Kazanova5b8217b2024-07-18 17:44:51 +00001529
1530 int32_t prevId = keyArgs.id;
1531 nsecs_t prevTimestamp = keyArgs.eventTime;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001532
1533 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1534 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001535 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001536 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001537 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001538}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001539
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001540TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1541 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1542 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1543
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001544 const auto device = waitForDevice(stylus->getName());
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001545 ASSERT_TRUE(device.has_value());
1546
Prabir Pradhana3621852022-10-14 18:57:23 +00001547 // An external stylus with buttons should also be recognized as a keyboard.
1548 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001549 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1550 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1551
1552 const auto DOWN =
1553 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1554 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1555
1556 stylus->pressAndReleaseKey(BTN_STYLUS);
1557 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1558 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1559 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1560 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1561
1562 stylus->pressAndReleaseKey(BTN_STYLUS2);
1563 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1564 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1565 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1566 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1567
1568 stylus->pressAndReleaseKey(BTN_STYLUS3);
1569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1570 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1571 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1572 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1573}
1574
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001575TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1576 std::unique_ptr<UinputKeyboard> keyboard =
1577 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1578 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1579 KEY_R, KEY_T, KEY_Y,
1580 BTN_STYLUS, BTN_STYLUS2,
1581 BTN_STYLUS3});
1582 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1583
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001584 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001585 ASSERT_TRUE(device.has_value());
1586
1587 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1588 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1589 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1590 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1591}
1592
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001593TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1594 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1595 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1596 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1597 createUinputDevice<UinputKeyboardWithHidUsage>(
1598 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1599 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1600
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001601 const auto device = waitForDevice(keyboard->getName());
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001602 ASSERT_TRUE(device.has_value());
1603
1604 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1605 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1606
1607 // If a device supports reporting HID usage codes, it shouldn't automatically support
1608 // stylus keys.
1609 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1610 uint8_t outFlags[] = {0};
1611 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1612 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1613}
1614
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001615/**
1616 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1617 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1618 * are passed to the listener.
1619 */
1620static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1621TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1622 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1623 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1624 NotifyKeyArgs keyArgs;
1625
1626 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1627 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1628 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1629 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1630
1631 controller->pressAndReleaseKey(BTN_GEAR_UP);
1632 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1633 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1634 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1635}
1636
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001637// --- TouchIntegrationTest ---
1638
Arpit Singh440bf652023-08-09 09:23:43 +00001639class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001640protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001641 const std::string UNIQUE_ID = "local:0";
1642
Chris Yea52ade12020-08-27 16:49:20 -07001643 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001644#if !defined(__ANDROID__)
1645 GTEST_SKIP();
1646#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001647 InputReaderIntegrationTest::SetUp();
1648 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001649 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1650 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001651
1652 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1653 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001654 const auto info = waitForDevice(mDevice->getName());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001655 ASSERT_TRUE(info);
1656 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001657 }
1658
Linnan Li13bf76a2024-05-05 19:18:02 +08001659 void setDisplayInfoAndReconfigure(ui::LogicalDisplayId displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001660 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001661 std::optional<uint8_t> physicalPort,
1662 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001663 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001664 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001665 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001666 }
1667
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001668 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1669 NotifyMotionArgs args;
1670 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1671 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001672 ASSERT_EQ(points.size(), args.getPointerCount());
1673 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001674 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1675 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1676 }
1677 }
1678
Arthur Hungaab25622020-01-16 11:22:11 +08001679 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001680 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001681};
1682
Arpit Singh440bf652023-08-09 09:23:43 +00001683enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1684
1685class TouchIntegrationTest : public BaseTouchIntegrationTest,
1686 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1687protected:
1688 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1689 const std::string INPUT_PORT = "uinput_touch/input0";
1690
1691 void SetUp() override {
1692#if !defined(__ANDROID__)
1693 GTEST_SKIP();
1694#endif
1695 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1696 BaseTouchIntegrationTest::SetUp();
1697 return;
1698 }
1699
1700 // setup policy with a input-port or UniqueId association to the display
1701 bool isInputPortAssociation =
1702 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1703
1704 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1705 if (isInputPortAssociation) {
1706 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1707 } else {
1708 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1709 }
Arpit Singh440bf652023-08-09 09:23:43 +00001710
1711 InputReaderIntegrationTest::setupInputReader();
1712
1713 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1714 INPUT_PORT);
1715 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1716
1717 // Add a display linked to a physical port or UniqueId.
1718 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1719 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1720 ViewportType::INTERNAL);
1721 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08001722 const auto info = waitForDevice(mDevice->getName());
Arpit Singh440bf652023-08-09 09:23:43 +00001723 ASSERT_TRUE(info);
1724 mDeviceInfo = *info;
1725 }
1726};
1727
1728TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001729 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1730 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1731 // presses).
1732 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1733 mDeviceInfo.getSources());
1734}
1735
Arpit Singh440bf652023-08-09 09:23:43 +00001736TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001737 NotifyMotionArgs args;
1738 const Point centerPoint = mDevice->getCenterPoint();
1739
1740 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001741 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001742 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001743 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001744 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1746
1747 // ACTION_MOVE
1748 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001749 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001750 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1751 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1752
1753 // ACTION_UP
1754 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001755 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001756 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1757 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1758}
1759
Arpit Singh440bf652023-08-09 09:23:43 +00001760TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001761 NotifyMotionArgs args;
1762 const Point centerPoint = mDevice->getCenterPoint();
1763
1764 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001765 mDevice->sendSlot(FIRST_SLOT);
1766 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001767 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001768 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001769 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1770 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1771
1772 // ACTION_POINTER_DOWN (Second slot)
1773 const Point secondPoint = centerPoint + Point(100, 100);
1774 mDevice->sendSlot(SECOND_SLOT);
1775 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001776 mDevice->sendDown(secondPoint);
1777 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001778 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001779 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001780
1781 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001782 mDevice->sendMove(secondPoint + Point(1, 1));
1783 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001784 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1785 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1786
1787 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001788 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001789 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001790 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001791 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001792
1793 // ACTION_UP
1794 mDevice->sendSlot(FIRST_SLOT);
1795 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001796 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001797 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1798 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1799}
1800
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001801/**
1802 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1803 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1804 * data?
1805 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1806 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1807 * for Pointer 0 only is generated after.
1808 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1809 * events, we will not miss any information.
1810 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1811 * event generated afterwards that contains the newest movement of pointer 0.
1812 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1813 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1814 * losing information about non-palm pointers.
1815 */
Arpit Singh440bf652023-08-09 09:23:43 +00001816TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001817 NotifyMotionArgs args;
1818 const Point centerPoint = mDevice->getCenterPoint();
1819
1820 // ACTION_DOWN
1821 mDevice->sendSlot(FIRST_SLOT);
1822 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1823 mDevice->sendDown(centerPoint);
1824 mDevice->sendSync();
1825 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1826
1827 // ACTION_POINTER_DOWN (Second slot)
1828 const Point secondPoint = centerPoint + Point(100, 100);
1829 mDevice->sendSlot(SECOND_SLOT);
1830 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1831 mDevice->sendDown(secondPoint);
1832 mDevice->sendSync();
1833 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1834
1835 // ACTION_MOVE (First slot)
1836 mDevice->sendSlot(FIRST_SLOT);
1837 mDevice->sendMove(centerPoint + Point(5, 5));
1838 // ACTION_POINTER_UP (Second slot)
1839 mDevice->sendSlot(SECOND_SLOT);
1840 mDevice->sendPointerUp();
1841 // Send a single sync for the above 2 pointer updates
1842 mDevice->sendSync();
1843
1844 // First, we should get POINTER_UP for the second pointer
1845 assertReceivedMotion(ACTION_POINTER_1_UP,
1846 {/*first pointer */ centerPoint + Point(5, 5),
1847 /*second pointer*/ secondPoint});
1848
1849 // Next, the MOVE event for the first pointer
1850 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1851}
1852
1853/**
1854 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1855 * move, and then it will go up, all in the same frame.
1856 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1857 * gets sent to the listener.
1858 */
Arpit Singh440bf652023-08-09 09:23:43 +00001859TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001860 NotifyMotionArgs args;
1861 const Point centerPoint = mDevice->getCenterPoint();
1862
1863 // ACTION_DOWN
1864 mDevice->sendSlot(FIRST_SLOT);
1865 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1866 mDevice->sendDown(centerPoint);
1867 mDevice->sendSync();
1868 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1869
1870 // ACTION_POINTER_DOWN (Second slot)
1871 const Point secondPoint = centerPoint + Point(100, 100);
1872 mDevice->sendSlot(SECOND_SLOT);
1873 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1874 mDevice->sendDown(secondPoint);
1875 mDevice->sendSync();
1876 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1877
1878 // ACTION_MOVE (First slot)
1879 mDevice->sendSlot(FIRST_SLOT);
1880 mDevice->sendMove(centerPoint + Point(5, 5));
1881 // ACTION_POINTER_UP (Second slot)
1882 mDevice->sendSlot(SECOND_SLOT);
1883 mDevice->sendMove(secondPoint + Point(6, 6));
1884 mDevice->sendPointerUp();
1885 // Send a single sync for the above 2 pointer updates
1886 mDevice->sendSync();
1887
1888 // First, we should get POINTER_UP for the second pointer
1889 // The movement of the second pointer during the liftoff frame is ignored.
1890 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1891 assertReceivedMotion(ACTION_POINTER_1_UP,
1892 {/*first pointer */ centerPoint + Point(5, 5),
1893 /*second pointer*/ secondPoint});
1894
1895 // Next, the MOVE event for the first pointer
1896 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1897}
1898
Arpit Singh440bf652023-08-09 09:23:43 +00001899TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001900 NotifyMotionArgs args;
1901 const Point centerPoint = mDevice->getCenterPoint();
1902
1903 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001904 mDevice->sendSlot(FIRST_SLOT);
1905 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001906 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001907 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001908 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1909 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1910
arthurhungcc7f9802020-04-30 17:55:40 +08001911 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001912 const Point secondPoint = centerPoint + Point(100, 100);
1913 mDevice->sendSlot(SECOND_SLOT);
1914 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1915 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001916 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001917 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001918 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001919
arthurhungcc7f9802020-04-30 17:55:40 +08001920 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001921 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001922 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001923 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1924 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1925
arthurhungcc7f9802020-04-30 17:55:40 +08001926 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1927 // a palm event.
1928 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001929 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001930 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001931 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001932 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001933 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001934
arthurhungcc7f9802020-04-30 17:55:40 +08001935 // Send up to second slot, expect first slot send moving.
1936 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001937 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001938 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1939 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001940
arthurhungcc7f9802020-04-30 17:55:40 +08001941 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001942 mDevice->sendSlot(FIRST_SLOT);
1943 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001944 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001945
arthurhungcc7f9802020-04-30 17:55:40 +08001946 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1947 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001948}
1949
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001950/**
1951 * Some drivers historically have reported axis values outside of the range specified in the
1952 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1953 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1954 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1955 * and no units for pressure (resolution) is specified by the evdev documentation.
1956 */
1957TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1958 const Point centerPoint = mDevice->getCenterPoint();
1959
1960 // Down with pressure outside the reported range
1961 mDevice->sendSlot(FIRST_SLOT);
1962 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1963 mDevice->sendDown(centerPoint);
1964 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1965 mDevice->sendSync();
1966 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1967 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1968
1969 // Move to a point outside the reported range
1970 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1971 mDevice->sendSync();
1972 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1973 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1974
1975 // Up
1976 mDevice->sendUp();
1977 mDevice->sendSync();
1978 ASSERT_NO_FATAL_FAILURE(
1979 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1980}
1981
Arpit Singh440bf652023-08-09 09:23:43 +00001982TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001983 const Point centerPoint = mDevice->getCenterPoint();
1984
1985 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1986 mDevice->sendSlot(FIRST_SLOT);
1987 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1988 mDevice->sendToolType(MT_TOOL_PEN);
1989 mDevice->sendDown(centerPoint);
1990 mDevice->sendSync();
1991 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1992 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001993 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001994
1995 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1996
1997 // Release the stylus touch.
1998 mDevice->sendUp();
1999 mDevice->sendSync();
2000 ASSERT_NO_FATAL_FAILURE(
2001 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2002
2003 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2004
2005 // Touch down with the finger, without the pen tool selected. The policy is not notified.
2006 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2007 mDevice->sendToolType(MT_TOOL_FINGER);
2008 mDevice->sendDown(centerPoint);
2009 mDevice->sendSync();
2010 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2011 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002012 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002013
2014 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
2015
2016 mDevice->sendUp();
2017 mDevice->sendSync();
2018 ASSERT_NO_FATAL_FAILURE(
2019 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2020
2021 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
2022 // The policy should be notified of the stylus presence.
2023 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2024 mDevice->sendToolType(MT_TOOL_PEN);
2025 mDevice->sendMove(centerPoint);
2026 mDevice->sendSync();
2027 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2028 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002029 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00002030
2031 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
2032}
2033
Arpit Singh440bf652023-08-09 09:23:43 +00002034TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002035 const Point centerPoint = mDevice->getCenterPoint();
2036
2037 // Down
2038 mDevice->sendSlot(FIRST_SLOT);
2039 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2040 mDevice->sendDown(centerPoint);
2041 mDevice->sendSync();
2042 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2043 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
2044
2045 // Move
2046 mDevice->sendMove(centerPoint + Point(1, 1));
2047 mDevice->sendSync();
2048 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2049 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2050
2051 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2052 auto externalStylus = createUinputDevice<UinputExternalStylus>();
2053 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002054 const auto stylusInfo = waitForDevice(externalStylus->getName());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002055 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002056
2057 // Move
2058 mDevice->sendMove(centerPoint + Point(2, 2));
2059 mDevice->sendSync();
2060 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2061 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2062
2063 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2064 externalStylus.reset();
2065 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00002066 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2067
2068 // Up
2069 mDevice->sendUp();
2070 mDevice->sendSync();
2071 ASSERT_NO_FATAL_FAILURE(
2072 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2073
2074 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2075}
2076
Arpit Singh440bf652023-08-09 09:23:43 +00002077INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2078 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2079 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2080 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2081
Prabir Pradhan124ea442022-10-28 20:27:44 +00002082// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002083
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2085// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2086// stylus.
2087template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002088class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002089protected:
2090 void SetUp() override {
2091#if !defined(__ANDROID__)
2092 GTEST_SKIP();
2093#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002094 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002095 mTouchscreen = mDevice.get();
2096 mTouchscreenInfo = mDeviceInfo;
2097
2098 setUpStylusDevice();
2099 }
2100
2101 UinputStylusDevice* mStylus{nullptr};
2102 InputDeviceInfo mStylusInfo{};
2103
2104 UinputTouchScreen* mTouchscreen{nullptr};
2105 InputDeviceInfo mTouchscreenInfo{};
2106
2107private:
2108 // When we are attempting to test stylus button events that are sent from the touchscreen,
2109 // use the same Uinput device for the touchscreen and the stylus.
2110 template <typename T = UinputStylusDevice>
2111 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2112 mStylus = mDevice.get();
2113 mStylusInfo = mDeviceInfo;
2114 }
2115
2116 // When we are attempting to stylus buttons from an external stylus being merged with touches
2117 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2118 template <typename T = UinputStylusDevice>
2119 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2120 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2121 mStylus = mStylusDeviceLifecycleTracker.get();
2122 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002123 const auto info = waitForDevice(mStylus->getName());
Prabir Pradhan124ea442022-10-28 20:27:44 +00002124 ASSERT_TRUE(info);
2125 mStylusInfo = *info;
2126 }
2127
2128 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2129
2130 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002131 using BaseTouchIntegrationTest::mDevice;
2132 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002133};
2134
2135using StylusButtonIntegrationTestTypes =
2136 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2137TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2138
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002139TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002140 const auto stylusId = TestFixture::mStylusInfo.getId();
2141
2142 TestFixture::mStylus->pressKey(BTN_STYLUS);
2143 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2144 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2145 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2146
2147 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2148 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002149 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002150 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002151}
2152
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002153TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002154 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2155 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2156 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002157
2158 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002159 TestFixture::mStylus->pressKey(BTN_STYLUS);
2160 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002161 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002162 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002163
2164 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002165 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2166 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2167 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2168 TestFixture::mTouchscreen->sendDown(centerPoint);
2169 TestFixture::mTouchscreen->sendSync();
2170 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002171 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002172 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002173 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2174 WithDeviceId(touchscreenId))));
2175 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002176 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002177 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002178 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2179 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002180
Prabir Pradhan124ea442022-10-28 20:27:44 +00002181 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2182 TestFixture::mTouchscreen->sendSync();
2183 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002185 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002186 WithDeviceId(touchscreenId))));
2187 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002188 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002189 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002190 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002191
2192 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002193 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2194 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002195 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002196 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002197}
2198
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002199TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002200 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2201 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2202 const auto stylusId = TestFixture::mStylusInfo.getId();
2203 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002204 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002205
2206 // Press the stylus button.
2207 TestFixture::mStylus->pressKey(BTN_STYLUS);
2208 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2209 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2210 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2211
2212 // Start hovering with the stylus.
2213 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2214 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2215 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2216 TestFixture::mTouchscreen->sendMove(centerPoint);
2217 TestFixture::mTouchscreen->sendSync();
2218 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2219 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2220 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2221 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2222 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2223 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2224 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2225 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2226 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2227
2228 // Touch down with the stylus.
2229 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2230 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2231 TestFixture::mTouchscreen->sendDown(centerPoint);
2232 TestFixture::mTouchscreen->sendSync();
2233 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2234 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2235 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2236
2237 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2238 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2239 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2240
2241 // Stop touching with the stylus, and start hovering.
2242 TestFixture::mTouchscreen->sendUp();
2243 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2244 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2245 TestFixture::mTouchscreen->sendMove(centerPoint);
2246 TestFixture::mTouchscreen->sendSync();
2247 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2248 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2249 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2250 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2251 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2252 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2253 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2254 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2255 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2256
2257 // Stop hovering.
2258 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2259 TestFixture::mTouchscreen->sendSync();
2260 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2261 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2262 WithButtonState(0))));
2263 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2264 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2265 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2266 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2267
2268 // Release the stylus button.
2269 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2270 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2271 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2272 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2273}
2274
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002275TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002276 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2277 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2278 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002279
2280 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002281 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2282 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2283 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2284 TestFixture::mTouchscreen->sendDown(centerPoint);
2285 TestFixture::mTouchscreen->sendSync();
2286 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002287 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002288 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002289 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002290
2291 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002292 TestFixture::mStylus->pressKey(BTN_STYLUS);
2293 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002294 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002295 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2296 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002297 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002298 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002299 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2300 WithDeviceId(touchscreenId))));
2301 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002302 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002303 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002304 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2305 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002306
Prabir Pradhan124ea442022-10-28 20:27:44 +00002307 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2308 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002309 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002310 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2311 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002312 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002313 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002314 WithDeviceId(touchscreenId))));
2315 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002316 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002317 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002318 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002319
2320 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002321 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2322 TestFixture::mTouchscreen->sendSync();
2323 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002324 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002325 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002326 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002327}
2328
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002329TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002330 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2331 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002332 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002333
2334 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2335 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2336 const auto stylusId = TestFixture::mStylusInfo.getId();
2337
2338 // Start a stylus gesture. By the time this event is processed, the configuration change that
2339 // was requested is guaranteed to be completed.
2340 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2341 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2342 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2343 TestFixture::mTouchscreen->sendDown(centerPoint);
2344 TestFixture::mTouchscreen->sendSync();
2345 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2346 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002347 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002348 WithDeviceId(touchscreenId))));
2349
2350 // Press and release a stylus button. Each change only generates a MOVE motion event.
2351 // Key events are unaffected.
2352 TestFixture::mStylus->pressKey(BTN_STYLUS);
2353 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2354 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2355 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2356 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2357 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002358 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002359 WithDeviceId(touchscreenId))));
2360
2361 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2362 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2363 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2364 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2365 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2366 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002367 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002368 WithDeviceId(touchscreenId))));
2369
2370 // Finish the stylus gesture.
2371 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2372 TestFixture::mTouchscreen->sendSync();
2373 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2374 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002375 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002376 WithDeviceId(touchscreenId))));
2377}
2378
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002379// --- ExternalStylusIntegrationTest ---
2380
2381// Verify the behavior of an external stylus. An external stylus can report pressure or button
2382// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2383// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002384using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002385
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002386TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2387 // Create an external stylus capable of reporting pressure data that
2388 // should be fused with a touch pointer.
2389 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2390 createUinputDevice<UinputExternalStylusWithPressure>();
2391 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002392 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002393 ASSERT_TRUE(stylusInfo);
2394
2395 // Connecting an external stylus changes the source of the touchscreen.
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002396 const auto deviceInfo = waitForDevice(mDevice->getName());
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002397 ASSERT_TRUE(deviceInfo);
2398 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2399}
2400
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002401TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002402 const Point centerPoint = mDevice->getCenterPoint();
2403
2404 // Create an external stylus capable of reporting pressure data that
2405 // should be fused with a touch pointer.
2406 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2407 createUinputDevice<UinputExternalStylusWithPressure>();
2408 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002409 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002410 ASSERT_TRUE(stylusInfo);
2411
2412 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2413
2414 const auto touchscreenId = mDeviceInfo.getId();
2415
2416 // Set a pressure value on the stylus. It doesn't generate any events.
2417 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2418 stylus->setPressure(100);
2419 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2420
2421 // Start a finger gesture, and ensure it shows up as stylus gesture
2422 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002423 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002424 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002425 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002426 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002427 mDevice->sendSync();
2428 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002429 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2430 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2431 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002432
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002433 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2434 // event with the updated pressure.
2435 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002436 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002437 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2438 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2439 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002440
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002441 // The external stylus did not generate any events.
2442 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2443 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2444}
2445
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002446TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002447 const Point centerPoint = mDevice->getCenterPoint();
2448
2449 // Create an external stylus capable of reporting pressure data that
2450 // should be fused with a touch pointer.
2451 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2452 createUinputDevice<UinputExternalStylusWithPressure>();
2453 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002454 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002455 ASSERT_TRUE(stylusInfo);
2456
2457 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2458
2459 const auto touchscreenId = mDeviceInfo.getId();
2460
2461 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2462 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002463 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2464 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002465 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002466 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002467
2468 // Start a finger gesture. The touch device will withhold generating any touches for
2469 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2470 mDevice->sendSlot(FIRST_SLOT);
2471 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2472 mDevice->sendToolType(MT_TOOL_FINGER);
2473 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002474 const auto syncTime = std::chrono::system_clock::now();
2475 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2476 // is generated in that period, there will be a race condition between the event being generated
2477 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2478 // will reduce the liklihood of the race condition occurring.
2479 const auto waitUntilTimeForNoEvent =
2480 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002481 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002482 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002483
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002484 // Since the external stylus did not report a pressure value within the timeout,
2485 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002486 const auto waitUntilTimeForEvent = syncTime +
2487 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2488 ASSERT_NO_FATAL_FAILURE(
2489 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2490 AMOTION_EVENT_ACTION_DOWN),
2491 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2492 AINPUT_SOURCE_STYLUS),
2493 WithToolType(ToolType::FINGER),
2494 WithDeviceId(touchscreenId),
2495 WithPressure(1.f)),
2496 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002497
2498 // Change the pressure on the external stylus. Since the pressure was not present at the start
2499 // of the gesture, it is ignored for now.
2500 stylus->setPressure(200);
2501 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2502
2503 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002504 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2505 mDevice->sendSync();
2506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2507 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002508 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002509 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002510
2511 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2512 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2513 mDevice->sendToolType(MT_TOOL_FINGER);
2514 mDevice->sendDown(centerPoint);
2515 mDevice->sendSync();
2516 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002517 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2518 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2519 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002520
2521 // The external stylus did not generate any events.
2522 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2523 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002524}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002525
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002526TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002527 const Point centerPoint = mDevice->getCenterPoint();
2528
2529 // Create an external stylus device that does not support pressure. It should not affect any
2530 // touch pointers.
2531 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2532 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Siarhei Vishniakoud790d6b2024-02-21 10:34:59 -08002533 const auto stylusInfo = waitForDevice(stylus->getName());
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002534 ASSERT_TRUE(stylusInfo);
2535
2536 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2537
2538 const auto touchscreenId = mDeviceInfo.getId();
2539
2540 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2541 // pressure data from the external stylus.
2542 mDevice->sendSlot(FIRST_SLOT);
2543 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2544 mDevice->sendToolType(MT_TOOL_FINGER);
2545 mDevice->sendDown(centerPoint);
2546 auto waitUntil = std::chrono::system_clock::now() +
2547 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2548 mDevice->sendSync();
2549 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002550 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2551 AMOTION_EVENT_ACTION_DOWN),
2552 WithToolType(ToolType::FINGER),
2553 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2554 AINPUT_SOURCE_STYLUS),
2555 WithButtonState(0),
2556 WithDeviceId(touchscreenId),
2557 WithPressure(1.f)),
2558 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002559
2560 // The external stylus did not generate any events.
2561 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2562 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2563}
2564
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565// --- InputDeviceTest ---
2566class InputDeviceTest : public testing::Test {
2567protected:
2568 static const char* DEVICE_NAME;
2569 static const char* DEVICE_LOCATION;
2570 static const int32_t DEVICE_ID;
2571 static const int32_t DEVICE_GENERATION;
2572 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002573 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002574 static const int32_t EVENTHUB_ID;
2575 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2576
2577 std::shared_ptr<FakeEventHub> mFakeEventHub;
2578 sp<FakeInputReaderPolicy> mFakePolicy;
2579 std::unique_ptr<TestInputListener> mFakeListener;
2580 std::unique_ptr<InstrumentedInputReader> mReader;
2581 std::shared_ptr<InputDevice> mDevice;
2582
2583 void SetUp() override {
2584 mFakeEventHub = std::make_unique<FakeEventHub>();
2585 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2586 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002587 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002588 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002589 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002590 identifier.name = DEVICE_NAME;
2591 identifier.location = DEVICE_LOCATION;
2592 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2593 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2594 identifier);
2595 mReader->pushNextDevice(mDevice);
2596 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002597 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002598 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002599
2600 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002601 mFakeListener.reset();
2602 mFakePolicy.clear();
2603 }
2604};
2605
2606const char* InputDeviceTest::DEVICE_NAME = "device";
2607const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2608const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2609const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002610const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002611const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2612 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002613const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002614const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2615
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002616TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002617 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002618 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2619 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002620}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002621
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2623 ASSERT_EQ(mDevice->isEnabled(), false);
2624}
2625
2626TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2627 // Configuration.
2628 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002629 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002630
2631 // Reset.
2632 unused += mDevice->reset(ARBITRARY_TIME);
2633
2634 NotifyDeviceResetArgs resetArgs;
2635 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2636 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2637 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2638
2639 // Metadata.
2640 ASSERT_TRUE(mDevice->isIgnored());
2641 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2642
2643 InputDeviceInfo info = mDevice->getDeviceInfo();
2644 ASSERT_EQ(DEVICE_ID, info.getId());
2645 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2646 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2647 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2648
2649 // State queries.
2650 ASSERT_EQ(0, mDevice->getMetaState());
2651
2652 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2653 << "Ignored device should return unknown key code state.";
2654 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2655 << "Ignored device should return unknown scan code state.";
2656 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2657 << "Ignored device should return unknown switch state.";
2658
2659 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2660 uint8_t flags[2] = { 0, 1 };
2661 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2662 << "Ignored device should never mark any key codes.";
2663 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2664 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2665}
2666
2667TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2668 // Configuration.
2669 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2670
2671 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002672 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2673 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002674 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2675 mapper1.setMetaState(AMETA_ALT_ON);
2676 mapper1.addSupportedKeyCode(AKEYCODE_A);
2677 mapper1.addSupportedKeyCode(AKEYCODE_B);
2678 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2679 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2680 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2681 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2682 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2683
2684 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002685 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2686 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 mapper2.setMetaState(AMETA_SHIFT_ON);
2688
2689 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002690 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002691
Harry Cuttsf13161a2023-03-08 14:15:49 +00002692 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2693 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002694 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002695 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002697 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2698 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002699
2700 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002701 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002702 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2703 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002704
2705 NotifyDeviceResetArgs resetArgs;
2706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2707 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2708 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2709
2710 // Metadata.
2711 ASSERT_FALSE(mDevice->isIgnored());
2712 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2713
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002714 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002715 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002716 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002717 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2718 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2719
2720 // State queries.
2721 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2722 << "Should query mappers and combine meta states.";
2723
2724 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2725 << "Should return unknown key code state when source not supported.";
2726 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2727 << "Should return unknown scan code state when source not supported.";
2728 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2729 << "Should return unknown switch state when source not supported.";
2730
2731 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2732 << "Should query mapper when source is supported.";
2733 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2734 << "Should query mapper when source is supported.";
2735 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2736 << "Should query mapper when source is supported.";
2737
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002738 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002739 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002740 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002741 << "Should do nothing when source is unsupported.";
2742 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2743 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2744 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2745 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2746
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002747 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002748 << "Should query mapper when source is supported.";
2749 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2750 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2751 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2752 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2753
2754 // Event handling.
2755 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002756 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002757 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002758
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002759 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2760 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002761}
2762
Yeabkal Wubshitb1b96db2024-01-24 12:47:00 -08002763TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorNotSet) {
2764 // Set some behavior to force the configuration to be update.
2765 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2766 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2767 AINPUT_SOURCE_KEYBOARD);
2768
2769 std::list<NotifyArgs> unused =
2770 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2771 /*changes=*/{});
2772
2773 ASSERT_FALSE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.has_value());
2774}
2775
2776TEST_F(InputDeviceTest, Configure_SmoothScrollViewBehaviorEnabled) {
2777 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.viewBehavior_smoothScroll", "1");
2778 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2779 AINPUT_SOURCE_KEYBOARD);
2780
2781 std::list<NotifyArgs> unused =
2782 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2783 /*changes=*/{});
2784
2785 ASSERT_TRUE(mDevice->getDeviceInfo().getViewBehavior().shouldSmoothScroll.value_or(false));
2786}
2787
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002788TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2789 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2790 FakeInputMapper& mapper =
2791 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2792 AINPUT_SOURCE_KEYBOARD);
2793 NotifyMotionArgs args1;
2794 NotifySwitchArgs args2;
2795 NotifyKeyArgs args3;
2796 mapper.setProcessResult({args1, args2, args3});
2797
2798 InputReaderConfiguration config;
2799 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2800
2801 RawEvent event;
2802 event.deviceId = EVENTHUB_ID;
2803 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2804
2805 for (auto& arg : notifyArgs) {
2806 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2807 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2808 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2809 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2810 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2811 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2812 }
2813 }
2814}
2815
2816TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2817 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2818 FakeInputMapper& mapper =
2819 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2820 AINPUT_SOURCE_KEYBOARD);
2821 NotifyMotionArgs args;
2822 mapper.setProcessResult({args});
2823
2824 InputReaderConfiguration config;
2825 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2826
2827 RawEvent event;
2828 event.deviceId = EVENTHUB_ID;
2829 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2830
2831 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2832 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2833}
2834
2835TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2836 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2837 FakeInputMapper& mapper =
2838 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2839 AINPUT_SOURCE_KEYBOARD);
2840 NotifyMotionArgs args;
2841 args.policyFlags = POLICY_FLAG_WAKE;
2842 mapper.setProcessResult({args});
2843
2844 InputReaderConfiguration config;
2845 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2846
2847 RawEvent event;
2848 event.deviceId = EVENTHUB_ID;
2849 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2850
2851 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2852 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2853}
2854
Arthur Hung2c9a3342019-07-23 14:18:59 +08002855// A single input device is associated with a specific display. Check that:
2856// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002857// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002858TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002859 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2860 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002861
2862 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002863 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002864 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2865 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002866
2867 // Device should be enabled by default.
2868 ASSERT_TRUE(mDevice->isEnabled());
2869
2870 // Prepare associated info.
2871 constexpr uint8_t hdmi = 1;
2872 const std::string UNIQUE_ID = "local:1";
2873
2874 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002875 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002876 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002877 // Device should be disabled because it is associated with a specific display via
2878 // input port <-> display port association, but the corresponding display is not found
2879 ASSERT_FALSE(mDevice->isEnabled());
2880
2881 // Prepare displays.
2882 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002883 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002884 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002885 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002886 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002887 ASSERT_TRUE(mDevice->isEnabled());
2888
2889 // Device should be disabled after set disable.
2890 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002891 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002892 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002893 ASSERT_FALSE(mDevice->isEnabled());
2894
2895 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002896 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002897 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002898 ASSERT_FALSE(mDevice->isEnabled());
2899}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002900
Christine Franks1ba71cc2021-04-07 14:37:42 -07002901TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2902 // Device should be enabled by default.
2903 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002904 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2905 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002906 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002907 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2908 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002909 ASSERT_TRUE(mDevice->isEnabled());
2910
2911 // Device should be disabled because it is associated with a specific display, but the
2912 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002913 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002914 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002915 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002916 ASSERT_FALSE(mDevice->isEnabled());
2917
2918 // Device should be enabled when a display is found.
2919 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002920 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002921 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002922 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002923 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002924 ASSERT_TRUE(mDevice->isEnabled());
2925
2926 // Device should be disabled after set disable.
2927 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002928 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002929 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002930 ASSERT_FALSE(mDevice->isEnabled());
2931
2932 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002933 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002934 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002935 ASSERT_FALSE(mDevice->isEnabled());
2936}
2937
Christine Franks2a2293c2022-01-18 11:51:16 -08002938TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2939 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002940 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2941 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002942 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002943 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2944 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002945
Christine Franks2a2293c2022-01-18 11:51:16 -08002946 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2947 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002948 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002949 NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002950 const auto initialGeneration = mDevice->getGeneration();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002951 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002952 InputReaderConfiguration::Change::DISPLAY_INFO);
Antonio Kantek0ac5e092024-04-22 17:10:27 +00002953 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueIdByPort());
Prabir Pradhan55c5ee22024-02-14 06:03:02 +00002954 ASSERT_GT(mDevice->getGeneration(), initialGeneration);
2955 ASSERT_EQ(mDevice->getDeviceInfo().getAssociatedDisplayId(), SECONDARY_DISPLAY_ID);
Christine Franks2a2293c2022-01-18 11:51:16 -08002956}
2957
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002958/**
2959 * This test reproduces a crash caused by a dangling reference that remains after device is added
2960 * and removed. The reference is accessed in InputDevice::dump(..);
2961 */
2962TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2963 constexpr int32_t TEST_EVENTHUB_ID = 10;
2964 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2965
Harry Cutts33476232023-01-30 19:57:29 +00002966 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002967 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2968 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002969 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2970 std::string dumpStr, eventHubDevStr;
2971 device.dump(dumpStr, eventHubDevStr);
2972}
2973
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002974TEST_F(InputDeviceTest, GetBluetoothAddress) {
2975 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2976 ASSERT_TRUE(address);
2977 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2978}
2979
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002980TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2981 mFakePolicy->clearViewports();
2982 FakeInputMapper& mapper =
2983 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2984 AINPUT_SOURCE_KEYBOARD);
2985 std::list<NotifyArgs> unused =
2986 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2987 /*changes=*/{});
2988
2989 mapper.assertConfigureWasCalled();
2990 mapper.assertResetWasNotCalled();
2991
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002992 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002993 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002994 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002995 .type = EV_SYN,
2996 .code = SYN_REPORT,
2997 .value = 0};
2998
2999 // Events are processed normally.
3000 unused = mDevice->process(&event, /*count=*/1);
3001 mapper.assertProcessWasCalled();
3002
3003 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003004 event.type = EV_SYN;
3005 event.code = SYN_DROPPED;
3006 event.value = 0;
3007 unused = mDevice->process(&event, /*count=*/1);
3008 mapper.assertProcessWasNotCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003009
3010 // All events until the next SYN_REPORT should be dropped.
3011 event.type = EV_KEY;
3012 event.code = KEY_A;
3013 event.value = 1;
3014 unused = mDevice->process(&event, /*count=*/1);
3015 mapper.assertProcessWasNotCalled();
3016
3017 // We get the SYN_REPORT event now, which is not forwarded to mappers.
Arpit Singh4b4a4572023-11-24 18:19:56 +00003018 // This should reset the mapper.
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003019 event.type = EV_SYN;
3020 event.code = SYN_REPORT;
3021 event.value = 0;
3022 unused = mDevice->process(&event, /*count=*/1);
3023 mapper.assertProcessWasNotCalled();
Arpit Singh4b4a4572023-11-24 18:19:56 +00003024 mapper.assertResetWasCalled();
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00003025
3026 // The mapper receives events normally now.
3027 event.type = EV_KEY;
3028 event.code = KEY_B;
3029 event.value = 1;
3030 unused = mDevice->process(&event, /*count=*/1);
3031 mapper.assertProcessWasCalled();
3032}
3033
Michael Wrightd02c5b62014-02-10 15:10:22 -08003034// --- KeyboardInputMapperTest ---
3035
3036class KeyboardInputMapperTest : public InputMapperTest {
3037protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003038 void SetUp() override {
3039 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3040 InputDeviceClass::ALPHAKEY);
3041 }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003042 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003043 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003044 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003045
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003046 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003047 int32_t originalKeyCode, int32_t rotatedKeyCode,
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003048 ui::LogicalDisplayId displayId = ui::LogicalDisplayId::INVALID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003049};
3050
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003051/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3052 * orientation.
3053 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003054void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003055 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3056 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003057}
3058
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003059void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003060 int32_t originalScanCode, int32_t originalKeyCode,
Linnan Li13bf76a2024-05-05 19:18:02 +08003061 int32_t rotatedKeyCode,
3062 ui::LogicalDisplayId displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003063 NotifyKeyArgs args;
3064
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003065 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3067 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3068 ASSERT_EQ(originalScanCode, args.scanCode);
3069 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003070 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003071
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003072 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003073 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3074 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3075 ASSERT_EQ(originalScanCode, args.scanCode);
3076 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003077 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003078}
3079
Michael Wrightd02c5b62014-02-10 15:10:22 -08003080TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003081 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003082 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003083
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003084 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003085}
3086
3087TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3088 const int32_t USAGE_A = 0x070004;
3089 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003090 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3091 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003092 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3093 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3094 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003095
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003096 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003097 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003098 // Initial metastate is AMETA_NONE.
3099 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003100
3101 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003102 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003103 NotifyKeyArgs args;
3104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3105 ASSERT_EQ(DEVICE_ID, args.deviceId);
3106 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3107 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3108 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3109 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3110 ASSERT_EQ(KEY_HOME, args.scanCode);
3111 ASSERT_EQ(AMETA_NONE, args.metaState);
3112 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3113 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3114 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3115
3116 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003117 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3119 ASSERT_EQ(DEVICE_ID, args.deviceId);
3120 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3121 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3122 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3123 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3124 ASSERT_EQ(KEY_HOME, args.scanCode);
3125 ASSERT_EQ(AMETA_NONE, args.metaState);
3126 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3127 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3128 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3129
3130 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3134 ASSERT_EQ(DEVICE_ID, args.deviceId);
3135 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3136 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3137 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3138 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3139 ASSERT_EQ(0, args.scanCode);
3140 ASSERT_EQ(AMETA_NONE, args.metaState);
3141 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3142 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3143 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3144
3145 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003146 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3147 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003148 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3149 ASSERT_EQ(DEVICE_ID, args.deviceId);
3150 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3151 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3152 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3153 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3154 ASSERT_EQ(0, args.scanCode);
3155 ASSERT_EQ(AMETA_NONE, args.metaState);
3156 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3157 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3158 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3159
3160 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003161 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3162 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003163 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3164 ASSERT_EQ(DEVICE_ID, args.deviceId);
3165 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3166 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3167 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3168 ASSERT_EQ(0, args.keyCode);
3169 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3170 ASSERT_EQ(AMETA_NONE, args.metaState);
3171 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3172 ASSERT_EQ(0U, args.policyFlags);
3173 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3174
3175 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003176 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3177 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3179 ASSERT_EQ(DEVICE_ID, args.deviceId);
3180 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3181 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3182 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3183 ASSERT_EQ(0, args.keyCode);
3184 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3185 ASSERT_EQ(AMETA_NONE, args.metaState);
3186 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3187 ASSERT_EQ(0U, args.policyFlags);
3188 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3189}
3190
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003191TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3192 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3193 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003194
3195 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003196 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003197
Linnan Lie5657f22024-09-13 21:54:37 +08003198 mFakeEventHub->setKeyRemapping(EVENTHUB_ID, {{AKEYCODE_A, AKEYCODE_B}});
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003199 // Key down by scan code.
3200 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3201 NotifyKeyArgs args;
3202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3203 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3204
3205 // Key up by scan code.
3206 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3208 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3209}
3210
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003211/**
3212 * Ensure that the readTime is set to the time when the EV_KEY is received.
3213 */
3214TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3215 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3216
3217 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003218 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003219 NotifyKeyArgs args;
3220
3221 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003222 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3224 ASSERT_EQ(12, args.readTime);
3225
3226 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003227 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3229 ASSERT_EQ(15, args.readTime);
3230}
3231
Michael Wrightd02c5b62014-02-10 15:10:22 -08003232TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003233 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3234 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003235 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3236 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3237 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003238
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003239 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003240 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241
Arthur Hung95f68612022-04-07 14:08:22 +08003242 // Initial metastate is AMETA_NONE.
3243 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003244
3245 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003247 NotifyKeyArgs args;
3248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3249 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003250 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003251 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
3253 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003254 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003257 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003258
3259 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003260 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3262 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003263 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
3265 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003266 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3268 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003269 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003270 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003271}
3272
3273TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003274 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3275 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3276 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3277 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003278
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003279 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003280 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003281
Michael Wrighta9cf4192022-12-01 23:46:39 +00003282 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003283 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3284 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3285 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3286 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3287 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3288 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3289 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3290 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3291}
3292
3293TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003294 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3295 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3296 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3297 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003298
Michael Wrightd02c5b62014-02-10 15:10:22 -08003299 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003300 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003301 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003302
Michael Wrighta9cf4192022-12-01 23:46:39 +00003303 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003304 ASSERT_NO_FATAL_FAILURE(
3305 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3306 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3307 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3308 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3309 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3310 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3311 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003312
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003313 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003314 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003315 ASSERT_NO_FATAL_FAILURE(
3316 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3317 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3318 AKEYCODE_DPAD_UP, DISPLAY_ID));
3319 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3320 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3321 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3322 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003323
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003324 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003325 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003326 ASSERT_NO_FATAL_FAILURE(
3327 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3328 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3329 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3330 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3331 AKEYCODE_DPAD_UP, DISPLAY_ID));
3332 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3333 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003334
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003335 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003336 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003337 ASSERT_NO_FATAL_FAILURE(
3338 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3339 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3340 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3341 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3342 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3343 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3344 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003345
3346 // Special case: if orientation changes while key is down, we still emit the same keycode
3347 // in the key up as we did in the key down.
3348 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003349 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003350 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003351 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3353 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3354 ASSERT_EQ(KEY_UP, args.scanCode);
3355 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3356
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003357 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003358 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003359 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3361 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3362 ASSERT_EQ(KEY_UP, args.scanCode);
3363 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3364}
3365
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003366TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3367 // If the keyboard is not orientation aware,
3368 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003369 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003370
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003371 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003372 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003373 NotifyKeyArgs args;
3374
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003375 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003376 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003377 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003378 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003379 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003380 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003381
Michael Wrighta9cf4192022-12-01 23:46:39 +00003382 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003383 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003385 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003387 ASSERT_EQ(ui::LogicalDisplayId::INVALID, args.displayId);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003388}
3389
3390TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3391 // If the keyboard is orientation aware,
3392 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003393 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003394
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003395 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003396 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003397 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003398 NotifyKeyArgs args;
3399
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07003400 // Display id should be LogicalDisplayId::INVALID without any display configuration.
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003401 // ^--- already checked by the previous test
3402
Michael Wrighta9cf4192022-12-01 23:46:39 +00003403 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003404 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003407 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3409 ASSERT_EQ(DISPLAY_ID, args.displayId);
3410
Linnan Li13bf76a2024-05-05 19:18:02 +08003411 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003412 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003413 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003414 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003415 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003416 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003418 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3419 ASSERT_EQ(newDisplayId, args.displayId);
3420}
3421
Michael Wrightd02c5b62014-02-10 15:10:22 -08003422TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003424 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003426 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003427 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003429 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431}
3432
Philip Junker4af3b3d2021-12-14 10:36:55 +01003433TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3434 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003435 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Philip Junker4af3b3d2021-12-14 10:36:55 +01003436
3437 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3438 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3439 << "If a mapping is available, the result is equal to the mapping";
3440
3441 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3442 << "If no mapping is available, the result is the key location";
3443}
3444
Michael Wrightd02c5b62014-02-10 15:10:22 -08003445TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003446 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003447 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003448
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003449 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003450 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003452 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003453 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003454}
3455
3456TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003457 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003458 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003459
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003460 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003461
Michael Wrightd02c5b62014-02-10 15:10:22 -08003462 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003463 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003464 ASSERT_TRUE(flags[0]);
3465 ASSERT_FALSE(flags[1]);
3466}
3467
3468TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003469 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3470 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3471 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3472 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3473 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3474 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003475
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003477 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003478 // Initial metastate is AMETA_NONE.
3479 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003480
3481 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003482 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3483 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3484 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003485
3486 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003487 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3488 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003489 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3490 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3491 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003492 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003493
3494 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003495 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003497 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3498 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3499 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003500 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003501
3502 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003503 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3504 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003505 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3506 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3507 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003508 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
3510 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003511 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3512 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003513 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3514 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3515 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003516 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003517
3518 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003519 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003521 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3522 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3523 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003524 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003525
3526 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003527 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3528 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003529 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3530 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3531 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003532 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003533}
3534
Arthur Hung2c9a3342019-07-23 14:18:59 +08003535TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3536 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003537 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3538 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3539 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3540 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003541
3542 // keyboard 2.
3543 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003544 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003545 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003546 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003547 std::shared_ptr<InputDevice> device2 =
3548 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003549 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003550
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003551 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3552 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3553 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3554 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003555
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003556 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003557 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003558
Arpit Singh67ca6842023-04-26 14:43:16 +00003559 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003560 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003561 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3562 mFakePolicy
3563 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003564 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003565 std::list<NotifyArgs> unused =
3566 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003567 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003568 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003569
3570 // Prepared displays and associated info.
3571 constexpr uint8_t hdmi1 = 0;
3572 constexpr uint8_t hdmi2 = 1;
3573 const std::string SECONDARY_UNIQUE_ID = "local:1";
3574
3575 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3576 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3577
3578 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003579 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003580 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003581 ASSERT_FALSE(device2->isEnabled());
3582
3583 // Prepare second display.
Linnan Li13bf76a2024-05-05 19:18:02 +08003584 constexpr ui::LogicalDisplayId newDisplayId = ui::LogicalDisplayId{2};
Michael Wrighta9cf4192022-12-01 23:46:39 +00003585 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003586 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003587 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003588 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003589 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003590 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003591 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003592
3593 // Device should be enabled after the associated display is found.
3594 ASSERT_TRUE(mDevice->isEnabled());
3595 ASSERT_TRUE(device2->isEnabled());
3596
3597 // Test pad key events
3598 ASSERT_NO_FATAL_FAILURE(
3599 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3600 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3601 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3602 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3603 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3604 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3605 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3606
3607 ASSERT_NO_FATAL_FAILURE(
3608 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3609 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3610 AKEYCODE_DPAD_RIGHT, newDisplayId));
3611 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3612 AKEYCODE_DPAD_DOWN, newDisplayId));
3613 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3614 AKEYCODE_DPAD_LEFT, newDisplayId));
3615}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003616
arthurhungc903df12020-08-11 15:08:42 +08003617TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3618 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3619 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3620 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3621 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3622 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3623 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3624
3625 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003626 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003627 // Initial metastate is AMETA_NONE.
3628 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003629
3630 // Initialization should have turned all of the lights off.
3631 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3632 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3633 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3634
3635 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3637 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003638 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3639 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3640
3641 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003642 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3643 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003644 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3645 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3646
3647 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003648 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003650 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3651 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3652
3653 mFakeEventHub->removeDevice(EVENTHUB_ID);
3654 mReader->loopOnce();
3655
3656 // keyboard 2 should default toggle keys.
3657 const std::string USB2 = "USB2";
3658 const std::string DEVICE_NAME2 = "KEYBOARD2";
3659 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3660 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3661 std::shared_ptr<InputDevice> device2 =
3662 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003663 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003664 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3665 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3666 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3667 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3668 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3669 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3670
Arpit Singh67ca6842023-04-26 14:43:16 +00003671 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003672 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003673 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3674 mFakePolicy
3675 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003676 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003677 std::list<NotifyArgs> unused =
3678 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003679 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003680 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003681
3682 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3683 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3684 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003685 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3686 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003687}
3688
Arthur Hungcb40a002021-08-03 14:31:01 +00003689TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3690 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3691 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3692 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3693
3694 // Suppose we have two mappers. (DPAD + KEYBOARD)
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003695 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
Arthur Hungcb40a002021-08-03 14:31:01 +00003696 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003697 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung95f68612022-04-07 14:08:22 +08003698 // Initial metastate is AMETA_NONE.
3699 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003700
3701 mReader->toggleCapsLockState(DEVICE_ID);
3702 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3703}
3704
Arthur Hungfb3cc112022-04-13 07:39:50 +00003705TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3706 // keyboard 1.
3707 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3708 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3709 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3710 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3711 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3712 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3713
3714 KeyboardInputMapper& mapper1 =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003715 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003716
3717 // keyboard 2.
3718 const std::string USB2 = "USB2";
3719 const std::string DEVICE_NAME2 = "KEYBOARD2";
3720 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3721 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3722 std::shared_ptr<InputDevice> device2 =
3723 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3724 ftl::Flags<InputDeviceClass>(0));
3725 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3726 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3727 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3728 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3729 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3730 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3731
Arpit Singh67ca6842023-04-26 14:43:16 +00003732 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003733 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003734 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3735 mFakePolicy
3736 ->getReaderConfiguration(),
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003737 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003738 std::list<NotifyArgs> unused =
3739 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003740 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003741 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003742
Arthur Hung95f68612022-04-07 14:08:22 +08003743 // Initial metastate is AMETA_NONE.
3744 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3745 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3746
3747 // Toggle num lock on and off.
3748 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3749 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003750 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3751 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3752 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3753
3754 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3755 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3756 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3757 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3758 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3759
3760 // Toggle caps lock on and off.
3761 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3762 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3763 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3764 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3765 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3766
3767 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3768 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3769 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3770 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3771 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3772
3773 // Toggle scroll lock on and off.
3774 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3775 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3776 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3777 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
3778 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
3779
3780 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3781 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
3782 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3783 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3784 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3785}
3786
Arthur Hung2141d542022-08-23 07:45:21 +00003787TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
3788 const int32_t USAGE_A = 0x070004;
3789 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3790 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
3791
3792 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003793 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Arthur Hung2141d542022-08-23 07:45:21 +00003794 // Key down by scan code.
3795 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3796 NotifyKeyArgs args;
3797 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3798 ASSERT_EQ(DEVICE_ID, args.deviceId);
3799 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3800 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3801 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3802 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3803 ASSERT_EQ(KEY_HOME, args.scanCode);
3804 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3805
3806 // Disable device, it should synthesize cancellation events for down events.
3807 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003808 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00003809
3810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3811 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3812 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3813 ASSERT_EQ(KEY_HOME, args.scanCode);
3814 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
3815}
3816
Zixuan Qufecb6062022-11-12 04:44:31 +00003817TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003818 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Zixuan Qufecb6062022-11-12 04:44:31 +00003819 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003820 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3821 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00003822
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003823 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00003824 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3825
3826 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003827 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00003828
3829 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
3830 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
3831 deviceInfo.getKeyboardLayoutInfo()->languageTag);
3832 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
3833 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00003834 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
3835
3836 // Call change layout association with the same values: Generation shouldn't change
3837 generation = mReader->getContext()->getGeneration();
3838 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
3839 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
3840 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
3841 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00003842}
3843
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003844TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
3845 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
3846 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
3847
3848 // Configuration
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003849 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003850 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003851 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00003852
3853 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
3854 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
3855}
3856
Justin Chung71ddb432023-03-27 04:29:07 +00003857TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
3858 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
3859 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003860 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Justin Chung71ddb432023-03-27 04:29:07 +00003861 NotifyKeyArgs args;
3862
3863 // Key down
3864 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
3865 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3866 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
3867}
3868
Henry Barnora7ed6372024-10-21 14:33:43 -07003869TEST_F_WITH_FLAGS(KeyboardInputMapperTest, WakeBehavior_AlphabeticKeyboard,
3870 REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
3871 enable_alphabetic_keyboard_wake))) {
3872 // For internal alphabetic devices, keys will trigger wake on key down.
3873
3874 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3875 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3876 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE, 0);
3877
3878 KeyboardInputMapper& mapper =
3879 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
3880
3881 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3882 NotifyKeyArgs args;
3883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3884 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3885
3886 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3887 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3888 ASSERT_EQ(uint32_t(0), args.policyFlags);
3889
3890 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
3891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3892 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3893
3894 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
3895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3896 ASSERT_EQ(uint32_t(0), args.policyFlags);
3897
3898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
3899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3900 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3901
3902 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
3903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3904 ASSERT_EQ(uint32_t(0), args.policyFlags);
3905}
3906
Prabir Pradhan38636652024-07-23 21:59:36 +00003907/**
3908 * When there is more than one KeyboardInputMapper for an InputDevice, each mapper should produce
3909 * events that use the shared keyboard source across all mappers. This is to ensure that each
3910 * input device generates key events in a consistent manner, regardless of which mapper produces
3911 * the event.
3912 */
3913TEST_F(KeyboardInputMapperTest, UsesSharedKeyboardSource) {
3914 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3915
3916 // Add a mapper with SOURCE_KEYBOARD
3917 KeyboardInputMapper& keyboardMapper =
3918 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
3919
3920 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
3921 ASSERT_NO_FATAL_FAILURE(
3922 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
3923 process(keyboardMapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
3924 ASSERT_NO_FATAL_FAILURE(
3925 mFakeListener->assertNotifyKeyWasCalled(WithSource(AINPUT_SOURCE_KEYBOARD)));
3926
3927 // Add a mapper with SOURCE_DPAD
3928 KeyboardInputMapper& dpadMapper =
3929 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD);
3930 for (auto* mapper : {&keyboardMapper, &dpadMapper}) {
3931 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
3932 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
3933 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
3934 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
3935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
3936 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD)));
3937 }
3938
3939 // Add a mapper with SOURCE_GAMEPAD
3940 KeyboardInputMapper& gamepadMapper =
3941 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_GAMEPAD);
3942 for (auto* mapper : {&keyboardMapper, &dpadMapper, &gamepadMapper}) {
3943 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 1);
3944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
3945 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
3946 process(*mapper, ARBITRARY_TIME, 0, EV_KEY, KEY_HOME, 0);
3947 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(
3948 WithSource(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_DPAD | AINPUT_SOURCE_GAMEPAD)));
3949 }
3950}
3951
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003952// --- KeyboardInputMapperTest_ExternalAlphabeticDevice ---
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003953
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003954class KeyboardInputMapperTest_ExternalAlphabeticDevice : public InputMapperTest {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003955protected:
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003956 void SetUp() override {
3957 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3958 InputDeviceClass::ALPHAKEY | InputDeviceClass::EXTERNAL);
3959 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003960};
3961
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003962// --- KeyboardInputMapperTest_ExternalNonAlphabeticDevice ---
3963
3964class KeyboardInputMapperTest_ExternalNonAlphabeticDevice : public InputMapperTest {
3965protected:
3966 void SetUp() override {
3967 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::KEYBOARD |
3968 InputDeviceClass::EXTERNAL);
3969 }
3970};
3971
3972TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, WakeBehavior_AlphabeticKeyboard) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003973 // For external devices, keys will trigger wake on key down. Media keys should also trigger
3974 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07003975
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003976 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
3977 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
3978 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
3979 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07003980
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003981 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00003982 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07003983
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003984 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003985 NotifyKeyArgs args;
3986 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3987 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3988
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003989 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3991 ASSERT_EQ(uint32_t(0), args.policyFlags);
3992
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003993 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07003994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00003995 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07003996
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003997 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07003998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3999 ASSERT_EQ(uint32_t(0), args.policyFlags);
4000
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004001 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4003 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4004
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004005 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4007 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4008}
4009
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004010TEST_F(KeyboardInputMapperTest_ExternalNonAlphabeticDevice, WakeBehavior_NonAlphabeticKeyboard) {
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004011 // For external devices, keys will trigger wake on key down. Media keys should not trigger
4012 // wake if triggered from external non-alphaebtic keyboard (e.g. headsets).
4013
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4015 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4016 POLICY_FLAG_WAKE);
4017
4018 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004019 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Vaibhav Devmurari2681a812024-01-11 00:15:35 +00004020
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
4022 NotifyKeyArgs args;
4023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4024 ASSERT_EQ(uint32_t(0), args.policyFlags);
4025
4026 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
4027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4028 ASSERT_EQ(uint32_t(0), args.policyFlags);
4029
4030 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
4031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4032 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4033
4034 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
4035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4036 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4037}
4038
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004039TEST_F(KeyboardInputMapperTest_ExternalAlphabeticDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004040 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004041
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004042 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4043 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4044 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004045
Powei Fengd041c5d2019-05-03 17:11:33 -07004046 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004047 KeyboardInputMapper& mapper =
Vaibhav Devmurarie58ffb92024-05-22 17:38:25 +00004048 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD);
Powei Fengd041c5d2019-05-03 17:11:33 -07004049
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004050 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004051 NotifyKeyArgs args;
4052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4053 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4054
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004055 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4057 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4058
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4061 ASSERT_EQ(uint32_t(0), args.policyFlags);
4062
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004063 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4065 ASSERT_EQ(uint32_t(0), args.policyFlags);
4066
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004067 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4069 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4070
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004071 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4073 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4074}
4075
Michael Wrightd02c5b62014-02-10 15:10:22 -08004076// --- TouchInputMapperTest ---
4077
4078class TouchInputMapperTest : public InputMapperTest {
4079protected:
4080 static const int32_t RAW_X_MIN;
4081 static const int32_t RAW_X_MAX;
4082 static const int32_t RAW_Y_MIN;
4083 static const int32_t RAW_Y_MAX;
4084 static const int32_t RAW_TOUCH_MIN;
4085 static const int32_t RAW_TOUCH_MAX;
4086 static const int32_t RAW_TOOL_MIN;
4087 static const int32_t RAW_TOOL_MAX;
4088 static const int32_t RAW_PRESSURE_MIN;
4089 static const int32_t RAW_PRESSURE_MAX;
4090 static const int32_t RAW_ORIENTATION_MIN;
4091 static const int32_t RAW_ORIENTATION_MAX;
4092 static const int32_t RAW_DISTANCE_MIN;
4093 static const int32_t RAW_DISTANCE_MAX;
4094 static const int32_t RAW_TILT_MIN;
4095 static const int32_t RAW_TILT_MAX;
4096 static const int32_t RAW_ID_MIN;
4097 static const int32_t RAW_ID_MAX;
4098 static const int32_t RAW_SLOT_MIN;
4099 static const int32_t RAW_SLOT_MAX;
4100 static const float X_PRECISION;
4101 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004102 static const float X_PRECISION_VIRTUAL;
4103 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004104
4105 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07004106 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004107
4108 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
4109
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004110 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004111 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004112
Michael Wrightd02c5b62014-02-10 15:10:22 -08004113 enum Axes {
4114 POSITION = 1 << 0,
4115 TOUCH = 1 << 1,
4116 TOOL = 1 << 2,
4117 PRESSURE = 1 << 3,
4118 ORIENTATION = 1 << 4,
4119 MINOR = 1 << 5,
4120 ID = 1 << 6,
4121 DISTANCE = 1 << 7,
4122 TILT = 1 << 8,
4123 SLOT = 1 << 9,
4124 TOOL_TYPE = 1 << 10,
4125 };
4126
Michael Wrighta9cf4192022-12-01 23:46:39 +00004127 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004128 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00004129 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004130 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07004131 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004132 int32_t toRawX(float displayX);
4133 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004134 int32_t toRotatedRawX(float displayX);
4135 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07004136 float toCookedX(float rawX, float rawY);
4137 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004138 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004139 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004140 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004141 float toDisplayY(int32_t rawY, int32_t displayHeight);
4142
Michael Wrightd02c5b62014-02-10 15:10:22 -08004143};
4144
4145const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
4146const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
4147const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
4148const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
4149const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
4150const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
4151const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
4152const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00004153const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
4154const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004155const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
4156const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
4157const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
4158const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
4159const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
4160const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
4161const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
4162const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
4163const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
4164const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
4165const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
4166const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07004167const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
4168 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
4169const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
4170 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07004171const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
4172 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004173
4174const float TouchInputMapperTest::GEOMETRIC_SCALE =
4175 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
4176 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
4177
4178const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
4179 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
4180 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
4181};
4182
Michael Wrighta9cf4192022-12-01 23:46:39 +00004183void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004184 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
4185 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004186}
4187
4188void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
4189 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004190 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191}
4192
Michael Wrighta9cf4192022-12-01 23:46:39 +00004193void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01004194 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
4195 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
4196 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004197}
4198
Michael Wrightd02c5b62014-02-10 15:10:22 -08004199void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004200 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
4201 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
4202 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4203 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004204}
4205
Jason Gerecke489fda82012-09-07 17:19:40 -07004206void TouchInputMapperTest::prepareLocationCalibration() {
4207 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
4208}
4209
Michael Wrightd02c5b62014-02-10 15:10:22 -08004210int32_t TouchInputMapperTest::toRawX(float displayX) {
4211 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
4212}
4213
4214int32_t TouchInputMapperTest::toRawY(float displayY) {
4215 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
4216}
4217
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004218int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
4219 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
4220}
4221
4222int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
4223 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
4224}
4225
Jason Gerecke489fda82012-09-07 17:19:40 -07004226float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
4227 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4228 return rawX;
4229}
4230
4231float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
4232 AFFINE_TRANSFORM.applyTo(rawX, rawY);
4233 return rawY;
4234}
4235
Michael Wrightd02c5b62014-02-10 15:10:22 -08004236float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004237 return toDisplayX(rawX, DISPLAY_WIDTH);
4238}
4239
4240float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
4241 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004242}
4243
4244float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004245 return toDisplayY(rawY, DISPLAY_HEIGHT);
4246}
4247
4248float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
4249 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004250}
4251
4252
4253// --- SingleTouchInputMapperTest ---
4254
4255class SingleTouchInputMapperTest : public TouchInputMapperTest {
4256protected:
4257 void prepareButtons();
4258 void prepareAxes(int axes);
4259
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004260 std::list<NotifyArgs> processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4261 std::list<NotifyArgs> processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
4262 std::list<NotifyArgs> processUp(SingleTouchInputMapper& mappery);
4263 std::list<NotifyArgs> processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
4264 std::list<NotifyArgs> processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
4265 std::list<NotifyArgs> processDistance(SingleTouchInputMapper& mapper, int32_t distance);
4266 std::list<NotifyArgs> processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
4267 std::list<NotifyArgs> processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
4268 std::list<NotifyArgs> processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004269};
4270
4271void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004272 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004273}
4274
4275void SingleTouchInputMapperTest::prepareAxes(int axes) {
4276 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004277 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
4278 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279 }
4280 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004281 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
4282 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004283 }
4284 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004285 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
4286 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004287 }
4288 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004289 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
4290 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004291 }
4292 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004293 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
4294 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004295 }
4296}
4297
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004298std::list<NotifyArgs> SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper,
4299 int32_t x, int32_t y) {
4300 std::list<NotifyArgs> args;
4301 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
4302 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4303 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4304 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004305}
4306
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004307std::list<NotifyArgs> SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper,
4308 int32_t x, int32_t y) {
4309 std::list<NotifyArgs> args;
4310 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
4311 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
4312 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004313}
4314
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004315std::list<NotifyArgs> SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
4316 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004317}
4318
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004319std::list<NotifyArgs> SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper,
4320 int32_t pressure) {
4321 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004322}
4323
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004324std::list<NotifyArgs> SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
4325 int32_t toolMajor) {
4326 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004327}
4328
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004329std::list<NotifyArgs> SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper,
4330 int32_t distance) {
4331 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332}
4333
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004334std::list<NotifyArgs> SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper,
4335 int32_t tiltX, int32_t tiltY) {
4336 std::list<NotifyArgs> args;
4337 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
4338 args += process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
4339 return args;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004340}
4341
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004342std::list<NotifyArgs> SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper,
4343 int32_t code, int32_t value) {
4344 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004345}
4346
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004347std::list<NotifyArgs> SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
4348 return process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004349}
4350
Michael Wrightd02c5b62014-02-10 15:10:22 -08004351TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004352 prepareButtons();
4353 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00004354 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004355
Josep del Río2d8c79a2023-01-23 19:33:50 +00004356 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357}
4358
Michael Wrightd02c5b62014-02-10 15:10:22 -08004359TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 prepareButtons();
4361 prepareAxes(POSITION);
4362 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00004363 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004364
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004365 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004366}
4367
4368TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004369 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004370 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004371 prepareButtons();
4372 prepareAxes(POSITION);
4373 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004374 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004375
4376 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004377 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004378
4379 // Virtual key is down.
4380 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4381 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4382 processDown(mapper, x, y);
4383 processSync(mapper);
4384 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4385
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004386 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004387
4388 // Virtual key is up.
4389 processUp(mapper);
4390 processSync(mapper);
4391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4392
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004393 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004394}
4395
4396TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004397 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004398 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004399 prepareButtons();
4400 prepareAxes(POSITION);
4401 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004402 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004403
4404 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004405 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406
4407 // Virtual key is down.
4408 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4409 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4410 processDown(mapper, x, y);
4411 processSync(mapper);
4412 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4413
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004414 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415
4416 // Virtual key is up.
4417 processUp(mapper);
4418 processSync(mapper);
4419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
4420
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004421 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422}
4423
4424TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004425 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004426 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004427 prepareButtons();
4428 prepareAxes(POSITION);
4429 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004430 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004431
Michael Wrightd02c5b62014-02-10 15:10:22 -08004432 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07004433 ASSERT_TRUE(
4434 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004435 ASSERT_TRUE(flags[0]);
4436 ASSERT_FALSE(flags[1]);
4437}
4438
Biswarup Pal9ce4d9f2024-06-14 15:05:27 +00004439TEST_F(SingleTouchInputMapperTest, DeviceTypeChange_RecalculatesRawToDisplayTransform) {
4440 prepareDisplay(ui::ROTATION_0);
4441 prepareAxes(POSITION);
4442 addConfigurationProperty("touch.deviceType", "touchScreen");
4443 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
4444
4445 const int32_t x = 900;
4446 const int32_t y = 75;
4447 std::list<NotifyArgs> args;
4448 args += processDown(mapper, x, y);
4449 args += processSync(mapper);
4450
4451 // Assert that motion event is received in display coordinate space for deviceType touchScreen.
4452 ASSERT_THAT(args,
4453 ElementsAre(VariantWith<NotifyMotionArgs>(
4454 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4455 WithCoords(toDisplayX(x), toDisplayY(y))))));
4456
4457 // Add device type association after the device was created.
4458 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
4459 // Send update to the mapper.
4460 std::list<NotifyArgs> unused =
4461 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4462 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
4463
4464 args.clear();
4465 args += processDown(mapper, x, y);
4466 args += processSync(mapper);
4467
4468 // Assert that motion event is received in raw coordinate space for deviceType touchNavigation.
4469 ASSERT_THAT(args,
4470 ElementsAre(VariantWith<NotifyMotionArgs>(
4471 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
4472 WithCoords(x - RAW_X_MIN, y - RAW_Y_MIN)))));
4473}
4474
Michael Wrightd02c5b62014-02-10 15:10:22 -08004475TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004476 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004477 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004478 prepareButtons();
4479 prepareAxes(POSITION);
4480 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004481 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
arthurhungdcef2dc2020-08-11 14:47:50 +08004483 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004484
4485 NotifyKeyArgs args;
4486
4487 // Press virtual key.
4488 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4489 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4490 processDown(mapper, x, y);
4491 processSync(mapper);
4492
4493 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4494 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4495 ASSERT_EQ(DEVICE_ID, args.deviceId);
4496 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4497 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4498 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4499 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4500 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4501 ASSERT_EQ(KEY_HOME, args.scanCode);
4502 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4503 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4504
4505 // Release virtual key.
4506 processUp(mapper);
4507 processSync(mapper);
4508
4509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4510 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4511 ASSERT_EQ(DEVICE_ID, args.deviceId);
4512 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4513 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
4514 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4515 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
4516 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4517 ASSERT_EQ(KEY_HOME, args.scanCode);
4518 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4519 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4520
4521 // Should not have sent any motions.
4522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4523}
4524
4525TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
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
arthurhungdcef2dc2020-08-11 14:47:50 +08004533 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004534
4535 NotifyKeyArgs keyArgs;
4536
4537 // Press virtual key.
4538 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
4539 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
4540 processDown(mapper, x, y);
4541 processSync(mapper);
4542
4543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4544 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4545 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4546 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4547 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4548 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4549 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
4550 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4551 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4552 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4553 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4554
4555 // Move out of bounds. This should generate a cancel and a pointer down since we moved
4556 // into the display area.
4557 y -= 100;
4558 processMove(mapper, x, y);
4559 processSync(mapper);
4560
4561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4562 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
4563 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
4564 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
4565 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
4566 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4567 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
4568 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
4569 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
4570 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
4571 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
4572 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
4573
4574 NotifyMotionArgs motionArgs;
4575 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4576 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4577 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4578 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4579 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4581 ASSERT_EQ(0, motionArgs.flags);
4582 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4583 ASSERT_EQ(0, motionArgs.buttonState);
4584 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004585 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004586 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004587 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004588 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4589 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4590 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4591 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4592 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4593
4594 // Keep moving out of bounds. Should generate a pointer move.
4595 y -= 50;
4596 processMove(mapper, x, y);
4597 processSync(mapper);
4598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4600 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4601 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4602 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4603 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4604 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4605 ASSERT_EQ(0, motionArgs.flags);
4606 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4607 ASSERT_EQ(0, motionArgs.buttonState);
4608 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004609 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004611 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004612 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4613 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4614 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4615 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4616 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4617
4618 // Release out of bounds. Should generate a pointer up.
4619 processUp(mapper);
4620 processSync(mapper);
4621
4622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4624 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4625 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4626 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4627 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4628 ASSERT_EQ(0, motionArgs.flags);
4629 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4630 ASSERT_EQ(0, motionArgs.buttonState);
4631 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004632 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004633 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004634 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004635 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4636 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4637 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4638 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4639 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4640
4641 // Should not have sent any more keys or motions.
4642 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4644}
4645
4646TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
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 NotifyMotionArgs motionArgs;
4657
4658 // Initially go down out of bounds.
4659 int32_t x = -10;
4660 int32_t y = -10;
4661 processDown(mapper, x, y);
4662 processSync(mapper);
4663
4664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4665
4666 // Move into the display area. Should generate a pointer down.
4667 x = 50;
4668 y = 75;
4669 processMove(mapper, x, y);
4670 processSync(mapper);
4671
4672 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4673 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4674 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4675 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4676 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4677 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4678 ASSERT_EQ(0, motionArgs.flags);
4679 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4680 ASSERT_EQ(0, motionArgs.buttonState);
4681 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004682 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004683 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004684 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004685 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4686 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4687 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4688 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4689 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4690
4691 // Release. Should generate a pointer up.
4692 processUp(mapper);
4693 processSync(mapper);
4694
4695 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4696 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4697 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4698 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4699 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4700 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4701 ASSERT_EQ(0, motionArgs.flags);
4702 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4703 ASSERT_EQ(0, motionArgs.buttonState);
4704 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004705 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004706 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004707 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4709 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4710 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4711 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4713
4714 // Should not have sent any more keys or motions.
4715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4717}
4718
Santos Cordonfa5cf462017-04-05 10:37:00 -07004719TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07004720 addConfigurationProperty("touch.deviceType", "touchScreen");
4721 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
4722
Michael Wrighta9cf4192022-12-01 23:46:39 +00004723 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004724 prepareButtons();
4725 prepareAxes(POSITION);
4726 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004727 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07004728
arthurhungdcef2dc2020-08-11 14:47:50 +08004729 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004730
4731 NotifyMotionArgs motionArgs;
4732
4733 // Down.
4734 int32_t x = 100;
4735 int32_t y = 125;
4736 processDown(mapper, x, y);
4737 processSync(mapper);
4738
4739 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4740 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4741 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4742 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4743 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4744 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4745 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4746 ASSERT_EQ(0, motionArgs.flags);
4747 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4748 ASSERT_EQ(0, motionArgs.buttonState);
4749 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004750 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004751 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004752 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004753 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4754 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4755 1, 0, 0, 0, 0, 0, 0, 0));
4756 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4757 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4758 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4759
4760 // Move.
4761 x += 50;
4762 y += 75;
4763 processMove(mapper, x, y);
4764 processSync(mapper);
4765
4766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4767 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4768 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4769 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4770 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4771 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4772 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4773 ASSERT_EQ(0, motionArgs.flags);
4774 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4775 ASSERT_EQ(0, motionArgs.buttonState);
4776 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004777 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004778 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004779 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004780 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4781 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4782 1, 0, 0, 0, 0, 0, 0, 0));
4783 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4784 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4786
4787 // Up.
4788 processUp(mapper);
4789 processSync(mapper);
4790
4791 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4792 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4793 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4794 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
4795 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4796 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4797 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4798 ASSERT_EQ(0, motionArgs.flags);
4799 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4800 ASSERT_EQ(0, motionArgs.buttonState);
4801 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004802 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07004803 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004804 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07004805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4806 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
4807 1, 0, 0, 0, 0, 0, 0, 0));
4808 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
4809 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
4810 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4811
4812 // Should not have sent any more keys or motions.
4813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4814 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4815}
4816
Michael Wrightd02c5b62014-02-10 15:10:22 -08004817TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004818 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00004819 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004820 prepareButtons();
4821 prepareAxes(POSITION);
4822 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00004823 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004824
arthurhungdcef2dc2020-08-11 14:47:50 +08004825 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
4827 NotifyMotionArgs motionArgs;
4828
4829 // Down.
4830 int32_t x = 100;
4831 int32_t y = 125;
4832 processDown(mapper, x, y);
4833 processSync(mapper);
4834
4835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4836 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4837 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4838 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4839 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4840 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4841 ASSERT_EQ(0, motionArgs.flags);
4842 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4843 ASSERT_EQ(0, motionArgs.buttonState);
4844 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004845 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004846 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004847 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004848 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4849 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4850 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4851 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4852 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4853
4854 // Move.
4855 x += 50;
4856 y += 75;
4857 processMove(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(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4864 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
4866 ASSERT_EQ(0, motionArgs.flags);
4867 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4868 ASSERT_EQ(0, motionArgs.buttonState);
4869 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004870 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004871 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004872 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004873 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4874 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4875 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4876 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4877 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4878
4879 // Up.
4880 processUp(mapper);
4881 processSync(mapper);
4882
4883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4884 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
4885 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
4886 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
4887 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
4888 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
4889 ASSERT_EQ(0, motionArgs.flags);
4890 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
4891 ASSERT_EQ(0, motionArgs.buttonState);
4892 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004893 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004894 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004895 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004896 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
4897 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
4898 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
4899 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
4900 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
4901
4902 // Should not have sent any more keys or motions.
4903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
4904 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
4905}
4906
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004907TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004908 addConfigurationProperty("touch.deviceType", "touchScreen");
4909 prepareButtons();
4910 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004911 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4912 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00004913 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004914
4915 NotifyMotionArgs args;
4916
4917 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004918 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004919 processDown(mapper, toRawX(50), toRawY(75));
4920 processSync(mapper);
4921
4922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4923 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4924 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4925
4926 processUp(mapper);
4927 processSync(mapper);
4928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4929}
4930
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004931TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004932 addConfigurationProperty("touch.deviceType", "touchScreen");
4933 prepareButtons();
4934 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004935 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4936 // orientation-aware are affected by display rotation.
4937 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00004938 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004939
4940 NotifyMotionArgs args;
4941
4942 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004943 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004944 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004945 processDown(mapper, toRawX(50), toRawY(75));
4946 processSync(mapper);
4947
4948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4949 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4950 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4951
4952 processUp(mapper);
4953 processSync(mapper);
4954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4955
4956 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004957 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004958 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00004959 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004960 processSync(mapper);
4961
4962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4963 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4964 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4965
4966 processUp(mapper);
4967 processSync(mapper);
4968 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4969
4970 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004971 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004972 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004973 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
4974 processSync(mapper);
4975
4976 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4977 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4978 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4979
4980 processUp(mapper);
4981 processSync(mapper);
4982 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4983
4984 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07004985 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004986 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00004987 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004988 processSync(mapper);
4989
4990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4991 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
4992 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
4993
4994 processUp(mapper);
4995 processSync(mapper);
4996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
4997}
4998
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07004999TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
5000 addConfigurationProperty("touch.deviceType", "touchScreen");
5001 prepareButtons();
5002 prepareAxes(POSITION);
5003 addConfigurationProperty("touch.orientationAware", "1");
5004 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
5005 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005006 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005007 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005008 NotifyMotionArgs args;
5009
5010 // Orientation 0.
5011 processDown(mapper, toRawX(50), toRawY(75));
5012 processSync(mapper);
5013
5014 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5015 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5016 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5017
5018 processUp(mapper);
5019 processSync(mapper);
5020 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5021}
5022
5023TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
5024 addConfigurationProperty("touch.deviceType", "touchScreen");
5025 prepareButtons();
5026 prepareAxes(POSITION);
5027 addConfigurationProperty("touch.orientationAware", "1");
5028 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
5029 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005030 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005031 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005032 NotifyMotionArgs args;
5033
5034 // Orientation 90.
5035 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5036 processSync(mapper);
5037
5038 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5039 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5040 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5041
5042 processUp(mapper);
5043 processSync(mapper);
5044 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5045}
5046
5047TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
5048 addConfigurationProperty("touch.deviceType", "touchScreen");
5049 prepareButtons();
5050 prepareAxes(POSITION);
5051 addConfigurationProperty("touch.orientationAware", "1");
5052 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
5053 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005054 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005055 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005056 NotifyMotionArgs args;
5057
5058 // Orientation 180.
5059 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
5060 processSync(mapper);
5061
5062 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5063 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5064 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5065
5066 processUp(mapper);
5067 processSync(mapper);
5068 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5069}
5070
5071TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
5072 addConfigurationProperty("touch.deviceType", "touchScreen");
5073 prepareButtons();
5074 prepareAxes(POSITION);
5075 addConfigurationProperty("touch.orientationAware", "1");
5076 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
5077 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005078 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005079 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005080 NotifyMotionArgs args;
5081
5082 // Orientation 270.
5083 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5084 processSync(mapper);
5085
5086 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5087 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5088 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5089
5090 processUp(mapper);
5091 processSync(mapper);
5092 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5093}
5094
5095TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
5096 addConfigurationProperty("touch.deviceType", "touchScreen");
5097 prepareButtons();
5098 prepareAxes(POSITION);
5099 // Since InputReader works in the un-rotated coordinate space, only devices that are not
5100 // orientation-aware are affected by display rotation.
5101 addConfigurationProperty("touch.orientationAware", "0");
5102 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00005103 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005104
5105 NotifyMotionArgs args;
5106
5107 // Orientation 90, Rotation 0.
5108 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005109 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005110 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
5111 processSync(mapper);
5112
5113 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5114 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5115 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5116
5117 processUp(mapper);
5118 processSync(mapper);
5119 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5120
5121 // Orientation 90, Rotation 90.
5122 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005123 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005124 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005125 processSync(mapper);
5126
5127 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5128 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5129 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5130
5131 processUp(mapper);
5132 processSync(mapper);
5133 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5134
5135 // Orientation 90, Rotation 180.
5136 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005137 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005138 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
5139 processSync(mapper);
5140
5141 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5142 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5143 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5144
5145 processUp(mapper);
5146 processSync(mapper);
5147 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5148
5149 // Orientation 90, Rotation 270.
5150 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00005151 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00005152 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 -07005153 processSync(mapper);
5154
5155 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5156 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5157 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5158
5159 processUp(mapper);
5160 processSync(mapper);
5161 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5162}
5163
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005164TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
5165 addConfigurationProperty("touch.deviceType", "touchScreen");
5166 prepareButtons();
5167 prepareAxes(POSITION);
5168 addConfigurationProperty("touch.orientationAware", "1");
5169 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005170 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005171
5172 // Set a physical frame in the display viewport.
5173 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5174 viewport->physicalLeft = 20;
5175 viewport->physicalTop = 600;
5176 viewport->physicalRight = 30;
5177 viewport->physicalBottom = 610;
5178 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005179 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00005180
5181 // Start the touch.
5182 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5183 processSync(mapper);
5184
5185 // Expect all input starting outside the physical frame to be ignored.
5186 const std::array<Point, 6> outsidePoints = {
5187 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5188 for (const auto& p : outsidePoints) {
5189 processMove(mapper, toRawX(p.x), toRawY(p.y));
5190 processSync(mapper);
5191 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5192 }
5193
5194 // Move the touch into the physical frame.
5195 processMove(mapper, toRawX(25), toRawY(605));
5196 processSync(mapper);
5197 NotifyMotionArgs args;
5198 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5199 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
5200 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5201 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5202
5203 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
5204 for (const auto& p : outsidePoints) {
5205 processMove(mapper, toRawX(p.x), toRawY(p.y));
5206 processSync(mapper);
5207 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5208 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5209 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
5210 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
5211 }
5212
5213 processUp(mapper);
5214 processSync(mapper);
5215 EXPECT_NO_FATAL_FAILURE(
5216 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
5217}
5218
Harry Cutts1db43992023-06-19 17:05:07 +00005219TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
Harry Cutts1db43992023-06-19 17:05:07 +00005220 addConfigurationProperty("touch.deviceType", "pointer");
5221 prepareAxes(POSITION);
5222 prepareDisplay(ui::ROTATION_0);
5223 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
5224
5225 // Set a physical frame in the display viewport.
5226 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5227 viewport->physicalLeft = 20;
5228 viewport->physicalTop = 600;
5229 viewport->physicalRight = 30;
5230 viewport->physicalBottom = 610;
5231 mFakePolicy->updateViewport(*viewport);
5232 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5233
5234 // Start the touch.
5235 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5236 processSync(mapper);
5237
5238 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
5239 // produced.
5240 const std::array<Point, 6> outsidePoints = {
5241 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
5242 for (const auto& p : outsidePoints) {
5243 processMove(mapper, toRawX(p.x), toRawY(p.y));
5244 processSync(mapper);
5245 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
5246 }
5247}
5248
Michael Wrightd02c5b62014-02-10 15:10:22 -08005249TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005250 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005251 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005252 prepareButtons();
5253 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00005254 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005255
5256 // These calculations are based on the input device calibration documentation.
5257 int32_t rawX = 100;
5258 int32_t rawY = 200;
5259 int32_t rawPressure = 10;
5260 int32_t rawToolMajor = 12;
5261 int32_t rawDistance = 2;
5262 int32_t rawTiltX = 30;
5263 int32_t rawTiltY = 110;
5264
5265 float x = toDisplayX(rawX);
5266 float y = toDisplayY(rawY);
5267 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
5268 float size = float(rawToolMajor) / RAW_TOOL_MAX;
5269 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
5270 float distance = float(rawDistance);
5271
5272 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
5273 float tiltScale = M_PI / 180;
5274 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
5275 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
5276 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
5277 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
5278
5279 processDown(mapper, rawX, rawY);
5280 processPressure(mapper, rawPressure);
5281 processToolMajor(mapper, rawToolMajor);
5282 processDistance(mapper, rawDistance);
5283 processTilt(mapper, rawTiltX, rawTiltY);
5284 processSync(mapper);
5285
5286 NotifyMotionArgs args;
5287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5288 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5289 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
5290 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00005291 ASSERT_EQ(args.flags,
5292 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION |
5293 AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_DIRECTIONAL_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005294}
5295
Jason Gerecke489fda82012-09-07 17:19:40 -07005296TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07005297 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005298 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07005299 prepareLocationCalibration();
5300 prepareButtons();
5301 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005302 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07005303
5304 int32_t rawX = 100;
5305 int32_t rawY = 200;
5306
5307 float x = toDisplayX(toCookedX(rawX, rawY));
5308 float y = toDisplayY(toCookedY(rawX, rawY));
5309
5310 processDown(mapper, rawX, rawY);
5311 processSync(mapper);
5312
5313 NotifyMotionArgs args;
5314 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5315 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
5316 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
5317}
5318
Michael Wrightd02c5b62014-02-10 15:10:22 -08005319TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005320 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005321 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005322 prepareButtons();
5323 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005324 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005325
5326 NotifyMotionArgs motionArgs;
5327 NotifyKeyArgs keyArgs;
5328
5329 processDown(mapper, 100, 200);
5330 processSync(mapper);
5331 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5332 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5333 ASSERT_EQ(0, motionArgs.buttonState);
5334
5335 // press BTN_LEFT, release BTN_LEFT
5336 processKey(mapper, BTN_LEFT, 1);
5337 processSync(mapper);
5338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5339 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5340 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5341
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5343 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5344 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5345
Michael Wrightd02c5b62014-02-10 15:10:22 -08005346 processKey(mapper, BTN_LEFT, 0);
5347 processSync(mapper);
5348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005349 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005350 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005351
5352 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005353 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005354 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005355
5356 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5357 processKey(mapper, BTN_RIGHT, 1);
5358 processKey(mapper, BTN_MIDDLE, 1);
5359 processSync(mapper);
5360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5361 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5362 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5363 motionArgs.buttonState);
5364
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5367 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5368
5369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5370 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5371 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5372 motionArgs.buttonState);
5373
Michael Wrightd02c5b62014-02-10 15:10:22 -08005374 processKey(mapper, BTN_RIGHT, 0);
5375 processSync(mapper);
5376 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005377 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005378 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005379
5380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005381 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005382 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005383
5384 processKey(mapper, BTN_MIDDLE, 0);
5385 processSync(mapper);
5386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005387 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005388 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005389
5390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005391 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005392 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005393
5394 // press BTN_BACK, release BTN_BACK
5395 processKey(mapper, BTN_BACK, 1);
5396 processSync(mapper);
5397 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5398 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5399 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005400
Michael Wrightd02c5b62014-02-10 15:10:22 -08005401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005402 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005403 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5404
5405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5406 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5407 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005408
5409 processKey(mapper, BTN_BACK, 0);
5410 processSync(mapper);
5411 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005412 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005413 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005414
5415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005417 ASSERT_EQ(0, motionArgs.buttonState);
5418
Michael Wrightd02c5b62014-02-10 15:10:22 -08005419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5420 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5421 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5422
5423 // press BTN_SIDE, release BTN_SIDE
5424 processKey(mapper, BTN_SIDE, 1);
5425 processSync(mapper);
5426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5427 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5428 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005429
Michael Wrightd02c5b62014-02-10 15:10:22 -08005430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005431 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005432 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5433
5434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5436 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005437
5438 processKey(mapper, BTN_SIDE, 0);
5439 processSync(mapper);
5440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005441 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005442 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005443
5444 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005445 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005446 ASSERT_EQ(0, motionArgs.buttonState);
5447
Michael Wrightd02c5b62014-02-10 15:10:22 -08005448 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5449 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5450 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5451
5452 // press BTN_FORWARD, release BTN_FORWARD
5453 processKey(mapper, BTN_FORWARD, 1);
5454 processSync(mapper);
5455 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5456 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5457 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005458
Michael Wrightd02c5b62014-02-10 15:10:22 -08005459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005460 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005461 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5462
5463 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5464 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5465 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005466
5467 processKey(mapper, BTN_FORWARD, 0);
5468 processSync(mapper);
5469 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005470 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005471 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005472
5473 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005474 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005475 ASSERT_EQ(0, motionArgs.buttonState);
5476
Michael Wrightd02c5b62014-02-10 15:10:22 -08005477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5478 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5479 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5480
5481 // press BTN_EXTRA, release BTN_EXTRA
5482 processKey(mapper, BTN_EXTRA, 1);
5483 processSync(mapper);
5484 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5485 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5486 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005487
Michael Wrightd02c5b62014-02-10 15:10:22 -08005488 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005489 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005490 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5491
5492 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5493 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5494 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005495
5496 processKey(mapper, BTN_EXTRA, 0);
5497 processSync(mapper);
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005499 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005500 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005501
5502 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005503 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005504 ASSERT_EQ(0, motionArgs.buttonState);
5505
Michael Wrightd02c5b62014-02-10 15:10:22 -08005506 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5507 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5508 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5509
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
5511
Michael Wrightd02c5b62014-02-10 15:10:22 -08005512 // press BTN_STYLUS, release BTN_STYLUS
5513 processKey(mapper, BTN_STYLUS, 1);
5514 processSync(mapper);
5515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5516 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005517 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
5518
5519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5520 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5521 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005522
5523 processKey(mapper, BTN_STYLUS, 0);
5524 processSync(mapper);
5525 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005526 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005527 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005528
5529 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005530 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005531 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005532
5533 // press BTN_STYLUS2, release BTN_STYLUS2
5534 processKey(mapper, BTN_STYLUS2, 1);
5535 processSync(mapper);
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5537 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005538 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
5539
5540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5541 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5542 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005543
5544 processKey(mapper, BTN_STYLUS2, 0);
5545 processSync(mapper);
5546 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005547 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005548 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005549
5550 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005551 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08005552 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005553
5554 // release touch
5555 processUp(mapper);
5556 processSync(mapper);
5557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5558 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5559 ASSERT_EQ(0, motionArgs.buttonState);
5560}
5561
5562TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005563 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005564 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005565 prepareButtons();
5566 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005567 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005568
5569 NotifyMotionArgs motionArgs;
5570
5571 // default tool type is finger
5572 processDown(mapper, 100, 200);
5573 processSync(mapper);
5574 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5575 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005576 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005577
5578 // eraser
5579 processKey(mapper, BTN_TOOL_RUBBER, 1);
5580 processSync(mapper);
5581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5582 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005583 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005584
5585 // stylus
5586 processKey(mapper, BTN_TOOL_RUBBER, 0);
5587 processKey(mapper, BTN_TOOL_PEN, 1);
5588 processSync(mapper);
5589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005591 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005592
5593 // brush
5594 processKey(mapper, BTN_TOOL_PEN, 0);
5595 processKey(mapper, BTN_TOOL_BRUSH, 1);
5596 processSync(mapper);
5597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005599 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005600
5601 // pencil
5602 processKey(mapper, BTN_TOOL_BRUSH, 0);
5603 processKey(mapper, BTN_TOOL_PENCIL, 1);
5604 processSync(mapper);
5605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5606 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005607 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005608
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08005609 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08005610 processKey(mapper, BTN_TOOL_PENCIL, 0);
5611 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
5612 processSync(mapper);
5613 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5614 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005615 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005616
5617 // mouse
5618 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
5619 processKey(mapper, BTN_TOOL_MOUSE, 1);
5620 processSync(mapper);
5621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005623 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005624
5625 // lens
5626 processKey(mapper, BTN_TOOL_MOUSE, 0);
5627 processKey(mapper, BTN_TOOL_LENS, 1);
5628 processSync(mapper);
5629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5630 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005631 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005632
5633 // double-tap
5634 processKey(mapper, BTN_TOOL_LENS, 0);
5635 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
5636 processSync(mapper);
5637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5638 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005639 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005640
5641 // triple-tap
5642 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
5643 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
5644 processSync(mapper);
5645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5646 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005647 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005648
5649 // quad-tap
5650 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
5651 processKey(mapper, BTN_TOOL_QUADTAP, 1);
5652 processSync(mapper);
5653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5654 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005655 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005656
5657 // finger
5658 processKey(mapper, BTN_TOOL_QUADTAP, 0);
5659 processKey(mapper, BTN_TOOL_FINGER, 1);
5660 processSync(mapper);
5661 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5662 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005663 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005664
5665 // stylus trumps finger
5666 processKey(mapper, BTN_TOOL_PEN, 1);
5667 processSync(mapper);
5668 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5669 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005670 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005671
5672 // eraser trumps stylus
5673 processKey(mapper, BTN_TOOL_RUBBER, 1);
5674 processSync(mapper);
5675 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5676 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005677 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005678
5679 // mouse trumps eraser
5680 processKey(mapper, BTN_TOOL_MOUSE, 1);
5681 processSync(mapper);
5682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07005684 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005685
5686 // back to default tool type
5687 processKey(mapper, BTN_TOOL_MOUSE, 0);
5688 processKey(mapper, BTN_TOOL_RUBBER, 0);
5689 processKey(mapper, BTN_TOOL_PEN, 0);
5690 processKey(mapper, BTN_TOOL_FINGER, 0);
5691 processSync(mapper);
5692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, 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
5697TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005698 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005699 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005700 prepareButtons();
5701 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005702 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00005703 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005704
5705 NotifyMotionArgs motionArgs;
5706
5707 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
5708 processKey(mapper, BTN_TOOL_FINGER, 1);
5709 processMove(mapper, 100, 200);
5710 processSync(mapper);
5711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5712 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5713 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5714 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5715
5716 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5717 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5718 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5719 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5720
5721 // move a little
5722 processMove(mapper, 150, 250);
5723 processSync(mapper);
5724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5725 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5726 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5727 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5728
5729 // down when BTN_TOUCH is pressed, pressure defaults to 1
5730 processKey(mapper, BTN_TOUCH, 1);
5731 processSync(mapper);
5732 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5733 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5734 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5735 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5736
5737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5738 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5739 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5740 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5741
5742 // up when BTN_TOUCH is released, hover restored
5743 processKey(mapper, BTN_TOUCH, 0);
5744 processSync(mapper);
5745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5746 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5747 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5748 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5749
5750 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5751 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5752 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5753 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5754
5755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5756 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5758 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5759
5760 // exit hover when pointer goes away
5761 processKey(mapper, BTN_TOOL_FINGER, 0);
5762 processSync(mapper);
5763 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5764 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5765 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5766 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5767}
5768
5769TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005770 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005771 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005772 prepareButtons();
5773 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005774 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005775
5776 NotifyMotionArgs motionArgs;
5777
5778 // initially hovering because pressure is 0
5779 processDown(mapper, 100, 200);
5780 processPressure(mapper, 0);
5781 processSync(mapper);
5782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5783 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5784 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5785 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5786
5787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5788 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5789 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5790 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
5791
5792 // move a little
5793 processMove(mapper, 150, 250);
5794 processSync(mapper);
5795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5796 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5797 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5798 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5799
5800 // down when pressure is non-zero
5801 processPressure(mapper, RAW_PRESSURE_MAX);
5802 processSync(mapper);
5803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5804 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5805 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5806 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5807
5808 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5809 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5811 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5812
5813 // up when pressure becomes 0, hover restored
5814 processPressure(mapper, 0);
5815 processSync(mapper);
5816 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5817 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5818 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5819 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
5820
5821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5822 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
5823 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5824 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5825
5826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5827 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5828 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5829 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5830
5831 // exit hover when pointer goes away
5832 processUp(mapper);
5833 processSync(mapper);
5834 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5835 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
5836 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
5837 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
5838}
5839
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005840TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
5841 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005842 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005843 prepareButtons();
5844 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005845 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005846
5847 // Touch down.
5848 processDown(mapper, 100, 200);
5849 processPressure(mapper, 1);
5850 processSync(mapper);
5851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5852 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
5853
5854 // Reset the mapper. This should cancel the ongoing gesture.
5855 resetMapper(mapper, ARBITRARY_TIME);
5856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5857 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
5858
5859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5860}
5861
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005862TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
5863 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005864 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005865 prepareButtons();
5866 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00005867 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005868
5869 // Set the initial state for the touch pointer.
5870 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
5871 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
5872 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
5873 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5874
5875 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00005876 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
5877 // does not generate any events.
5878 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005879
5880 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
5881 // the recreated touch state to generate a down event.
5882 processSync(mapper);
5883 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5884 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
5885
5886 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5887}
5888
lilinnan687e58f2022-07-19 16:00:50 +08005889TEST_F(SingleTouchInputMapperTest,
5890 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
5891 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005892 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08005893 prepareButtons();
5894 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005895 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08005896 NotifyMotionArgs motionArgs;
5897
5898 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00005899 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08005900 processSync(mapper);
5901
5902 // We should receive a down event
5903 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5904 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5905
5906 // Change display id
5907 clearViewports();
5908 prepareSecondaryDisplay(ViewportType::INTERNAL);
5909
5910 // We should receive a cancel event
5911 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5912 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
5913 // Then receive reset called
5914 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
5915}
5916
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005917TEST_F(SingleTouchInputMapperTest,
5918 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
5919 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005920 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005921 prepareButtons();
5922 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005923 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
5925 NotifyMotionArgs motionArgs;
5926
5927 // Start a new gesture.
5928 processDown(mapper, 100, 200);
5929 processSync(mapper);
5930 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5931 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5932
5933 // Make the viewport inactive. This will put the device in disabled mode.
5934 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
5935 viewport->isActive = false;
5936 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005937 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005938
5939 // We should receive a cancel event for the ongoing gesture.
5940 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5941 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
5942 // Then we should be notified that the device was reset.
5943 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
5944
5945 // No events are generated while the viewport is inactive.
5946 processMove(mapper, 101, 201);
5947 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005948 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005949 processSync(mapper);
5950 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5951
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005952 // Start a new gesture while the viewport is still inactive.
5953 processDown(mapper, 300, 400);
5954 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
5955 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
5956 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
5957 processSync(mapper);
5958
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005959 // Make the viewport active again. The device should resume processing events.
5960 viewport->isActive = true;
5961 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00005962 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005963
5964 // The device is reset because it changes back to direct mode, without generating any events.
5965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5967
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005968 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005969 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00005970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5971 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00005972
5973 // No more events.
5974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
5975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
5976}
5977
Prabir Pradhan211ba622022-10-31 21:09:21 +00005978TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
5979 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005980 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00005981 prepareButtons();
5982 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005983 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00005984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
5985
5986 // Press a stylus button.
5987 processKey(mapper, BTN_STYLUS, 1);
5988 processSync(mapper);
5989
5990 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
5991 processDown(mapper, 100, 200);
5992 processSync(mapper);
5993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5994 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
5995 WithCoords(toDisplayX(100), toDisplayY(200)),
5996 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
5997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5998 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
5999 WithCoords(toDisplayX(100), toDisplayY(200)),
6000 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6001
6002 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
6003 // the button has not actually been released, since there will be no pointers through which the
6004 // button state can be reported. The event is generated at the location of the pointer before
6005 // it went up.
6006 processUp(mapper);
6007 processSync(mapper);
6008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6009 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6010 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6011 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6012 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6013 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
6014}
6015
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006016TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
6017 addConfigurationProperty("touch.deviceType", "touchScreen");
6018 prepareDisplay(ui::ROTATION_0);
6019 prepareButtons();
6020 prepareAxes(POSITION);
6021
6022 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
6023
Arpit Singha8c236b2023-04-25 13:56:05 +00006024 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00006025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6026
6027 // Press a stylus button.
6028 processKey(mapper, BTN_STYLUS, 1);
6029 processSync(mapper);
6030
6031 // Start a touch gesture and ensure that the stylus button is not reported.
6032 processDown(mapper, 100, 200);
6033 processSync(mapper);
6034 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6035 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
6036
6037 // Release and press the stylus button again.
6038 processKey(mapper, BTN_STYLUS, 0);
6039 processSync(mapper);
6040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6042 processKey(mapper, BTN_STYLUS, 1);
6043 processSync(mapper);
6044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6045 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
6046
6047 // Release the touch gesture.
6048 processUp(mapper);
6049 processSync(mapper);
6050 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6051 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6052
6053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6054}
6055
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006056TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
6057 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6058 prepareDisplay(ui::ROTATION_0);
6059 prepareButtons();
6060 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006061 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
6063
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006064 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00006065}
6066
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006067TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
6068 // Initialize the device without setting device source to touch navigation.
6069 addConfigurationProperty("touch.deviceType", "touchScreen");
6070 prepareDisplay(ui::ROTATION_0);
6071 prepareButtons();
6072 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006073 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006074
6075 // Ensure that the device is created as a touchscreen, not touch navigation.
6076 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6077
6078 // Add device type association after the device was created.
6079 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
6080
6081 // Send update to the mapper.
6082 std::list<NotifyArgs> unused2 =
6083 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006084 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006085
6086 // Check whether device type update was successful.
Prabir Pradhan9cf8f322024-06-18 20:22:42 +00006087 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION | AINPUT_SOURCE_TOUCHPAD, mDevice->getSources());
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00006088}
6089
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006090TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
6091 // Initialize the device without setting device source to touch navigation.
6092 addConfigurationProperty("touch.deviceType", "touchScreen");
6093 prepareDisplay(ui::ROTATION_0);
6094 prepareButtons();
6095 prepareAxes(POSITION);
6096 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
6097
6098 // Set a physical frame in the display viewport.
6099 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6100 viewport->physicalLeft = 0;
6101 viewport->physicalTop = 0;
6102 viewport->physicalRight = DISPLAY_WIDTH / 2;
6103 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
6104 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006105 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006106
Arpit Singha8c236b2023-04-25 13:56:05 +00006107 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00006108
6109 // Hovering inside the physical frame produces events.
6110 processKey(mapper, BTN_TOOL_PEN, 1);
6111 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
6112 processSync(mapper);
6113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6114 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6116 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6117
6118 // Leaving the physical frame ends the hovering gesture.
6119 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
6120 processSync(mapper);
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6122 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
6123
6124 // Moving outside the physical frame does not produce events.
6125 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
6126 processSync(mapper);
6127 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6128
6129 // Re-entering the physical frame produces events.
6130 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
6131 processSync(mapper);
6132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6133 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
6134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6135 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
6136}
6137
Prabir Pradhan5632d622021-09-06 07:57:20 -07006138// --- TouchDisplayProjectionTest ---
6139
6140class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
6141public:
6142 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
6143 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
6144 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00006145 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
6146 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
6147 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006148 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006149 auto rotatedWidth = naturalDisplayWidth;
6150 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006151 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00006152 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006153 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006154 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006155 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006156 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006157 inverseRotationFlags = ui::Transform::ROT_180;
6158 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006159 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006160 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00006161 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006162 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00006163 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07006164 inverseRotationFlags = ui::Transform::ROT_0;
6165 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006166 }
6167
Prabir Pradhana9df3162022-12-05 23:57:27 +00006168 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006169 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
6170
6171 std::optional<DisplayViewport> internalViewport =
6172 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6173 DisplayViewport& v = *internalViewport;
6174 v.displayId = DISPLAY_ID;
6175 v.orientation = orientation;
6176
6177 v.logicalLeft = 0;
6178 v.logicalTop = 0;
6179 v.logicalRight = 100;
6180 v.logicalBottom = 100;
6181
6182 v.physicalLeft = rotatedPhysicalDisplay.left;
6183 v.physicalTop = rotatedPhysicalDisplay.top;
6184 v.physicalRight = rotatedPhysicalDisplay.right;
6185 v.physicalBottom = rotatedPhysicalDisplay.bottom;
6186
Prabir Pradhana9df3162022-12-05 23:57:27 +00006187 v.deviceWidth = rotatedWidth;
6188 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07006189
6190 v.isActive = true;
6191 v.uniqueId = UNIQUE_ID;
6192 v.type = ViewportType::INTERNAL;
6193 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006194 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006195 }
6196
6197 void assertReceivedMove(const Point& point) {
6198 NotifyMotionArgs motionArgs;
6199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6200 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006201 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006202 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
6203 1, 0, 0, 0, 0, 0, 0, 0));
6204 }
6205};
6206
6207TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
6208 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006209 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006210
6211 prepareButtons();
6212 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006213 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006214
6215 NotifyMotionArgs motionArgs;
6216
6217 // Configure the DisplayViewport such that the logical display maps to a subsection of
6218 // the display panel called the physical display. Here, the physical display is bounded by the
6219 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6220 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6221 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
6222 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
6223
Michael Wrighta9cf4192022-12-01 23:46:39 +00006224 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006225 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6226
6227 // Touches outside the physical display should be ignored, and should not generate any
6228 // events. Ensure touches at the following points that lie outside of the physical display
6229 // area do not generate any events.
6230 for (const auto& point : kPointsOutsidePhysicalDisplay) {
6231 processDown(mapper, toRawX(point.x), toRawY(point.y));
6232 processSync(mapper);
6233 processUp(mapper);
6234 processSync(mapper);
6235 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
6236 << "Unexpected event generated for touch outside physical display at point: "
6237 << point.x << ", " << point.y;
6238 }
6239 }
6240}
6241
6242TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
6243 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006244 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07006245
6246 prepareButtons();
6247 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006248 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07006249
6250 NotifyMotionArgs motionArgs;
6251
6252 // Configure the DisplayViewport such that the logical display maps to a subsection of
6253 // the display panel called the physical display. Here, the physical display is bounded by the
6254 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
6255 static const Rect kPhysicalDisplay{10, 20, 70, 160};
6256
Michael Wrighta9cf4192022-12-01 23:46:39 +00006257 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07006258 configurePhysicalDisplay(orientation, kPhysicalDisplay);
6259
6260 // Touches that start outside the physical display should be ignored until it enters the
6261 // physical display bounds, at which point it should generate a down event. Start a touch at
6262 // the point (5, 100), which is outside the physical display bounds.
6263 static const Point kOutsidePoint{5, 100};
6264 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6265 processSync(mapper);
6266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6267
6268 // Move the touch into the physical display area. This should generate a pointer down.
6269 processMove(mapper, toRawX(11), toRawY(21));
6270 processSync(mapper);
6271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6272 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006273 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07006274 ASSERT_NO_FATAL_FAILURE(
6275 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
6276
6277 // Move the touch inside the physical display area. This should generate a pointer move.
6278 processMove(mapper, toRawX(69), toRawY(159));
6279 processSync(mapper);
6280 assertReceivedMove({69, 159});
6281
6282 // Move outside the physical display area. Since the pointer is already down, this should
6283 // now continue generating events.
6284 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
6285 processSync(mapper);
6286 assertReceivedMove(kOutsidePoint);
6287
6288 // Release. This should generate a pointer up.
6289 processUp(mapper);
6290 processSync(mapper);
6291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6292 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6293 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
6294 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
6295
6296 // Ensure no more events were generated.
6297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6298 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6299 }
6300}
6301
Prabir Pradhana9df3162022-12-05 23:57:27 +00006302// --- TouchscreenPrecisionTests ---
6303
6304// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
6305// in various orientations and with different display rotations. We configure the touchscreen to
6306// have a higher resolution than that of the display by an integer scale factor in each axis so that
6307// we can enforce that coordinates match precisely as expected.
6308class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
6309 public ::testing::WithParamInterface<ui::Rotation> {
6310public:
6311 void SetUp() override {
6312 SingleTouchInputMapperTest::SetUp();
6313
6314 // Prepare the raw axes to have twice the resolution of the display in the X axis and
6315 // four times the resolution of the display in the Y axis.
6316 prepareButtons();
6317 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006318 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
6319 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006320 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006321 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
6322 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00006323 }
6324
6325 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
6326 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
6327 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
6328 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
6329
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006330 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
6331 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
6332
6333 static const int32_t PRECISION_RAW_X_FLAT = 16;
6334 static const int32_t PRECISION_RAW_Y_FLAT = 32;
6335
6336 static const int32_t PRECISION_RAW_X_FUZZ = 4;
6337 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
6338
Prabir Pradhana9df3162022-12-05 23:57:27 +00006339 static const std::array<Point, 4> kRawCorners;
6340};
6341
6342const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
6343 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
6344 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
6345 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
6346 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
6347}};
6348
6349// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
6350// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
6351// touchscreen panel that is used on a device whose natural display orientation is in landscape.
6352TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
6353 enum class Orientation {
6354 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
6355 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
6356 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
6357 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
6358 ftl_last = ORIENTATION_270,
6359 };
6360 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
6361 Orientation::ORIENTATION_270;
6362 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
6363 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6364 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
6365 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
6366 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6367 };
6368
6369 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
6370
6371 // Configure the touchscreen as being installed in the one of the four different orientations
6372 // relative to the display.
6373 addConfigurationProperty("touch.deviceType", "touchScreen");
6374 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
6375 prepareDisplay(ui::ROTATION_0);
6376
Arpit Singha8c236b2023-04-25 13:56:05 +00006377 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00006378
6379 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
6380 // orientations of either 90 or 270) this means the display's natural resolution will be
6381 // flipped.
6382 const bool displayRotated =
6383 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
6384 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
6385 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
6386 const Rect physicalFrame{0, 0, width, height};
6387 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
6388
6389 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
6390 const float expectedPrecisionX = displayRotated ? 4 : 2;
6391 const float expectedPrecisionY = displayRotated ? 2 : 4;
6392
6393 // Test all four corners.
6394 for (int i = 0; i < 4; i++) {
6395 const auto& raw = kRawCorners[i];
6396 processDown(mapper, raw.x, raw.y);
6397 processSync(mapper);
6398 const auto& expected = expectedPoints[i];
6399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6400 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6401 WithCoords(expected.x, expected.y),
6402 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
6403 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6404 << "with touchscreen orientation "
6405 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
6406 << expected.x << ", " << expected.y << ").";
6407 processUp(mapper);
6408 processSync(mapper);
6409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6410 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6411 WithCoords(expected.x, expected.y))));
6412 }
6413}
6414
Prabir Pradhan82687402022-12-06 01:32:53 +00006415TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
6416 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
6417 kMappedCorners = {
6418 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
6419 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
6420 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
6421 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
6422 };
6423
6424 const ui::Rotation displayRotation = GetParam();
6425
6426 addConfigurationProperty("touch.deviceType", "touchScreen");
6427 prepareDisplay(displayRotation);
6428
Arpit Singha8c236b2023-04-25 13:56:05 +00006429 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00006430
6431 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6432
6433 // Test all four corners.
6434 for (int i = 0; i < 4; i++) {
6435 const auto& expected = expectedPoints[i];
6436 const auto& raw = kRawCorners[i];
6437 processDown(mapper, raw.x, raw.y);
6438 processSync(mapper);
6439 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6440 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6441 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
6442 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6443 << "with display rotation " << ui::toCString(displayRotation)
6444 << ", expected point (" << expected.x << ", " << expected.y << ").";
6445 processUp(mapper);
6446 processSync(mapper);
6447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6448 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6449 WithCoords(expected.x, expected.y))));
6450 }
6451}
6452
Prabir Pradhan3e798762022-12-02 21:02:11 +00006453TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
6454 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
6455 kMappedCorners = {
6456 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
6457 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
6458 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
6459 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
6460 };
6461
6462 const ui::Rotation displayRotation = GetParam();
6463
6464 addConfigurationProperty("touch.deviceType", "touchScreen");
6465 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6466
Arpit Singha8c236b2023-04-25 13:56:05 +00006467 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00006468
6469 // Ori 270, so width and height swapped
6470 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
6471 prepareDisplay(displayRotation);
6472 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
6473
6474 const auto& expectedPoints = kMappedCorners.at(displayRotation);
6475
6476 // Test all four corners.
6477 for (int i = 0; i < 4; i++) {
6478 const auto& expected = expectedPoints[i];
6479 const auto& raw = kRawCorners[i];
6480 processDown(mapper, raw.x, raw.y);
6481 processSync(mapper);
6482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6483 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6484 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
6485 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
6486 << "with display rotation " << ui::toCString(displayRotation)
6487 << ", expected point (" << expected.x << ", " << expected.y << ").";
6488 processUp(mapper);
6489 processSync(mapper);
6490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6491 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
6492 WithCoords(expected.x, expected.y))));
6493 }
6494}
6495
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006496TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
6497 const ui::Rotation displayRotation = GetParam();
6498
6499 addConfigurationProperty("touch.deviceType", "touchScreen");
6500 prepareDisplay(displayRotation);
6501
6502 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00006503 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00006504
6505 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
6506 // MotionRanges use display pixels as their units
6507 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
6508 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
6509
6510 // The MotionRanges should be oriented in the rotated display's coordinate space
6511 const bool displayRotated =
6512 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
6513
6514 constexpr float MAX_X = 479.5;
6515 constexpr float MAX_Y = 799.75;
6516 EXPECT_EQ(xRange->min, 0.f);
6517 EXPECT_EQ(yRange->min, 0.f);
6518 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
6519 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
6520
6521 EXPECT_EQ(xRange->flat, 8.f);
6522 EXPECT_EQ(yRange->flat, 8.f);
6523
6524 EXPECT_EQ(xRange->fuzz, 2.f);
6525 EXPECT_EQ(yRange->fuzz, 2.f);
6526
6527 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
6528 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
6529}
6530
Prabir Pradhana9df3162022-12-05 23:57:27 +00006531// Run the precision tests for all rotations.
6532INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
6533 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
6534 ui::ROTATION_270),
6535 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
6536 return ftl::enum_string(testParamInfo.param);
6537 });
6538
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006539// --- ExternalStylusFusionTest ---
6540
6541class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
6542public:
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006543 void SetUp() override {
6544 SingleTouchInputMapperTest::SetUp();
6545 mExternalStylusDeviceInfo = {};
6546 mStylusState = {};
6547 }
6548
6549 SingleTouchInputMapper& initializeInputMapperWithExternalStylus(bool supportsPressure = true) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006550 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006551 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006552 prepareButtons();
6553 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006554 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006555
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006556 if (supportsPressure) {
6557 mExternalStylusDeviceInfo.addMotionRange(AMOTION_EVENT_AXIS_PRESSURE,
6558 AINPUT_SOURCE_STYLUS, 0.0f, 1.0f, 0.0f, 0.0f,
6559 0.0f);
6560 mStylusState.pressure = 0.f;
6561 }
6562
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006563 mStylusState.when = ARBITRARY_TIME;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006564 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006565 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006566 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006567 processExternalStylusState(mapper);
6568 return mapper;
6569 }
6570
6571 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
6572 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
6573 for (const NotifyArgs& args : generatedArgs) {
6574 mFakeListener->notify(args);
6575 }
6576 // Loop the reader to flush the input listener queue.
6577 mReader->loopOnce();
6578 return generatedArgs;
6579 }
6580
6581protected:
6582 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006583
6584 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
6585 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006586 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006587
6588 // The first pointer is withheld.
6589 processDown(mapper, 100, 200);
6590 processSync(mapper);
6591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6592 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6593 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6594
6595 // The external stylus reports pressure. The withheld finger pointer is released as a
6596 // stylus.
6597 mStylusState.pressure = 1.f;
6598 processExternalStylusState(mapper);
6599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6600 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6601 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6602
6603 // Subsequent pointer events are not withheld.
6604 processMove(mapper, 101, 201);
6605 processSync(mapper);
6606 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6607 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6608
6609 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6610 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6611 }
6612
6613 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
6614 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6615
6616 // Releasing the touch pointer ends the gesture.
6617 processUp(mapper);
6618 processSync(mapper);
6619 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006620 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006621 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006622
6623 mStylusState.pressure = 0.f;
6624 processExternalStylusState(mapper);
6625 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6627 }
6628
6629 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006630 // When stylus fusion is not successful, events should be reported with the original source.
6631 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006632 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006633 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006634
6635 // The first pointer is withheld when an external stylus is connected,
6636 // and a timeout is requested.
6637 processDown(mapper, 100, 200);
6638 processSync(mapper);
6639 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6640 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6641 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6642
6643 // If the timeout expires early, it is requested again.
6644 handleTimeout(mapper, ARBITRARY_TIME + 1);
6645 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
6646 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
6647
6648 // When the timeout expires, the withheld touch is released as a finger pointer.
6649 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
6650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6651 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6652
6653 // Subsequent pointer events are not withheld.
6654 processMove(mapper, 101, 201);
6655 processSync(mapper);
6656 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6657 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6658 processUp(mapper);
6659 processSync(mapper);
6660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6661 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6662
6663 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6664 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6665 }
6666
6667private:
6668 InputDeviceInfo mExternalStylusDeviceInfo{};
6669};
6670
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006671TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSourceWithPressure) {
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006672 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006673 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006674}
6675
Prabir Pradhan7ff19a12024-07-29 22:40:31 +00006676TEST_F(ExternalStylusFusionTest, DoesNotUseBluetoothStylusSourceWithoutPressure) {
6677 SingleTouchInputMapper& mapper =
6678 initializeInputMapperWithExternalStylus(/*supportsPressure=*/false);
6679 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
6680}
6681
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006682TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
6683 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6684 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6685}
6686
6687TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
6688 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6689 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6690}
6691
6692// Test a successful stylus fusion gesture where the pressure is reported by the external
6693// before the touch is reported by the touchscreen.
6694TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
6695 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006696 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006697
6698 // The external stylus reports pressure first. It is ignored for now.
6699 mStylusState.pressure = 1.f;
6700 processExternalStylusState(mapper);
6701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6702 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6703
6704 // When the touch goes down afterwards, it is reported as a stylus pointer.
6705 processDown(mapper, 100, 200);
6706 processSync(mapper);
6707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6708 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
6709 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6710
6711 processMove(mapper, 101, 201);
6712 processSync(mapper);
6713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6714 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
6715 processUp(mapper);
6716 processSync(mapper);
6717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6718 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
6719
6720 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6722}
6723
6724TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
6725 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
6726
6727 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6728 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6729
6730 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6731 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
6732 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6733 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
6734}
6735
6736TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
6737 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006738 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006739
6740 mStylusState.pressure = 0.8f;
6741 processExternalStylusState(mapper);
6742 processDown(mapper, 100, 200);
6743 processSync(mapper);
6744 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6745 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
6746 WithPressure(0.8f))));
6747 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6748
6749 // The external stylus reports a pressure change. We wait for some time for a touch event.
6750 mStylusState.pressure = 0.6f;
6751 processExternalStylusState(mapper);
6752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6753 ASSERT_NO_FATAL_FAILURE(
6754 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6755
6756 // If a touch is reported within the timeout, it reports the updated pressure.
6757 processMove(mapper, 101, 201);
6758 processSync(mapper);
6759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6760 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6761 WithPressure(0.6f))));
6762 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6763
6764 // There is another pressure change.
6765 mStylusState.pressure = 0.5f;
6766 processExternalStylusState(mapper);
6767 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6768 ASSERT_NO_FATAL_FAILURE(
6769 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6770
6771 // If a touch is not reported within the timeout, a move event is generated to report
6772 // the new pressure.
6773 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6775 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6776 WithPressure(0.5f))));
6777
6778 // If a zero pressure is reported before the touch goes up, the previous pressure value is
6779 // repeated indefinitely.
6780 mStylusState.pressure = 0.0f;
6781 processExternalStylusState(mapper);
6782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6783 ASSERT_NO_FATAL_FAILURE(
6784 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6785 processMove(mapper, 102, 202);
6786 processSync(mapper);
6787 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6788 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6789 WithPressure(0.5f))));
6790 processMove(mapper, 103, 203);
6791 processSync(mapper);
6792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6793 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6794 WithPressure(0.5f))));
6795
6796 processUp(mapper);
6797 processSync(mapper);
6798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006800 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006801
6802 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6804}
6805
6806TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
6807 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006808 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006809
6810 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006811 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006812 processExternalStylusState(mapper);
6813 processDown(mapper, 100, 200);
6814 processSync(mapper);
6815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6816 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006817 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006818 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6819
6820 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006821 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006822 processExternalStylusState(mapper);
6823 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6824 ASSERT_NO_FATAL_FAILURE(
6825 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6826
6827 // If a touch is reported within the timeout, it reports the updated pressure.
6828 processMove(mapper, 101, 201);
6829 processSync(mapper);
6830 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6831 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006832 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006833 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6834
6835 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006836 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006837 processExternalStylusState(mapper);
6838 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6839 ASSERT_NO_FATAL_FAILURE(
6840 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6841
6842 // If a touch is not reported within the timeout, a move event is generated to report
6843 // the new tool type.
6844 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
6845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6846 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006847 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006848
6849 processUp(mapper);
6850 processSync(mapper);
6851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6852 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006853 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006854
6855 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6857}
6858
6859TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
6860 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00006861 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006862
6863 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
6864
6865 // The external stylus reports a button change. We wait for some time for a touch event.
6866 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
6867 processExternalStylusState(mapper);
6868 ASSERT_NO_FATAL_FAILURE(
6869 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6870
6871 // If a touch is reported within the timeout, it reports the updated button state.
6872 processMove(mapper, 101, 201);
6873 processSync(mapper);
6874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6875 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6876 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6878 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
6879 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
6880 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6881
6882 // The button is now released.
6883 mStylusState.buttons = 0;
6884 processExternalStylusState(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6886 ASSERT_NO_FATAL_FAILURE(
6887 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
6888
6889 // If a touch is not reported within the timeout, a move event is generated to report
6890 // the new button state.
6891 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
6893 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
6894 WithButtonState(0))));
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00006896 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
6897 WithButtonState(0))));
6898
6899 processUp(mapper);
6900 processSync(mapper);
6901 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00006902 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
6903
6904 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
6905 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6906}
6907
Michael Wrightd02c5b62014-02-10 15:10:22 -08006908// --- MultiTouchInputMapperTest ---
6909
6910class MultiTouchInputMapperTest : public TouchInputMapperTest {
6911protected:
6912 void prepareAxes(int axes);
6913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006914 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
6915 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
6916 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
6917 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
6918 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
6919 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
6920 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
6921 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
6922 void processId(MultiTouchInputMapper& mapper, int32_t id);
6923 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
6924 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
6925 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00006926 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006927 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00006928 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
6929 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930};
6931
6932void MultiTouchInputMapperTest::prepareAxes(int axes) {
6933 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006934 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
6935 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006936 }
6937 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006938 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
6939 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006941 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
6942 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006943 }
6944 }
6945 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006946 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
6947 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08006949 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006950 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006951 }
6952 }
6953 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006954 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
6955 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006956 }
6957 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006958 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
6959 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960 }
6961 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006962 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
6963 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006964 }
6965 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006966 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
6967 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006968 }
6969 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006970 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
6971 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006972 }
6973 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08006974 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006975 }
6976}
6977
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006978void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
6979 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006980 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
6981 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006982}
6983
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006984void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
6985 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006986 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006987}
6988
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006989void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
6990 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006991 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992}
6993
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006994void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006995 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006996}
6997
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006998void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00006999 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007000}
7001
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007002void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
7003 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007004 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007005}
7006
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007007void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007008 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007009}
7010
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007011void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007012 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013}
7014
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007015void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007016 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007017}
7018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007019void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007020 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021}
7022
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007023void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007024 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007025}
7026
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007027void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
7028 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007029 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007030}
7031
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00007032void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
7033 int32_t value) {
7034 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
7035 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
7036}
7037
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08007038void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00007039 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007040}
7041
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00007042void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
7043 nsecs_t readTime) {
7044 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007045}
7046
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007048 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007049 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 prepareAxes(POSITION);
7051 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007052 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053
arthurhungdcef2dc2020-08-11 14:47:50 +08007054 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007055
7056 NotifyMotionArgs motionArgs;
7057
7058 // Two fingers down at once.
7059 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7060 processPosition(mapper, x1, y1);
7061 processMTSync(mapper);
7062 processPosition(mapper, x2, y2);
7063 processMTSync(mapper);
7064 processSync(mapper);
7065
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7068 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7070 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7071 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7072 ASSERT_EQ(0, motionArgs.flags);
7073 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7074 ASSERT_EQ(0, motionArgs.buttonState);
7075 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007076 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007078 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7080 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7081 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7082 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7083 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7084
7085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7086 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7087 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7088 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7089 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007090 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007091 ASSERT_EQ(0, motionArgs.flags);
7092 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7093 ASSERT_EQ(0, motionArgs.buttonState);
7094 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007095 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007096 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007097 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007099 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7101 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7102 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7103 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7104 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7105 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7106 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7107
7108 // Move.
7109 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7110 processPosition(mapper, x1, y1);
7111 processMTSync(mapper);
7112 processPosition(mapper, x2, y2);
7113 processMTSync(mapper);
7114 processSync(mapper);
7115
7116 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7117 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7118 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7119 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7120 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7121 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7122 ASSERT_EQ(0, motionArgs.flags);
7123 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7124 ASSERT_EQ(0, motionArgs.buttonState);
7125 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007126 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007127 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007128 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007129 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007130 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7132 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7133 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7134 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7135 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7136 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7137 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7138
7139 // First finger up.
7140 x2 += 15; y2 -= 20;
7141 processPosition(mapper, x2, y2);
7142 processMTSync(mapper);
7143 processSync(mapper);
7144
7145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007150 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007151 ASSERT_EQ(0, motionArgs.flags);
7152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7153 ASSERT_EQ(0, motionArgs.buttonState);
7154 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007155 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007157 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007158 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007159 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007160 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7161 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7162 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7163 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7164 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7165 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7166 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7167
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7173 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7174 ASSERT_EQ(0, motionArgs.flags);
7175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7176 ASSERT_EQ(0, motionArgs.buttonState);
7177 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007178 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7182 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7186
7187 // Move.
7188 x2 += 20; y2 -= 25;
7189 processPosition(mapper, x2, y2);
7190 processMTSync(mapper);
7191 processSync(mapper);
7192
7193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7194 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7195 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7196 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7197 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7198 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7199 ASSERT_EQ(0, motionArgs.flags);
7200 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7201 ASSERT_EQ(0, motionArgs.buttonState);
7202 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007203 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007204 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007205 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007206 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7207 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7208 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7209 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7210 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7211
7212 // New finger down.
7213 int32_t x3 = 700, y3 = 300;
7214 processPosition(mapper, x2, y2);
7215 processMTSync(mapper);
7216 processPosition(mapper, x3, y3);
7217 processMTSync(mapper);
7218 processSync(mapper);
7219
7220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7221 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7222 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7223 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7224 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007225 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007226 ASSERT_EQ(0, motionArgs.flags);
7227 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7228 ASSERT_EQ(0, motionArgs.buttonState);
7229 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007230 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007231 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007232 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007233 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007234 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007235 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7236 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7237 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7238 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7239 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7240 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7241 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7242
7243 // Second finger up.
7244 x3 += 30; y3 -= 20;
7245 processPosition(mapper, x3, y3);
7246 processMTSync(mapper);
7247 processSync(mapper);
7248
7249 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7250 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7251 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7252 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7253 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007254 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007255 ASSERT_EQ(0, motionArgs.flags);
7256 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7257 ASSERT_EQ(0, motionArgs.buttonState);
7258 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007259 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007262 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007263 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007264 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7265 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7266 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7267 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7268 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7269 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7270 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7271
7272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7273 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7274 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7275 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7276 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7277 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7278 ASSERT_EQ(0, motionArgs.flags);
7279 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7280 ASSERT_EQ(0, motionArgs.buttonState);
7281 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007282 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007283 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007284 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007285 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7286 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7287 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7288 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7289 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7290
7291 // Last finger up.
7292 processMTSync(mapper);
7293 processSync(mapper);
7294
7295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7296 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
7297 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
7298 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
7299 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
7300 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7301 ASSERT_EQ(0, motionArgs.flags);
7302 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
7303 ASSERT_EQ(0, motionArgs.buttonState);
7304 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007305 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007306 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007307 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007308 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7309 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7310 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
7311 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
7312 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
7313
7314 // Should not have sent any more keys or motions.
7315 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7316 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7317}
7318
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007319TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
7320 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007321 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007322
7323 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7324 /*fuzz*/ 0, /*resolution*/ 10);
7325 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7326 /*fuzz*/ 0, /*resolution*/ 11);
7327 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7328 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
7329 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
7330 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
7331 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7332 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
7333 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
7334 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
7335
Arpit Singha8c236b2023-04-25 13:56:05 +00007336 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007337
7338 // X and Y axes
7339 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
7340 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
7341 // Touch major and minor
7342 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
7343 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
7344 // Tool major and minor
7345 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
7346 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
7347}
7348
7349TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
7350 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007351 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007352
7353 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
7354 /*fuzz*/ 0, /*resolution*/ 10);
7355 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
7356 /*fuzz*/ 0, /*resolution*/ 11);
7357
7358 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
7359
Arpit Singha8c236b2023-04-25 13:56:05 +00007360 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08007361
7362 // Touch major and minor
7363 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
7364 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
7365 // Tool major and minor
7366 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
7367 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
7368}
7369
Michael Wrightd02c5b62014-02-10 15:10:22 -08007370TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007371 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007372 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007373 prepareAxes(POSITION | ID);
7374 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007375 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007376
arthurhungdcef2dc2020-08-11 14:47:50 +08007377 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007378
7379 NotifyMotionArgs motionArgs;
7380
7381 // Two fingers down at once.
7382 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7383 processPosition(mapper, x1, y1);
7384 processId(mapper, 1);
7385 processMTSync(mapper);
7386 processPosition(mapper, x2, y2);
7387 processId(mapper, 2);
7388 processMTSync(mapper);
7389 processSync(mapper);
7390
7391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007393 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007394 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007395 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007396 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7397 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7398
7399 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007400 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007401 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007402 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007403 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007404 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007405 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007406 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7407 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7408 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7409 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7410
7411 // Move.
7412 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7413 processPosition(mapper, x1, y1);
7414 processId(mapper, 1);
7415 processMTSync(mapper);
7416 processPosition(mapper, x2, y2);
7417 processId(mapper, 2);
7418 processMTSync(mapper);
7419 processSync(mapper);
7420
7421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7422 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007423 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007424 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007425 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007426 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007427 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007428 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7429 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7430 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7431 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7432
7433 // First finger up.
7434 x2 += 15; y2 -= 20;
7435 processPosition(mapper, x2, y2);
7436 processId(mapper, 2);
7437 processMTSync(mapper);
7438 processSync(mapper);
7439
7440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007441 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007442 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007443 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007444 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007445 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007446 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007447 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7448 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7449 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7450 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7451
7452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7453 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007454 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007455 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007456 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007457 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7458 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7459
7460 // Move.
7461 x2 += 20; y2 -= 25;
7462 processPosition(mapper, x2, y2);
7463 processId(mapper, 2);
7464 processMTSync(mapper);
7465 processSync(mapper);
7466
7467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7468 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007469 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007470 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007471 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007472 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7473 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7474
7475 // New finger down.
7476 int32_t x3 = 700, y3 = 300;
7477 processPosition(mapper, x2, y2);
7478 processId(mapper, 2);
7479 processMTSync(mapper);
7480 processPosition(mapper, x3, y3);
7481 processId(mapper, 3);
7482 processMTSync(mapper);
7483 processSync(mapper);
7484
7485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007486 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007487 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007488 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007489 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007490 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007491 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7493 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7494 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7495 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7496
7497 // Second finger up.
7498 x3 += 30; y3 -= 20;
7499 processPosition(mapper, x3, y3);
7500 processId(mapper, 3);
7501 processMTSync(mapper);
7502 processSync(mapper);
7503
7504 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007505 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007506 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007507 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007508 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007509 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007510 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007511 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7512 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7513 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7514 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7515
7516 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7517 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007518 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007519 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007520 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007521 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7522 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7523
7524 // Last finger up.
7525 processMTSync(mapper);
7526 processSync(mapper);
7527
7528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7529 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007530 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007531 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007532 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007533 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7534 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7535
7536 // Should not have sent any more keys or motions.
7537 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7538 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7539}
7540
7541TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007542 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007543 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007544 prepareAxes(POSITION | ID | SLOT);
7545 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00007546 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007547
arthurhungdcef2dc2020-08-11 14:47:50 +08007548 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007549
7550 NotifyMotionArgs motionArgs;
7551
7552 // Two fingers down at once.
7553 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
7554 processPosition(mapper, x1, y1);
7555 processId(mapper, 1);
7556 processSlot(mapper, 1);
7557 processPosition(mapper, x2, y2);
7558 processId(mapper, 2);
7559 processSync(mapper);
7560
7561 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7562 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007563 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007564 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007565 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007566 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7567 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7568
7569 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007570 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007571 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007572 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007573 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007574 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007575 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007576 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7577 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7578 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7579 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7580
7581 // Move.
7582 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
7583 processSlot(mapper, 0);
7584 processPosition(mapper, x1, y1);
7585 processSlot(mapper, 1);
7586 processPosition(mapper, x2, y2);
7587 processSync(mapper);
7588
7589 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7590 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007591 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007592 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007593 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007594 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007595 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007596 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7597 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7598 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7599 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7600
7601 // First finger up.
7602 x2 += 15; y2 -= 20;
7603 processSlot(mapper, 0);
7604 processId(mapper, -1);
7605 processSlot(mapper, 1);
7606 processPosition(mapper, x2, y2);
7607 processSync(mapper);
7608
7609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007610 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007611 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007612 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007613 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007614 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007615 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007616 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7617 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
7618 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7619 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7620
7621 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7622 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007623 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007624 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007625 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007626 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7627 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7628
7629 // Move.
7630 x2 += 20; y2 -= 25;
7631 processPosition(mapper, x2, y2);
7632 processSync(mapper);
7633
7634 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7635 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007636 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007637 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007638 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007639 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7640 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7641
7642 // New finger down.
7643 int32_t x3 = 700, y3 = 300;
7644 processPosition(mapper, x2, y2);
7645 processSlot(mapper, 0);
7646 processId(mapper, 3);
7647 processPosition(mapper, x3, y3);
7648 processSync(mapper);
7649
7650 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007651 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007652 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007653 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007654 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007655 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007656 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007657 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7658 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7659 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7660 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7661
7662 // Second finger up.
7663 x3 += 30; y3 -= 20;
7664 processSlot(mapper, 1);
7665 processId(mapper, -1);
7666 processSlot(mapper, 0);
7667 processPosition(mapper, x3, y3);
7668 processSync(mapper);
7669
7670 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007671 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007672 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007673 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007675 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007676 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7678 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7679 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
7680 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
7681
7682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007684 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007685 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007686 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007687 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7688 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7689
7690 // Last finger up.
7691 processId(mapper, -1);
7692 processSync(mapper);
7693
7694 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7695 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007696 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007697 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007698 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007699 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7700 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
7701
7702 // Should not have sent any more keys or motions.
7703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7705}
7706
7707TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007708 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007709 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007710 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007711 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007712
7713 // These calculations are based on the input device calibration documentation.
7714 int32_t rawX = 100;
7715 int32_t rawY = 200;
7716 int32_t rawTouchMajor = 7;
7717 int32_t rawTouchMinor = 6;
7718 int32_t rawToolMajor = 9;
7719 int32_t rawToolMinor = 8;
7720 int32_t rawPressure = 11;
7721 int32_t rawDistance = 0;
7722 int32_t rawOrientation = 3;
7723 int32_t id = 5;
7724
7725 float x = toDisplayX(rawX);
7726 float y = toDisplayY(rawY);
7727 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
7728 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7729 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7730 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7731 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7732 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7733 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
7734 float distance = float(rawDistance);
7735
7736 processPosition(mapper, rawX, rawY);
7737 processTouchMajor(mapper, rawTouchMajor);
7738 processTouchMinor(mapper, rawTouchMinor);
7739 processToolMajor(mapper, rawToolMajor);
7740 processToolMinor(mapper, rawToolMinor);
7741 processPressure(mapper, rawPressure);
7742 processOrientation(mapper, rawOrientation);
7743 processDistance(mapper, rawDistance);
7744 processId(mapper, id);
7745 processMTSync(mapper);
7746 processSync(mapper);
7747
7748 NotifyMotionArgs args;
7749 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7750 ASSERT_EQ(0, args.pointerProperties[0].id);
7751 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7752 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
7753 orientation, distance));
Prabir Pradhan9a53b552024-06-04 02:59:40 +00007754 ASSERT_EQ(args.flags, AMOTION_EVENT_PRIVATE_FLAG_SUPPORTS_ORIENTATION);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007755}
7756
7757TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007758 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007759 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007760 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
7761 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00007762 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007763
7764 // These calculations are based on the input device calibration documentation.
7765 int32_t rawX = 100;
7766 int32_t rawY = 200;
7767 int32_t rawTouchMajor = 140;
7768 int32_t rawTouchMinor = 120;
7769 int32_t rawToolMajor = 180;
7770 int32_t rawToolMinor = 160;
7771
7772 float x = toDisplayX(rawX);
7773 float y = toDisplayY(rawY);
7774 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
7775 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
7776 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
7777 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
7778 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
7779
7780 processPosition(mapper, rawX, rawY);
7781 processTouchMajor(mapper, rawTouchMajor);
7782 processTouchMinor(mapper, rawTouchMinor);
7783 processToolMajor(mapper, rawToolMajor);
7784 processToolMinor(mapper, rawToolMinor);
7785 processMTSync(mapper);
7786 processSync(mapper);
7787
7788 NotifyMotionArgs args;
7789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7790 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7791 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
7792}
7793
7794TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007795 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007796 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007797 prepareAxes(POSITION | TOUCH | TOOL);
7798 addConfigurationProperty("touch.size.calibration", "diameter");
7799 addConfigurationProperty("touch.size.scale", "10");
7800 addConfigurationProperty("touch.size.bias", "160");
7801 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00007802 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007803
7804 // These calculations are based on the input device calibration documentation.
7805 // Note: We only provide a single common touch/tool value because the device is assumed
7806 // not to emit separate values for each pointer (isSummed = 1).
7807 int32_t rawX = 100;
7808 int32_t rawY = 200;
7809 int32_t rawX2 = 150;
7810 int32_t rawY2 = 250;
7811 int32_t rawTouchMajor = 5;
7812 int32_t rawToolMajor = 8;
7813
7814 float x = toDisplayX(rawX);
7815 float y = toDisplayY(rawY);
7816 float x2 = toDisplayX(rawX2);
7817 float y2 = toDisplayY(rawY2);
7818 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
7819 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
7820 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
7821
7822 processPosition(mapper, rawX, rawY);
7823 processTouchMajor(mapper, rawTouchMajor);
7824 processToolMajor(mapper, rawToolMajor);
7825 processMTSync(mapper);
7826 processPosition(mapper, rawX2, rawY2);
7827 processTouchMajor(mapper, rawTouchMajor);
7828 processToolMajor(mapper, rawToolMajor);
7829 processMTSync(mapper);
7830 processSync(mapper);
7831
7832 NotifyMotionArgs args;
7833 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7834 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
7835
7836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08007837 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007838 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08007839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7840 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
7842 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
7843}
7844
7845TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007846 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007847 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007848 prepareAxes(POSITION | TOUCH | TOOL);
7849 addConfigurationProperty("touch.size.calibration", "area");
7850 addConfigurationProperty("touch.size.scale", "43");
7851 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00007852 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007853
7854 // These calculations are based on the input device calibration documentation.
7855 int32_t rawX = 100;
7856 int32_t rawY = 200;
7857 int32_t rawTouchMajor = 5;
7858 int32_t rawToolMajor = 8;
7859
7860 float x = toDisplayX(rawX);
7861 float y = toDisplayY(rawY);
7862 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
7863 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
7864 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
7865
7866 processPosition(mapper, rawX, rawY);
7867 processTouchMajor(mapper, rawTouchMajor);
7868 processToolMajor(mapper, rawToolMajor);
7869 processMTSync(mapper);
7870 processSync(mapper);
7871
7872 NotifyMotionArgs args;
7873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7874 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7875 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
7876}
7877
7878TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007879 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007880 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007881 prepareAxes(POSITION | PRESSURE);
7882 addConfigurationProperty("touch.pressure.calibration", "amplitude");
7883 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00007884 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007885
Michael Wrightaa449c92017-12-13 21:21:43 +00007886 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00007887 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00007888 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
7889 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
7890 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
7891
Michael Wrightd02c5b62014-02-10 15:10:22 -08007892 // These calculations are based on the input device calibration documentation.
7893 int32_t rawX = 100;
7894 int32_t rawY = 200;
7895 int32_t rawPressure = 60;
7896
7897 float x = toDisplayX(rawX);
7898 float y = toDisplayY(rawY);
7899 float pressure = float(rawPressure) * 0.01f;
7900
7901 processPosition(mapper, rawX, rawY);
7902 processPressure(mapper, rawPressure);
7903 processMTSync(mapper);
7904 processSync(mapper);
7905
7906 NotifyMotionArgs args;
7907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
7908 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
7909 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
7910}
7911
7912TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007913 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007914 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007915 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00007916 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007917
7918 NotifyMotionArgs motionArgs;
7919 NotifyKeyArgs keyArgs;
7920
7921 processId(mapper, 1);
7922 processPosition(mapper, 100, 200);
7923 processSync(mapper);
7924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7925 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7926 ASSERT_EQ(0, motionArgs.buttonState);
7927
7928 // press BTN_LEFT, release BTN_LEFT
7929 processKey(mapper, BTN_LEFT, 1);
7930 processSync(mapper);
7931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7933 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7934
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007935 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7936 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7937 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
7938
Michael Wrightd02c5b62014-02-10 15:10:22 -08007939 processKey(mapper, BTN_LEFT, 0);
7940 processSync(mapper);
7941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007942 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007943 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007944
7945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007946 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007947 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007948
7949 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
7950 processKey(mapper, BTN_RIGHT, 1);
7951 processKey(mapper, BTN_MIDDLE, 1);
7952 processSync(mapper);
7953 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7954 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
7955 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7956 motionArgs.buttonState);
7957
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7960 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
7961
7962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7963 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7964 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
7965 motionArgs.buttonState);
7966
Michael Wrightd02c5b62014-02-10 15:10:22 -08007967 processKey(mapper, BTN_RIGHT, 0);
7968 processSync(mapper);
7969 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007970 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007971 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007972
7973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007974 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007975 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007976
7977 processKey(mapper, BTN_MIDDLE, 0);
7978 processSync(mapper);
7979 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007980 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007981 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007982
7983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007984 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007985 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007986
7987 // press BTN_BACK, release BTN_BACK
7988 processKey(mapper, BTN_BACK, 1);
7989 processSync(mapper);
7990 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7991 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7992 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007993
Michael Wrightd02c5b62014-02-10 15:10:22 -08007994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007995 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007996 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
7997
7998 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7999 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8000 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008001
8002 processKey(mapper, BTN_BACK, 0);
8003 processSync(mapper);
8004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008005 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008006 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008007
8008 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008009 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008010 ASSERT_EQ(0, motionArgs.buttonState);
8011
Michael Wrightd02c5b62014-02-10 15:10:22 -08008012 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8013 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8014 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8015
8016 // press BTN_SIDE, release BTN_SIDE
8017 processKey(mapper, BTN_SIDE, 1);
8018 processSync(mapper);
8019 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8020 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8021 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008022
Michael Wrightd02c5b62014-02-10 15:10:22 -08008023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008024 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008025 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
8026
8027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8028 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8029 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008030
8031 processKey(mapper, BTN_SIDE, 0);
8032 processSync(mapper);
8033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008034 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008035 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008036
8037 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008038 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008039 ASSERT_EQ(0, motionArgs.buttonState);
8040
Michael Wrightd02c5b62014-02-10 15:10:22 -08008041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8042 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8043 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
8044
8045 // press BTN_FORWARD, release BTN_FORWARD
8046 processKey(mapper, BTN_FORWARD, 1);
8047 processSync(mapper);
8048 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8049 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8050 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008051
Michael Wrightd02c5b62014-02-10 15:10:22 -08008052 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008053 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008054 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8055
8056 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8057 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8058 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008059
8060 processKey(mapper, BTN_FORWARD, 0);
8061 processSync(mapper);
8062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008063 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008064 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008065
8066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008067 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008068 ASSERT_EQ(0, motionArgs.buttonState);
8069
Michael Wrightd02c5b62014-02-10 15:10:22 -08008070 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8071 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8072 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8073
8074 // press BTN_EXTRA, release BTN_EXTRA
8075 processKey(mapper, BTN_EXTRA, 1);
8076 processSync(mapper);
8077 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8078 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
8079 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008080
Michael Wrightd02c5b62014-02-10 15:10:22 -08008081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008082 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008083 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
8084
8085 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8086 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8087 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008088
8089 processKey(mapper, BTN_EXTRA, 0);
8090 processSync(mapper);
8091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008092 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008093 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008094
8095 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008096 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008097 ASSERT_EQ(0, motionArgs.buttonState);
8098
Michael Wrightd02c5b62014-02-10 15:10:22 -08008099 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
8100 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
8101 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
8102
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8104
Michael Wrightd02c5b62014-02-10 15:10:22 -08008105 // press BTN_STYLUS, release BTN_STYLUS
8106 processKey(mapper, BTN_STYLUS, 1);
8107 processSync(mapper);
8108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8109 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008110 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
8111
8112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8113 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8114 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008115
8116 processKey(mapper, BTN_STYLUS, 0);
8117 processSync(mapper);
8118 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008119 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008120 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008121
8122 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(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008125
8126 // press BTN_STYLUS2, release BTN_STYLUS2
8127 processKey(mapper, BTN_STYLUS2, 1);
8128 processSync(mapper);
8129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008131 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
8132
8133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
8135 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008136
8137 processKey(mapper, BTN_STYLUS2, 0);
8138 processSync(mapper);
8139 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008140 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008141 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008142
8143 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08008144 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08008145 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008146
8147 // release touch
8148 processId(mapper, -1);
8149 processSync(mapper);
8150 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8151 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8152 ASSERT_EQ(0, motionArgs.buttonState);
8153}
8154
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008155TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
8156 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008157 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008158 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00008159 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008160
8161 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
8162 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
8163
8164 // Touch down.
8165 processId(mapper, 1);
8166 processPosition(mapper, 100, 200);
8167 processSync(mapper);
8168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8169 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
8170
8171 // Press and release button mapped to the primary stylus button.
8172 processKey(mapper, BTN_A, 1);
8173 processSync(mapper);
8174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8175 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8176 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8178 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8179 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8180
8181 processKey(mapper, BTN_A, 0);
8182 processSync(mapper);
8183 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8184 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8185 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8186 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8187
8188 // Press and release the HID usage mapped to the secondary stylus button.
8189 processHidUsage(mapper, 0xabcd, 1);
8190 processSync(mapper);
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8192 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8193 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8194 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8195 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8196 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
8197
8198 processHidUsage(mapper, 0xabcd, 0);
8199 processSync(mapper);
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
8202 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8203 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
8204
8205 // Release touch.
8206 processId(mapper, -1);
8207 processSync(mapper);
8208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8209 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8210}
8211
Michael Wrightd02c5b62014-02-10 15:10:22 -08008212TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008213 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008214 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008215 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008216 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008217
8218 NotifyMotionArgs motionArgs;
8219
8220 // default tool type is finger
8221 processId(mapper, 1);
8222 processPosition(mapper, 100, 200);
8223 processSync(mapper);
8224 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8225 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008226 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008227
8228 // eraser
8229 processKey(mapper, BTN_TOOL_RUBBER, 1);
8230 processSync(mapper);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8232 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008233 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008234
8235 // stylus
8236 processKey(mapper, BTN_TOOL_RUBBER, 0);
8237 processKey(mapper, BTN_TOOL_PEN, 1);
8238 processSync(mapper);
8239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008241 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008242
8243 // brush
8244 processKey(mapper, BTN_TOOL_PEN, 0);
8245 processKey(mapper, BTN_TOOL_BRUSH, 1);
8246 processSync(mapper);
8247 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8248 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008249 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008250
8251 // pencil
8252 processKey(mapper, BTN_TOOL_BRUSH, 0);
8253 processKey(mapper, BTN_TOOL_PENCIL, 1);
8254 processSync(mapper);
8255 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8256 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008257 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008258
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08008259 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08008260 processKey(mapper, BTN_TOOL_PENCIL, 0);
8261 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
8262 processSync(mapper);
8263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8264 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008265 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008266
8267 // mouse
8268 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
8269 processKey(mapper, BTN_TOOL_MOUSE, 1);
8270 processSync(mapper);
8271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8272 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008273 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008274
8275 // lens
8276 processKey(mapper, BTN_TOOL_MOUSE, 0);
8277 processKey(mapper, BTN_TOOL_LENS, 1);
8278 processSync(mapper);
8279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8280 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008281 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008282
8283 // double-tap
8284 processKey(mapper, BTN_TOOL_LENS, 0);
8285 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
8286 processSync(mapper);
8287 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8288 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008289 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008290
8291 // triple-tap
8292 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
8293 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
8294 processSync(mapper);
8295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008297 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008298
8299 // quad-tap
8300 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
8301 processKey(mapper, BTN_TOOL_QUADTAP, 1);
8302 processSync(mapper);
8303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8304 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008305 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008306
8307 // finger
8308 processKey(mapper, BTN_TOOL_QUADTAP, 0);
8309 processKey(mapper, BTN_TOOL_FINGER, 1);
8310 processSync(mapper);
8311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8312 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008313 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008314
8315 // stylus trumps finger
8316 processKey(mapper, BTN_TOOL_PEN, 1);
8317 processSync(mapper);
8318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8319 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008320 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008321
8322 // eraser trumps stylus
8323 processKey(mapper, BTN_TOOL_RUBBER, 1);
8324 processSync(mapper);
8325 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8326 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008327 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008328
8329 // mouse trumps eraser
8330 processKey(mapper, BTN_TOOL_MOUSE, 1);
8331 processSync(mapper);
8332 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8333 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008334 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008335
8336 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
8337 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
8338 processSync(mapper);
8339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8340 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008341 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008342
8343 // MT tool type trumps BTN tool types: MT_TOOL_PEN
8344 processToolType(mapper, MT_TOOL_PEN);
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 // back to default tool type
8351 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
8352 processKey(mapper, BTN_TOOL_MOUSE, 0);
8353 processKey(mapper, BTN_TOOL_RUBBER, 0);
8354 processKey(mapper, BTN_TOOL_PEN, 0);
8355 processKey(mapper, BTN_TOOL_FINGER, 0);
8356 processSync(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8358 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008359 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008360}
8361
8362TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008363 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008364 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008365 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008366 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008367 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008368
8369 NotifyMotionArgs motionArgs;
8370
8371 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
8372 processId(mapper, 1);
8373 processPosition(mapper, 100, 200);
8374 processSync(mapper);
8375 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8376 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8377 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8378 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8379
8380 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8381 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8382 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8383 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8384
8385 // move a little
8386 processPosition(mapper, 150, 250);
8387 processSync(mapper);
8388 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8389 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8390 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8391 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8392
8393 // down when BTN_TOUCH is pressed, pressure defaults to 1
8394 processKey(mapper, BTN_TOUCH, 1);
8395 processSync(mapper);
8396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8397 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8398 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8399 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8400
8401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8402 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8403 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8404 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8405
8406 // up when BTN_TOUCH is released, hover restored
8407 processKey(mapper, BTN_TOUCH, 0);
8408 processSync(mapper);
8409 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8410 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8411 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8412 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8413
8414 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8415 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8416 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8417 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8418
8419 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8420 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8421 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8422 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8423
8424 // exit hover when pointer goes away
8425 processId(mapper, -1);
8426 processSync(mapper);
8427 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8428 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8429 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8430 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8431}
8432
8433TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008434 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008435 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008436 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008437 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008438
8439 NotifyMotionArgs motionArgs;
8440
8441 // initially hovering because pressure is 0
8442 processId(mapper, 1);
8443 processPosition(mapper, 100, 200);
8444 processPressure(mapper, 0);
8445 processSync(mapper);
8446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8447 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8449 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8450
8451 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8452 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8453 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8454 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
8455
8456 // move a little
8457 processPosition(mapper, 150, 250);
8458 processSync(mapper);
8459 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8460 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8461 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8462 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8463
8464 // down when pressure becomes non-zero
8465 processPressure(mapper, RAW_PRESSURE_MAX);
8466 processSync(mapper);
8467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8468 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8469 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8470 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8471
8472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8473 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8474 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8475 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8476
8477 // up when pressure becomes 0, hover restored
8478 processPressure(mapper, 0);
8479 processSync(mapper);
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8481 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8483 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
8484
8485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8486 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
8487 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8488 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8489
8490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8491 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
8492 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8493 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8494
8495 // exit hover when pointer goes away
8496 processId(mapper, -1);
8497 processSync(mapper);
8498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8499 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
8500 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8501 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
8502}
8503
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008504/**
8505 * Set the input device port <--> display port associations, and check that the
8506 * events are routed to the display that matches the display port.
8507 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
8508 */
8509TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008510 const std::string usb2 = "USB2";
8511 const uint8_t hdmi1 = 0;
8512 const uint8_t hdmi2 = 1;
8513 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008514 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008515
8516 addConfigurationProperty("touch.deviceType", "touchScreen");
8517 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008518 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008519
8520 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
8521 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
8522
8523 // We are intentionally not adding the viewport for display 1 yet. Since the port association
8524 // for this input device is specified, and the matching viewport is not present,
8525 // the input device should be disabled (at the mapper level).
8526
8527 // Add viewport for display 2 on hdmi2
8528 prepareSecondaryDisplay(type, hdmi2);
8529 // Send a touch event
8530 processPosition(mapper, 100, 100);
8531 processSync(mapper);
8532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8533
8534 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +00008535 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07008536 // Send a touch event again
8537 processPosition(mapper, 100, 100);
8538 processSync(mapper);
8539
8540 NotifyMotionArgs args;
8541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8542 ASSERT_EQ(DISPLAY_ID, args.displayId);
8543}
Michael Wrightd02c5b62014-02-10 15:10:22 -08008544
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008545TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
8546 addConfigurationProperty("touch.deviceType", "touchScreen");
8547 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008548 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008549
8550 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
8551
Michael Wrighta9cf4192022-12-01 23:46:39 +00008552 prepareDisplay(ui::ROTATION_0);
8553 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +00008554
8555 // Send a touch event
8556 processPosition(mapper, 100, 100);
8557 processSync(mapper);
8558
8559 NotifyMotionArgs args;
8560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8561 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
8562}
8563
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008564TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008565 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -08008566
Michael Wrighta9cf4192022-12-01 23:46:39 +00008567 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008568 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008569 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008570
Josep del Río2d8c79a2023-01-23 19:33:50 +00008571 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008572
8573 NotifyMotionArgs motionArgs;
8574 processPosition(mapper, 100, 100);
8575 processSync(mapper);
8576
8577 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8578 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07008579 ASSERT_EQ(ui::LogicalDisplayId::INVALID, motionArgs.displayId);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08008580}
8581
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008582/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008583 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
8584 */
8585TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
8586 addConfigurationProperty("touch.deviceType", "touchScreen");
8587 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008588 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008589
Michael Wrighta9cf4192022-12-01 23:46:39 +00008590 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +00008591 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
8592 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
8593 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
8594 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008595
8596 NotifyMotionArgs args;
8597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8598 ASSERT_EQ(26, args.readTime);
8599
Harry Cutts33476232023-01-30 19:57:29 +00008600 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
8601 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
8602 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008603
8604 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8605 ASSERT_EQ(33, args.readTime);
8606}
8607
8608/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008609 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
8610 * events should not be delivered to the listener.
8611 */
8612TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
8613 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008614 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008615 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008616 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008617 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008618 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008619 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00008620
8621 NotifyMotionArgs motionArgs;
8622 processPosition(mapper, 100, 100);
8623 processSync(mapper);
8624
8625 mFakeListener->assertNotifyMotionWasNotCalled();
8626}
8627
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008628/**
8629 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
8630 * the touch mapper can process the events and the events can be delivered to the listener.
8631 */
8632TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
8633 addConfigurationProperty("touch.deviceType", "touchScreen");
8634 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008635 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008636 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008637 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008638 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008639 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008640
8641 NotifyMotionArgs motionArgs;
8642 processPosition(mapper, 100, 100);
8643 processSync(mapper);
8644
8645 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8646 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8647}
8648
Josh Thielene986aed2023-06-01 14:17:30 +00008649/**
8650 * When the viewport is deactivated (isActive transitions from true to false),
8651 * and touch.enableForInactiveViewport is false, touches prior to the transition
8652 * should be cancelled.
8653 */
Garfield Tanc734e4f2021-01-15 20:01:39 -08008654TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
8655 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -07008656 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008657 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00008658 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008659 std::optional<DisplayViewport> optionalDisplayViewport =
8660 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8661 ASSERT_TRUE(optionalDisplayViewport.has_value());
8662 DisplayViewport displayViewport = *optionalDisplayViewport;
8663
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008664 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008665 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008666 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -08008667
8668 // Finger down
8669 int32_t x = 100, y = 100;
8670 processPosition(mapper, x, y);
8671 processSync(mapper);
8672
8673 NotifyMotionArgs motionArgs;
8674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8675 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8676
8677 // Deactivate display viewport
8678 displayViewport.isActive = false;
8679 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008680 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008681
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008682 // The ongoing touch should be canceled immediately
8683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8684 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8685
8686 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -08008687 x += 10, y += 10;
8688 processPosition(mapper, x, y);
8689 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008690 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -08008691
8692 // Reactivate display viewport
8693 displayViewport.isActive = true;
8694 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008695 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008696
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008697 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -08008698 x += 10, y += 10;
8699 processPosition(mapper, x, y);
8700 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00008701 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8702 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -08008703}
8704
Josh Thielene986aed2023-06-01 14:17:30 +00008705/**
8706 * When the viewport is deactivated (isActive transitions from true to false),
8707 * and touch.enableForInactiveViewport is true, touches prior to the transition
8708 * should not be cancelled.
8709 */
8710TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
8711 addConfigurationProperty("touch.deviceType", "touchScreen");
8712 addConfigurationProperty("touch.enableForInactiveViewport", "1");
8713 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
8714 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
8715 std::optional<DisplayViewport> optionalDisplayViewport =
8716 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
8717 ASSERT_TRUE(optionalDisplayViewport.has_value());
8718 DisplayViewport displayViewport = *optionalDisplayViewport;
8719
8720 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8721 prepareAxes(POSITION);
8722 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
8723
8724 // Finger down
8725 int32_t x = 100, y = 100;
8726 processPosition(mapper, x, y);
8727 processSync(mapper);
8728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8729 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
8730
8731 // Deactivate display viewport
8732 displayViewport.isActive = false;
8733 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8734 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8735
8736 // The ongoing touch should not be canceled
8737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8738
8739 // Finger move is not ignored
8740 x += 10, y += 10;
8741 processPosition(mapper, x, y);
8742 processSync(mapper);
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8744 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8745
8746 // Reactivate display viewport
8747 displayViewport.isActive = true;
8748 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
8749 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
8750
8751 // Finger move continues and does not start new gesture
8752 x += 10, y += 10;
8753 processPosition(mapper, x, y);
8754 processSync(mapper);
8755 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8756 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
8757}
8758
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008759TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008760 prepareAxes(POSITION);
8761 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008762 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00008763 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008764
8765 NotifyMotionArgs motionArgs;
8766 // Unrotated video frame
8767 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8768 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008769 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -06008770 processPosition(mapper, 100, 200);
8771 processSync(mapper);
8772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8773 ASSERT_EQ(frames, motionArgs.videoFrames);
8774
8775 // Subsequent touch events should not have any videoframes
8776 // This is implemented separately in FakeEventHub,
8777 // but that should match the behaviour of TouchVideoDevice.
8778 processPosition(mapper, 200, 200);
8779 processSync(mapper);
8780 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8781 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
8782}
8783
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008784TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008785 prepareAxes(POSITION);
8786 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008787 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008788 // Unrotated video frame
8789 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8790 NotifyMotionArgs motionArgs;
8791
8792 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008793 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008794 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008795 clearViewports();
8796 prepareDisplay(orientation);
8797 std::vector<TouchVideoFrame> frames{frame};
8798 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8799 processPosition(mapper, 100, 200);
8800 processSync(mapper);
8801 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8802 ASSERT_EQ(frames, motionArgs.videoFrames);
8803 }
8804}
8805
8806TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
8807 prepareAxes(POSITION);
8808 addConfigurationProperty("touch.deviceType", "touchScreen");
8809 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8810 // orientation-aware are affected by display rotation.
8811 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008812 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008813 // Unrotated video frame
8814 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8815 NotifyMotionArgs motionArgs;
8816
8817 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +00008818 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Harry Cuttsc57cd3c2024-04-24 13:52:55 +00008819 SCOPED_TRACE(StringPrintf("Orientation %s", ftl::enum_string(orientation).c_str()));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008820 clearViewports();
8821 prepareDisplay(orientation);
8822 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008823 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008824 processPosition(mapper, 100, 200);
8825 processSync(mapper);
8826 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008827 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8828 // compared to the display. This is so that when the window transform (which contains the
8829 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8830 // window's coordinate space.
8831 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008832 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +08008833
8834 // Release finger.
8835 processSync(mapper);
8836 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008837 }
8838}
8839
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008840TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008841 prepareAxes(POSITION);
8842 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00008843 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008844 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8845 // so mix these.
8846 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8847 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8848 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8849 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8850 NotifyMotionArgs motionArgs;
8851
Michael Wrighta9cf4192022-12-01 23:46:39 +00008852 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008853 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008854 processPosition(mapper, 100, 200);
8855 processSync(mapper);
8856 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008857 ASSERT_EQ(frames, motionArgs.videoFrames);
8858}
8859
8860TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
8861 prepareAxes(POSITION);
8862 addConfigurationProperty("touch.deviceType", "touchScreen");
8863 // Since InputReader works in the un-rotated coordinate space, only devices that are not
8864 // orientation-aware are affected by display rotation.
8865 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00008866 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008867 // Unrotated video frames. There's no rule that they must all have the same dimensions,
8868 // so mix these.
8869 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
8870 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
8871 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
8872 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
8873 NotifyMotionArgs motionArgs;
8874
Michael Wrighta9cf4192022-12-01 23:46:39 +00008875 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008876 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
8877 processPosition(mapper, 100, 200);
8878 processSync(mapper);
8879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8880 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
8881 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
8882 // compared to the display. This is so that when the window transform (which contains the
8883 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
8884 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +00008885 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -07008886 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -06008887 ASSERT_EQ(frames, motionArgs.videoFrames);
8888}
8889
Arthur Hung9da14732019-09-02 16:16:58 +08008890/**
8891 * If we had defined port associations, but the viewport is not ready, the touch device would be
8892 * expected to be disabled, and it should be enabled after the viewport has found.
8893 */
8894TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +08008895 constexpr uint8_t hdmi2 = 1;
8896 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +01008897 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +08008898
8899 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
8900
8901 addConfigurationProperty("touch.deviceType", "touchScreen");
8902 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008903 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +08008904
8905 ASSERT_EQ(mDevice->isEnabled(), false);
8906
8907 // Add display on hdmi2, the device should be enabled and can receive touch event.
8908 prepareSecondaryDisplay(type, hdmi2);
8909 ASSERT_EQ(mDevice->isEnabled(), true);
8910
8911 // Send a touch event.
8912 processPosition(mapper, 100, 100);
8913 processSync(mapper);
8914
8915 NotifyMotionArgs args;
8916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
8917 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
8918}
8919
Arthur Hung421eb1c2020-01-16 00:09:42 +08008920TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008921 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008922 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008923 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008924 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008925
8926 NotifyMotionArgs motionArgs;
8927
8928 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
8929 // finger down
8930 processId(mapper, 1);
8931 processPosition(mapper, x1, y1);
8932 processSync(mapper);
8933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8934 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008935 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008936
8937 // finger move
8938 processId(mapper, 1);
8939 processPosition(mapper, x2, y2);
8940 processSync(mapper);
8941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8942 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008943 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008944
8945 // finger up.
8946 processId(mapper, -1);
8947 processSync(mapper);
8948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8949 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008950 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008951
8952 // new finger down
8953 processId(mapper, 1);
8954 processPosition(mapper, x3, y3);
8955 processSync(mapper);
8956 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8957 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008958 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008959}
8960
8961/**
arthurhungcc7f9802020-04-30 17:55:40 +08008962 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
8963 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +08008964 */
arthurhungcc7f9802020-04-30 17:55:40 +08008965TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +08008966 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008967 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008968 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00008969 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +08008970
8971 NotifyMotionArgs motionArgs;
8972
8973 // default tool type is finger
8974 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +08008975 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008976 processPosition(mapper, x1, y1);
8977 processSync(mapper);
8978 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8979 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008981
8982 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
8983 processToolType(mapper, MT_TOOL_PALM);
8984 processSync(mapper);
8985 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8986 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
8987
8988 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +08008989 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008990 processPosition(mapper, x2, y2);
8991 processSync(mapper);
8992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8993
8994 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +08008995 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08008996 processSync(mapper);
8997 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8998
8999 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +08009000 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009001 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009002 processPosition(mapper, x3, y3);
9003 processSync(mapper);
9004 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9005 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009006 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +08009007}
9008
arthurhungbf89a482020-04-17 17:37:55 +08009009/**
arthurhungcc7f9802020-04-30 17:55:40 +08009010 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9011 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +08009012 */
arthurhungcc7f9802020-04-30 17:55:40 +08009013TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +08009014 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009015 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +08009016 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009017 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +08009018
9019 NotifyMotionArgs motionArgs;
9020
9021 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +08009022 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9023 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009024 processPosition(mapper, x1, y1);
9025 processSync(mapper);
9026 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9027 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009028 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009029
9030 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +08009031 processSlot(mapper, SECOND_SLOT);
9032 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009033 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +08009034 processSync(mapper);
9035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009036 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009037 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009038
9039 // If the tool type of the first finger changes to MT_TOOL_PALM,
9040 // we expect to receive ACTION_POINTER_UP with cancel flag.
9041 processSlot(mapper, FIRST_SLOT);
9042 processId(mapper, FIRST_TRACKING_ID);
9043 processToolType(mapper, MT_TOOL_PALM);
9044 processSync(mapper);
9045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009046 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009047 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9048
9049 // The following MOVE events of second finger should be processed.
9050 processSlot(mapper, SECOND_SLOT);
9051 processId(mapper, SECOND_TRACKING_ID);
9052 processPosition(mapper, x2 + 1, y2 + 1);
9053 processSync(mapper);
9054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9055 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009056 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009057
9058 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
9059 // it. Second finger receive move.
9060 processSlot(mapper, FIRST_SLOT);
9061 processId(mapper, INVALID_TRACKING_ID);
9062 processSync(mapper);
9063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9064 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009065 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009066
9067 // Second finger keeps moving.
9068 processSlot(mapper, SECOND_SLOT);
9069 processId(mapper, SECOND_TRACKING_ID);
9070 processPosition(mapper, x2 + 2, y2 + 2);
9071 processSync(mapper);
9072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9073 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009074 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009075
9076 // Second finger up.
9077 processId(mapper, INVALID_TRACKING_ID);
9078 processSync(mapper);
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9080 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9081 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9082}
9083
9084/**
9085 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
9086 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
9087 */
9088TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
9089 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009090 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009091 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009092 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009093
9094 NotifyMotionArgs motionArgs;
9095
9096 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
9097 // First finger down.
9098 processId(mapper, FIRST_TRACKING_ID);
9099 processPosition(mapper, x1, y1);
9100 processSync(mapper);
9101 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9102 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009103 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009104
9105 // Second finger down.
9106 processSlot(mapper, SECOND_SLOT);
9107 processId(mapper, SECOND_TRACKING_ID);
9108 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +08009109 processSync(mapper);
9110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009111 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009112 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +08009113
arthurhungcc7f9802020-04-30 17:55:40 +08009114 // If the tool type of the first finger changes to MT_TOOL_PALM,
9115 // we expect to receive ACTION_POINTER_UP with cancel flag.
9116 processSlot(mapper, FIRST_SLOT);
9117 processId(mapper, FIRST_TRACKING_ID);
9118 processToolType(mapper, MT_TOOL_PALM);
9119 processSync(mapper);
9120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009121 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009122 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9123
9124 // Second finger keeps moving.
9125 processSlot(mapper, SECOND_SLOT);
9126 processId(mapper, SECOND_TRACKING_ID);
9127 processPosition(mapper, x2 + 1, y2 + 1);
9128 processSync(mapper);
9129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9130 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9131
9132 // second finger becomes palm, receive cancel due to only 1 finger is active.
9133 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +08009134 processToolType(mapper, MT_TOOL_PALM);
9135 processSync(mapper);
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9138
arthurhungcc7f9802020-04-30 17:55:40 +08009139 // third finger down.
9140 processSlot(mapper, THIRD_SLOT);
9141 processId(mapper, THIRD_TRACKING_ID);
9142 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +08009143 processPosition(mapper, x3, y3);
9144 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +08009145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9146 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009147 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009148 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009149
9150 // third finger move
9151 processId(mapper, THIRD_TRACKING_ID);
9152 processPosition(mapper, x3 + 1, y3 + 1);
9153 processSync(mapper);
9154 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9155 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9156
9157 // first finger up, third finger receive move.
9158 processSlot(mapper, FIRST_SLOT);
9159 processId(mapper, INVALID_TRACKING_ID);
9160 processSync(mapper);
9161 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9162 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009163 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009164
9165 // second finger up, third finger receive move.
9166 processSlot(mapper, SECOND_SLOT);
9167 processId(mapper, INVALID_TRACKING_ID);
9168 processSync(mapper);
9169 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9170 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009171 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009172
9173 // third finger up.
9174 processSlot(mapper, THIRD_SLOT);
9175 processId(mapper, INVALID_TRACKING_ID);
9176 processSync(mapper);
9177 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9178 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9179 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9180}
9181
9182/**
9183 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
9184 * and the active finger could still be allowed to receive the events
9185 */
9186TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
9187 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009188 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +08009189 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009190 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +08009191
9192 NotifyMotionArgs motionArgs;
9193
9194 // default tool type is finger
9195 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
9196 processId(mapper, FIRST_TRACKING_ID);
9197 processPosition(mapper, x1, y1);
9198 processSync(mapper);
9199 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9200 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009201 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009202
9203 // Second finger down.
9204 processSlot(mapper, SECOND_SLOT);
9205 processId(mapper, SECOND_TRACKING_ID);
9206 processPosition(mapper, x2, y2);
9207 processSync(mapper);
9208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009209 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +08009211
9212 // If the tool type of the second finger changes to MT_TOOL_PALM,
9213 // we expect to receive ACTION_POINTER_UP with cancel flag.
9214 processId(mapper, SECOND_TRACKING_ID);
9215 processToolType(mapper, MT_TOOL_PALM);
9216 processSync(mapper);
9217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009218 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +08009219 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
9220
9221 // The following MOVE event should be processed.
9222 processSlot(mapper, FIRST_SLOT);
9223 processId(mapper, FIRST_TRACKING_ID);
9224 processPosition(mapper, x1 + 1, y1 + 1);
9225 processSync(mapper);
9226 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9227 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009228 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +08009229
9230 // second finger up.
9231 processSlot(mapper, SECOND_SLOT);
9232 processId(mapper, INVALID_TRACKING_ID);
9233 processSync(mapper);
9234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9235 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9236
9237 // first finger keep moving
9238 processSlot(mapper, FIRST_SLOT);
9239 processId(mapper, FIRST_TRACKING_ID);
9240 processPosition(mapper, x1 + 2, y1 + 2);
9241 processSync(mapper);
9242 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9243 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9244
9245 // first finger up.
9246 processId(mapper, INVALID_TRACKING_ID);
9247 processSync(mapper);
9248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9249 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9250 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +08009251}
9252
Arthur Hung9ad18942021-06-19 02:04:46 +00009253/**
9254 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
9255 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
9256 * cause slot be valid again.
9257 */
9258TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
9259 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009260 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +00009261 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009262 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +00009263
9264 NotifyMotionArgs motionArgs;
9265
9266 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
9267 // First finger down.
9268 processId(mapper, FIRST_TRACKING_ID);
9269 processPosition(mapper, x1, y1);
9270 processPressure(mapper, RAW_PRESSURE_MAX);
9271 processSync(mapper);
9272 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9273 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009274 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009275
9276 // First finger move.
9277 processId(mapper, FIRST_TRACKING_ID);
9278 processPosition(mapper, x1 + 1, y1 + 1);
9279 processPressure(mapper, RAW_PRESSURE_MAX);
9280 processSync(mapper);
9281 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9282 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009283 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009284
9285 // Second finger down.
9286 processSlot(mapper, SECOND_SLOT);
9287 processId(mapper, SECOND_TRACKING_ID);
9288 processPosition(mapper, x2, y2);
9289 processPressure(mapper, RAW_PRESSURE_MAX);
9290 processSync(mapper);
9291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009292 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009293 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009294
9295 // second finger up with some unexpected data.
9296 processSlot(mapper, SECOND_SLOT);
9297 processId(mapper, INVALID_TRACKING_ID);
9298 processPosition(mapper, x2, y2);
9299 processSync(mapper);
9300 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009301 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009302 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009303
9304 // first finger up with some unexpected data.
9305 processSlot(mapper, FIRST_SLOT);
9306 processId(mapper, INVALID_TRACKING_ID);
9307 processPosition(mapper, x2, y2);
9308 processPressure(mapper, RAW_PRESSURE_MAX);
9309 processSync(mapper);
9310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9311 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009312 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +00009313}
9314
Arpit Singh4b4a4572023-11-24 18:19:56 +00009315TEST_F(MultiTouchInputMapperTest, Reset_RepopulatesMultiTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009316 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009317 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009318 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009319 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009320
9321 // First finger down.
Arpit Singh4b4a4572023-11-24 18:19:56 +00009322 constexpr int32_t x1 = 100, y1 = 200, x2 = 300, y2 = 400;
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009323 processId(mapper, FIRST_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009324 processPosition(mapper, x1, y1);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009325 processPressure(mapper, RAW_PRESSURE_MAX);
9326 processSync(mapper);
9327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9328 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9329
9330 // Second finger down.
9331 processSlot(mapper, SECOND_SLOT);
9332 processId(mapper, SECOND_TRACKING_ID);
Arpit Singh4b4a4572023-11-24 18:19:56 +00009333 processPosition(mapper, x2, y2);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009334 processPressure(mapper, RAW_PRESSURE_MAX);
9335 processSync(mapper);
9336 ASSERT_NO_FATAL_FAILURE(
9337 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
9338
Arpit Singh4b4a4572023-11-24 18:19:56 +00009339 // Set MT Slot state to be repopulated for the required slots
9340 std::vector<int32_t> mtSlotValues(RAW_SLOT_MAX + 1, -1);
9341 mtSlotValues[0] = FIRST_TRACKING_ID;
9342 mtSlotValues[1] = SECOND_TRACKING_ID;
9343 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_TRACKING_ID, mtSlotValues);
9344
9345 mtSlotValues[0] = x1;
9346 mtSlotValues[1] = x2;
9347 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_X, mtSlotValues);
9348
9349 mtSlotValues[0] = y1;
9350 mtSlotValues[1] = y2;
9351 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_POSITION_Y, mtSlotValues);
9352
9353 mtSlotValues[0] = RAW_PRESSURE_MAX;
9354 mtSlotValues[1] = RAW_PRESSURE_MAX;
9355 mFakeEventHub->setMtSlotValues(EVENTHUB_ID, ABS_MT_PRESSURE, mtSlotValues);
9356
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009357 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
Arpit Singh4b4a4572023-11-24 18:19:56 +00009358 // repopulated. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009359 resetMapper(mapper, ARBITRARY_TIME);
9360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9361 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009362
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009363 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
9364 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009365 processPosition(mapper, 301, 302);
9366 processSync(mapper);
9367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9368 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009369 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9370 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009371
9372 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9373}
9374
Arpit Singh4bb0bd52023-12-20 14:41:10 +00009375TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009376 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009377 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009378 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009379 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009380
9381 // First finger touches down and releases.
9382 processId(mapper, FIRST_TRACKING_ID);
9383 processPosition(mapper, 100, 200);
9384 processPressure(mapper, RAW_PRESSURE_MAX);
9385 processSync(mapper);
9386 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9387 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
9388 processId(mapper, INVALID_TRACKING_ID);
9389 processSync(mapper);
9390 ASSERT_NO_FATAL_FAILURE(
9391 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
9392
9393 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
9394 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00009395 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00009396 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9397
9398 // Send an empty sync frame. Since there are no pointers, no events are generated.
9399 processSync(mapper);
9400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9401}
9402
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009403TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009404 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009405 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009406 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009407 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009408 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009409
9410 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
9411 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
9412 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
9413 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
9414 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9415
9416 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009417 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009418 processId(mapper, FIRST_TRACKING_ID);
9419 processToolType(mapper, MT_TOOL_PEN);
9420 processPosition(mapper, 100, 200);
9421 processPressure(mapper, RAW_PRESSURE_MAX);
9422 processSync(mapper);
9423 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9424 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9425 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009426 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009427
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +00009428 // Now that we know the device supports styluses, ensure that the device is re-configured with
9429 // the stylus source.
9430 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
9431 {
9432 const auto& devices = mReader->getInputDevices();
9433 auto deviceInfo =
9434 std::find_if(devices.begin(), devices.end(),
9435 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
9436 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
9437 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
9438 }
9439
9440 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
9441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
9442
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009443 processId(mapper, INVALID_TRACKING_ID);
9444 processSync(mapper);
9445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9447 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009448 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +00009449}
9450
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009451// --- MultiTouchInputMapperTest_ExternalDevice ---
9452
9453class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
9454protected:
Chris Yea52ade12020-08-27 16:49:20 -07009455 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009456};
9457
9458/**
9459 * Expect fallback to internal viewport if device is external and external viewport is not present.
9460 */
9461TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
9462 prepareAxes(POSITION);
9463 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009464 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009465 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009466
9467 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
9468
9469 NotifyMotionArgs motionArgs;
9470
9471 // Expect the event to be sent to the internal viewport,
9472 // because an external viewport is not present.
9473 processPosition(mapper, 100, 100);
9474 processSync(mapper);
9475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakoucfbee532024-05-10 13:41:35 -07009476 ASSERT_EQ(ui::LogicalDisplayId::DEFAULT, motionArgs.displayId);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009477
9478 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +01009479 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009480 processPosition(mapper, 100, 100);
9481 processSync(mapper);
9482 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9483 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
9484}
Arthur Hung4197f6b2020-03-16 15:39:59 +08009485
Prabir Pradhan3ed7e352024-05-03 23:59:43 +00009486// TODO(b/281840344): Remove the test when the old touchpad stack is removed. It is currently
9487// unclear what the behavior of the touchpad logic in TouchInputMapper should do after the
9488// PointerChoreographer refactor.
9489TEST_F(MultiTouchInputMapperTest, DISABLED_Process_TouchpadPointer) {
Harry Cutts8722be92024-04-05 14:46:05 +00009490 // prepare device
Michael Wrighta9cf4192022-12-01 23:46:39 +00009491 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009492 prepareAxes(POSITION | ID | SLOT);
9493 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
9494 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009495 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009496 // run uncaptured pointer tests - pushes out generic events
9497 // FINGER 0 DOWN
9498 processId(mapper, 3);
9499 processPosition(mapper, 100, 100);
9500 processKey(mapper, BTN_TOUCH, 1);
9501 processSync(mapper);
9502
9503 // start at (100,100), cursor should be at (0,0) * scale
9504 NotifyMotionArgs args;
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9507 ASSERT_NO_FATAL_FAILURE(
9508 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
9509
9510 // FINGER 0 MOVE
9511 processPosition(mapper, 200, 200);
9512 processSync(mapper);
9513
9514 // compute scaling to help with touch position checking
9515 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9516 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9517 float scale =
9518 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9519
9520 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
9521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9522 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
9523 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
9524 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +08009525
9526 // BUTTON DOWN
9527 processKey(mapper, BTN_LEFT, 1);
9528 processSync(mapper);
9529
9530 // touchinputmapper design sends a move before button press
9531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9532 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9533 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9534 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
9535
9536 // BUTTON UP
9537 processKey(mapper, BTN_LEFT, 0);
9538 processSync(mapper);
9539
9540 // touchinputmapper design sends a move after button release
9541 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9542 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9544 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009545}
9546
Harry Cutts8722be92024-04-05 14:46:05 +00009547TEST_F(MultiTouchInputMapperTest, Touchpad_GetSources) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00009548 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009549 prepareAxes(POSITION | ID | SLOT);
9550 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Hiroki Sato25040232024-02-22 17:21:22 +09009551 mFakePolicy->setPointerCapture(/*window=*/nullptr);
Arpit Singha8c236b2023-04-25 13:56:05 +00009552 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009553
Josep del Río2d8c79a2023-01-23 19:33:50 +00009554 // uncaptured touchpad should be a pointer device
9555 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -08009556}
9557
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009558// --- BluetoothMultiTouchInputMapperTest ---
9559
9560class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
9561protected:
9562 void SetUp() override {
9563 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
9564 }
9565};
9566
9567TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
9568 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009569 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009570 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009571 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00009572
9573 nsecs_t kernelEventTime = ARBITRARY_TIME;
9574 nsecs_t expectedEventTime = ARBITRARY_TIME;
9575 // Touch down.
9576 processId(mapper, FIRST_TRACKING_ID);
9577 processPosition(mapper, 100, 200);
9578 processPressure(mapper, RAW_PRESSURE_MAX);
9579 processSync(mapper, ARBITRARY_TIME);
9580 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9581 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
9582
9583 // Process several events that come in quick succession, according to their timestamps.
9584 for (int i = 0; i < 3; i++) {
9585 constexpr static nsecs_t delta = ms2ns(1);
9586 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
9587 kernelEventTime += delta;
9588 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
9589
9590 processPosition(mapper, 101 + i, 201 + i);
9591 processSync(mapper, kernelEventTime);
9592 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9593 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9594 WithEventTime(expectedEventTime))));
9595 }
9596
9597 // Release the touch.
9598 processId(mapper, INVALID_TRACKING_ID);
9599 processPressure(mapper, RAW_PRESSURE_MIN);
9600 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9602 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
9603 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
9604}
9605
9606// --- MultiTouchPointerModeTest ---
9607
HQ Liue6983c72022-04-19 22:14:56 +00009608class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
9609protected:
9610 float mPointerMovementScale;
9611 float mPointerXZoomScale;
9612 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
9613 addConfigurationProperty("touch.deviceType", "pointer");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009614 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +00009615
9616 prepareAxes(POSITION);
9617 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
9618 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
9619 // needs to be disabled, and the pointer gesture needs to be enabled.
Hiroki Sato25040232024-02-22 17:21:22 +09009620 mFakePolicy->setPointerCapture(/*window=*/nullptr);
HQ Liue6983c72022-04-19 22:14:56 +00009621 mFakePolicy->setPointerGestureEnabled(true);
HQ Liue6983c72022-04-19 22:14:56 +00009622
9623 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
9624 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
9625 mPointerMovementScale =
9626 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
9627 mPointerXZoomScale =
9628 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
9629 }
9630
9631 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
9632 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
9633 /*flat*/ 0,
9634 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
9635 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
9636 /*flat*/ 0,
9637 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
9638 }
9639};
9640
9641/**
9642 * Two fingers down on a pointer mode touch pad. The width
9643 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
9644 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
9645 * be greater than the both value to be freeform gesture, so that after two
9646 * fingers start to move downwards, the gesture should be swipe.
9647 */
9648TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
9649 // The min freeform gesture width is 25units/mm x 30mm = 750
9650 // which is greater than fraction of the diagnal length of the touchpad (349).
9651 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +00009652 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009653 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009654 NotifyMotionArgs motionArgs;
9655
9656 // Two fingers down at once.
9657 // The two fingers are 450 units apart, expects the current gesture to be PRESS
9658 // Pointer's initial position is used the [0,0] coordinate.
9659 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9660
9661 processId(mapper, FIRST_TRACKING_ID);
9662 processPosition(mapper, x1, y1);
9663 processMTSync(mapper);
9664 processId(mapper, SECOND_TRACKING_ID);
9665 processPosition(mapper, x2, y2);
9666 processMTSync(mapper);
9667 processSync(mapper);
9668
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009670 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009671 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009672 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009673 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009674 ASSERT_NO_FATAL_FAILURE(
9675 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9676
9677 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9678 // that there should be 1 pointer.
9679 int32_t movingDistance = 200;
9680 y1 += movingDistance;
9681 y2 += movingDistance;
9682
9683 processId(mapper, FIRST_TRACKING_ID);
9684 processPosition(mapper, x1, y1);
9685 processMTSync(mapper);
9686 processId(mapper, SECOND_TRACKING_ID);
9687 processPosition(mapper, x2, y2);
9688 processMTSync(mapper);
9689 processSync(mapper);
9690
9691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009692 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009693 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009694 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009695 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009696 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9697 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9698 0, 0, 0, 0));
9699}
9700
9701/**
9702 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
9703 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
9704 * the touch pack diagnal length. Two fingers' distance must be greater than the both
9705 * value to be freeform gesture, so that after two fingers start to move downwards,
9706 * the gesture should be swipe.
9707 */
9708TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
9709 // The min freeform gesture width is 5units/mm x 30mm = 150
9710 // which is greater than fraction of the diagnal length of the touchpad (349).
9711 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +00009712 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +00009713 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009714 NotifyMotionArgs motionArgs;
9715
9716 // Two fingers down at once.
9717 // The two fingers are 250 units apart, expects the current gesture to be PRESS
9718 // Pointer's initial position is used the [0,0] coordinate.
9719 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
9720
9721 processId(mapper, FIRST_TRACKING_ID);
9722 processPosition(mapper, x1, y1);
9723 processMTSync(mapper);
9724 processId(mapper, SECOND_TRACKING_ID);
9725 processPosition(mapper, x2, y2);
9726 processMTSync(mapper);
9727 processSync(mapper);
9728
9729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009730 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009731 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009732 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009733 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009734 ASSERT_NO_FATAL_FAILURE(
9735 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9736
9737 // It should be recognized as a SWIPE gesture when two fingers start to move down,
9738 // and there should be 1 pointer.
9739 int32_t movingDistance = 200;
9740 y1 += movingDistance;
9741 y2 += movingDistance;
9742
9743 processId(mapper, FIRST_TRACKING_ID);
9744 processPosition(mapper, x1, y1);
9745 processMTSync(mapper);
9746 processId(mapper, SECOND_TRACKING_ID);
9747 processPosition(mapper, x2, y2);
9748 processMTSync(mapper);
9749 processSync(mapper);
9750
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009752 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009753 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009754 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009755 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009756 // New coordinate is the scaled relative coordinate from the initial coordinate.
9757 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
9758 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9759 0, 0, 0, 0));
9760}
9761
9762/**
9763 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
9764 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
9765 * freeform gestures after two fingers start to move downwards.
9766 */
9767TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +00009768 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009769 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +00009770
9771 NotifyMotionArgs motionArgs;
9772
9773 // Two fingers down at once. Wider than the max swipe width.
9774 // The gesture is expected to be PRESS, then transformed to FREEFORM
9775 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
9776
9777 processId(mapper, FIRST_TRACKING_ID);
9778 processPosition(mapper, x1, y1);
9779 processMTSync(mapper);
9780 processId(mapper, SECOND_TRACKING_ID);
9781 processPosition(mapper, x2, y2);
9782 processMTSync(mapper);
9783 processSync(mapper);
9784
9785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009786 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009787 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009788 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009789 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009790 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
9791 ASSERT_NO_FATAL_FAILURE(
9792 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
9793
9794 int32_t movingDistance = 200;
9795
9796 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
9797 // then two down events for two pointers.
9798 y1 += movingDistance;
9799 y2 += movingDistance;
9800
9801 processId(mapper, FIRST_TRACKING_ID);
9802 processPosition(mapper, x1, y1);
9803 processMTSync(mapper);
9804 processId(mapper, SECOND_TRACKING_ID);
9805 processPosition(mapper, x2, y2);
9806 processMTSync(mapper);
9807 processSync(mapper);
9808
9809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9810 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009811 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009812 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009814 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009815 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009816 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9817 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009818 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009819 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009820 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009821 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009822 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009823 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009824 // Two pointers' scaled relative coordinates from their initial centroid.
9825 // Initial y coordinates are 0 as y1 and y2 have the same value.
9826 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
9827 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
9828 // When pointers move, the new coordinates equal to the initial coordinates plus
9829 // scaled moving distance.
9830 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9831 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9832 0, 0, 0, 0));
9833 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9834 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
9835 0, 0, 0, 0));
9836
9837 // Move two fingers down again, expect one MOVE motion event.
9838 y1 += movingDistance;
9839 y2 += movingDistance;
9840
9841 processId(mapper, FIRST_TRACKING_ID);
9842 processPosition(mapper, x1, y1);
9843 processMTSync(mapper);
9844 processId(mapper, SECOND_TRACKING_ID);
9845 processPosition(mapper, x2, y2);
9846 processMTSync(mapper);
9847 processSync(mapper);
9848
9849 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009850 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +00009851 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009852 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +00009853 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +00009854 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
9855 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9856 0, 0, 0, 0, 0));
9857 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
9858 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
9859 0, 0, 0, 0, 0));
9860}
9861
Harry Cutts39b7ca22022-10-05 15:55:48 +00009862TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +00009863 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +00009864 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +00009865 NotifyMotionArgs motionArgs;
9866
9867 // Place two fingers down.
9868 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
9869
9870 processId(mapper, FIRST_TRACKING_ID);
9871 processPosition(mapper, x1, y1);
9872 processMTSync(mapper);
9873 processId(mapper, SECOND_TRACKING_ID);
9874 processPosition(mapper, x2, y2);
9875 processMTSync(mapper);
9876 processSync(mapper);
9877
9878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009879 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +00009880 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9881 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
9882 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
9883 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
9884
9885 // Move the two fingers down and to the left.
9886 int32_t movingDistance = 200;
9887 x1 -= movingDistance;
9888 y1 += movingDistance;
9889 x2 -= movingDistance;
9890 y2 += movingDistance;
9891
9892 processId(mapper, FIRST_TRACKING_ID);
9893 processPosition(mapper, x1, y1);
9894 processMTSync(mapper);
9895 processId(mapper, SECOND_TRACKING_ID);
9896 processPosition(mapper, x2, y2);
9897 processMTSync(mapper);
9898 processSync(mapper);
9899
9900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009901 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +00009902 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9903 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
9904 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
9905 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
9906}
9907
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009908TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +00009909 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009910 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009911 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
9913
9914 // Start a stylus gesture.
9915 processKey(mapper, BTN_TOOL_PEN, 1);
9916 processId(mapper, FIRST_TRACKING_ID);
9917 processPosition(mapper, 100, 200);
9918 processSync(mapper);
9919 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9920 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
9921 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009922 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009923 // TODO(b/257078296): Pointer mode generates extra event.
9924 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9925 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9926 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009927 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009928 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9929
9930 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
9931 // gesture should be disabled.
9932 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
9933 viewport->isActive = false;
9934 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00009935 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009936 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9937 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
9938 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009939 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009940 // TODO(b/257078296): Pointer mode generates extra event.
9941 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9942 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
9943 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009944 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +00009945 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9946}
9947
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009948// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -08009949
Chris Ye1dd2e5c2021-04-04 23:12:41 -07009950class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009951protected:
9952 static const char* DEVICE_NAME;
9953 static const char* DEVICE_LOCATION;
9954 static const int32_t DEVICE_ID;
9955 static const int32_t DEVICE_GENERATION;
9956 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009957 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009958 static const int32_t EVENTHUB_ID;
9959
9960 std::shared_ptr<FakeEventHub> mFakeEventHub;
9961 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009962 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -08009963 std::unique_ptr<InstrumentedInputReader> mReader;
9964 std::shared_ptr<InputDevice> mDevice;
9965
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009966 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009967 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07009968 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009969 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009970 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009971 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -08009972 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
9973 }
9974
9975 void SetUp() override { SetUp(DEVICE_CLASSES); }
9976
9977 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -07009978 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -08009979 mFakePolicy.clear();
9980 }
9981
Chris Yee2b1e5c2021-03-10 22:45:12 -08009982 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
9983 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07009984 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -08009985 InputDeviceIdentifier identifier;
9986 identifier.name = name;
9987 identifier.location = location;
9988 std::shared_ptr<InputDevice> device =
9989 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
9990 identifier);
9991 mReader->pushNextDevice(device);
9992 mFakeEventHub->addDevice(eventHubId, name, classes);
9993 mReader->loopOnce();
9994 return device;
9995 }
9996
9997 template <class T, typename... Args>
9998 T& addControllerAndConfigure(Args... args) {
9999 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
10000
10001 return controller;
10002 }
10003};
10004
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010005const char* PeripheralControllerTest::DEVICE_NAME = "device";
10006const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
10007const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
10008const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
10009const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010010const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
10011 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010012const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080010013
10014// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010015class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010016protected:
10017 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010018 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010019 }
10020};
10021
10022TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010023 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010024
Harry Cuttsa5b71292022-11-28 12:56:17 +000010025 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
10026 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10027 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010028}
10029
10030TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010031 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010032
Harry Cuttsa5b71292022-11-28 12:56:17 +000010033 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
10034 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
10035 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010036}
10037
10038// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010039class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010040protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010041 void SetUp() override {
10042 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
10043 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080010044};
10045
Chris Ye85758332021-05-16 23:05:17 -070010046TEST_F(LightControllerTest, MonoLight) {
10047 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010048 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070010049 .maxBrightness = 255,
10050 .flags = InputLightClass::BRIGHTNESS,
10051 .path = ""};
10052 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010053
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010054 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010055 InputDeviceInfo info;
10056 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010057 std::vector<InputDeviceLightInfo> lights = info.getLights();
10058 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010059 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10060 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10061
10062 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10063 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
10064}
10065
DingYong99f2c3c2023-12-20 15:46:06 +080010066TEST_F(LightControllerTest, MonoKeyboardMuteLight) {
10067 RawLightInfo infoMono = {.id = 1,
10068 .name = "mono_keyboard_mute",
10069 .maxBrightness = 255,
10070 .flags = InputLightClass::BRIGHTNESS |
10071 InputLightClass::KEYBOARD_MIC_MUTE,
10072 .path = ""};
10073 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10074
10075 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10076 std::list<NotifyArgs> unused =
10077 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10078 /*changes=*/{});
10079
10080 InputDeviceInfo info;
10081 controller.populateDeviceInfo(&info);
10082 std::vector<InputDeviceLightInfo> lights = info.getLights();
10083 ASSERT_EQ(1U, lights.size());
10084 ASSERT_EQ(InputDeviceLightType::KEYBOARD_MIC_MUTE, lights[0].type);
10085 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10086}
10087
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010088TEST_F(LightControllerTest, MonoKeyboardBacklight) {
10089 RawLightInfo infoMono = {.id = 1,
10090 .name = "mono_keyboard_backlight",
10091 .maxBrightness = 255,
10092 .flags = InputLightClass::BRIGHTNESS |
10093 InputLightClass::KEYBOARD_BACKLIGHT,
10094 .path = ""};
10095 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10096
10097 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10098 InputDeviceInfo info;
10099 controller.populateDeviceInfo(&info);
10100 std::vector<InputDeviceLightInfo> lights = info.getLights();
10101 ASSERT_EQ(1U, lights.size());
10102 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10103 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010104
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010105 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
10106 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010107}
10108
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000010109TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
10110 RawLightInfo infoMono = {.id = 1,
10111 .name = "mono_light",
10112 .maxBrightness = 255,
10113 .flags = InputLightClass::BRIGHTNESS,
10114 .path = ""};
10115 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10116 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10117 "0,100,200");
10118
10119 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10120 std::list<NotifyArgs> unused =
10121 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10122 /*changes=*/{});
10123
10124 InputDeviceInfo info;
10125 controller.populateDeviceInfo(&info);
10126 std::vector<InputDeviceLightInfo> lights = info.getLights();
10127 ASSERT_EQ(1U, lights.size());
10128 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10129}
10130
10131TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
10132 RawLightInfo infoMono = {.id = 1,
10133 .name = "mono_keyboard_backlight",
10134 .maxBrightness = 255,
10135 .flags = InputLightClass::BRIGHTNESS |
10136 InputLightClass::KEYBOARD_BACKLIGHT,
10137 .path = ""};
10138 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10139
10140 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10141 std::list<NotifyArgs> unused =
10142 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10143 /*changes=*/{});
10144
10145 InputDeviceInfo info;
10146 controller.populateDeviceInfo(&info);
10147 std::vector<InputDeviceLightInfo> lights = info.getLights();
10148 ASSERT_EQ(1U, lights.size());
10149 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10150}
10151
10152TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
10153 RawLightInfo infoMono = {.id = 1,
10154 .name = "mono_keyboard_backlight",
10155 .maxBrightness = 255,
10156 .flags = InputLightClass::BRIGHTNESS |
10157 InputLightClass::KEYBOARD_BACKLIGHT,
10158 .path = ""};
10159 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10160 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10161 "0,100,200");
10162
10163 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10164 std::list<NotifyArgs> unused =
10165 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10166 /*changes=*/{});
10167
10168 InputDeviceInfo info;
10169 controller.populateDeviceInfo(&info);
10170 std::vector<InputDeviceLightInfo> lights = info.getLights();
10171 ASSERT_EQ(1U, lights.size());
10172 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
10173 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
10174 ASSERT_EQ(BrightnessLevel(0), *it);
10175 std::advance(it, 1);
10176 ASSERT_EQ(BrightnessLevel(100), *it);
10177 std::advance(it, 1);
10178 ASSERT_EQ(BrightnessLevel(200), *it);
10179}
10180
10181TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
10182 RawLightInfo infoMono = {.id = 1,
10183 .name = "mono_keyboard_backlight",
10184 .maxBrightness = 255,
10185 .flags = InputLightClass::BRIGHTNESS |
10186 InputLightClass::KEYBOARD_BACKLIGHT,
10187 .path = ""};
10188 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
10189 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
10190 "0,100,200,300,400,500");
10191
10192 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10193 std::list<NotifyArgs> unused =
10194 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
10195 /*changes=*/{});
10196
10197 InputDeviceInfo info;
10198 controller.populateDeviceInfo(&info);
10199 std::vector<InputDeviceLightInfo> lights = info.getLights();
10200 ASSERT_EQ(1U, lights.size());
10201 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
10202}
10203
Chris Yee2b1e5c2021-03-10 22:45:12 -080010204TEST_F(LightControllerTest, RGBLight) {
10205 RawLightInfo infoRed = {.id = 1,
10206 .name = "red",
10207 .maxBrightness = 255,
10208 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10209 .path = ""};
10210 RawLightInfo infoGreen = {.id = 2,
10211 .name = "green",
10212 .maxBrightness = 255,
10213 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10214 .path = ""};
10215 RawLightInfo infoBlue = {.id = 3,
10216 .name = "blue",
10217 .maxBrightness = 255,
10218 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10219 .path = ""};
10220 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10221 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10222 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10223
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010224 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010225 InputDeviceInfo info;
10226 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010227 std::vector<InputDeviceLightInfo> lights = info.getLights();
10228 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010229 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10230 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10231 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10232
10233 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10234 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10235}
10236
10237TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
10238 RawLightInfo infoRed = {.id = 1,
10239 .name = "red_keyboard_backlight",
10240 .maxBrightness = 255,
10241 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
10242 InputLightClass::KEYBOARD_BACKLIGHT,
10243 .path = ""};
10244 RawLightInfo infoGreen = {.id = 2,
10245 .name = "green_keyboard_backlight",
10246 .maxBrightness = 255,
10247 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
10248 InputLightClass::KEYBOARD_BACKLIGHT,
10249 .path = ""};
10250 RawLightInfo infoBlue = {.id = 3,
10251 .name = "blue_keyboard_backlight",
10252 .maxBrightness = 255,
10253 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
10254 InputLightClass::KEYBOARD_BACKLIGHT,
10255 .path = ""};
10256 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10257 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10258 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10259
10260 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10261 InputDeviceInfo info;
10262 controller.populateDeviceInfo(&info);
10263 std::vector<InputDeviceLightInfo> lights = info.getLights();
10264 ASSERT_EQ(1U, lights.size());
10265 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10266 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10267 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10268
10269 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10270 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10271}
10272
10273TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
10274 RawLightInfo infoRed = {.id = 1,
10275 .name = "red",
10276 .maxBrightness = 255,
10277 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
10278 .path = ""};
10279 RawLightInfo infoGreen = {.id = 2,
10280 .name = "green",
10281 .maxBrightness = 255,
10282 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
10283 .path = ""};
10284 RawLightInfo infoBlue = {.id = 3,
10285 .name = "blue",
10286 .maxBrightness = 255,
10287 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
10288 .path = ""};
10289 RawLightInfo infoGlobal = {.id = 3,
10290 .name = "global_keyboard_backlight",
10291 .maxBrightness = 255,
10292 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
10293 InputLightClass::KEYBOARD_BACKLIGHT,
10294 .path = ""};
10295 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
10296 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
10297 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
10298 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
10299
10300 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10301 InputDeviceInfo info;
10302 controller.populateDeviceInfo(&info);
10303 std::vector<InputDeviceLightInfo> lights = info.getLights();
10304 ASSERT_EQ(1U, lights.size());
10305 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10306 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10307 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010308
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010309 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10310 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010311}
10312
10313TEST_F(LightControllerTest, MultiColorRGBLight) {
10314 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010315 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010316 .maxBrightness = 255,
10317 .flags = InputLightClass::BRIGHTNESS |
10318 InputLightClass::MULTI_INTENSITY |
10319 InputLightClass::MULTI_INDEX,
10320 .path = ""};
10321
10322 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10323
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010324 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010325 InputDeviceInfo info;
10326 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010327 std::vector<InputDeviceLightInfo> lights = info.getLights();
10328 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010329 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
10330 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10331 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10332
10333 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10334 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
10335}
10336
10337TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
10338 RawLightInfo infoColor = {.id = 1,
10339 .name = "multi_color_keyboard_backlight",
10340 .maxBrightness = 255,
10341 .flags = InputLightClass::BRIGHTNESS |
10342 InputLightClass::MULTI_INTENSITY |
10343 InputLightClass::MULTI_INDEX |
10344 InputLightClass::KEYBOARD_BACKLIGHT,
10345 .path = ""};
10346
10347 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
10348
10349 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10350 InputDeviceInfo info;
10351 controller.populateDeviceInfo(&info);
10352 std::vector<InputDeviceLightInfo> lights = info.getLights();
10353 ASSERT_EQ(1U, lights.size());
10354 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
10355 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10356 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010357
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010358 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10359 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010360}
10361
Josep del Rioa1046a82023-08-24 19:57:27 +000010362TEST_F(LightControllerTest, SonyPlayerIdLight) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080010363 RawLightInfo info1 = {.id = 1,
Josep del Rioa1046a82023-08-24 19:57:27 +000010364 .name = "sony1",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010365 .maxBrightness = 255,
10366 .flags = InputLightClass::BRIGHTNESS,
10367 .path = ""};
10368 RawLightInfo info2 = {.id = 2,
Josep del Rioa1046a82023-08-24 19:57:27 +000010369 .name = "sony2",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010370 .maxBrightness = 255,
10371 .flags = InputLightClass::BRIGHTNESS,
10372 .path = ""};
10373 RawLightInfo info3 = {.id = 3,
Josep del Rioa1046a82023-08-24 19:57:27 +000010374 .name = "sony3",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010375 .maxBrightness = 255,
10376 .flags = InputLightClass::BRIGHTNESS,
10377 .path = ""};
10378 RawLightInfo info4 = {.id = 4,
Josep del Rioa1046a82023-08-24 19:57:27 +000010379 .name = "sony4",
Chris Yee2b1e5c2021-03-10 22:45:12 -080010380 .maxBrightness = 255,
10381 .flags = InputLightClass::BRIGHTNESS,
10382 .path = ""};
10383 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10384 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10385 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10386 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10387
Chris Ye1dd2e5c2021-04-04 23:12:41 -070010388 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080010389 InputDeviceInfo info;
10390 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010391 std::vector<InputDeviceLightInfo> lights = info.getLights();
10392 ASSERT_EQ(1U, lights.size());
Josep del Rioa1046a82023-08-24 19:57:27 +000010393 ASSERT_STREQ("sony", lights[0].name.c_str());
10394 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
10395 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10396 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
10397
10398 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10399 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10400 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
10401 ASSERT_STREQ("sony", lights[0].name.c_str());
10402}
10403
10404TEST_F(LightControllerTest, PlayerIdLight) {
10405 RawLightInfo info1 = {.id = 1,
10406 .name = "player-1",
10407 .maxBrightness = 255,
10408 .flags = InputLightClass::BRIGHTNESS,
10409 .path = ""};
10410 RawLightInfo info2 = {.id = 2,
10411 .name = "player-2",
10412 .maxBrightness = 255,
10413 .flags = InputLightClass::BRIGHTNESS,
10414 .path = ""};
10415 RawLightInfo info3 = {.id = 3,
10416 .name = "player-3",
10417 .maxBrightness = 255,
10418 .flags = InputLightClass::BRIGHTNESS,
10419 .path = ""};
10420 RawLightInfo info4 = {.id = 4,
10421 .name = "player-4",
10422 .maxBrightness = 255,
10423 .flags = InputLightClass::BRIGHTNESS,
10424 .path = ""};
10425 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
10426 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
10427 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
10428 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
10429
10430 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
10431 InputDeviceInfo info;
10432 controller.populateDeviceInfo(&info);
10433 std::vector<InputDeviceLightInfo> lights = info.getLights();
10434 ASSERT_EQ(1U, lights.size());
10435 ASSERT_STREQ("player", lights[0].name.c_str());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010436 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000010437 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
10438 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080010439
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000010440 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
10441 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
10442 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080010443}
10444
Michael Wrightd02c5b62014-02-10 15:10:22 -080010445} // namespace android