blob: c03e82b3783f3e08d7815113efeabc17c6e44e9c [file] [log] [blame]
Michael Wrightd02c5b62014-02-10 15:10:22 -08001/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Dominik Laskowski2f01d772022-03-23 16:01:29 -070017#include <cinttypes>
18#include <memory>
Harry Cuttsf13161a2023-03-08 14:15:49 +000019#include <optional>
Dominik Laskowski2f01d772022-03-23 16:01:29 -070020
Prabir Pradhan2770d242019-09-02 18:07:11 -070021#include <CursorInputMapper.h>
22#include <InputDevice.h>
23#include <InputMapper.h>
24#include <InputReader.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080025#include <InputReaderBase.h>
26#include <InputReaderFactory.h>
Arthur Hung6d5b4b22022-01-21 07:21:10 +000027#include <JoystickInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070028#include <KeyboardInputMapper.h>
29#include <MultiTouchInputMapper.h>
Chris Ye1dd2e5c2021-04-04 23:12:41 -070030#include <PeripheralController.h>
Chris Yef59a2f42020-10-16 12:55:26 -070031#include <SensorInputMapper.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070032#include <SingleTouchInputMapper.h>
33#include <SwitchInputMapper.h>
Prabir Pradhane3b28dd2023-10-06 04:19:29 +000034#include <TestEventMatchers.h>
Prabir Pradhan2770d242019-09-02 18:07:11 -070035#include <TestInputListener.h>
36#include <TouchInputMapper.h>
Prabir Pradhan1aed8582019-12-30 11:46:51 -080037#include <UinputDevice.h>
Chris Ye87143712020-11-10 05:05:58 +000038#include <VibratorInputMapper.h>
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070039#include <android-base/thread_annotations.h>
Byoungho Jungda10dd32023-10-06 17:03:45 +090040#include <com_android_input_flags.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000041#include <ftl/enum.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080042#include <gtest/gtest.h>
chaviw3277faf2021-05-19 16:45:23 -050043#include <gui/constants.h>
Michael Wrighta9cf4192022-12-01 23:46:39 +000044#include <ui/Rotation.h>
Michael Wrightd02c5b62014-02-10 15:10:22 -080045
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -070046#include <thread>
Harry Cuttsa5b71292022-11-28 12:56:17 +000047#include "FakeEventHub.h"
Harry Cutts6b5fbc52022-11-28 16:37:43 +000048#include "FakeInputReaderPolicy.h"
Harry Cuttsb57f1702022-11-28 15:34:22 +000049#include "FakePointerController.h"
Harry Cuttse6512e12022-11-28 18:44:01 +000050#include "InputMapperTest.h"
Harry Cutts144ff542022-11-28 17:41:06 +000051#include "InstrumentedInputReader.h"
Harry Cuttsa5b71292022-11-28 12:56:17 +000052#include "TestConstants.h"
Michael Wrightdde67b82020-10-27 16:09:22 +000053#include "input/DisplayViewport.h"
54#include "input/Input.h"
Michael Wright17db18e2020-06-26 20:51:44 +010055
Michael Wrightd02c5b62014-02-10 15:10:22 -080056namespace android {
57
Dominik Laskowski2f01d772022-03-23 16:01:29 -070058using namespace ftl::flag_operators;
Prabir Pradhan739dca42022-09-09 20:12:01 +000059using testing::AllOf;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -070060using std::chrono_literals::operator""ms;
61
Michael Wrightd02c5b62014-02-10 15:10:22 -080062// Arbitrary display properties.
arthurhungcc7f9802020-04-30 17:55:40 +080063static constexpr int32_t DISPLAY_ID = 0;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000064static const std::string DISPLAY_UNIQUE_ID = "local:1";
arthurhungcc7f9802020-04-30 17:55:40 +080065static constexpr int32_t SECONDARY_DISPLAY_ID = DISPLAY_ID + 1;
Prabir Pradhanc13ff082022-09-08 22:03:30 +000066static const std::string SECONDARY_DISPLAY_UNIQUE_ID = "local:2";
arthurhungcc7f9802020-04-30 17:55:40 +080067static constexpr int32_t DISPLAY_WIDTH = 480;
68static constexpr int32_t DISPLAY_HEIGHT = 800;
69static constexpr int32_t VIRTUAL_DISPLAY_ID = 1;
70static constexpr int32_t VIRTUAL_DISPLAY_WIDTH = 400;
71static constexpr int32_t VIRTUAL_DISPLAY_HEIGHT = 500;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -070072static const char* VIRTUAL_DISPLAY_UNIQUE_ID = "virtual:1";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070073static constexpr std::optional<uint8_t> NO_PORT = std::nullopt; // no physical port is specified
Michael Wrightd02c5b62014-02-10 15:10:22 -080074
arthurhungcc7f9802020-04-30 17:55:40 +080075static constexpr int32_t FIRST_SLOT = 0;
76static constexpr int32_t SECOND_SLOT = 1;
77static constexpr int32_t THIRD_SLOT = 2;
78static constexpr int32_t INVALID_TRACKING_ID = -1;
79static constexpr int32_t FIRST_TRACKING_ID = 0;
80static constexpr int32_t SECOND_TRACKING_ID = 1;
81static constexpr int32_t THIRD_TRACKING_ID = 2;
Chris Ye3fdbfef2021-01-06 18:45:18 -080082static constexpr int32_t LIGHT_BRIGHTNESS = 0x55000000;
83static constexpr int32_t LIGHT_COLOR = 0x7F448866;
84static constexpr int32_t LIGHT_PLAYER_ID = 2;
arthurhungcc7f9802020-04-30 17:55:40 +080085
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080086static constexpr int32_t ACTION_POINTER_0_DOWN =
87 AMOTION_EVENT_ACTION_POINTER_DOWN | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
88static constexpr int32_t ACTION_POINTER_0_UP =
89 AMOTION_EVENT_ACTION_POINTER_UP | (0 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
90static constexpr int32_t ACTION_POINTER_1_DOWN =
91 AMOTION_EVENT_ACTION_POINTER_DOWN | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
92static constexpr int32_t ACTION_POINTER_1_UP =
93 AMOTION_EVENT_ACTION_POINTER_UP | (1 << AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT);
94
Prabir Pradhanb08a0e82023-09-14 22:28:32 +000095static constexpr uint32_t STYLUS_FUSION_SOURCE =
96 AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_BLUETOOTH_STYLUS;
97
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000098// Minimum timestamp separation between subsequent input events from a Bluetooth device.
99static constexpr nsecs_t MIN_BLUETOOTH_TIMESTAMP_DELTA = ms2ns(4);
100// Maximum smoothing time delta so that we don't generate events too far into the future.
101constexpr static nsecs_t MAX_BLUETOOTH_SMOOTHING_DELTA = ms2ns(32);
102
Byoungho Jungda10dd32023-10-06 17:03:45 +0900103namespace input_flags = com::android::input::flags;
104
Michael Wrightd02c5b62014-02-10 15:10:22 -0800105template<typename T>
106static inline T min(T a, T b) {
107 return a < b ? a : b;
108}
109
110static inline float avg(float x, float y) {
111 return (x + y) / 2;
112}
113
Chris Ye3fdbfef2021-01-06 18:45:18 -0800114// Mapping for light color name and the light color
115const std::unordered_map<std::string, LightColor> LIGHT_COLORS = {{"red", LightColor::RED},
116 {"green", LightColor::GREEN},
117 {"blue", LightColor::BLUE}};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800118
Michael Wrighta9cf4192022-12-01 23:46:39 +0000119static ui::Rotation getInverseRotation(ui::Rotation orientation) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700120 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +0000121 case ui::ROTATION_90:
122 return ui::ROTATION_270;
123 case ui::ROTATION_270:
124 return ui::ROTATION_90;
Prabir Pradhanc14266f2021-05-12 15:56:24 -0700125 default:
126 return orientation;
127 }
128}
129
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800130static void assertAxisResolution(MultiTouchInputMapper& mapper, int axis, float resolution) {
131 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000132 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800133
134 const InputDeviceInfo::MotionRange* motionRange =
135 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
136 ASSERT_NEAR(motionRange->resolution, resolution, EPSILON);
137}
138
139static void assertAxisNotPresent(MultiTouchInputMapper& mapper, int axis) {
140 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +0000141 mapper.populateDeviceInfo(info);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -0800142
143 const InputDeviceInfo::MotionRange* motionRange =
144 info.getMotionRange(axis, AINPUT_SOURCE_TOUCHSCREEN);
145 ASSERT_EQ(nullptr, motionRange);
146}
147
Siarhei Vishniakou21e96e62022-10-27 10:23:37 -0700148[[maybe_unused]] static void dumpReader(InputReader& reader) {
149 std::string dump;
150 reader.dump(dump);
151 std::istringstream iss(dump);
152 for (std::string line; std::getline(iss, line);) {
153 ALOGE("%s", line.c_str());
154 std::this_thread::sleep_for(std::chrono::milliseconds(1));
155 }
156}
157
Michael Wrightd02c5b62014-02-10 15:10:22 -0800158// --- FakeInputMapper ---
159
160class FakeInputMapper : public InputMapper {
161 uint32_t mSources;
162 int32_t mKeyboardType;
163 int32_t mMetaState;
164 KeyedVector<int32_t, int32_t> mKeyCodeStates;
165 KeyedVector<int32_t, int32_t> mScanCodeStates;
166 KeyedVector<int32_t, int32_t> mSwitchStates;
Philip Junker4af3b3d2021-12-14 10:36:55 +0100167 // fake mapping which would normally come from keyCharacterMap
168 std::unordered_map<int32_t, int32_t> mKeyCodeMapping;
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800169 std::vector<int32_t> mSupportedKeyCodes;
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700170 std::list<NotifyArgs> mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800171
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700172 std::mutex mLock;
173 std::condition_variable mStateChangedCondition;
174 bool mConfigureWasCalled GUARDED_BY(mLock);
175 bool mResetWasCalled GUARDED_BY(mLock);
176 bool mProcessWasCalled GUARDED_BY(mLock);
177 RawEvent mLastEvent GUARDED_BY(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800178
Arthur Hungc23540e2018-11-29 20:42:11 +0800179 std::optional<DisplayViewport> mViewport;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800180public:
Arpit Singh8e6fb252023-04-06 11:49:17 +0000181 FakeInputMapper(InputDeviceContext& deviceContext, const InputReaderConfiguration& readerConfig,
182 uint32_t sources)
183 : InputMapper(deviceContext, readerConfig),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800184 mSources(sources),
185 mKeyboardType(AINPUT_KEYBOARD_TYPE_NONE),
Michael Wrightd02c5b62014-02-10 15:10:22 -0800186 mMetaState(0),
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800187 mConfigureWasCalled(false),
188 mResetWasCalled(false),
189 mProcessWasCalled(false) {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800190
Chris Yea52ade12020-08-27 16:49:20 -0700191 virtual ~FakeInputMapper() {}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800192
193 void setKeyboardType(int32_t keyboardType) {
194 mKeyboardType = keyboardType;
195 }
196
197 void setMetaState(int32_t metaState) {
198 mMetaState = metaState;
199 }
200
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700201 // Sets the return value for the `process` call.
202 void setProcessResult(std::list<NotifyArgs> notifyArgs) {
203 mProcessResult.clear();
204 for (auto notifyArg : notifyArgs) {
205 mProcessResult.push_back(notifyArg);
206 }
207 }
208
Michael Wrightd02c5b62014-02-10 15:10:22 -0800209 void assertConfigureWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700210 std::unique_lock<std::mutex> lock(mLock);
211 base::ScopedLockAssertion assumeLocked(mLock);
212 const bool configureCalled =
213 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
214 return mConfigureWasCalled;
215 });
216 if (!configureCalled) {
217 FAIL() << "Expected configure() to have been called.";
218 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800219 mConfigureWasCalled = false;
220 }
221
222 void assertResetWasCalled() {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700223 std::unique_lock<std::mutex> lock(mLock);
224 base::ScopedLockAssertion assumeLocked(mLock);
225 const bool resetCalled =
226 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
227 return mResetWasCalled;
228 });
229 if (!resetCalled) {
230 FAIL() << "Expected reset() to have been called.";
231 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800232 mResetWasCalled = false;
233 }
234
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000235 void assertResetWasNotCalled() {
236 std::scoped_lock lock(mLock);
237 ASSERT_FALSE(mResetWasCalled) << "Expected reset to not have been called.";
238 }
239
Yi Kong9b14ac62018-07-17 13:48:38 -0700240 void assertProcessWasCalled(RawEvent* outLastEvent = nullptr) {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700241 std::unique_lock<std::mutex> lock(mLock);
242 base::ScopedLockAssertion assumeLocked(mLock);
243 const bool processCalled =
244 mStateChangedCondition.wait_for(lock, WAIT_TIMEOUT, [this]() REQUIRES(mLock) {
245 return mProcessWasCalled;
246 });
247 if (!processCalled) {
248 FAIL() << "Expected process() to have been called.";
249 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800250 if (outLastEvent) {
251 *outLastEvent = mLastEvent;
252 }
253 mProcessWasCalled = false;
254 }
255
Prabir Pradhanf8d9e442023-12-06 22:06:13 +0000256 void assertProcessWasNotCalled() {
257 std::scoped_lock lock(mLock);
258 ASSERT_FALSE(mProcessWasCalled) << "Expected process to not have been called.";
259 }
260
Michael Wrightd02c5b62014-02-10 15:10:22 -0800261 void setKeyCodeState(int32_t keyCode, int32_t state) {
262 mKeyCodeStates.replaceValueFor(keyCode, state);
263 }
264
265 void setScanCodeState(int32_t scanCode, int32_t state) {
266 mScanCodeStates.replaceValueFor(scanCode, state);
267 }
268
269 void setSwitchState(int32_t switchCode, int32_t state) {
270 mSwitchStates.replaceValueFor(switchCode, state);
271 }
272
273 void addSupportedKeyCode(int32_t keyCode) {
Arthur Hung7c3ae9c2019-03-11 11:23:03 +0800274 mSupportedKeyCodes.push_back(keyCode);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800275 }
276
Philip Junker4af3b3d2021-12-14 10:36:55 +0100277 void addKeyCodeMapping(int32_t fromKeyCode, int32_t toKeyCode) {
278 mKeyCodeMapping.insert_or_assign(fromKeyCode, toKeyCode);
279 }
280
Michael Wrightd02c5b62014-02-10 15:10:22 -0800281private:
Philip Junker4af3b3d2021-12-14 10:36:55 +0100282 uint32_t getSources() const override { return mSources; }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800283
Harry Cuttsd02ea102023-03-17 18:21:30 +0000284 void populateDeviceInfo(InputDeviceInfo& deviceInfo) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800285 InputMapper::populateDeviceInfo(deviceInfo);
286
287 if (mKeyboardType != AINPUT_KEYBOARD_TYPE_NONE) {
Harry Cuttsd02ea102023-03-17 18:21:30 +0000288 deviceInfo.setKeyboardType(mKeyboardType);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800289 }
290 }
291
Arpit Singhed6c3de2023-04-05 19:24:37 +0000292 std::list<NotifyArgs> reconfigure(nsecs_t, const InputReaderConfiguration& config,
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000293 ConfigurationChanges changes) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700294 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800295 mConfigureWasCalled = true;
Arthur Hungc23540e2018-11-29 20:42:11 +0800296
297 // Find the associated viewport if exist.
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800298 const std::optional<uint8_t> displayPort = getDeviceContext().getAssociatedDisplayPort();
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000299 if (displayPort && changes.test(InputReaderConfiguration::Change::DISPLAY_INFO)) {
Arpit Singhed6c3de2023-04-05 19:24:37 +0000300 mViewport = config.getDisplayViewportByPort(*displayPort);
Arthur Hungc23540e2018-11-29 20:42:11 +0800301 }
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700302
303 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700304 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800305 }
306
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700307 std::list<NotifyArgs> reset(nsecs_t) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700308 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800309 mResetWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700310 mStateChangedCondition.notify_all();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700311 return {};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800312 }
313
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700314 std::list<NotifyArgs> process(const RawEvent* rawEvent) override {
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700315 std::scoped_lock<std::mutex> lock(mLock);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800316 mLastEvent = *rawEvent;
317 mProcessWasCalled = true;
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700318 mStateChangedCondition.notify_all();
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -0700319 return mProcessResult;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800320 }
321
Chris Yea52ade12020-08-27 16:49:20 -0700322 int32_t getKeyCodeState(uint32_t, int32_t keyCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800323 ssize_t index = mKeyCodeStates.indexOfKey(keyCode);
324 return index >= 0 ? mKeyCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
325 }
326
Philip Junker4af3b3d2021-12-14 10:36:55 +0100327 int32_t getKeyCodeForKeyLocation(int32_t locationKeyCode) const override {
328 auto it = mKeyCodeMapping.find(locationKeyCode);
329 return it != mKeyCodeMapping.end() ? it->second : locationKeyCode;
330 }
331
Chris Yea52ade12020-08-27 16:49:20 -0700332 int32_t getScanCodeState(uint32_t, int32_t scanCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800333 ssize_t index = mScanCodeStates.indexOfKey(scanCode);
334 return index >= 0 ? mScanCodeStates.valueAt(index) : AKEY_STATE_UNKNOWN;
335 }
336
Chris Yea52ade12020-08-27 16:49:20 -0700337 int32_t getSwitchState(uint32_t, int32_t switchCode) override {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800338 ssize_t index = mSwitchStates.indexOfKey(switchCode);
339 return index >= 0 ? mSwitchStates.valueAt(index) : AKEY_STATE_UNKNOWN;
340 }
341
Chris Yea52ade12020-08-27 16:49:20 -0700342 // Return true if the device has non-empty key layout.
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700343 bool markSupportedKeyCodes(uint32_t, const std::vector<int32_t>& keyCodes,
Chris Yea52ade12020-08-27 16:49:20 -0700344 uint8_t* outFlags) override {
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700345 for (size_t i = 0; i < keyCodes.size(); i++) {
Michael Wrightd02c5b62014-02-10 15:10:22 -0800346 for (size_t j = 0; j < mSupportedKeyCodes.size(); j++) {
347 if (keyCodes[i] == mSupportedKeyCodes[j]) {
348 outFlags[i] = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800349 }
350 }
351 }
Chris Yea52ade12020-08-27 16:49:20 -0700352 bool result = mSupportedKeyCodes.size() > 0;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800353 return result;
354 }
355
356 virtual int32_t getMetaState() {
357 return mMetaState;
358 }
359
360 virtual void fadePointer() {
361 }
Arthur Hungc23540e2018-11-29 20:42:11 +0800362
363 virtual std::optional<int32_t> getAssociatedDisplay() {
364 if (mViewport) {
365 return std::make_optional(mViewport->displayId);
366 }
367 return std::nullopt;
368 }
Michael Wrightd02c5b62014-02-10 15:10:22 -0800369};
370
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700371// --- InputReaderPolicyTest ---
372class InputReaderPolicyTest : public testing::Test {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700373protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700374 sp<FakeInputReaderPolicy> mFakePolicy;
375
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700376 void SetUp() override { mFakePolicy = sp<FakeInputReaderPolicy>::make(); }
Chris Yea52ade12020-08-27 16:49:20 -0700377 void TearDown() override { mFakePolicy.clear(); }
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700378};
379
380/**
381 * Check that empty set of viewports is an acceptable configuration.
382 * Also try to get internal viewport two different ways - by type and by uniqueId.
383 *
384 * There will be confusion if two viewports with empty uniqueId and identical type are present.
385 * Such configuration is not currently allowed.
386 */
387TEST_F(InputReaderPolicyTest, Viewports_GetCleared) {
Siarhei Vishniakoucd7ac1e2018-10-15 13:39:50 -0700388 static const std::string uniqueId = "local:0";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700389
390 // We didn't add any viewports yet, so there shouldn't be any.
391 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100392 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700393 ASSERT_FALSE(internalViewport);
394
395 // Add an internal viewport, then clear it
Michael Wrighta9cf4192022-12-01 23:46:39 +0000396 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000397 /*isActive=*/true, uniqueId, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700398
399 // Check matching by uniqueId
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700400 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700401 ASSERT_TRUE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100402 ASSERT_EQ(ViewportType::INTERNAL, internalViewport->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700403
404 // Check matching by viewport type
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100405 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700406 ASSERT_TRUE(internalViewport);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700407 ASSERT_EQ(uniqueId, internalViewport->uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700408
409 mFakePolicy->clearViewports();
410 // Make sure nothing is found after clear
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700411 internalViewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700412 ASSERT_FALSE(internalViewport);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100413 internalViewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700414 ASSERT_FALSE(internalViewport);
415}
416
417TEST_F(InputReaderPolicyTest, Viewports_GetByType) {
418 const std::string internalUniqueId = "local:0";
419 const std::string externalUniqueId = "local:1";
420 const std::string virtualUniqueId1 = "virtual:2";
421 const std::string virtualUniqueId2 = "virtual:3";
422 constexpr int32_t virtualDisplayId1 = 2;
423 constexpr int32_t virtualDisplayId2 = 3;
424
425 // Add an internal 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, internalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000428 ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700429 // Add an external viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000430 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000431 /*isActive=*/true, externalUniqueId, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000432 ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700433 // Add an virtual viewport
434 mFakePolicy->addDisplayViewport(virtualDisplayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000435 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId1, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000436 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700437 // Add another virtual viewport
438 mFakePolicy->addDisplayViewport(virtualDisplayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000439 ui::ROTATION_0, /*isActive=*/true, virtualUniqueId2, NO_PORT,
Michael Wrighta9cf4192022-12-01 23:46:39 +0000440 ViewportType::VIRTUAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700441
442 // Check matching by type for internal
443 std::optional<DisplayViewport> internalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100444 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700445 ASSERT_TRUE(internalViewport);
446 ASSERT_EQ(internalUniqueId, internalViewport->uniqueId);
447
448 // Check matching by type for external
449 std::optional<DisplayViewport> externalViewport =
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100450 mFakePolicy->getDisplayViewportByType(ViewportType::EXTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700451 ASSERT_TRUE(externalViewport);
452 ASSERT_EQ(externalUniqueId, externalViewport->uniqueId);
453
454 // Check matching by uniqueId for virtual viewport #1
455 std::optional<DisplayViewport> virtualViewport1 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700456 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700457 ASSERT_TRUE(virtualViewport1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100458 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport1->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700459 ASSERT_EQ(virtualUniqueId1, virtualViewport1->uniqueId);
460 ASSERT_EQ(virtualDisplayId1, virtualViewport1->displayId);
461
462 // Check matching by uniqueId for virtual viewport #2
463 std::optional<DisplayViewport> virtualViewport2 =
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700464 mFakePolicy->getDisplayViewportByUniqueId(virtualUniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700465 ASSERT_TRUE(virtualViewport2);
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100466 ASSERT_EQ(ViewportType::VIRTUAL, virtualViewport2->type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700467 ASSERT_EQ(virtualUniqueId2, virtualViewport2->uniqueId);
468 ASSERT_EQ(virtualDisplayId2, virtualViewport2->displayId);
469}
470
471
472/**
473 * We can have 2 viewports of the same kind. We can distinguish them by uniqueId, and confirm
474 * that lookup works by checking display id.
475 * Check that 2 viewports of each kind is possible, for all existing viewport types.
476 */
477TEST_F(InputReaderPolicyTest, Viewports_TwoOfSameType) {
478 const std::string uniqueId1 = "uniqueId1";
479 const std::string uniqueId2 = "uniqueId2";
480 constexpr int32_t displayId1 = 2;
481 constexpr int32_t displayId2 = 3;
482
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100483 std::vector<ViewportType> types = {ViewportType::INTERNAL, ViewportType::EXTERNAL,
484 ViewportType::VIRTUAL};
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700485 for (const ViewportType& type : types) {
486 mFakePolicy->clearViewports();
487 // Add a viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000488 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000489 /*isActive=*/true, uniqueId1, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700490 // Add another viewport
Michael Wrighta9cf4192022-12-01 23:46:39 +0000491 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000492 /*isActive=*/true, uniqueId2, NO_PORT, type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700493
494 // Check that correct display viewport was returned by comparing the display IDs.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700495 std::optional<DisplayViewport> viewport1 =
496 mFakePolicy->getDisplayViewportByUniqueId(uniqueId1);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700497 ASSERT_TRUE(viewport1);
498 ASSERT_EQ(displayId1, viewport1->displayId);
499 ASSERT_EQ(type, viewport1->type);
500
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700501 std::optional<DisplayViewport> viewport2 =
502 mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700503 ASSERT_TRUE(viewport2);
504 ASSERT_EQ(displayId2, viewport2->displayId);
505 ASSERT_EQ(type, viewport2->type);
506
507 // When there are multiple viewports of the same kind, and uniqueId is not specified
508 // in the call to getDisplayViewport, then that situation is not supported.
509 // The viewports can be stored in any order, so we cannot rely on the order, since that
510 // is just implementation detail.
511 // However, we can check that it still returns *a* viewport, we just cannot assert
512 // which one specifically is returned.
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700513 std::optional<DisplayViewport> someViewport = mFakePolicy->getDisplayViewportByType(type);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -0700514 ASSERT_TRUE(someViewport);
515 }
516}
Michael Wrightd02c5b62014-02-10 15:10:22 -0800517
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700518/**
Michael Wrightdde67b82020-10-27 16:09:22 +0000519 * When we have multiple internal displays make sure we always return the default display when
520 * querying by type.
521 */
522TEST_F(InputReaderPolicyTest, Viewports_ByTypeReturnsDefaultForInternal) {
523 const std::string uniqueId1 = "uniqueId1";
524 const std::string uniqueId2 = "uniqueId2";
525 constexpr int32_t nonDefaultDisplayId = 2;
526 static_assert(nonDefaultDisplayId != ADISPLAY_ID_DEFAULT,
527 "Test display ID should not be ADISPLAY_ID_DEFAULT");
528
529 // Add the default display first and ensure it gets returned.
530 mFakePolicy->clearViewports();
531 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000532 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000533 ViewportType::INTERNAL);
534 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000535 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000536 ViewportType::INTERNAL);
537
538 std::optional<DisplayViewport> viewport =
539 mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
540 ASSERT_TRUE(viewport);
541 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
542 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
543
544 // Add the default display second to make sure order doesn't matter.
545 mFakePolicy->clearViewports();
546 mFakePolicy->addDisplayViewport(nonDefaultDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000547 ui::ROTATION_0, /*isActive=*/true, uniqueId2, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000548 ViewportType::INTERNAL);
549 mFakePolicy->addDisplayViewport(ADISPLAY_ID_DEFAULT, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +0000550 ui::ROTATION_0, /*isActive=*/true, uniqueId1, NO_PORT,
Michael Wrightdde67b82020-10-27 16:09:22 +0000551 ViewportType::INTERNAL);
552
553 viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
554 ASSERT_TRUE(viewport);
555 ASSERT_EQ(ADISPLAY_ID_DEFAULT, viewport->displayId);
556 ASSERT_EQ(ViewportType::INTERNAL, viewport->type);
557}
558
559/**
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700560 * Check getDisplayViewportByPort
561 */
562TEST_F(InputReaderPolicyTest, Viewports_GetByPort) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +0100563 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700564 const std::string uniqueId1 = "uniqueId1";
565 const std::string uniqueId2 = "uniqueId2";
566 constexpr int32_t displayId1 = 1;
567 constexpr int32_t displayId2 = 2;
568 const uint8_t hdmi1 = 0;
569 const uint8_t hdmi2 = 1;
570 const uint8_t hdmi3 = 2;
571
572 mFakePolicy->clearViewports();
573 // Add a viewport that's associated with some display port that's not of interest.
Michael Wrighta9cf4192022-12-01 23:46:39 +0000574 mFakePolicy->addDisplayViewport(displayId1, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000575 /*isActive=*/true, uniqueId1, hdmi3, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700576 // Add another viewport, connected to HDMI1 port
Michael Wrighta9cf4192022-12-01 23:46:39 +0000577 mFakePolicy->addDisplayViewport(displayId2, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +0000578 /*isActive=*/true, uniqueId2, hdmi1, type);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -0700579
580 // Check that correct display viewport was returned by comparing the display ports.
581 std::optional<DisplayViewport> hdmi1Viewport = mFakePolicy->getDisplayViewportByPort(hdmi1);
582 ASSERT_TRUE(hdmi1Viewport);
583 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
584 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
585
586 // Check that we can still get the same viewport using the uniqueId
587 hdmi1Viewport = mFakePolicy->getDisplayViewportByUniqueId(uniqueId2);
588 ASSERT_TRUE(hdmi1Viewport);
589 ASSERT_EQ(displayId2, hdmi1Viewport->displayId);
590 ASSERT_EQ(uniqueId2, hdmi1Viewport->uniqueId);
591 ASSERT_EQ(type, hdmi1Viewport->type);
592
593 // Check that we cannot find a port with "HDMI2", because we never added one
594 std::optional<DisplayViewport> hdmi2Viewport = mFakePolicy->getDisplayViewportByPort(hdmi2);
595 ASSERT_FALSE(hdmi2Viewport);
596}
597
Michael Wrightd02c5b62014-02-10 15:10:22 -0800598// --- InputReaderTest ---
599
600class InputReaderTest : public testing::Test {
601protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700602 std::unique_ptr<TestInputListener> mFakeListener;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800603 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700604 std::shared_ptr<FakeEventHub> mFakeEventHub;
Prabir Pradhan28efc192019-11-05 01:10:04 +0000605 std::unique_ptr<InstrumentedInputReader> mReader;
Michael Wrightd02c5b62014-02-10 15:10:22 -0800606
Chris Yea52ade12020-08-27 16:49:20 -0700607 void SetUp() override {
Siarhei Vishniakou3bc7e092019-07-24 17:43:30 -0700608 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -0700609 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700610 mFakeListener = std::make_unique<TestInputListener>();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800611
Prabir Pradhan28efc192019-11-05 01:10:04 +0000612 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700613 *mFakeListener);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800614 }
615
Chris Yea52ade12020-08-27 16:49:20 -0700616 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -0700617 mFakeListener.reset();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800618 mFakePolicy.clear();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800619 }
620
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700621 void addDevice(int32_t eventHubId, const std::string& name,
622 ftl::Flags<InputDeviceClass> classes, const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800623 mFakeEventHub->addDevice(eventHubId, name, classes);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800624
625 if (configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800626 mFakeEventHub->addConfigurationMap(eventHubId, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800627 }
628 mFakeEventHub->finishDeviceScan();
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000629 mReader->loopOnce();
630 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700631 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
Prabir Pradhane3da4bb2023-04-05 23:51:23 +0000632 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyInputDevicesChangedWasCalled());
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700633 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800634 }
635
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800636 void disableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700637 mFakePolicy->addDisabledDevice(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. Lewisf4916ef2020-01-14 11:57:18 -0800641 void enableDevice(int32_t deviceId) {
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700642 mFakePolicy->removeDisabledDevice(deviceId);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +0000643 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::ENABLED_STATE);
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700644 }
645
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800646 FakeInputMapper& addDeviceWithFakeInputMapper(int32_t deviceId, int32_t eventHubId,
Chris Ye1b0c7342020-07-28 21:57:03 -0700647 const std::string& name,
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700648 ftl::Flags<InputDeviceClass> classes,
649 uint32_t sources,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800650 const PropertyMap* configuration) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800651 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, name);
Arpit Singh8e6fb252023-04-06 11:49:17 +0000652 FakeInputMapper& mapper =
653 device->addMapper<FakeInputMapper>(eventHubId,
654 mFakePolicy->getReaderConfiguration(), sources);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800655 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800656 addDevice(eventHubId, name, classes, configuration);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800657 return mapper;
658 }
659};
660
Chris Ye98d3f532020-10-01 21:48:59 -0700661TEST_F(InputReaderTest, PolicyGetInputDevices) {
662 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700663 ASSERT_NO_FATAL_FAILURE(addDevice(2, "ignored", ftl::Flags<InputDeviceClass>(0),
Chris Ye98d3f532020-10-01 21:48:59 -0700664 nullptr)); // no classes so device will be ignored
Michael Wrightd02c5b62014-02-10 15:10:22 -0800665
666 // Should also have received a notification describing the new input devices.
Chris Ye98d3f532020-10-01 21:48:59 -0700667 const std::vector<InputDeviceInfo>& inputDevices = mFakePolicy->getInputDevices();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800668 ASSERT_EQ(1U, inputDevices.size());
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800669 ASSERT_EQ(END_RESERVED_ID + 1, inputDevices[0].getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100670 ASSERT_STREQ("keyboard", inputDevices[0].getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800671 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, inputDevices[0].getKeyboardType());
672 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, inputDevices[0].getSources());
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000673 ASSERT_EQ(0U, inputDevices[0].getMotionRanges().size());
Michael Wrightd02c5b62014-02-10 15:10:22 -0800674}
675
Vaibhav Devmurari5fc7d852023-03-17 18:43:33 +0000676TEST_F(InputReaderTest, InputDeviceRecreatedOnSysfsNodeChanged) {
677 ASSERT_NO_FATAL_FAILURE(addDevice(1, "keyboard", InputDeviceClass::KEYBOARD, nullptr));
678 mFakeEventHub->setSysfsRootPath(1, "xyz");
679
680 // Should also have received a notification describing the new input device.
681 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
682 InputDeviceInfo inputDevice = mFakePolicy->getInputDevices()[0];
683 ASSERT_EQ(0U, inputDevice.getLights().size());
684
685 RawLightInfo infoMonolight = {.id = 123,
686 .name = "mono_keyboard_backlight",
687 .maxBrightness = 255,
688 .flags = InputLightClass::BRIGHTNESS,
689 .path = ""};
690 mFakeEventHub->addRawLightInfo(/*rawId=*/123, std::move(infoMonolight));
691 mReader->sysfsNodeChanged("xyz");
692 mReader->loopOnce();
693
694 // Should also have received a notification describing the new recreated input device.
695 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
696 inputDevice = mFakePolicy->getInputDevices()[0];
697 ASSERT_EQ(1U, inputDevice.getLights().size());
698}
699
Chris Yee7310032020-09-22 15:36:28 -0700700TEST_F(InputReaderTest, GetMergedInputDevices) {
701 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
702 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
703 // Add two subdevices to device
704 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
705 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000706 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
707 AINPUT_SOURCE_KEYBOARD);
708 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
709 AINPUT_SOURCE_KEYBOARD);
Chris Yee7310032020-09-22 15:36:28 -0700710
711 // Push same device instance for next device to be added, so they'll have same identifier.
712 mReader->pushNextDevice(device);
713 mReader->pushNextDevice(device);
714 ASSERT_NO_FATAL_FAILURE(
715 addDevice(eventHubIds[0], "fake1", InputDeviceClass::KEYBOARD, nullptr));
716 ASSERT_NO_FATAL_FAILURE(
717 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
718
719 // Two devices will be merged to one input device as they have same identifier
Siarhei Vishniakou1983a712021-06-04 19:27:09 +0000720 ASSERT_EQ(1U, mFakePolicy->getInputDevices().size());
Chris Yee7310032020-09-22 15:36:28 -0700721}
722
Chris Yee14523a2020-12-19 13:46:00 -0800723TEST_F(InputReaderTest, GetMergedInputDevicesEnabled) {
724 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
725 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
726 // Add two subdevices to device
727 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
728 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000729 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
730 AINPUT_SOURCE_KEYBOARD);
731 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
732 AINPUT_SOURCE_KEYBOARD);
Chris Yee14523a2020-12-19 13:46:00 -0800733
734 // Push same device instance for next device to be added, so they'll have same identifier.
735 mReader->pushNextDevice(device);
736 mReader->pushNextDevice(device);
737 // Sensor device is initially disabled
738 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1",
739 InputDeviceClass::KEYBOARD | InputDeviceClass::SENSOR,
740 nullptr));
741 // Device is disabled because the only sub device is a sensor device and disabled initially.
742 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
743 ASSERT_FALSE(device->isEnabled());
744 ASSERT_NO_FATAL_FAILURE(
745 addDevice(eventHubIds[1], "fake2", InputDeviceClass::KEYBOARD, nullptr));
746 // The merged device is enabled if any sub device is enabled
747 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
748 ASSERT_TRUE(device->isEnabled());
749}
750
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700751TEST_F(InputReaderTest, WhenEnabledChanges_SendsDeviceResetNotification) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800752 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700753 constexpr ftl::Flags<InputDeviceClass> deviceClass(InputDeviceClass::KEYBOARD);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800754 constexpr int32_t eventHubId = 1;
755 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700756 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +0000757 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
758 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -0800759 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800760 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700761
Yi Kong9b14ac62018-07-17 13:48:38 -0700762 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700763
764 NotifyDeviceResetArgs resetArgs;
765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700766 ASSERT_EQ(deviceId, resetArgs.deviceId);
767
768 ASSERT_EQ(device->isEnabled(), true);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800769 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000770 mReader->loopOnce();
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700771
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700773 ASSERT_EQ(deviceId, resetArgs.deviceId);
774 ASSERT_EQ(device->isEnabled(), false);
775
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800776 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000777 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
779 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasNotCalled());
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700780 ASSERT_EQ(device->isEnabled(), false);
781
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800782 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000783 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -0700784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -0700785 ASSERT_EQ(deviceId, resetArgs.deviceId);
786 ASSERT_EQ(device->isEnabled(), true);
787}
788
Michael Wrightd02c5b62014-02-10 15:10:22 -0800789TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800790 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700791 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800792 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800793 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800794 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800795 AINPUT_SOURCE_KEYBOARD, nullptr);
796 mapper.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800797
798 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(0,
799 AINPUT_SOURCE_ANY, AKEYCODE_A))
800 << "Should return unknown when the device id is >= 0 but unknown.";
801
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800802 ASSERT_EQ(AKEY_STATE_UNKNOWN,
803 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
804 << "Should return unknown when the device id is valid but the sources are not "
805 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800806
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800807 ASSERT_EQ(AKEY_STATE_DOWN,
808 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
809 AKEYCODE_A))
810 << "Should return value provided by mapper when device id is valid and the device "
811 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800812
813 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getKeyCodeState(-1,
814 AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
815 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
816
817 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getKeyCodeState(-1,
818 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
819 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
820}
821
Philip Junker4af3b3d2021-12-14 10:36:55 +0100822TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_ForwardsRequestsToMappers) {
823 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
824 constexpr int32_t eventHubId = 1;
825 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "keyboard",
826 InputDeviceClass::KEYBOARD,
827 AINPUT_SOURCE_KEYBOARD, nullptr);
828 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
829
830 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(0, AKEYCODE_Y))
831 << "Should return unknown when the device with the specified id is not found.";
832
833 ASSERT_EQ(AKEYCODE_Z, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
834 << "Should return correct mapping when device id is valid and mapping exists.";
835
836 ASSERT_EQ(AKEYCODE_A, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_A))
837 << "Should return the location key code when device id is valid and there's no "
838 "mapping.";
839}
840
841TEST_F(InputReaderTest, GetKeyCodeForKeyLocation_NoKeyboardMapper) {
842 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
843 constexpr int32_t eventHubId = 1;
844 FakeInputMapper& mapper = addDeviceWithFakeInputMapper(deviceId, eventHubId, "joystick",
845 InputDeviceClass::JOYSTICK,
846 AINPUT_SOURCE_GAMEPAD, nullptr);
847 mapper.addKeyCodeMapping(AKEYCODE_Y, AKEYCODE_Z);
848
849 ASSERT_EQ(AKEYCODE_UNKNOWN, mReader->getKeyCodeForKeyLocation(deviceId, AKEYCODE_Y))
850 << "Should return unknown when the device id is valid but there is no keyboard mapper";
851}
852
Michael Wrightd02c5b62014-02-10 15:10:22 -0800853TEST_F(InputReaderTest, GetScanCodeState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800854 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700855 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800856 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800857 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800858 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800859 AINPUT_SOURCE_KEYBOARD, nullptr);
860 mapper.setScanCodeState(KEY_A, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800861
862 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(0,
863 AINPUT_SOURCE_ANY, KEY_A))
864 << "Should return unknown when the device id is >= 0 but unknown.";
865
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800866 ASSERT_EQ(AKEY_STATE_UNKNOWN,
867 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_TRACKBALL, KEY_A))
868 << "Should return unknown when the device id is valid but the sources are not "
869 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800870
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800871 ASSERT_EQ(AKEY_STATE_DOWN,
872 mReader->getScanCodeState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
873 KEY_A))
874 << "Should return value provided by mapper when device id is valid and the device "
875 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800876
877 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getScanCodeState(-1,
878 AINPUT_SOURCE_TRACKBALL, KEY_A))
879 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
880
881 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getScanCodeState(-1,
882 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, KEY_A))
883 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
884}
885
886TEST_F(InputReaderTest, GetSwitchState_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800887 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700888 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800889 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800890 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800891 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800892 AINPUT_SOURCE_KEYBOARD, nullptr);
893 mapper.setSwitchState(SW_LID, AKEY_STATE_DOWN);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800894
895 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(0,
896 AINPUT_SOURCE_ANY, SW_LID))
897 << "Should return unknown when the device id is >= 0 but unknown.";
898
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800899 ASSERT_EQ(AKEY_STATE_UNKNOWN,
900 mReader->getSwitchState(deviceId, AINPUT_SOURCE_TRACKBALL, SW_LID))
901 << "Should return unknown when the device id is valid but the sources are not "
902 "supported by the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800903
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800904 ASSERT_EQ(AKEY_STATE_DOWN,
905 mReader->getSwitchState(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
906 SW_LID))
907 << "Should return value provided by mapper when device id is valid and the device "
908 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800909
910 ASSERT_EQ(AKEY_STATE_UNKNOWN, mReader->getSwitchState(-1,
911 AINPUT_SOURCE_TRACKBALL, SW_LID))
912 << "Should return unknown when the device id is < 0 but the sources are not supported by any device.";
913
914 ASSERT_EQ(AKEY_STATE_DOWN, mReader->getSwitchState(-1,
915 AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, SW_LID))
916 << "Should return value provided by mapper when device id is < 0 and one of the devices supports some of the sources.";
917}
918
919TEST_F(InputReaderTest, MarkSupportedKeyCodes_ForwardsRequestsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800920 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700921 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800922 constexpr int32_t eventHubId = 1;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800923 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800924 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800925 AINPUT_SOURCE_KEYBOARD, nullptr);
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +0100926
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800927 mapper.addSupportedKeyCode(AKEYCODE_A);
928 mapper.addSupportedKeyCode(AKEYCODE_B);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800929
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700930 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -0800931 uint8_t flags[4] = { 0, 0, 0, 1 };
932
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700933 ASSERT_FALSE(mReader->hasKeys(0, AINPUT_SOURCE_ANY, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -0800934 << "Should return false when device id is >= 0 but unknown.";
935 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
936
937 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700938 ASSERT_FALSE(mReader->hasKeys(deviceId, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800939 << "Should return false when device id is valid but the sources are not supported by "
940 "the device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800941 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
942
943 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700944 ASSERT_TRUE(mReader->hasKeys(deviceId, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800945 keyCodes, flags))
946 << "Should return value provided by mapper when device id is valid and the device "
947 "supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800948 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
949
950 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700951 ASSERT_FALSE(mReader->hasKeys(-1, AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
952 << "Should return false when the device id is < 0 but the sources are not supported by "
953 "any device.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800954 ASSERT_TRUE(!flags[0] && !flags[1] && !flags[2] && !flags[3]);
955
956 flags[3] = 1;
Siarhei Vishniakou74007942022-06-13 13:57:47 -0700957 ASSERT_TRUE(
958 mReader->hasKeys(-1, AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
959 << "Should return value provided by mapper when device id is < 0 and one of the "
960 "devices supports some of the sources.";
Michael Wrightd02c5b62014-02-10 15:10:22 -0800961 ASSERT_TRUE(flags[0] && flags[1] && !flags[2] && !flags[3]);
962}
963
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000964TEST_F(InputReaderTest, LoopOnce_WhenDeviceScanFinished_SendsConfigurationChanged) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800965 constexpr int32_t eventHubId = 1;
Chris Ye1b0c7342020-07-28 21:57:03 -0700966 addDevice(eventHubId, "ignored", InputDeviceClass::KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800967
968 NotifyConfigurationChangedArgs args;
969
970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(&args));
971 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
972}
973
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000974TEST_F(InputReaderTest, LoopOnce_ForwardsRawEventsToMappers) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800975 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -0700976 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000977 constexpr nsecs_t when = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800978 constexpr int32_t eventHubId = 1;
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000979 constexpr nsecs_t readTime = 2;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800980 FakeInputMapper& mapper =
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800981 addDeviceWithFakeInputMapper(deviceId, eventHubId, "fake", deviceClass,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800982 AINPUT_SOURCE_KEYBOARD, nullptr);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800983
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000984 mFakeEventHub->enqueueEvent(when, readTime, eventHubId, EV_KEY, KEY_A, 1);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +0000985 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -0800986 ASSERT_NO_FATAL_FAILURE(mFakeEventHub->assertQueueIsEmpty());
987
988 RawEvent event;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -0800989 ASSERT_NO_FATAL_FAILURE(mapper.assertProcessWasCalled(&event));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +0000990 ASSERT_EQ(when, event.when);
991 ASSERT_EQ(readTime, event.readTime);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800992 ASSERT_EQ(eventHubId, event.deviceId);
Michael Wrightd02c5b62014-02-10 15:10:22 -0800993 ASSERT_EQ(EV_KEY, event.type);
994 ASSERT_EQ(KEY_A, event.code);
995 ASSERT_EQ(1, event.value);
996}
997
Garfield Tan1c7bc862020-01-28 13:24:04 -0800998TEST_F(InputReaderTest, DeviceReset_RandomId) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -0800999 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001000 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001001 constexpr int32_t eventHubId = 1;
1002 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
Prabir Pradhan42611e02018-11-27 14:04:02 -08001003 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001004 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1005 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001006 mReader->pushNextDevice(device);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001007 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan42611e02018-11-27 14:04:02 -08001008
1009 NotifyDeviceResetArgs resetArgs;
1010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001011 int32_t prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001012
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001013 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001014 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001016 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001017 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001018
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001019 enableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001020 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001021 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001022 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001023 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001024
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001025 disableDevice(deviceId);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001026 mReader->loopOnce();
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Garfield Tan1c7bc862020-01-28 13:24:04 -08001028 ASSERT_NE(prevId, resetArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001029 prevId = resetArgs.id;
Prabir Pradhan42611e02018-11-27 14:04:02 -08001030}
1031
Garfield Tan1c7bc862020-01-28 13:24:04 -08001032TEST_F(InputReaderTest, DeviceReset_GenerateIdWithInputReaderSource) {
1033 constexpr int32_t deviceId = 1;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001034 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Garfield Tan1c7bc862020-01-28 13:24:04 -08001035 constexpr int32_t eventHubId = 1;
1036 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1037 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001038 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1039 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001040 mReader->pushNextDevice(device);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001041 ASSERT_NO_FATAL_FAILURE(addDevice(deviceId, "fake", deviceClass, nullptr));
1042
1043 NotifyDeviceResetArgs resetArgs;
1044 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1045 ASSERT_EQ(IdGenerator::Source::INPUT_READER, IdGenerator::getSource(resetArgs.id));
1046}
1047
Arthur Hungc23540e2018-11-29 20:42:11 +08001048TEST_F(InputReaderTest, Device_CanDispatchToDisplay) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001049 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001050 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001051 constexpr int32_t eventHubId = 1;
Arthur Hungc23540e2018-11-29 20:42:11 +08001052 const char* DEVICE_LOCATION = "USB1";
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001053 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1054 FakeInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001055 device->addMapper<FakeInputMapper>(eventHubId, mFakePolicy->getReaderConfiguration(),
1056 AINPUT_SOURCE_TOUCHSCREEN);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001057 mReader->pushNextDevice(device);
Arthur Hungc23540e2018-11-29 20:42:11 +08001058
1059 const uint8_t hdmi1 = 1;
1060
1061 // Associated touch screen with second display.
1062 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
1063
1064 // Add default and second display.
Prabir Pradhan28efc192019-11-05 01:10:04 +00001065 mFakePolicy->clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00001066 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +00001067 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
Arthur Hungc23540e2018-11-29 20:42:11 +08001068 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00001069 ui::ROTATION_0, /*isActive=*/true, "local:1", hdmi1,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01001070 ViewportType::EXTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001071 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6cbc9782019-11-15 17:59:25 +00001072 mReader->loopOnce();
Prabir Pradhan28efc192019-11-05 01:10:04 +00001073
1074 // Add the device, and make sure all of the callbacks are triggered.
1075 // The device is added after the input port associations are processed since
1076 // we do not yet support dynamic device-to-display associations.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08001077 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Prabir Pradhan2574dfa2019-10-16 16:35:07 -07001078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhan28efc192019-11-05 01:10:04 +00001079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001080 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
Arthur Hungc23540e2018-11-29 20:42:11 +08001081
Arthur Hung2c9a3342019-07-23 14:18:59 +08001082 // Device should only dispatch to the specified display.
Arthur Hungc23540e2018-11-29 20:42:11 +08001083 ASSERT_EQ(deviceId, device->getId());
1084 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, DISPLAY_ID));
1085 ASSERT_TRUE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hung2c9a3342019-07-23 14:18:59 +08001086
1087 // Can't dispatch event from a disabled device.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08001088 disableDevice(deviceId);
Prabir Pradhan28efc192019-11-05 01:10:04 +00001089 mReader->loopOnce();
Arthur Hung2c9a3342019-07-23 14:18:59 +08001090 ASSERT_FALSE(mReader->canDispatchToDisplay(deviceId, SECONDARY_DISPLAY_ID));
Arthur Hungc23540e2018-11-29 20:42:11 +08001091}
1092
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001093TEST_F(InputReaderTest, WhenEnabledChanges_AllSubdevicesAreUpdated) {
1094 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001095 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001096 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1097 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1098 // Must add at least one mapper or the device will be ignored!
Arpit Singh8e6fb252023-04-06 11:49:17 +00001099 device->addMapper<FakeInputMapper>(eventHubIds[0], mFakePolicy->getReaderConfiguration(),
1100 AINPUT_SOURCE_KEYBOARD);
1101 device->addMapper<FakeInputMapper>(eventHubIds[1], mFakePolicy->getReaderConfiguration(),
1102 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001103 mReader->pushNextDevice(device);
1104 mReader->pushNextDevice(device);
1105 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1106 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1107
1108 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyConfigurationChangedWasCalled(nullptr));
1109
1110 NotifyDeviceResetArgs resetArgs;
1111 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1112 ASSERT_EQ(deviceId, resetArgs.deviceId);
1113 ASSERT_TRUE(device->isEnabled());
1114 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1115 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1116
1117 disableDevice(deviceId);
1118 mReader->loopOnce();
1119
1120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1121 ASSERT_EQ(deviceId, resetArgs.deviceId);
1122 ASSERT_FALSE(device->isEnabled());
1123 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1124 ASSERT_FALSE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1125
1126 enableDevice(deviceId);
1127 mReader->loopOnce();
1128
1129 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
1130 ASSERT_EQ(deviceId, resetArgs.deviceId);
1131 ASSERT_TRUE(device->isEnabled());
1132 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[0]));
1133 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(eventHubIds[1]));
1134}
1135
1136TEST_F(InputReaderTest, GetKeyCodeState_ForwardsRequestsToSubdeviceMappers) {
1137 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001138 constexpr ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD;
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001139 constexpr int32_t eventHubIds[2] = {END_RESERVED_ID, END_RESERVED_ID + 1};
1140 // Add two subdevices to device
1141 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake");
1142 FakeInputMapper& mapperDevice1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001143 device->addMapper<FakeInputMapper>(eventHubIds[0],
1144 mFakePolicy->getReaderConfiguration(),
1145 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001146 FakeInputMapper& mapperDevice2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001147 device->addMapper<FakeInputMapper>(eventHubIds[1],
1148 mFakePolicy->getReaderConfiguration(),
1149 AINPUT_SOURCE_KEYBOARD);
Nathaniel R. Lewisc8bfa542020-02-24 14:05:11 -08001150 mReader->pushNextDevice(device);
1151 mReader->pushNextDevice(device);
1152 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[0], "fake1", deviceClass, nullptr));
1153 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubIds[1], "fake2", deviceClass, nullptr));
1154
1155 mapperDevice1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
1156 mapperDevice2.setKeyCodeState(AKEYCODE_B, AKEY_STATE_DOWN);
1157
1158 ASSERT_EQ(AKEY_STATE_DOWN,
1159 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_A));
1160 ASSERT_EQ(AKEY_STATE_DOWN,
1161 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_B));
1162 ASSERT_EQ(AKEY_STATE_UNKNOWN,
1163 mReader->getKeyCodeState(deviceId, AINPUT_SOURCE_KEYBOARD, AKEYCODE_C));
1164}
1165
Prabir Pradhan7e186182020-11-10 13:56:45 -08001166TEST_F(InputReaderTest, ChangingPointerCaptureNotifiesInputListener) {
1167 NotifyPointerCaptureChangedArgs args;
1168
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001169 auto request = mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001170 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001171 mReader->loopOnce();
1172 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001173 ASSERT_TRUE(args.request.enable) << "Pointer Capture should be enabled.";
1174 ASSERT_EQ(args.request, request) << "Pointer Capture sequence number should match.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001175
1176 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001177 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001178 mReader->loopOnce();
1179 mFakeListener->assertNotifyCaptureWasCalled(&args);
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001180 ASSERT_FALSE(args.request.enable) << "Pointer Capture should be disabled.";
Prabir Pradhan7e186182020-11-10 13:56:45 -08001181
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001182 // Verify that the Pointer Capture state is not updated when the configuration value
Prabir Pradhan7e186182020-11-10 13:56:45 -08001183 // does not change.
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001184 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan7e186182020-11-10 13:56:45 -08001185 mReader->loopOnce();
Prabir Pradhan5cc1a692021-08-06 14:01:18 +00001186 mFakeListener->assertNotifyCaptureWasNotCalled();
Prabir Pradhan7e186182020-11-10 13:56:45 -08001187}
1188
Chris Ye87143712020-11-10 05:05:58 +00001189class FakeVibratorInputMapper : public FakeInputMapper {
1190public:
Arpit Singh8e6fb252023-04-06 11:49:17 +00001191 FakeVibratorInputMapper(InputDeviceContext& deviceContext,
1192 const InputReaderConfiguration& readerConfig, uint32_t sources)
1193 : FakeInputMapper(deviceContext, readerConfig, sources) {}
Chris Ye87143712020-11-10 05:05:58 +00001194
1195 std::vector<int32_t> getVibratorIds() override { return getDeviceContext().getVibratorIds(); }
1196};
1197
1198TEST_F(InputReaderTest, VibratorGetVibratorIds) {
1199 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001200 ftl::Flags<InputDeviceClass> deviceClass =
1201 InputDeviceClass::KEYBOARD | InputDeviceClass::VIBRATOR;
Chris Ye87143712020-11-10 05:05:58 +00001202 constexpr int32_t eventHubId = 1;
1203 const char* DEVICE_LOCATION = "BLUETOOTH";
1204 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1205 FakeVibratorInputMapper& mapper =
Arpit Singh8e6fb252023-04-06 11:49:17 +00001206 device->addMapper<FakeVibratorInputMapper>(eventHubId,
1207 mFakePolicy->getReaderConfiguration(),
1208 AINPUT_SOURCE_KEYBOARD);
Chris Ye87143712020-11-10 05:05:58 +00001209 mReader->pushNextDevice(device);
1210
1211 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1212 ASSERT_NO_FATAL_FAILURE(mapper.assertConfigureWasCalled());
1213
1214 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
1215 ASSERT_EQ(mReader->getVibratorIds(deviceId).size(), 2U);
1216}
1217
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001218// --- FakePeripheralController ---
Kim Low03ea0352020-11-06 12:45:07 -08001219
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001220class FakePeripheralController : public PeripheralControllerInterface {
Chris Yee2b1e5c2021-03-10 22:45:12 -08001221public:
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001222 FakePeripheralController(InputDeviceContext& deviceContext) : mDeviceContext(deviceContext) {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001223
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001224 ~FakePeripheralController() override {}
Chris Yee2b1e5c2021-03-10 22:45:12 -08001225
Andy Chenf9f1a022022-08-29 20:07:10 -04001226 int32_t getEventHubId() const { return getDeviceContext().getEventHubId(); }
1227
Chris Yee2b1e5c2021-03-10 22:45:12 -08001228 void populateDeviceInfo(InputDeviceInfo* deviceInfo) override {}
1229
1230 void dump(std::string& dump) override {}
1231
1232 std::optional<int32_t> getBatteryCapacity(int32_t batteryId) override {
1233 return getDeviceContext().getBatteryCapacity(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001234 }
1235
Chris Yee2b1e5c2021-03-10 22:45:12 -08001236 std::optional<int32_t> getBatteryStatus(int32_t batteryId) override {
1237 return getDeviceContext().getBatteryStatus(batteryId);
Kim Low03ea0352020-11-06 12:45:07 -08001238 }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001239
1240 bool setLightColor(int32_t lightId, int32_t color) override {
1241 getDeviceContext().setLightBrightness(lightId, color >> 24);
1242 return true;
1243 }
1244
1245 std::optional<int32_t> getLightColor(int32_t lightId) override {
1246 std::optional<int32_t> result = getDeviceContext().getLightBrightness(lightId);
1247 if (!result.has_value()) {
1248 return std::nullopt;
1249 }
1250 return result.value() << 24;
1251 }
Chris Yee2b1e5c2021-03-10 22:45:12 -08001252
1253 bool setLightPlayerId(int32_t lightId, int32_t playerId) override { return true; }
1254
1255 std::optional<int32_t> getLightPlayerId(int32_t lightId) override { return std::nullopt; }
1256
1257private:
1258 InputDeviceContext& mDeviceContext;
1259 inline int32_t getDeviceId() { return mDeviceContext.getId(); }
1260 inline InputDeviceContext& getDeviceContext() { return mDeviceContext; }
Andy Chenf9f1a022022-08-29 20:07:10 -04001261 inline InputDeviceContext& getDeviceContext() const { return mDeviceContext; }
Chris Ye3fdbfef2021-01-06 18:45:18 -08001262};
1263
Chris Yee2b1e5c2021-03-10 22:45:12 -08001264TEST_F(InputReaderTest, BatteryGetCapacity) {
1265 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001266 ftl::Flags<InputDeviceClass> deviceClass =
1267 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001268 constexpr int32_t eventHubId = 1;
1269 const char* DEVICE_LOCATION = "BLUETOOTH";
1270 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001271 FakePeripheralController& controller =
1272 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001273 mReader->pushNextDevice(device);
1274
1275 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1276
Harry Cuttsa5b71292022-11-28 12:56:17 +00001277 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY),
1278 FakeEventHub::BATTERY_CAPACITY);
1279 ASSERT_EQ(mReader->getBatteryCapacity(deviceId), FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001280}
1281
1282TEST_F(InputReaderTest, BatteryGetStatus) {
1283 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001284 ftl::Flags<InputDeviceClass> deviceClass =
1285 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
Chris Yee2b1e5c2021-03-10 22:45:12 -08001286 constexpr int32_t eventHubId = 1;
1287 const char* DEVICE_LOCATION = "BLUETOOTH";
1288 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001289 FakePeripheralController& controller =
1290 device->addController<FakePeripheralController>(eventHubId);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001291 mReader->pushNextDevice(device);
1292
1293 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1294
Harry Cuttsa5b71292022-11-28 12:56:17 +00001295 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY),
1296 FakeEventHub::BATTERY_STATUS);
1297 ASSERT_EQ(mReader->getBatteryStatus(deviceId), FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -08001298}
1299
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001300TEST_F(InputReaderTest, BatteryGetDevicePath) {
1301 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
1302 ftl::Flags<InputDeviceClass> deviceClass =
1303 InputDeviceClass::KEYBOARD | InputDeviceClass::BATTERY;
1304 constexpr int32_t eventHubId = 1;
1305 const char* DEVICE_LOCATION = "BLUETOOTH";
1306 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
1307 device->addController<FakePeripheralController>(eventHubId);
1308 mReader->pushNextDevice(device);
1309
1310 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
1311
Harry Cuttsa5b71292022-11-28 12:56:17 +00001312 ASSERT_EQ(mReader->getBatteryDevicePath(deviceId), FakeEventHub::BATTERY_DEVPATH);
Prabir Pradhane287ecd2022-09-07 21:18:05 +00001313}
1314
Chris Ye3fdbfef2021-01-06 18:45:18 -08001315TEST_F(InputReaderTest, LightGetColor) {
1316 constexpr int32_t deviceId = END_RESERVED_ID + 1000;
Dominik Laskowski2f01d772022-03-23 16:01:29 -07001317 ftl::Flags<InputDeviceClass> deviceClass = InputDeviceClass::KEYBOARD | InputDeviceClass::LIGHT;
Chris Ye3fdbfef2021-01-06 18:45:18 -08001318 constexpr int32_t eventHubId = 1;
1319 const char* DEVICE_LOCATION = "BLUETOOTH";
1320 std::shared_ptr<InputDevice> device = mReader->newDevice(deviceId, "fake", DEVICE_LOCATION);
Chris Ye1dd2e5c2021-04-04 23:12:41 -07001321 FakePeripheralController& controller =
1322 device->addController<FakePeripheralController>(eventHubId);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001323 mReader->pushNextDevice(device);
1324 RawLightInfo info = {.id = 1,
1325 .name = "Mono",
1326 .maxBrightness = 255,
1327 .flags = InputLightClass::BRIGHTNESS,
1328 .path = ""};
Harry Cutts33476232023-01-30 19:57:29 +00001329 mFakeEventHub->addRawLightInfo(/*rawId=*/1, std::move(info));
1330 mFakeEventHub->fakeLightBrightness(/*rawId=*/1, 0x55);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001331
1332 ASSERT_NO_FATAL_FAILURE(addDevice(eventHubId, "fake", deviceClass, nullptr));
Chris Ye3fdbfef2021-01-06 18:45:18 -08001333
Harry Cutts33476232023-01-30 19:57:29 +00001334 ASSERT_TRUE(controller.setLightColor(/*lightId=*/1, LIGHT_BRIGHTNESS));
1335 ASSERT_EQ(controller.getLightColor(/*lightId=*/1), LIGHT_BRIGHTNESS);
1336 ASSERT_TRUE(mReader->setLightColor(deviceId, /*lightId=*/1, LIGHT_BRIGHTNESS));
1337 ASSERT_EQ(mReader->getLightColor(deviceId, /*lightId=*/1), LIGHT_BRIGHTNESS);
Chris Ye3fdbfef2021-01-06 18:45:18 -08001338}
1339
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001340// --- InputReaderIntegrationTest ---
1341
1342// These tests create and interact with the InputReader only through its interface.
1343// The InputReader is started during SetUp(), which starts its processing in its own
1344// thread. The tests use linux uinput to emulate input devices.
1345// NOTE: Interacting with the physical device while these tests are running may cause
1346// the tests to fail.
1347class InputReaderIntegrationTest : public testing::Test {
1348protected:
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001349 std::unique_ptr<TestInputListener> mTestListener;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001350 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001351 std::unique_ptr<InputReaderInterface> mReader;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001352
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001353 std::shared_ptr<FakePointerController> mFakePointerController;
1354
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001355 constexpr static auto EVENT_HAPPENED_TIMEOUT = 2000ms;
1356 constexpr static auto EVENT_DID_NOT_HAPPEN_TIMEOUT = 30ms;
1357
Chris Yea52ade12020-08-27 16:49:20 -07001358 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001359#if !defined(__ANDROID__)
1360 GTEST_SKIP();
1361#endif
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -07001362 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00001363 mFakePointerController = std::make_shared<FakePointerController>();
1364 mFakePolicy->setPointerController(mFakePointerController);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001365
Arpit Singh440bf652023-08-09 09:23:43 +00001366 setupInputReader();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001367 }
1368
Chris Yea52ade12020-08-27 16:49:20 -07001369 void TearDown() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001370#if !defined(__ANDROID__)
1371 return;
1372#endif
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001373 ASSERT_EQ(mReader->stop(), OK);
Siarhei Vishniakou18050092021-09-01 13:32:49 -07001374 mReader.reset();
1375 mTestListener.reset();
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001376 mFakePolicy.clear();
1377 }
Prabir Pradhanda20b172022-09-26 17:01:18 +00001378
1379 std::optional<InputDeviceInfo> findDeviceByName(const std::string& name) {
1380 const std::vector<InputDeviceInfo> inputDevices = mFakePolicy->getInputDevices();
1381 const auto& it = std::find_if(inputDevices.begin(), inputDevices.end(),
1382 [&name](const InputDeviceInfo& info) {
1383 return info.getIdentifier().name == name;
1384 });
1385 return it != inputDevices.end() ? std::make_optional(*it) : std::nullopt;
1386 }
Arpit Singh440bf652023-08-09 09:23:43 +00001387
1388 void setupInputReader() {
Prabir Pradhanafb7d612024-01-08 22:45:24 +00001389 mTestListener = std::make_unique<TestInputListener>(EVENT_HAPPENED_TIMEOUT,
1390 EVENT_DID_NOT_HAPPEN_TIMEOUT);
Arpit Singh440bf652023-08-09 09:23:43 +00001391
1392 mReader = std::make_unique<InputReader>(std::make_shared<EventHub>(), mFakePolicy,
1393 *mTestListener);
1394 ASSERT_EQ(mReader->start(), OK);
1395
1396 // Since this test is run on a real device, all the input devices connected
1397 // to the test device will show up in mReader. We wait for those input devices to
1398 // show up before beginning the tests.
1399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyInputDevicesChangedWasCalled());
1401 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1402 }
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001403};
1404
1405TEST_F(InputReaderIntegrationTest, TestInvalidDevice) {
1406 // An invalid input device that is only used for this test.
1407 class InvalidUinputDevice : public UinputDevice {
1408 public:
Harry Cutts33476232023-01-30 19:57:29 +00001409 InvalidUinputDevice() : UinputDevice("Invalid Device", /*productId=*/99) {}
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001410
1411 private:
1412 void configureDevice(int fd, uinput_user_dev* device) override {}
1413 };
1414
1415 const size_t numDevices = mFakePolicy->getInputDevices().size();
1416
1417 // UinputDevice does not set any event or key bits, so InputReader should not
1418 // consider it as a valid device.
1419 std::unique_ptr<UinputDevice> invalidDevice = createUinputDevice<InvalidUinputDevice>();
1420 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1421 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1422 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1423
1424 invalidDevice.reset();
1425 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesNotChanged());
1426 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasNotCalled());
1427 ASSERT_EQ(numDevices, mFakePolicy->getInputDevices().size());
1428}
1429
1430TEST_F(InputReaderIntegrationTest, AddNewDevice) {
1431 const size_t initialNumDevices = mFakePolicy->getInputDevices().size();
1432
1433 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1434 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1435 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1436 ASSERT_EQ(initialNumDevices + 1, mFakePolicy->getInputDevices().size());
1437
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001438 const auto device = findDeviceByName(keyboard->getName());
1439 ASSERT_TRUE(device.has_value());
1440 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1441 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources());
1442 ASSERT_EQ(0U, device->getMotionRanges().size());
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001443
1444 keyboard.reset();
1445 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1446 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1447 ASSERT_EQ(initialNumDevices, mFakePolicy->getInputDevices().size());
1448}
1449
1450TEST_F(InputReaderIntegrationTest, SendsEventsToInputListener) {
1451 std::unique_ptr<UinputHomeKey> keyboard = createUinputDevice<UinputHomeKey>();
1452 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1453
1454 NotifyConfigurationChangedArgs configChangedArgs;
1455 ASSERT_NO_FATAL_FAILURE(
1456 mTestListener->assertNotifyConfigurationChangedWasCalled(&configChangedArgs));
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001457 int32_t prevId = configChangedArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001458 nsecs_t prevTimestamp = configChangedArgs.eventTime;
1459
1460 NotifyKeyArgs keyArgs;
1461 keyboard->pressAndReleaseHomeKey();
1462 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1463 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001464 ASSERT_NE(prevId, keyArgs.id);
Garfield Tanc51d1ba2020-01-28 13:24:04 -08001465 prevId = keyArgs.id;
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001466 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001467 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001468 prevTimestamp = keyArgs.eventTime;
1469
1470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs));
1471 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
Garfield Tan1c7bc862020-01-28 13:24:04 -08001472 ASSERT_NE(prevId, keyArgs.id);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001473 ASSERT_LE(prevTimestamp, keyArgs.eventTime);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00001474 ASSERT_LE(keyArgs.eventTime, keyArgs.readTime);
Prabir Pradhan1aed8582019-12-30 11:46:51 -08001475}
Michael Wrightd02c5b62014-02-10 15:10:22 -08001476
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001477TEST_F(InputReaderIntegrationTest, ExternalStylusesButtons) {
1478 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
1479 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1480
1481 const auto device = findDeviceByName(stylus->getName());
1482 ASSERT_TRUE(device.has_value());
1483
Prabir Pradhana3621852022-10-14 18:57:23 +00001484 // An external stylus with buttons should also be recognized as a keyboard.
1485 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_STYLUS, device->getSources())
Prabir Pradhane1a41a82022-10-14 18:06:50 +00001486 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1487 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC, device->getKeyboardType());
1488
1489 const auto DOWN =
1490 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD));
1491 const auto UP = AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD));
1492
1493 stylus->pressAndReleaseKey(BTN_STYLUS);
1494 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1495 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1496 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1497 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY))));
1498
1499 stylus->pressAndReleaseKey(BTN_STYLUS2);
1500 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1501 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1502 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1503 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_SECONDARY))));
1504
1505 stylus->pressAndReleaseKey(BTN_STYLUS3);
1506 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1507 AllOf(DOWN, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1508 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(
1509 AllOf(UP, WithKeyCode(AKEYCODE_STYLUS_BUTTON_TERTIARY))));
1510}
1511
Prabir Pradhan3c28b942023-08-18 20:02:01 +00001512TEST_F(InputReaderIntegrationTest, KeyboardWithStylusButtons) {
1513 std::unique_ptr<UinputKeyboard> keyboard =
1514 createUinputDevice<UinputKeyboard>("KeyboardWithStylusButtons", /*productId=*/99,
1515 std::initializer_list<int>{KEY_Q, KEY_W, KEY_E,
1516 KEY_R, KEY_T, KEY_Y,
1517 BTN_STYLUS, BTN_STYLUS2,
1518 BTN_STYLUS3});
1519 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1520
1521 const auto device = findDeviceByName(keyboard->getName());
1522 ASSERT_TRUE(device.has_value());
1523
1524 // An alphabetical keyboard that reports stylus buttons should not be recognized as a stylus.
1525 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1526 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1527 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, device->getKeyboardType());
1528}
1529
Prabir Pradhan37a819b2023-08-22 23:20:16 +00001530TEST_F(InputReaderIntegrationTest, HidUsageKeyboardIsNotAStylus) {
1531 // Create a Uinput keyboard that simulates a keyboard that can report HID usage codes. The
1532 // hid-input driver reports HID usage codes using the value for EV_MSC MSC_SCAN event.
1533 std::unique_ptr<UinputKeyboardWithHidUsage> keyboard =
1534 createUinputDevice<UinputKeyboardWithHidUsage>(
1535 std::initializer_list<int>{KEY_VOLUMEUP, KEY_VOLUMEDOWN});
1536 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1537
1538 const auto device = findDeviceByName(keyboard->getName());
1539 ASSERT_TRUE(device.has_value());
1540
1541 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, device->getSources())
1542 << "Unexpected source " << inputEventSourceToString(device->getSources()).c_str();
1543
1544 // If a device supports reporting HID usage codes, it shouldn't automatically support
1545 // stylus keys.
1546 const std::vector<int> keycodes{AKEYCODE_STYLUS_BUTTON_PRIMARY};
1547 uint8_t outFlags[] = {0};
1548 ASSERT_TRUE(mReader->hasKeys(device->getId(), AINPUT_SOURCE_KEYBOARD, keycodes, outFlags));
1549 ASSERT_EQ(0, outFlags[0]) << "Keyboard should not have stylus button";
1550}
1551
Siarhei Vishniakoua0d2b802020-05-13 14:00:31 -07001552/**
1553 * The Steam controller sends BTN_GEAR_DOWN and BTN_GEAR_UP for the two "paddle" buttons
1554 * on the back. In this test, we make sure that BTN_GEAR_DOWN / BTN_WHEEL and BTN_GEAR_UP
1555 * are passed to the listener.
1556 */
1557static_assert(BTN_GEAR_DOWN == BTN_WHEEL);
1558TEST_F(InputReaderIntegrationTest, SendsGearDownAndUpToInputListener) {
1559 std::unique_ptr<UinputSteamController> controller = createUinputDevice<UinputSteamController>();
1560 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1561 NotifyKeyArgs keyArgs;
1562
1563 controller->pressAndReleaseKey(BTN_GEAR_DOWN);
1564 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1565 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1566 ASSERT_EQ(BTN_GEAR_DOWN, keyArgs.scanCode);
1567
1568 controller->pressAndReleaseKey(BTN_GEAR_UP);
1569 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_DOWN
1570 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasCalled(&keyArgs)); // ACTION_UP
1571 ASSERT_EQ(BTN_GEAR_UP, keyArgs.scanCode);
1572}
1573
Prabir Pradhan484d55a2022-10-14 23:17:16 +00001574// --- TouchIntegrationTest ---
1575
Arpit Singh440bf652023-08-09 09:23:43 +00001576class BaseTouchIntegrationTest : public InputReaderIntegrationTest {
Arthur Hungaab25622020-01-16 11:22:11 +08001577protected:
Arthur Hungaab25622020-01-16 11:22:11 +08001578 const std::string UNIQUE_ID = "local:0";
1579
Chris Yea52ade12020-08-27 16:49:20 -07001580 void SetUp() override {
Siarhei Vishniakou31977182022-09-30 08:51:23 -07001581#if !defined(__ANDROID__)
1582 GTEST_SKIP();
1583#endif
Arthur Hungaab25622020-01-16 11:22:11 +08001584 InputReaderIntegrationTest::SetUp();
1585 // At least add an internal display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00001586 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1587 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Arthur Hungaab25622020-01-16 11:22:11 +08001588
1589 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT));
1590 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1591 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
Prabir Pradhanda20b172022-09-26 17:01:18 +00001592 const auto info = findDeviceByName(mDevice->getName());
1593 ASSERT_TRUE(info);
1594 mDeviceInfo = *info;
Arthur Hungaab25622020-01-16 11:22:11 +08001595 }
1596
1597 void setDisplayInfoAndReconfigure(int32_t displayId, int32_t width, int32_t height,
Michael Wrighta9cf4192022-12-01 23:46:39 +00001598 ui::Rotation orientation, const std::string& uniqueId,
Arthur Hungaab25622020-01-16 11:22:11 +08001599 std::optional<uint8_t> physicalPort,
1600 ViewportType viewportType) {
Harry Cutts33476232023-01-30 19:57:29 +00001601 mFakePolicy->addDisplayViewport(displayId, width, height, orientation, /*isActive=*/true,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00001602 uniqueId, physicalPort, viewportType);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00001603 mReader->requestRefreshConfiguration(InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hungaab25622020-01-16 11:22:11 +08001604 }
1605
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001606 void assertReceivedMotion(int32_t action, const std::vector<Point>& points) {
1607 NotifyMotionArgs args;
1608 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1609 EXPECT_EQ(action, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07001610 ASSERT_EQ(points.size(), args.getPointerCount());
1611 for (size_t i = 0; i < args.getPointerCount(); i++) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001612 EXPECT_EQ(points[i].x, args.pointerCoords[i].getX());
1613 EXPECT_EQ(points[i].y, args.pointerCoords[i].getY());
1614 }
1615 }
1616
Arthur Hungaab25622020-01-16 11:22:11 +08001617 std::unique_ptr<UinputTouchScreen> mDevice;
Prabir Pradhanda20b172022-09-26 17:01:18 +00001618 InputDeviceInfo mDeviceInfo;
Arthur Hungaab25622020-01-16 11:22:11 +08001619};
1620
Arpit Singh440bf652023-08-09 09:23:43 +00001621enum class TouchIntegrationTestDisplays { DISPLAY_INTERNAL, DISPLAY_INPUT_PORT, DISPLAY_UNIQUE_ID };
1622
1623class TouchIntegrationTest : public BaseTouchIntegrationTest,
1624 public testing::WithParamInterface<TouchIntegrationTestDisplays> {
1625protected:
1626 static constexpr std::optional<uint8_t> DISPLAY_PORT = 0;
1627 const std::string INPUT_PORT = "uinput_touch/input0";
1628
1629 void SetUp() override {
1630#if !defined(__ANDROID__)
1631 GTEST_SKIP();
1632#endif
1633 if (GetParam() == TouchIntegrationTestDisplays::DISPLAY_INTERNAL) {
1634 BaseTouchIntegrationTest::SetUp();
1635 return;
1636 }
1637
1638 // setup policy with a input-port or UniqueId association to the display
1639 bool isInputPortAssociation =
1640 GetParam() == TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT;
1641
1642 mFakePolicy = sp<FakeInputReaderPolicy>::make();
1643 if (isInputPortAssociation) {
1644 mFakePolicy->addInputPortAssociation(INPUT_PORT, DISPLAY_PORT.value());
1645 } else {
1646 mFakePolicy->addInputUniqueIdAssociation(INPUT_PORT, UNIQUE_ID);
1647 }
1648 mFakePointerController = std::make_shared<FakePointerController>();
1649 mFakePolicy->setPointerController(mFakePointerController);
1650
1651 InputReaderIntegrationTest::setupInputReader();
1652
1653 mDevice = createUinputDevice<UinputTouchScreen>(Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
1654 INPUT_PORT);
1655 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1656
1657 // Add a display linked to a physical port or UniqueId.
1658 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
1659 UNIQUE_ID, isInputPortAssociation ? DISPLAY_PORT : NO_PORT,
1660 ViewportType::INTERNAL);
1661 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1662 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1663 const auto info = findDeviceByName(mDevice->getName());
1664 ASSERT_TRUE(info);
1665 mDeviceInfo = *info;
1666 }
1667};
1668
1669TEST_P(TouchIntegrationTest, MultiTouchDeviceSource) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +00001670 // The UinputTouchScreen is an MT device that supports MT_TOOL_TYPE and also supports stylus
1671 // buttons. It should show up as a touchscreen, stylus, and keyboard (for reporting button
1672 // presses).
1673 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD,
1674 mDeviceInfo.getSources());
1675}
1676
Arpit Singh440bf652023-08-09 09:23:43 +00001677TEST_P(TouchIntegrationTest, InputEvent_ProcessSingleTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001678 NotifyMotionArgs args;
1679 const Point centerPoint = mDevice->getCenterPoint();
1680
1681 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001682 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001683 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001684 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001685 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1686 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1687
1688 // ACTION_MOVE
1689 mDevice->sendMove(centerPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001690 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001691 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1692 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1693
1694 // ACTION_UP
1695 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001696 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001697 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1698 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1699}
1700
Arpit Singh440bf652023-08-09 09:23:43 +00001701TEST_P(TouchIntegrationTest, InputEvent_ProcessMultiTouch) {
Arthur Hungaab25622020-01-16 11:22:11 +08001702 NotifyMotionArgs args;
1703 const Point centerPoint = mDevice->getCenterPoint();
1704
1705 // ACTION_DOWN
Arthur Hung9ad18942021-06-19 02:04:46 +00001706 mDevice->sendSlot(FIRST_SLOT);
1707 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001708 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001709 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001710 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1711 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1712
1713 // ACTION_POINTER_DOWN (Second slot)
1714 const Point secondPoint = centerPoint + Point(100, 100);
1715 mDevice->sendSlot(SECOND_SLOT);
1716 mDevice->sendTrackingId(SECOND_TRACKING_ID);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001717 mDevice->sendDown(secondPoint);
1718 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001719 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001720 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001721
1722 // ACTION_MOVE (Second slot)
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001723 mDevice->sendMove(secondPoint + Point(1, 1));
1724 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001725 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1726 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1727
1728 // ACTION_POINTER_UP (Second slot)
arthurhungcc7f9802020-04-30 17:55:40 +08001729 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001730 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001731 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001732 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001733
1734 // ACTION_UP
1735 mDevice->sendSlot(FIRST_SLOT);
1736 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001737 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001738 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1739 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
1740}
1741
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001742/**
1743 * What happens when a pointer goes up while another pointer moves in the same frame? Are POINTER_UP
1744 * events guaranteed to contain the same data as a preceding MOVE, or can they contain different
1745 * data?
1746 * In this test, we try to send a change in coordinates in Pointer 0 in the same frame as the
1747 * liftoff of Pointer 1. We check that POINTER_UP event is generated first, and the MOVE event
1748 * for Pointer 0 only is generated after.
1749 * Suppose we are only interested in learning the movement of Pointer 0. If we only observe MOVE
1750 * events, we will not miss any information.
1751 * Even though the Pointer 1 up event contains updated Pointer 0 coordinates, there is another MOVE
1752 * event generated afterwards that contains the newest movement of pointer 0.
1753 * This is important for palm rejection. If there is a subsequent InputListener stage that detects
1754 * palms, and wants to cancel Pointer 1, then it is safe to simply drop POINTER_1_UP event without
1755 * losing information about non-palm pointers.
1756 */
Arpit Singh440bf652023-08-09 09:23:43 +00001757TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001758 NotifyMotionArgs args;
1759 const Point centerPoint = mDevice->getCenterPoint();
1760
1761 // ACTION_DOWN
1762 mDevice->sendSlot(FIRST_SLOT);
1763 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1764 mDevice->sendDown(centerPoint);
1765 mDevice->sendSync();
1766 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1767
1768 // ACTION_POINTER_DOWN (Second slot)
1769 const Point secondPoint = centerPoint + Point(100, 100);
1770 mDevice->sendSlot(SECOND_SLOT);
1771 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1772 mDevice->sendDown(secondPoint);
1773 mDevice->sendSync();
1774 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1775
1776 // ACTION_MOVE (First slot)
1777 mDevice->sendSlot(FIRST_SLOT);
1778 mDevice->sendMove(centerPoint + Point(5, 5));
1779 // ACTION_POINTER_UP (Second slot)
1780 mDevice->sendSlot(SECOND_SLOT);
1781 mDevice->sendPointerUp();
1782 // Send a single sync for the above 2 pointer updates
1783 mDevice->sendSync();
1784
1785 // First, we should get POINTER_UP for the second pointer
1786 assertReceivedMotion(ACTION_POINTER_1_UP,
1787 {/*first pointer */ centerPoint + Point(5, 5),
1788 /*second pointer*/ secondPoint});
1789
1790 // Next, the MOVE event for the first pointer
1791 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1792}
1793
1794/**
1795 * Similar scenario as above. The difference is that when the second pointer goes up, it will first
1796 * move, and then it will go up, all in the same frame.
1797 * In this scenario, the movement of the second pointer just prior to liftoff is ignored, and never
1798 * gets sent to the listener.
1799 */
Arpit Singh440bf652023-08-09 09:23:43 +00001800TEST_P(TouchIntegrationTest, MultiTouch_PointerMoveAndSecondPointerMoveAndUp) {
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001801 NotifyMotionArgs args;
1802 const Point centerPoint = mDevice->getCenterPoint();
1803
1804 // ACTION_DOWN
1805 mDevice->sendSlot(FIRST_SLOT);
1806 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1807 mDevice->sendDown(centerPoint);
1808 mDevice->sendSync();
1809 assertReceivedMotion(AMOTION_EVENT_ACTION_DOWN, {centerPoint});
1810
1811 // ACTION_POINTER_DOWN (Second slot)
1812 const Point secondPoint = centerPoint + Point(100, 100);
1813 mDevice->sendSlot(SECOND_SLOT);
1814 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1815 mDevice->sendDown(secondPoint);
1816 mDevice->sendSync();
1817 assertReceivedMotion(ACTION_POINTER_1_DOWN, {centerPoint, secondPoint});
1818
1819 // ACTION_MOVE (First slot)
1820 mDevice->sendSlot(FIRST_SLOT);
1821 mDevice->sendMove(centerPoint + Point(5, 5));
1822 // ACTION_POINTER_UP (Second slot)
1823 mDevice->sendSlot(SECOND_SLOT);
1824 mDevice->sendMove(secondPoint + Point(6, 6));
1825 mDevice->sendPointerUp();
1826 // Send a single sync for the above 2 pointer updates
1827 mDevice->sendSync();
1828
1829 // First, we should get POINTER_UP for the second pointer
1830 // The movement of the second pointer during the liftoff frame is ignored.
1831 // The coordinates 'secondPoint + Point(6, 6)' are never sent to the listener.
1832 assertReceivedMotion(ACTION_POINTER_1_UP,
1833 {/*first pointer */ centerPoint + Point(5, 5),
1834 /*second pointer*/ secondPoint});
1835
1836 // Next, the MOVE event for the first pointer
1837 assertReceivedMotion(AMOTION_EVENT_ACTION_MOVE, {centerPoint + Point(5, 5)});
1838}
1839
Arpit Singh440bf652023-08-09 09:23:43 +00001840TEST_P(TouchIntegrationTest, InputEvent_ProcessPalm) {
Arthur Hungaab25622020-01-16 11:22:11 +08001841 NotifyMotionArgs args;
1842 const Point centerPoint = mDevice->getCenterPoint();
1843
1844 // ACTION_DOWN
arthurhungcc7f9802020-04-30 17:55:40 +08001845 mDevice->sendSlot(FIRST_SLOT);
1846 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Arthur Hungaab25622020-01-16 11:22:11 +08001847 mDevice->sendDown(centerPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001848 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001849 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1850 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
1851
arthurhungcc7f9802020-04-30 17:55:40 +08001852 // ACTION_POINTER_DOWN (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001853 const Point secondPoint = centerPoint + Point(100, 100);
1854 mDevice->sendSlot(SECOND_SLOT);
1855 mDevice->sendTrackingId(SECOND_TRACKING_ID);
1856 mDevice->sendDown(secondPoint);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001857 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001858 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001859 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001860
arthurhungcc7f9802020-04-30 17:55:40 +08001861 // ACTION_MOVE (second slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001862 mDevice->sendMove(secondPoint + Point(1, 1));
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001863 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001864 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1865 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
1866
arthurhungcc7f9802020-04-30 17:55:40 +08001867 // Send MT_TOOL_PALM (second slot), which indicates that the touch IC has determined this to be
1868 // a palm event.
1869 // Expect to receive the ACTION_POINTER_UP with cancel flag.
Arthur Hungaab25622020-01-16 11:22:11 +08001870 mDevice->sendToolType(MT_TOOL_PALM);
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001871 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001872 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08001873 ASSERT_EQ(ACTION_POINTER_1_UP, args.action);
arthurhungcc7f9802020-04-30 17:55:40 +08001874 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, args.flags);
Arthur Hungaab25622020-01-16 11:22:11 +08001875
arthurhungcc7f9802020-04-30 17:55:40 +08001876 // Send up to second slot, expect first slot send moving.
1877 mDevice->sendPointerUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001878 mDevice->sendSync();
arthurhungcc7f9802020-04-30 17:55:40 +08001879 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1880 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001881
arthurhungcc7f9802020-04-30 17:55:40 +08001882 // Send ACTION_UP (first slot)
Arthur Hungaab25622020-01-16 11:22:11 +08001883 mDevice->sendSlot(FIRST_SLOT);
1884 mDevice->sendUp();
Siarhei Vishniakoufd97e9d2022-01-04 16:59:04 -08001885 mDevice->sendSync();
Arthur Hungaab25622020-01-16 11:22:11 +08001886
arthurhungcc7f9802020-04-30 17:55:40 +08001887 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(&args));
1888 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Arthur Hungaab25622020-01-16 11:22:11 +08001889}
1890
Prabir Pradhanc09ec6d2023-08-14 22:31:43 +00001891/**
1892 * Some drivers historically have reported axis values outside of the range specified in the
1893 * evdev axis info. Ensure we don't crash when this happens. For example, a driver may report a
1894 * pressure value greater than the reported maximum, since it unclear what specific meaning the
1895 * maximum value for pressure has (beyond the maximum value that can be produced by a sensor),
1896 * and no units for pressure (resolution) is specified by the evdev documentation.
1897 */
1898TEST_P(TouchIntegrationTest, AcceptsAxisValuesOutsideReportedRange) {
1899 const Point centerPoint = mDevice->getCenterPoint();
1900
1901 // Down with pressure outside the reported range
1902 mDevice->sendSlot(FIRST_SLOT);
1903 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1904 mDevice->sendDown(centerPoint);
1905 mDevice->sendPressure(UinputTouchScreen::RAW_PRESSURE_MAX + 2);
1906 mDevice->sendSync();
1907 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1908 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1909
1910 // Move to a point outside the reported range
1911 mDevice->sendMove(Point(DISPLAY_WIDTH, DISPLAY_HEIGHT) + Point(1, 1));
1912 mDevice->sendSync();
1913 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1914 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1915
1916 // Up
1917 mDevice->sendUp();
1918 mDevice->sendSync();
1919 ASSERT_NO_FATAL_FAILURE(
1920 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1921}
1922
Arpit Singh440bf652023-08-09 09:23:43 +00001923TEST_P(TouchIntegrationTest, NotifiesPolicyWhenStylusGestureStarted) {
Prabir Pradhanda20b172022-09-26 17:01:18 +00001924 const Point centerPoint = mDevice->getCenterPoint();
1925
1926 // Send down with the pen tool selected. The policy should be notified of the stylus presence.
1927 mDevice->sendSlot(FIRST_SLOT);
1928 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1929 mDevice->sendToolType(MT_TOOL_PEN);
1930 mDevice->sendDown(centerPoint);
1931 mDevice->sendSync();
1932 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1933 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001934 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001935
1936 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1937
1938 // Release the stylus touch.
1939 mDevice->sendUp();
1940 mDevice->sendSync();
1941 ASSERT_NO_FATAL_FAILURE(
1942 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1943
1944 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1945
1946 // Touch down with the finger, without the pen tool selected. The policy is not notified.
1947 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1948 mDevice->sendToolType(MT_TOOL_FINGER);
1949 mDevice->sendDown(centerPoint);
1950 mDevice->sendSync();
1951 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1952 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001953 WithToolType(ToolType::FINGER))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001954
1955 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotNotified());
1956
1957 mDevice->sendUp();
1958 mDevice->sendSync();
1959 ASSERT_NO_FATAL_FAILURE(
1960 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
1961
1962 // Send a move event with the stylus tool without BTN_TOUCH to generate a hover enter.
1963 // The policy should be notified of the stylus presence.
1964 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1965 mDevice->sendToolType(MT_TOOL_PEN);
1966 mDevice->sendMove(centerPoint);
1967 mDevice->sendSync();
1968 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1969 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07001970 WithToolType(ToolType::STYLUS))));
Prabir Pradhanda20b172022-09-26 17:01:18 +00001971
1972 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertStylusGestureNotified(mDeviceInfo.getId()));
1973}
1974
Arpit Singh440bf652023-08-09 09:23:43 +00001975TEST_P(TouchIntegrationTest, ExternalStylusConnectedDuringTouchGesture) {
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001976 const Point centerPoint = mDevice->getCenterPoint();
1977
1978 // Down
1979 mDevice->sendSlot(FIRST_SLOT);
1980 mDevice->sendTrackingId(FIRST_TRACKING_ID);
1981 mDevice->sendDown(centerPoint);
1982 mDevice->sendSync();
1983 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1984 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
1985
1986 // Move
1987 mDevice->sendMove(centerPoint + Point(1, 1));
1988 mDevice->sendSync();
1989 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
1990 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
1991
1992 // Connecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
1993 auto externalStylus = createUinputDevice<UinputExternalStylus>();
1994 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
1995 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
1996 const auto stylusInfo = findDeviceByName(externalStylus->getName());
1997 ASSERT_TRUE(stylusInfo);
Prabir Pradhan85cf63e2023-08-07 21:02:13 +00001998
1999 // Move
2000 mDevice->sendMove(centerPoint + Point(2, 2));
2001 mDevice->sendSync();
2002 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2003 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
2004
2005 // Disconnecting an external stylus mid-gesture should not interrupt the ongoing gesture stream.
2006 externalStylus.reset();
2007 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2008 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2009 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2010
2011 // Up
2012 mDevice->sendUp();
2013 mDevice->sendSync();
2014 ASSERT_NO_FATAL_FAILURE(
2015 mTestListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
2016
2017 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2018}
2019
Arpit Singh440bf652023-08-09 09:23:43 +00002020INSTANTIATE_TEST_SUITE_P(TouchIntegrationTestDisplayVariants, TouchIntegrationTest,
2021 testing::Values(TouchIntegrationTestDisplays::DISPLAY_INTERNAL,
2022 TouchIntegrationTestDisplays::DISPLAY_INPUT_PORT,
2023 TouchIntegrationTestDisplays::DISPLAY_UNIQUE_ID));
2024
Prabir Pradhan124ea442022-10-28 20:27:44 +00002025// --- StylusButtonIntegrationTest ---
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002026
Prabir Pradhan124ea442022-10-28 20:27:44 +00002027// Verify the behavior of button presses reported by various kinds of styluses, including buttons
2028// reported by the touchscreen's device, by a fused external stylus, and by an un-fused external
2029// stylus.
2030template <typename UinputStylusDevice>
Arpit Singh440bf652023-08-09 09:23:43 +00002031class StylusButtonIntegrationTest : public BaseTouchIntegrationTest {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002032protected:
2033 void SetUp() override {
2034#if !defined(__ANDROID__)
2035 GTEST_SKIP();
2036#endif
Arpit Singh440bf652023-08-09 09:23:43 +00002037 BaseTouchIntegrationTest::SetUp();
Prabir Pradhan124ea442022-10-28 20:27:44 +00002038 mTouchscreen = mDevice.get();
2039 mTouchscreenInfo = mDeviceInfo;
2040
2041 setUpStylusDevice();
2042 }
2043
2044 UinputStylusDevice* mStylus{nullptr};
2045 InputDeviceInfo mStylusInfo{};
2046
2047 UinputTouchScreen* mTouchscreen{nullptr};
2048 InputDeviceInfo mTouchscreenInfo{};
2049
2050private:
2051 // When we are attempting to test stylus button events that are sent from the touchscreen,
2052 // use the same Uinput device for the touchscreen and the stylus.
2053 template <typename T = UinputStylusDevice>
2054 std::enable_if_t<std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2055 mStylus = mDevice.get();
2056 mStylusInfo = mDeviceInfo;
2057 }
2058
2059 // When we are attempting to stylus buttons from an external stylus being merged with touches
2060 // from a touchscreen, create a new Uinput device through which stylus buttons can be injected.
2061 template <typename T = UinputStylusDevice>
2062 std::enable_if_t<!std::is_same_v<UinputTouchScreen, T>, void> setUpStylusDevice() {
2063 mStylusDeviceLifecycleTracker = createUinputDevice<T>();
2064 mStylus = mStylusDeviceLifecycleTracker.get();
2065 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2066 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2067 const auto info = findDeviceByName(mStylus->getName());
2068 ASSERT_TRUE(info);
2069 mStylusInfo = *info;
2070 }
2071
2072 std::unique_ptr<UinputStylusDevice> mStylusDeviceLifecycleTracker{};
2073
2074 // Hide the base class's device to expose it with a different name for readability.
Arpit Singh440bf652023-08-09 09:23:43 +00002075 using BaseTouchIntegrationTest::mDevice;
2076 using BaseTouchIntegrationTest::mDeviceInfo;
Prabir Pradhan124ea442022-10-28 20:27:44 +00002077};
2078
2079using StylusButtonIntegrationTestTypes =
2080 ::testing::Types<UinputTouchScreen, UinputExternalStylus, UinputExternalStylusWithPressure>;
2081TYPED_TEST_SUITE(StylusButtonIntegrationTest, StylusButtonIntegrationTestTypes);
2082
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002083TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsGenerateKeyEvents) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002084 const auto stylusId = TestFixture::mStylusInfo.getId();
2085
2086 TestFixture::mStylus->pressKey(BTN_STYLUS);
2087 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2088 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2089 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2090
2091 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2092 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002093 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002094 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhane1a41a82022-10-14 18:06:50 +00002095}
2096
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002097TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002098 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2099 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2100 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002101
2102 // Press the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002103 TestFixture::mStylus->pressKey(BTN_STYLUS);
2104 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002105 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002106 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002107
2108 // Start and finish a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002109 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2110 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2111 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2112 TestFixture::mTouchscreen->sendDown(centerPoint);
2113 TestFixture::mTouchscreen->sendSync();
2114 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002115 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002116 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002117 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2118 WithDeviceId(touchscreenId))));
2119 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002120 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002121 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002122 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2123 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002124
Prabir Pradhan124ea442022-10-28 20:27:44 +00002125 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2126 TestFixture::mTouchscreen->sendSync();
2127 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002128 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002129 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002130 WithDeviceId(touchscreenId))));
2131 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002132 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002133 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002134 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002135
2136 // Release the stylus button.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002137 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2138 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002139 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002140 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002141}
2142
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002143TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsSurroundingHoveringTouchGesture) {
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002144 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2145 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2146 const auto stylusId = TestFixture::mStylusInfo.getId();
2147 auto toolTypeDevice =
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002148 AllOf(WithToolType(ToolType::STYLUS), WithDeviceId(touchscreenId));
Prabir Pradhan9a561c22022-11-07 16:11:23 +00002149
2150 // Press the stylus button.
2151 TestFixture::mStylus->pressKey(BTN_STYLUS);
2152 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2153 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2154 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2155
2156 // Start hovering with the stylus.
2157 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2158 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2159 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2160 TestFixture::mTouchscreen->sendMove(centerPoint);
2161 TestFixture::mTouchscreen->sendSync();
2162 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2163 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2164 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2165 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2166 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2167 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2168 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2169 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
2170 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2171
2172 // Touch down with the stylus.
2173 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2174 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2175 TestFixture::mTouchscreen->sendDown(centerPoint);
2176 TestFixture::mTouchscreen->sendSync();
2177 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2178 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2179 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2180
2181 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2182 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
2183 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2184
2185 // Stop touching with the stylus, and start hovering.
2186 TestFixture::mTouchscreen->sendUp();
2187 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2188 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2189 TestFixture::mTouchscreen->sendMove(centerPoint);
2190 TestFixture::mTouchscreen->sendSync();
2191 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2192 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_UP),
2193 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2194 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2195 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
2196 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2197 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2198 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
2199 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2200
2201 // Stop hovering.
2202 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2203 TestFixture::mTouchscreen->sendSync();
2204 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2205 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
2206 WithButtonState(0))));
2207 // TODO(b/257971675): Fix inconsistent button state when exiting hover.
2208 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2209 AllOf(toolTypeDevice, WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT),
2210 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
2211
2212 // Release the stylus button.
2213 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2214 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2215 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2216 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2217}
2218
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002219TYPED_TEST(StylusButtonIntegrationTest, StylusButtonsWithinTouchGesture) {
Prabir Pradhan124ea442022-10-28 20:27:44 +00002220 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2221 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2222 const auto stylusId = TestFixture::mStylusInfo.getId();
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002223
2224 // Start a stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002225 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2226 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2227 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2228 TestFixture::mTouchscreen->sendDown(centerPoint);
2229 TestFixture::mTouchscreen->sendSync();
2230 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002231 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002232 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002233 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002234
2235 // Press and release a stylus button. Each change in button state also generates a MOVE event.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002236 TestFixture::mStylus->pressKey(BTN_STYLUS);
2237 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002238 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002239 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2240 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002241 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002242 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002243 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2244 WithDeviceId(touchscreenId))));
2245 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002246 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002247 WithToolType(ToolType::STYLUS),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002248 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY),
2249 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002250
Prabir Pradhan124ea442022-10-28 20:27:44 +00002251 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2252 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002253 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002254 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2255 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002256 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002257 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002258 WithDeviceId(touchscreenId))));
2259 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002260 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002261 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002262 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002263
2264 // Finish the stylus gesture.
Prabir Pradhan124ea442022-10-28 20:27:44 +00002265 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2266 TestFixture::mTouchscreen->sendSync();
2267 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002268 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002269 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan124ea442022-10-28 20:27:44 +00002270 WithDeviceId(touchscreenId))));
Prabir Pradhan7bffbf52022-10-14 20:31:53 +00002271}
2272
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002273TYPED_TEST(StylusButtonIntegrationTest, StylusButtonMotionEventsDisabled) {
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002274 TestFixture::mFakePolicy->setStylusButtonMotionEventsEnabled(false);
2275 TestFixture::mReader->requestRefreshConfiguration(
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002276 InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002277
2278 const Point centerPoint = TestFixture::mTouchscreen->getCenterPoint();
2279 const auto touchscreenId = TestFixture::mTouchscreenInfo.getId();
2280 const auto stylusId = TestFixture::mStylusInfo.getId();
2281
2282 // Start a stylus gesture. By the time this event is processed, the configuration change that
2283 // was requested is guaranteed to be completed.
2284 TestFixture::mTouchscreen->sendSlot(FIRST_SLOT);
2285 TestFixture::mTouchscreen->sendTrackingId(FIRST_TRACKING_ID);
2286 TestFixture::mTouchscreen->sendToolType(MT_TOOL_PEN);
2287 TestFixture::mTouchscreen->sendDown(centerPoint);
2288 TestFixture::mTouchscreen->sendSync();
2289 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2290 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002291 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002292 WithDeviceId(touchscreenId))));
2293
2294 // Press and release a stylus button. Each change only generates a MOVE motion event.
2295 // Key events are unaffected.
2296 TestFixture::mStylus->pressKey(BTN_STYLUS);
2297 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2298 AllOf(WithKeyAction(AKEY_EVENT_ACTION_DOWN), WithSource(AINPUT_SOURCE_KEYBOARD),
2299 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2300 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2301 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002302 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002303 WithDeviceId(touchscreenId))));
2304
2305 TestFixture::mStylus->releaseKey(BTN_STYLUS);
2306 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyKeyWasCalled(
2307 AllOf(WithKeyAction(AKEY_EVENT_ACTION_UP), WithSource(AINPUT_SOURCE_KEYBOARD),
2308 WithKeyCode(AKEYCODE_STYLUS_BUTTON_PRIMARY), WithDeviceId(stylusId))));
2309 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2310 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002311 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002312 WithDeviceId(touchscreenId))));
2313
2314 // Finish the stylus gesture.
2315 TestFixture::mTouchscreen->sendTrackingId(INVALID_TRACKING_ID);
2316 TestFixture::mTouchscreen->sendSync();
2317 ASSERT_NO_FATAL_FAILURE(TestFixture::mTestListener->assertNotifyMotionWasCalled(
2318 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002319 WithToolType(ToolType::STYLUS), WithButtonState(0),
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00002320 WithDeviceId(touchscreenId))));
2321}
2322
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002323// --- ExternalStylusIntegrationTest ---
2324
2325// Verify the behavior of an external stylus. An external stylus can report pressure or button
2326// data independently of the touchscreen, which is then sent as a MotionEvent as part of an
2327// ongoing stylus gesture that is being emitted by the touchscreen.
Arpit Singh440bf652023-08-09 09:23:43 +00002328using ExternalStylusIntegrationTest = BaseTouchIntegrationTest;
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002329
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002330TEST_F(ExternalStylusIntegrationTest, ExternalStylusConnectionChangesTouchscreenSource) {
2331 // Create an external stylus capable of reporting pressure data that
2332 // should be fused with a touch pointer.
2333 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2334 createUinputDevice<UinputExternalStylusWithPressure>();
2335 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2336 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2337 const auto stylusInfo = findDeviceByName(stylus->getName());
2338 ASSERT_TRUE(stylusInfo);
2339
2340 // Connecting an external stylus changes the source of the touchscreen.
2341 const auto deviceInfo = findDeviceByName(mDevice->getName());
2342 ASSERT_TRUE(deviceInfo);
2343 ASSERT_TRUE(isFromSource(deviceInfo->getSources(), STYLUS_FUSION_SOURCE));
2344}
2345
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002346TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002347 const Point centerPoint = mDevice->getCenterPoint();
2348
2349 // Create an external stylus capable of reporting pressure data that
2350 // should be fused with a touch pointer.
2351 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2352 createUinputDevice<UinputExternalStylusWithPressure>();
2353 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2354 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2355 const auto stylusInfo = findDeviceByName(stylus->getName());
2356 ASSERT_TRUE(stylusInfo);
2357
2358 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2359
2360 const auto touchscreenId = mDeviceInfo.getId();
2361
2362 // Set a pressure value on the stylus. It doesn't generate any events.
2363 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
2364 stylus->setPressure(100);
2365 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2366
2367 // Start a finger gesture, and ensure it shows up as stylus gesture
2368 // with the pressure set by the external stylus.
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002369 mDevice->sendSlot(FIRST_SLOT);
Chris Ye1b0c7342020-07-28 21:57:03 -07002370 mDevice->sendTrackingId(FIRST_TRACKING_ID);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002371 mDevice->sendToolType(MT_TOOL_FINGER);
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002372 mDevice->sendDown(centerPoint);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002373 mDevice->sendSync();
2374 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002375 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithToolType(ToolType::STYLUS),
2376 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2377 WithPressure(100.f / RAW_PRESSURE_MAX))));
Dominik Laskowski2f01d772022-03-23 16:01:29 -07002378
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002379 // Change the pressure on the external stylus, and ensure the touchscreen generates a MOVE
2380 // event with the updated pressure.
2381 stylus->setPressure(200);
Vaibhav Devmuraridd82b8e2022-08-16 15:34:01 +00002382 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002383 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithToolType(ToolType::STYLUS),
2384 WithButtonState(0), WithSource(STYLUS_FUSION_SOURCE), WithDeviceId(touchscreenId),
2385 WithPressure(200.f / RAW_PRESSURE_MAX))));
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002386
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002387 // The external stylus did not generate any events.
2388 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2389 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2390}
2391
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002392TEST_F(ExternalStylusIntegrationTest, FusedExternalStylusPressureNotReported) {
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002393 const Point centerPoint = mDevice->getCenterPoint();
2394
2395 // Create an external stylus capable of reporting pressure data that
2396 // should be fused with a touch pointer.
2397 std::unique_ptr<UinputExternalStylusWithPressure> stylus =
2398 createUinputDevice<UinputExternalStylusWithPressure>();
2399 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2400 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2401 const auto stylusInfo = findDeviceByName(stylus->getName());
2402 ASSERT_TRUE(stylusInfo);
2403
2404 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2405
2406 const auto touchscreenId = mDeviceInfo.getId();
2407
2408 // Set a pressure value of 0 on the stylus. It doesn't generate any events.
2409 const auto& RAW_PRESSURE_MAX = UinputExternalStylusWithPressure::RAW_PRESSURE_MAX;
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002410 // Send a non-zero value first to prevent the kernel from consuming the zero event.
2411 stylus->setPressure(100);
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002412 stylus->setPressure(0);
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002413 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002414
2415 // Start a finger gesture. The touch device will withhold generating any touches for
2416 // up to 72 milliseconds while waiting for pressure data from the external stylus.
2417 mDevice->sendSlot(FIRST_SLOT);
2418 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2419 mDevice->sendToolType(MT_TOOL_FINGER);
2420 mDevice->sendDown(centerPoint);
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002421 const auto syncTime = std::chrono::system_clock::now();
2422 // After 72 ms, the event *will* be generated. If we wait the full 72 ms to check that NO event
2423 // is generated in that period, there will be a race condition between the event being generated
2424 // and the test's wait timeout expiring. Thus, we wait for a shorter duration in the test, which
2425 // will reduce the liklihood of the race condition occurring.
2426 const auto waitUntilTimeForNoEvent =
2427 syncTime + std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT / 2));
Siarhei Vishniakoue54cb852017-03-21 17:48:16 -07002428 mDevice->sendSync();
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002429 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled(waitUntilTimeForNoEvent));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002430
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002431 // Since the external stylus did not report a pressure value within the timeout,
2432 // it shows up as a finger pointer.
Prabir Pradhanafb7d612024-01-08 22:45:24 +00002433 const auto waitUntilTimeForEvent = syncTime +
2434 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT)) + EVENT_HAPPENED_TIMEOUT;
2435 ASSERT_NO_FATAL_FAILURE(
2436 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2437 AMOTION_EVENT_ACTION_DOWN),
2438 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2439 AINPUT_SOURCE_STYLUS),
2440 WithToolType(ToolType::FINGER),
2441 WithDeviceId(touchscreenId),
2442 WithPressure(1.f)),
2443 waitUntilTimeForEvent));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002444
2445 // Change the pressure on the external stylus. Since the pressure was not present at the start
2446 // of the gesture, it is ignored for now.
2447 stylus->setPressure(200);
2448 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2449
2450 // Finish the finger gesture.
Michael Wrightd02c5b62014-02-10 15:10:22 -08002451 mDevice->sendTrackingId(INVALID_TRACKING_ID);
2452 mDevice->sendSync();
2453 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
2454 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002455 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07002456 WithToolType(ToolType::FINGER))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002457
2458 // Start a new gesture. Since we have a valid pressure value, it shows up as a stylus.
2459 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2460 mDevice->sendToolType(MT_TOOL_FINGER);
2461 mDevice->sendDown(centerPoint);
2462 mDevice->sendSync();
2463 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002464 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithSource(STYLUS_FUSION_SOURCE),
2465 WithToolType(ToolType::STYLUS), WithButtonState(0), WithDeviceId(touchscreenId),
2466 WithPressure(200.f / RAW_PRESSURE_MAX))));
Prabir Pradhan484d55a2022-10-14 23:17:16 +00002467
2468 // The external stylus did not generate any events.
2469 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2470 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002471}
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002472
Prabir Pradhan39d60aa2023-07-13 22:01:04 +00002473TEST_F(ExternalStylusIntegrationTest, UnfusedExternalStylus) {
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002474 const Point centerPoint = mDevice->getCenterPoint();
2475
2476 // Create an external stylus device that does not support pressure. It should not affect any
2477 // touch pointers.
2478 std::unique_ptr<UinputExternalStylus> stylus = createUinputDevice<UinputExternalStylus>();
2479 ASSERT_NO_FATAL_FAILURE(mFakePolicy->assertInputDevicesChanged());
2480 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyConfigurationChangedWasCalled());
2481 const auto stylusInfo = findDeviceByName(stylus->getName());
2482 ASSERT_TRUE(stylusInfo);
2483
2484 ASSERT_EQ(AINPUT_SOURCE_STYLUS | AINPUT_SOURCE_KEYBOARD, stylusInfo->getSources());
2485
2486 const auto touchscreenId = mDeviceInfo.getId();
2487
2488 // Start a finger gesture and ensure a finger pointer is generated for it, without waiting for
2489 // pressure data from the external stylus.
2490 mDevice->sendSlot(FIRST_SLOT);
2491 mDevice->sendTrackingId(FIRST_TRACKING_ID);
2492 mDevice->sendToolType(MT_TOOL_FINGER);
2493 mDevice->sendDown(centerPoint);
2494 auto waitUntil = std::chrono::system_clock::now() +
2495 std::chrono::milliseconds(ns2ms(EXTERNAL_STYLUS_DATA_TIMEOUT));
2496 mDevice->sendSync();
2497 ASSERT_NO_FATAL_FAILURE(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00002498 mTestListener->assertNotifyMotionWasCalled(AllOf(WithMotionAction(
2499 AMOTION_EVENT_ACTION_DOWN),
2500 WithToolType(ToolType::FINGER),
2501 WithSource(AINPUT_SOURCE_TOUCHSCREEN |
2502 AINPUT_SOURCE_STYLUS),
2503 WithButtonState(0),
2504 WithDeviceId(touchscreenId),
2505 WithPressure(1.f)),
2506 waitUntil));
Prabir Pradhan3f7545f2022-10-19 16:56:39 +00002507
2508 // The external stylus did not generate any events.
2509 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyMotionWasNotCalled());
2510 ASSERT_NO_FATAL_FAILURE(mTestListener->assertNotifyKeyWasNotCalled());
2511}
2512
Michael Wrightd02c5b62014-02-10 15:10:22 -08002513// --- InputDeviceTest ---
2514class InputDeviceTest : public testing::Test {
2515protected:
2516 static const char* DEVICE_NAME;
2517 static const char* DEVICE_LOCATION;
2518 static const int32_t DEVICE_ID;
2519 static const int32_t DEVICE_GENERATION;
2520 static const int32_t DEVICE_CONTROLLER_NUMBER;
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002521 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002522 static const int32_t EVENTHUB_ID;
2523 static const std::string DEVICE_BLUETOOTH_ADDRESS;
2524
2525 std::shared_ptr<FakeEventHub> mFakeEventHub;
2526 sp<FakeInputReaderPolicy> mFakePolicy;
2527 std::unique_ptr<TestInputListener> mFakeListener;
2528 std::unique_ptr<InstrumentedInputReader> mReader;
2529 std::shared_ptr<InputDevice> mDevice;
2530
2531 void SetUp() override {
2532 mFakeEventHub = std::make_unique<FakeEventHub>();
2533 mFakePolicy = sp<FakeInputReaderPolicy>::make();
2534 mFakeListener = std::make_unique<TestInputListener>();
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002535 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Michael Wrightd02c5b62014-02-10 15:10:22 -08002536 *mFakeListener);
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002537 InputDeviceIdentifier identifier;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002538 identifier.name = DEVICE_NAME;
2539 identifier.location = DEVICE_LOCATION;
2540 identifier.bluetoothAddress = DEVICE_BLUETOOTH_ADDRESS;
2541 mDevice = std::make_shared<InputDevice>(mReader->getContext(), DEVICE_ID, DEVICE_GENERATION,
2542 identifier);
2543 mReader->pushNextDevice(mDevice);
2544 mFakeEventHub->addDevice(EVENTHUB_ID, DEVICE_NAME, ftl::Flags<InputDeviceClass>(0));
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002545 mReader->loopOnce();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002546 }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002547
2548 void TearDown() override {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002549 mFakeListener.reset();
2550 mFakePolicy.clear();
2551 }
2552};
2553
2554const char* InputDeviceTest::DEVICE_NAME = "device";
2555const char* InputDeviceTest::DEVICE_LOCATION = "USB1";
2556const int32_t InputDeviceTest::DEVICE_ID = END_RESERVED_ID + 1000;
2557const int32_t InputDeviceTest::DEVICE_GENERATION = 2;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002558const int32_t InputDeviceTest::DEVICE_CONTROLLER_NUMBER = 0;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002559const ftl::Flags<InputDeviceClass> InputDeviceTest::DEVICE_CLASSES =
2560 InputDeviceClass::KEYBOARD | InputDeviceClass::TOUCH | InputDeviceClass::JOYSTICK;
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002561const int32_t InputDeviceTest::EVENTHUB_ID = 1;
Michael Wrightd02c5b62014-02-10 15:10:22 -08002562const std::string InputDeviceTest::DEVICE_BLUETOOTH_ADDRESS = "11:AA:22:BB:33:CC";
2563
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002564TEST_F(InputDeviceTest, ImmutableProperties) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08002565 ASSERT_EQ(DEVICE_ID, mDevice->getId());
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002566 ASSERT_STREQ(DEVICE_NAME, mDevice->getName().c_str());
2567 ASSERT_EQ(ftl::Flags<InputDeviceClass>(0), mDevice->getClasses());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002568}
Siarhei Vishniakou4f94c1a2022-07-13 07:29:51 -07002569
Michael Wrightd02c5b62014-02-10 15:10:22 -08002570TEST_F(InputDeviceTest, WhenDeviceCreated_EnabledIsFalse) {
2571 ASSERT_EQ(mDevice->isEnabled(), false);
2572}
2573
2574TEST_F(InputDeviceTest, WhenNoMappersAreRegistered_DeviceIsIgnored) {
2575 // Configuration.
2576 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002577 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002578
2579 // Reset.
2580 unused += mDevice->reset(ARBITRARY_TIME);
2581
2582 NotifyDeviceResetArgs resetArgs;
2583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2584 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2585 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2586
2587 // Metadata.
2588 ASSERT_TRUE(mDevice->isIgnored());
2589 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mDevice->getSources());
2590
2591 InputDeviceInfo info = mDevice->getDeviceInfo();
2592 ASSERT_EQ(DEVICE_ID, info.getId());
2593 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
2594 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_NONE, info.getKeyboardType());
2595 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, info.getSources());
2596
2597 // State queries.
2598 ASSERT_EQ(0, mDevice->getMetaState());
2599
2600 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2601 << "Ignored device should return unknown key code state.";
2602 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 0))
2603 << "Ignored device should return unknown scan code state.";
2604 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 0))
2605 << "Ignored device should return unknown switch state.";
2606
2607 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B};
2608 uint8_t flags[2] = { 0, 1 };
2609 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
2610 << "Ignored device should never mark any key codes.";
2611 ASSERT_EQ(0, flags[0]) << "Flag for unsupported key should be unchanged.";
2612 ASSERT_EQ(1, flags[1]) << "Flag for unsupported key should be unchanged.";
2613}
2614
2615TEST_F(InputDeviceTest, WhenMappersAreRegistered_DeviceIsNotIgnoredAndForwardsRequestsToMappers) {
2616 // Configuration.
2617 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "key", "value");
2618
2619 FakeInputMapper& mapper1 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002620 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2621 AINPUT_SOURCE_KEYBOARD);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002622 mapper1.setKeyboardType(AINPUT_KEYBOARD_TYPE_ALPHABETIC);
2623 mapper1.setMetaState(AMETA_ALT_ON);
2624 mapper1.addSupportedKeyCode(AKEYCODE_A);
2625 mapper1.addSupportedKeyCode(AKEYCODE_B);
2626 mapper1.setKeyCodeState(AKEYCODE_A, AKEY_STATE_DOWN);
2627 mapper1.setKeyCodeState(AKEYCODE_B, AKEY_STATE_UP);
2628 mapper1.setScanCodeState(2, AKEY_STATE_DOWN);
2629 mapper1.setScanCodeState(3, AKEY_STATE_UP);
2630 mapper1.setSwitchState(4, AKEY_STATE_DOWN);
2631
2632 FakeInputMapper& mapper2 =
Arpit Singh8e6fb252023-04-06 11:49:17 +00002633 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2634 AINPUT_SOURCE_TOUCHSCREEN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002635 mapper2.setMetaState(AMETA_SHIFT_ON);
2636
2637 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002638 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Michael Wrightd02c5b62014-02-10 15:10:22 -08002639
Harry Cuttsf13161a2023-03-08 14:15:49 +00002640 std::optional<std::string> propertyValue = mDevice->getConfiguration().getString("key");
2641 ASSERT_TRUE(propertyValue.has_value())
Michael Wrightd02c5b62014-02-10 15:10:22 -08002642 << "Device should have read configuration during configuration phase.";
Harry Cuttsf13161a2023-03-08 14:15:49 +00002643 ASSERT_EQ("value", *propertyValue);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002644
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002645 ASSERT_NO_FATAL_FAILURE(mapper1.assertConfigureWasCalled());
2646 ASSERT_NO_FATAL_FAILURE(mapper2.assertConfigureWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002647
2648 // Reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002649 unused += mDevice->reset(ARBITRARY_TIME);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002650 ASSERT_NO_FATAL_FAILURE(mapper1.assertResetWasCalled());
2651 ASSERT_NO_FATAL_FAILURE(mapper2.assertResetWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002652
2653 NotifyDeviceResetArgs resetArgs;
2654 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
2655 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
2656 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
2657
2658 // Metadata.
2659 ASSERT_FALSE(mDevice->isIgnored());
2660 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), mDevice->getSources());
2661
Siarhei Vishniakou1983a712021-06-04 19:27:09 +00002662 InputDeviceInfo info = mDevice->getDeviceInfo();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002663 ASSERT_EQ(DEVICE_ID, info.getId());
Siarhei Vishniakouec8f7252018-07-06 11:19:32 +01002664 ASSERT_STREQ(DEVICE_NAME, info.getIdentifier().name.c_str());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002665 ASSERT_EQ(AINPUT_KEYBOARD_TYPE_ALPHABETIC, info.getKeyboardType());
2666 ASSERT_EQ(uint32_t(AINPUT_SOURCE_KEYBOARD | AINPUT_SOURCE_TOUCHSCREEN), info.getSources());
2667
2668 // State queries.
2669 ASSERT_EQ(AMETA_ALT_ON | AMETA_SHIFT_ON, mDevice->getMetaState())
2670 << "Should query mappers and combine meta states.";
2671
2672 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2673 << "Should return unknown key code state when source not supported.";
2674 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getScanCodeState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2675 << "Should return unknown scan code state when source not supported.";
2676 ASSERT_EQ(AKEY_STATE_UNKNOWN, mDevice->getSwitchState(AINPUT_SOURCE_TRACKBALL, AKEYCODE_A))
2677 << "Should return unknown switch state when source not supported.";
2678
2679 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getKeyCodeState(AINPUT_SOURCE_KEYBOARD, AKEYCODE_A))
2680 << "Should query mapper when source is supported.";
2681 ASSERT_EQ(AKEY_STATE_UP, mDevice->getScanCodeState(AINPUT_SOURCE_KEYBOARD, 3))
2682 << "Should query mapper when source is supported.";
2683 ASSERT_EQ(AKEY_STATE_DOWN, mDevice->getSwitchState(AINPUT_SOURCE_KEYBOARD, 4))
2684 << "Should query mapper when source is supported.";
2685
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002686 const std::vector<int32_t> keyCodes{AKEYCODE_A, AKEYCODE_B, AKEYCODE_1, AKEYCODE_2};
Michael Wrightd02c5b62014-02-10 15:10:22 -08002687 uint8_t flags[4] = { 0, 0, 0, 1 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002688 ASSERT_FALSE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_TRACKBALL, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002689 << "Should do nothing when source is unsupported.";
2690 ASSERT_EQ(0, flags[0]) << "Flag should be unchanged when source is unsupported.";
2691 ASSERT_EQ(0, flags[1]) << "Flag should be unchanged when source is unsupported.";
2692 ASSERT_EQ(0, flags[2]) << "Flag should be unchanged when source is unsupported.";
2693 ASSERT_EQ(1, flags[3]) << "Flag should be unchanged when source is unsupported.";
2694
Siarhei Vishniakou74007942022-06-13 13:57:47 -07002695 ASSERT_TRUE(mDevice->markSupportedKeyCodes(AINPUT_SOURCE_KEYBOARD, keyCodes, flags))
Michael Wrightd02c5b62014-02-10 15:10:22 -08002696 << "Should query mapper when source is supported.";
2697 ASSERT_EQ(1, flags[0]) << "Flag for supported key should be set.";
2698 ASSERT_EQ(1, flags[1]) << "Flag for supported key should be set.";
2699 ASSERT_EQ(0, flags[2]) << "Flag for unsupported key should be unchanged.";
2700 ASSERT_EQ(1, flags[3]) << "Flag for unsupported key should be unchanged.";
2701
2702 // Event handling.
2703 RawEvent event;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002704 event.deviceId = EVENTHUB_ID;
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002705 unused += mDevice->process(&event, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002706
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002707 ASSERT_NO_FATAL_FAILURE(mapper1.assertProcessWasCalled());
2708 ASSERT_NO_FATAL_FAILURE(mapper2.assertProcessWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002709}
2710
Yeabkal Wubshite03e8b12023-06-27 16:23:12 -07002711TEST_F(InputDeviceTest, WakeDevice_AddsWakeFlagToProcessNotifyArgs) {
2712 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "1");
2713 FakeInputMapper& mapper =
2714 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2715 AINPUT_SOURCE_KEYBOARD);
2716 NotifyMotionArgs args1;
2717 NotifySwitchArgs args2;
2718 NotifyKeyArgs args3;
2719 mapper.setProcessResult({args1, args2, args3});
2720
2721 InputReaderConfiguration config;
2722 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2723
2724 RawEvent event;
2725 event.deviceId = EVENTHUB_ID;
2726 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2727
2728 for (auto& arg : notifyArgs) {
2729 if (const auto notifyMotionArgs = std::get_if<NotifyMotionArgs>(&arg)) {
2730 ASSERT_EQ(POLICY_FLAG_WAKE, notifyMotionArgs->policyFlags);
2731 } else if (const auto notifySwitchArgs = std::get_if<NotifySwitchArgs>(&arg)) {
2732 ASSERT_EQ(POLICY_FLAG_WAKE, notifySwitchArgs->policyFlags);
2733 } else if (const auto notifyKeyArgs = std::get_if<NotifyKeyArgs>(&arg)) {
2734 ASSERT_EQ(POLICY_FLAG_WAKE, notifyKeyArgs->policyFlags);
2735 }
2736 }
2737}
2738
2739TEST_F(InputDeviceTest, NotWakeDevice_DoesNotAddWakeFlagToProcessNotifyArgs) {
2740 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2741 FakeInputMapper& mapper =
2742 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2743 AINPUT_SOURCE_KEYBOARD);
2744 NotifyMotionArgs args;
2745 mapper.setProcessResult({args});
2746
2747 InputReaderConfiguration config;
2748 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2749
2750 RawEvent event;
2751 event.deviceId = EVENTHUB_ID;
2752 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2753
2754 // POLICY_FLAG_WAKE is not added to the NotifyArgs.
2755 ASSERT_EQ(0u, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2756}
2757
2758TEST_F(InputDeviceTest, NotWakeDevice_DoesNotRemoveExistingWakeFlagFromProcessNotifyArgs) {
2759 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "device.wake", "0");
2760 FakeInputMapper& mapper =
2761 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2762 AINPUT_SOURCE_KEYBOARD);
2763 NotifyMotionArgs args;
2764 args.policyFlags = POLICY_FLAG_WAKE;
2765 mapper.setProcessResult({args});
2766
2767 InputReaderConfiguration config;
2768 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
2769
2770 RawEvent event;
2771 event.deviceId = EVENTHUB_ID;
2772 std::list<NotifyArgs> notifyArgs = mDevice->process(&event, 1);
2773
2774 // The POLICY_FLAG_WAKE is preserved, despite the device being a non-wake device.
2775 ASSERT_EQ(POLICY_FLAG_WAKE, std::get<NotifyMotionArgs>(notifyArgs.front()).policyFlags);
2776}
2777
Arthur Hung2c9a3342019-07-23 14:18:59 +08002778// A single input device is associated with a specific display. Check that:
2779// 1. Device is disabled if the viewport corresponding to the associated display is not found
Arpit Singh48189772023-05-30 14:12:49 +00002780// 2. Device is disabled when configure API is called
Arthur Hung2c9a3342019-07-23 14:18:59 +08002781TEST_F(InputDeviceTest, Configure_AssignsDisplayPort) {
Arpit Singh8e6fb252023-04-06 11:49:17 +00002782 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2783 AINPUT_SOURCE_TOUCHSCREEN);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002784
2785 // First Configuration.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002786 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002787 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2788 /*changes=*/{});
Arthur Hung2c9a3342019-07-23 14:18:59 +08002789
2790 // Device should be enabled by default.
2791 ASSERT_TRUE(mDevice->isEnabled());
2792
2793 // Prepare associated info.
2794 constexpr uint8_t hdmi = 1;
2795 const std::string UNIQUE_ID = "local:1";
2796
2797 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002798 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002799 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002800 // Device should be disabled because it is associated with a specific display via
2801 // input port <-> display port association, but the corresponding display is not found
2802 ASSERT_FALSE(mDevice->isEnabled());
2803
2804 // Prepare displays.
2805 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Harry Cutts33476232023-01-30 19:57:29 +00002806 ui::ROTATION_0, /*isActive=*/true, UNIQUE_ID, hdmi,
Siarhei Vishniakou6f778462020-12-09 23:39:07 +00002807 ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002808 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002809 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002810 ASSERT_TRUE(mDevice->isEnabled());
2811
2812 // Device should be disabled after set disable.
2813 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002814 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002815 InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002816 ASSERT_FALSE(mDevice->isEnabled());
2817
2818 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002819 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002820 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08002821 ASSERT_FALSE(mDevice->isEnabled());
2822}
Michael Wrightd02c5b62014-02-10 15:10:22 -08002823
Christine Franks1ba71cc2021-04-07 14:37:42 -07002824TEST_F(InputDeviceTest, Configure_AssignsDisplayUniqueId) {
2825 // Device should be enabled by default.
2826 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002827 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2828 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002829 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002830 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2831 /*changes=*/{});
Christine Franks1ba71cc2021-04-07 14:37:42 -07002832 ASSERT_TRUE(mDevice->isEnabled());
2833
2834 // Device should be disabled because it is associated with a specific display, but the
2835 // corresponding display is not found.
Christine Franks2a2293c2022-01-18 11:51:16 -08002836 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002837 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002838 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002839 ASSERT_FALSE(mDevice->isEnabled());
2840
2841 // Device should be enabled when a display is found.
2842 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002843 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks1ba71cc2021-04-07 14:37:42 -07002844 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002845 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002846 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002847 ASSERT_TRUE(mDevice->isEnabled());
2848
2849 // Device should be disabled after set disable.
2850 mFakePolicy->addDisabledDevice(mDevice->getId());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002851 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002852 InputReaderConfiguration::Change::ENABLED_STATE);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002853 ASSERT_FALSE(mDevice->isEnabled());
2854
2855 // Device should still be disabled even found the associated display.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002856 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002857 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks1ba71cc2021-04-07 14:37:42 -07002858 ASSERT_FALSE(mDevice->isEnabled());
2859}
2860
Christine Franks2a2293c2022-01-18 11:51:16 -08002861TEST_F(InputDeviceTest, Configure_UniqueId_CorrectlyMatches) {
2862 mFakePolicy->clearViewports();
Arpit Singh8e6fb252023-04-06 11:49:17 +00002863 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2864 AINPUT_SOURCE_KEYBOARD);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002865 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002866 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2867 /*changes=*/{});
Christine Franks2a2293c2022-01-18 11:51:16 -08002868
Christine Franks2a2293c2022-01-18 11:51:16 -08002869 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
2870 mFakePolicy->addDisplayViewport(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00002871 ui::ROTATION_0, /* isActive= */ true, DISPLAY_UNIQUE_ID,
Christine Franks2a2293c2022-01-18 11:51:16 -08002872 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002873 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00002874 InputReaderConfiguration::Change::DISPLAY_INFO);
Christine Franks2a2293c2022-01-18 11:51:16 -08002875 ASSERT_EQ(DISPLAY_UNIQUE_ID, mDevice->getAssociatedDisplayUniqueId());
2876}
2877
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002878/**
2879 * This test reproduces a crash caused by a dangling reference that remains after device is added
2880 * and removed. The reference is accessed in InputDevice::dump(..);
2881 */
2882TEST_F(InputDeviceTest, DumpDoesNotCrash) {
2883 constexpr int32_t TEST_EVENTHUB_ID = 10;
2884 mFakeEventHub->addDevice(TEST_EVENTHUB_ID, "Test EventHub device", InputDeviceClass::BATTERY);
2885
Harry Cutts33476232023-01-30 19:57:29 +00002886 InputDevice device(mReader->getContext(), /*id=*/1, /*generation=*/2, /*identifier=*/{});
Arpit Singh82f29a12023-06-13 15:05:53 +00002887 auto _ = device.addEventHubDevice(ARBITRARY_TIME, TEST_EVENTHUB_ID,
2888 mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou30feb8c2022-09-28 10:48:29 -07002889 device.removeEventHubDevice(TEST_EVENTHUB_ID);
2890 std::string dumpStr, eventHubDevStr;
2891 device.dump(dumpStr, eventHubDevStr);
2892}
2893
Prabir Pradhanb54ffb22022-10-27 18:03:34 +00002894TEST_F(InputDeviceTest, GetBluetoothAddress) {
2895 const auto& address = mReader->getBluetoothAddress(DEVICE_ID);
2896 ASSERT_TRUE(address);
2897 ASSERT_EQ(DEVICE_BLUETOOTH_ADDRESS, *address);
2898}
2899
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002900TEST_F(InputDeviceTest, KernelBufferOverflowResetsMappers) {
2901 mFakePolicy->clearViewports();
2902 FakeInputMapper& mapper =
2903 mDevice->addMapper<FakeInputMapper>(EVENTHUB_ID, mFakePolicy->getReaderConfiguration(),
2904 AINPUT_SOURCE_KEYBOARD);
2905 std::list<NotifyArgs> unused =
2906 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
2907 /*changes=*/{});
2908
2909 mapper.assertConfigureWasCalled();
2910 mapper.assertResetWasNotCalled();
2911
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002912 RawEvent event{.when = ARBITRARY_TIME,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002913 .readTime = ARBITRARY_TIME,
Siarhei Vishniakou1ff00cc2023-12-13 16:12:13 -08002914 .deviceId = EVENTHUB_ID,
Prabir Pradhanf8d9e442023-12-06 22:06:13 +00002915 .type = EV_SYN,
2916 .code = SYN_REPORT,
2917 .value = 0};
2918
2919 // Events are processed normally.
2920 unused = mDevice->process(&event, /*count=*/1);
2921 mapper.assertProcessWasCalled();
2922
2923 // Simulate a kernel buffer overflow, which generates a SYN_DROPPED event.
2924 // This should reset the mapper.
2925 event.type = EV_SYN;
2926 event.code = SYN_DROPPED;
2927 event.value = 0;
2928 unused = mDevice->process(&event, /*count=*/1);
2929 mapper.assertProcessWasNotCalled();
2930 mapper.assertResetWasCalled();
2931
2932 // All events until the next SYN_REPORT should be dropped.
2933 event.type = EV_KEY;
2934 event.code = KEY_A;
2935 event.value = 1;
2936 unused = mDevice->process(&event, /*count=*/1);
2937 mapper.assertProcessWasNotCalled();
2938
2939 // We get the SYN_REPORT event now, which is not forwarded to mappers.
2940 event.type = EV_SYN;
2941 event.code = SYN_REPORT;
2942 event.value = 0;
2943 unused = mDevice->process(&event, /*count=*/1);
2944 mapper.assertProcessWasNotCalled();
2945
2946 // The mapper receives events normally now.
2947 event.type = EV_KEY;
2948 event.code = KEY_B;
2949 event.value = 1;
2950 unused = mDevice->process(&event, /*count=*/1);
2951 mapper.assertProcessWasCalled();
2952}
2953
Michael Wrightd02c5b62014-02-10 15:10:22 -08002954// --- SwitchInputMapperTest ---
2955
2956class SwitchInputMapperTest : public InputMapperTest {
2957protected:
2958};
2959
2960TEST_F(SwitchInputMapperTest, GetSources) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002961 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002962
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002963 ASSERT_EQ(uint32_t(AINPUT_SOURCE_SWITCH), mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002964}
2965
2966TEST_F(SwitchInputMapperTest, GetSwitchState) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002967 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08002968
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002969 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002970 ASSERT_EQ(1, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002971
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08002972 mFakeEventHub->setSwitchState(EVENTHUB_ID, SW_LID, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08002973 ASSERT_EQ(0, mapper.getSwitchState(AINPUT_SOURCE_ANY, SW_LID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08002974}
2975
2976TEST_F(SwitchInputMapperTest, Process) {
Arpit Singhdf992eb2023-04-26 16:12:10 +00002977 SwitchInputMapper& mapper = constructAndAddMapper<SwitchInputMapper>();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002978 std::list<NotifyArgs> out;
2979 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_LID, 1);
2980 ASSERT_TRUE(out.empty());
2981 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_JACK_PHYSICAL_INSERT, 1);
2982 ASSERT_TRUE(out.empty());
2983 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SW, SW_HEADPHONE_INSERT, 0);
2984 ASSERT_TRUE(out.empty());
2985 out = process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08002986
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07002987 ASSERT_EQ(1u, out.size());
2988 const NotifySwitchArgs& args = std::get<NotifySwitchArgs>(*out.begin());
Michael Wrightd02c5b62014-02-10 15:10:22 -08002989 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
Dan Albert1bd2fc02016-02-02 15:11:57 -08002990 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT), args.switchValues);
2991 ASSERT_EQ((1U << SW_LID) | (1U << SW_JACK_PHYSICAL_INSERT) | (1 << SW_HEADPHONE_INSERT),
Michael Wrightd02c5b62014-02-10 15:10:22 -08002992 args.switchMask);
2993 ASSERT_EQ(uint32_t(0), args.policyFlags);
2994}
2995
Chris Ye87143712020-11-10 05:05:58 +00002996// --- VibratorInputMapperTest ---
2997class VibratorInputMapperTest : public InputMapperTest {
2998protected:
2999 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::VIBRATOR); }
3000};
3001
3002TEST_F(VibratorInputMapperTest, GetSources) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003003 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003004
3005 ASSERT_EQ(AINPUT_SOURCE_UNKNOWN, mapper.getSources());
3006}
3007
3008TEST_F(VibratorInputMapperTest, GetVibratorIds) {
Arpit Singh0f26b302023-04-26 16:23:13 +00003009 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003010
3011 ASSERT_EQ(mapper.getVibratorIds().size(), 2U);
3012}
3013
3014TEST_F(VibratorInputMapperTest, Vibrate) {
3015 constexpr uint8_t DEFAULT_AMPLITUDE = 192;
Chris Yefb552902021-02-03 17:18:37 -08003016 constexpr int32_t VIBRATION_TOKEN = 100;
Arpit Singh0f26b302023-04-26 16:23:13 +00003017 VibratorInputMapper& mapper = constructAndAddMapper<VibratorInputMapper>();
Chris Ye87143712020-11-10 05:05:58 +00003018
3019 VibrationElement pattern(2);
3020 VibrationSequence sequence(2);
3021 pattern.duration = std::chrono::milliseconds(200);
Harry Cutts33476232023-01-30 19:57:29 +00003022 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 2},
3023 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003024 sequence.addElement(pattern);
3025 pattern.duration = std::chrono::milliseconds(500);
Harry Cutts33476232023-01-30 19:57:29 +00003026 pattern.channels = {{/*vibratorId=*/0, DEFAULT_AMPLITUDE / 4},
3027 {/*vibratorId=*/1, DEFAULT_AMPLITUDE}};
Chris Ye87143712020-11-10 05:05:58 +00003028 sequence.addElement(pattern);
3029
3030 std::vector<int64_t> timings = {0, 1};
3031 std::vector<uint8_t> amplitudes = {DEFAULT_AMPLITUDE, DEFAULT_AMPLITUDE / 2};
3032
3033 ASSERT_FALSE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003034 // Start vibrating
Harry Cutts33476232023-01-30 19:57:29 +00003035 std::list<NotifyArgs> out = mapper.vibrate(sequence, /*repeat=*/-1, VIBRATION_TOKEN);
Chris Ye87143712020-11-10 05:05:58 +00003036 ASSERT_TRUE(mapper.isVibrating());
Chris Yefb552902021-02-03 17:18:37 -08003037 // Verify vibrator state listener was notified.
3038 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003039 ASSERT_EQ(1u, out.size());
3040 const NotifyVibratorStateArgs& vibrateArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3041 ASSERT_EQ(DEVICE_ID, vibrateArgs.deviceId);
3042 ASSERT_TRUE(vibrateArgs.isOn);
Chris Yefb552902021-02-03 17:18:37 -08003043 // Stop vibrating
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003044 out = mapper.cancelVibrate(VIBRATION_TOKEN);
Chris Yefb552902021-02-03 17:18:37 -08003045 ASSERT_FALSE(mapper.isVibrating());
3046 // Verify vibrator state listener was notified.
3047 mReader->loopOnce();
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003048 ASSERT_EQ(1u, out.size());
3049 const NotifyVibratorStateArgs& cancelArgs = std::get<NotifyVibratorStateArgs>(*out.begin());
3050 ASSERT_EQ(DEVICE_ID, cancelArgs.deviceId);
3051 ASSERT_FALSE(cancelArgs.isOn);
Chris Ye87143712020-11-10 05:05:58 +00003052}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003053
Chris Yef59a2f42020-10-16 12:55:26 -07003054// --- SensorInputMapperTest ---
3055
3056class SensorInputMapperTest : public InputMapperTest {
3057protected:
3058 static const int32_t ACCEL_RAW_MIN;
3059 static const int32_t ACCEL_RAW_MAX;
3060 static const int32_t ACCEL_RAW_FUZZ;
3061 static const int32_t ACCEL_RAW_FLAT;
3062 static const int32_t ACCEL_RAW_RESOLUTION;
3063
3064 static const int32_t GYRO_RAW_MIN;
3065 static const int32_t GYRO_RAW_MAX;
3066 static const int32_t GYRO_RAW_FUZZ;
3067 static const int32_t GYRO_RAW_FLAT;
3068 static const int32_t GYRO_RAW_RESOLUTION;
3069
3070 static const float GRAVITY_MS2_UNIT;
3071 static const float DEGREE_RADIAN_UNIT;
3072
3073 void prepareAccelAxes();
3074 void prepareGyroAxes();
3075 void setAccelProperties();
3076 void setGyroProperties();
3077 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::SENSOR); }
3078};
3079
3080const int32_t SensorInputMapperTest::ACCEL_RAW_MIN = -32768;
3081const int32_t SensorInputMapperTest::ACCEL_RAW_MAX = 32768;
3082const int32_t SensorInputMapperTest::ACCEL_RAW_FUZZ = 16;
3083const int32_t SensorInputMapperTest::ACCEL_RAW_FLAT = 0;
3084const int32_t SensorInputMapperTest::ACCEL_RAW_RESOLUTION = 8192;
3085
3086const int32_t SensorInputMapperTest::GYRO_RAW_MIN = -2097152;
3087const int32_t SensorInputMapperTest::GYRO_RAW_MAX = 2097152;
3088const int32_t SensorInputMapperTest::GYRO_RAW_FUZZ = 16;
3089const int32_t SensorInputMapperTest::GYRO_RAW_FLAT = 0;
3090const int32_t SensorInputMapperTest::GYRO_RAW_RESOLUTION = 1024;
3091
3092const float SensorInputMapperTest::GRAVITY_MS2_UNIT = 9.80665f;
3093const float SensorInputMapperTest::DEGREE_RADIAN_UNIT = 0.0174533f;
3094
3095void SensorInputMapperTest::prepareAccelAxes() {
3096 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3097 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3098 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3099 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3100 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Z, ACCEL_RAW_MIN, ACCEL_RAW_MAX, ACCEL_RAW_FUZZ,
3101 ACCEL_RAW_FLAT, ACCEL_RAW_RESOLUTION);
3102}
3103
3104void SensorInputMapperTest::prepareGyroAxes() {
3105 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RX, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3106 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3107 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RY, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3108 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3109 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_RZ, GYRO_RAW_MIN, GYRO_RAW_MAX, GYRO_RAW_FUZZ,
3110 GYRO_RAW_FLAT, GYRO_RAW_RESOLUTION);
3111}
3112
3113void SensorInputMapperTest::setAccelProperties() {
3114 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 0, InputDeviceSensorType::ACCELEROMETER,
3115 /* sensorDataIndex */ 0);
3116 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 1, InputDeviceSensorType::ACCELEROMETER,
3117 /* sensorDataIndex */ 1);
3118 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 2, InputDeviceSensorType::ACCELEROMETER,
3119 /* sensorDataIndex */ 2);
3120 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3121 addConfigurationProperty("sensor.accelerometer.reportingMode", "0");
3122 addConfigurationProperty("sensor.accelerometer.maxDelay", "100000");
3123 addConfigurationProperty("sensor.accelerometer.minDelay", "5000");
3124 addConfigurationProperty("sensor.accelerometer.power", "1.5");
3125}
3126
3127void SensorInputMapperTest::setGyroProperties() {
3128 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 3, InputDeviceSensorType::GYROSCOPE,
3129 /* sensorDataIndex */ 0);
3130 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 4, InputDeviceSensorType::GYROSCOPE,
3131 /* sensorDataIndex */ 1);
3132 mFakeEventHub->addSensorAxis(EVENTHUB_ID, /* absCode */ 5, InputDeviceSensorType::GYROSCOPE,
3133 /* sensorDataIndex */ 2);
3134 mFakeEventHub->setMscEvent(EVENTHUB_ID, MSC_TIMESTAMP);
3135 addConfigurationProperty("sensor.gyroscope.reportingMode", "0");
3136 addConfigurationProperty("sensor.gyroscope.maxDelay", "100000");
3137 addConfigurationProperty("sensor.gyroscope.minDelay", "5000");
3138 addConfigurationProperty("sensor.gyroscope.power", "0.8");
3139}
3140
3141TEST_F(SensorInputMapperTest, GetSources) {
Arpit Singhfb706c32023-04-26 15:07:55 +00003142 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003143
3144 ASSERT_EQ(static_cast<uint32_t>(AINPUT_SOURCE_SENSOR), mapper.getSources());
3145}
3146
3147TEST_F(SensorInputMapperTest, ProcessAccelerometerSensor) {
3148 setAccelProperties();
3149 prepareAccelAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003150 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003151
3152 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::ACCELEROMETER,
3153 std::chrono::microseconds(10000),
3154 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003155 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, 20000);
3157 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, -20000);
3158 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Z, 40000);
3159 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3160 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003161
3162 NotifySensorArgs args;
3163 std::vector<float> values = {20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3164 -20000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT,
3165 40000.0f / ACCEL_RAW_RESOLUTION * GRAVITY_MS2_UNIT};
3166
3167 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3168 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3169 ASSERT_EQ(args.deviceId, DEVICE_ID);
3170 ASSERT_EQ(args.sensorType, InputDeviceSensorType::ACCELEROMETER);
3171 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3172 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3173 ASSERT_EQ(args.values, values);
3174 mapper.flushSensor(InputDeviceSensorType::ACCELEROMETER);
3175}
3176
3177TEST_F(SensorInputMapperTest, ProcessGyroscopeSensor) {
3178 setGyroProperties();
3179 prepareGyroAxes();
Arpit Singhfb706c32023-04-26 15:07:55 +00003180 SensorInputMapper& mapper = constructAndAddMapper<SensorInputMapper>();
Chris Yef59a2f42020-10-16 12:55:26 -07003181
3182 ASSERT_TRUE(mapper.enableSensor(InputDeviceSensorType::GYROSCOPE,
3183 std::chrono::microseconds(10000),
3184 std::chrono::microseconds(0)));
Chris Yee14523a2020-12-19 13:46:00 -08003185 ASSERT_TRUE(mFakeEventHub->isDeviceEnabled(EVENTHUB_ID));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003186 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RX, 20000);
3187 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RY, -20000);
3188 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_RZ, 40000);
3189 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_TIMESTAMP, 1000);
3190 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Chris Yef59a2f42020-10-16 12:55:26 -07003191
3192 NotifySensorArgs args;
3193 std::vector<float> values = {20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3194 -20000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT,
3195 40000.0f / GYRO_RAW_RESOLUTION * DEGREE_RADIAN_UNIT};
3196
3197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifySensorWasCalled(&args));
3198 ASSERT_EQ(args.source, AINPUT_SOURCE_SENSOR);
3199 ASSERT_EQ(args.deviceId, DEVICE_ID);
3200 ASSERT_EQ(args.sensorType, InputDeviceSensorType::GYROSCOPE);
3201 ASSERT_EQ(args.accuracy, InputDeviceSensorAccuracy::ACCURACY_HIGH);
3202 ASSERT_EQ(args.hwTimestamp, ARBITRARY_TIME);
3203 ASSERT_EQ(args.values, values);
3204 mapper.flushSensor(InputDeviceSensorType::GYROSCOPE);
3205}
3206
Michael Wrightd02c5b62014-02-10 15:10:22 -08003207// --- KeyboardInputMapperTest ---
3208
3209class KeyboardInputMapperTest : public InputMapperTest {
3210protected:
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003211 const std::string UNIQUE_ID = "local:0";
Zixuan Qufecb6062022-11-12 04:44:31 +00003212 const KeyboardLayoutInfo DEVICE_KEYBOARD_LAYOUT_INFO = KeyboardLayoutInfo("en-US", "qwerty");
Michael Wrighta9cf4192022-12-01 23:46:39 +00003213 void prepareDisplay(ui::Rotation orientation);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003214
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003215 void testDPadKeyRotation(KeyboardInputMapper& mapper, int32_t originalScanCode,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003216 int32_t originalKeyCode, int32_t rotatedKeyCode,
3217 int32_t displayId = ADISPLAY_ID_NONE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003218};
3219
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003220/* Similar to setDisplayInfoAndReconfigure, but pre-populates all parameters except for the
3221 * orientation.
3222 */
Michael Wrighta9cf4192022-12-01 23:46:39 +00003223void KeyboardInputMapperTest::prepareDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003224 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
3225 NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003226}
3227
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003228void KeyboardInputMapperTest::testDPadKeyRotation(KeyboardInputMapper& mapper,
Arthur Hung2c9a3342019-07-23 14:18:59 +08003229 int32_t originalScanCode, int32_t originalKeyCode,
3230 int32_t rotatedKeyCode, int32_t displayId) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08003231 NotifyKeyArgs args;
3232
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003233 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3235 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3236 ASSERT_EQ(originalScanCode, args.scanCode);
3237 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003238 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003239
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003240 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, originalScanCode, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3242 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3243 ASSERT_EQ(originalScanCode, args.scanCode);
3244 ASSERT_EQ(rotatedKeyCode, args.keyCode);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003245 ASSERT_EQ(displayId, args.displayId);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003246}
3247
Michael Wrightd02c5b62014-02-10 15:10:22 -08003248TEST_F(KeyboardInputMapperTest, GetSources) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003249 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003250 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003251 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003252
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003253 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003254}
3255
3256TEST_F(KeyboardInputMapperTest, Process_SimpleKeyPress) {
3257 const int32_t USAGE_A = 0x070004;
3258 const int32_t USAGE_UNKNOWN = 0x07ffff;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003259 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3260 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
Chris Yea52ade12020-08-27 16:49:20 -07003261 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, POLICY_FLAG_WAKE);
3262 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, POLICY_FLAG_WAKE);
3263 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003264
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003265 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003266 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003267 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003268 // Initial metastate is AMETA_NONE.
3269 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003270
3271 // Key down by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003272 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003273 NotifyKeyArgs args;
3274 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3275 ASSERT_EQ(DEVICE_ID, args.deviceId);
3276 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3277 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3278 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3279 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3280 ASSERT_EQ(KEY_HOME, args.scanCode);
3281 ASSERT_EQ(AMETA_NONE, args.metaState);
3282 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3283 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3284 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3285
3286 // Key up by scan code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003287 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3289 ASSERT_EQ(DEVICE_ID, args.deviceId);
3290 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3291 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3292 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3293 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
3294 ASSERT_EQ(KEY_HOME, args.scanCode);
3295 ASSERT_EQ(AMETA_NONE, args.metaState);
3296 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3297 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3298 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3299
3300 // Key down by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003301 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3302 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, 0, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003303 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3304 ASSERT_EQ(DEVICE_ID, args.deviceId);
3305 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3306 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3307 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3308 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3309 ASSERT_EQ(0, args.scanCode);
3310 ASSERT_EQ(AMETA_NONE, args.metaState);
3311 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3312 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3313 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3314
3315 // Key up by usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_A);
3317 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003318 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3319 ASSERT_EQ(DEVICE_ID, args.deviceId);
3320 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3321 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3322 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3323 ASSERT_EQ(AKEYCODE_A, args.keyCode);
3324 ASSERT_EQ(0, args.scanCode);
3325 ASSERT_EQ(AMETA_NONE, args.metaState);
3326 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3327 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
3328 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3329
3330 // Key down with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003331 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3332 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3334 ASSERT_EQ(DEVICE_ID, args.deviceId);
3335 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3336 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
3337 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3338 ASSERT_EQ(0, args.keyCode);
3339 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3340 ASSERT_EQ(AMETA_NONE, args.metaState);
3341 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3342 ASSERT_EQ(0U, args.policyFlags);
3343 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3344
3345 // Key up with unknown scan code or usage code.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003346 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, USAGE_UNKNOWN);
3347 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003348 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3349 ASSERT_EQ(DEVICE_ID, args.deviceId);
3350 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
3351 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
3352 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3353 ASSERT_EQ(0, args.keyCode);
3354 ASSERT_EQ(KEY_UNKNOWN, args.scanCode);
3355 ASSERT_EQ(AMETA_NONE, args.metaState);
3356 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
3357 ASSERT_EQ(0U, args.policyFlags);
3358 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
3359}
3360
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003361TEST_F(KeyboardInputMapperTest, Process_KeyRemapping) {
3362 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
3363 mFakeEventHub->addKey(EVENTHUB_ID, KEY_B, 0, AKEYCODE_B, 0);
3364 mFakeEventHub->addKeyRemapping(EVENTHUB_ID, AKEYCODE_A, AKEYCODE_B);
3365
3366 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003367 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmuraricbba14c2022-10-10 16:54:49 +00003368 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3369
3370 // Key down by scan code.
3371 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_A, 1);
3372 NotifyKeyArgs args;
3373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3374 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3375
3376 // Key up by scan code.
3377 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 0);
3378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3379 ASSERT_EQ(AKEYCODE_B, args.keyCode);
3380}
3381
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003382/**
3383 * Ensure that the readTime is set to the time when the EV_KEY is received.
3384 */
3385TEST_F(KeyboardInputMapperTest, Process_SendsReadTime) {
3386 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
3387
3388 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003389 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003390 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3391 NotifyKeyArgs args;
3392
3393 // Key down
Harry Cutts33476232023-01-30 19:57:29 +00003394 process(mapper, ARBITRARY_TIME, /*readTime=*/12, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003395 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3396 ASSERT_EQ(12, args.readTime);
3397
3398 // Key up
Harry Cutts33476232023-01-30 19:57:29 +00003399 process(mapper, ARBITRARY_TIME, /*readTime=*/15, EV_KEY, KEY_HOME, 1);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003400 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3401 ASSERT_EQ(15, args.readTime);
3402}
3403
Michael Wrightd02c5b62014-02-10 15:10:22 -08003404TEST_F(KeyboardInputMapperTest, Process_ShouldUpdateMetaState) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003405 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFTSHIFT, 0, AKEYCODE_SHIFT_LEFT, 0);
3406 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003407 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_NUMLOCK, AKEYCODE_NUM_LOCK, 0);
3408 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_CAPSLOCK, AKEYCODE_CAPS_LOCK, 0);
3409 mFakeEventHub->addKey(EVENTHUB_ID, 0, KEY_SCROLLLOCK, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003410
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003411 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003412 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003413 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003414
Arthur Hung95f68612022-04-07 14:08:22 +08003415 // Initial metastate is AMETA_NONE.
3416 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003417
3418 // Metakey down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003420 NotifyKeyArgs args;
3421 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3422 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003423 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003424 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003425
3426 // Key down.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003427 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_A, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003428 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3429 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003430 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003431
3432 // Key up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003433 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, KEY_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3435 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003437
3438 // Metakey up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003439 process(mapper, ARBITRARY_TIME + 3, READ_TIME, EV_KEY, KEY_LEFTSHIFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003440 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3441 ASSERT_EQ(AMETA_NONE, args.metaState);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003442 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungdcef2dc2020-08-11 14:47:50 +08003443 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertUpdateGlobalMetaStateWasCalled());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003444}
3445
3446TEST_F(KeyboardInputMapperTest, Process_WhenNotOrientationAware_ShouldNotRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003447 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3448 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3449 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3450 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003451
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003452 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003453 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003454 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003455
Michael Wrighta9cf4192022-12-01 23:46:39 +00003456 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003457 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3458 KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP));
3459 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3460 KEY_RIGHT, AKEYCODE_DPAD_RIGHT, AKEYCODE_DPAD_RIGHT));
3461 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3462 KEY_DOWN, AKEYCODE_DPAD_DOWN, AKEYCODE_DPAD_DOWN));
3463 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper,
3464 KEY_LEFT, AKEYCODE_DPAD_LEFT, AKEYCODE_DPAD_LEFT));
3465}
3466
3467TEST_F(KeyboardInputMapperTest, Process_WhenOrientationAware_ShouldRotateDPad) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003468 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3469 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3470 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3471 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003472
Michael Wrightd02c5b62014-02-10 15:10:22 -08003473 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003474 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003475 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003476 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003477
Michael Wrighta9cf4192022-12-01 23:46:39 +00003478 prepareDisplay(ui::ROTATION_0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003479 ASSERT_NO_FATAL_FAILURE(
3480 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3481 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3482 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3483 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3484 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3485 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3486 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003487
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003488 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003489 prepareDisplay(ui::ROTATION_90);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003490 ASSERT_NO_FATAL_FAILURE(
3491 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3492 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3493 AKEYCODE_DPAD_UP, DISPLAY_ID));
3494 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3495 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3496 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3497 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003498
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003499 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003500 prepareDisplay(ui::ROTATION_180);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003501 ASSERT_NO_FATAL_FAILURE(
3502 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3503 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3504 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3505 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3506 AKEYCODE_DPAD_UP, DISPLAY_ID));
3507 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3508 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003509
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003510 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003511 prepareDisplay(ui::ROTATION_270);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003512 ASSERT_NO_FATAL_FAILURE(
3513 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3514 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3515 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3516 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3517 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3518 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3519 AKEYCODE_DPAD_UP, DISPLAY_ID));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003520
3521 // Special case: if orientation changes while key is down, we still emit the same keycode
3522 // in the key up as we did in the key down.
3523 NotifyKeyArgs args;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003524 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003525 prepareDisplay(ui::ROTATION_270);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003526 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3528 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3529 ASSERT_EQ(KEY_UP, args.scanCode);
3530 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3531
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003532 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003533 prepareDisplay(ui::ROTATION_180);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003534 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003535 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3536 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3537 ASSERT_EQ(KEY_UP, args.scanCode);
3538 ASSERT_EQ(AKEYCODE_DPAD_RIGHT, args.keyCode);
3539}
3540
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003541TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_NotOrientationAware) {
3542 // If the keyboard is not orientation aware,
3543 // key events should not be associated with a specific display id
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003544 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003545
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003546 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003547 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003548 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003549 NotifyKeyArgs args;
3550
3551 // Display id should be ADISPLAY_ID_NONE without any display configuration.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003552 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003554 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3556 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3557
Michael Wrighta9cf4192022-12-01 23:46:39 +00003558 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003559 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003560 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003562 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3563 ASSERT_EQ(ADISPLAY_ID_NONE, args.displayId);
3564}
3565
3566TEST_F(KeyboardInputMapperTest, DisplayIdConfigurationChange_OrientationAware) {
3567 // If the keyboard is orientation aware,
3568 // key events should be associated with the internal viewport
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003569 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003570
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003571 addConfigurationProperty("keyboard.orientationAware", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003572 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003573 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003574 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003575 NotifyKeyArgs args;
3576
3577 // Display id should be ADISPLAY_ID_NONE without any display configuration.
3578 // ^--- already checked by the previous test
3579
Michael Wrighta9cf4192022-12-01 23:46:39 +00003580 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003581 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003582 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003583 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003584 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3586 ASSERT_EQ(DISPLAY_ID, args.displayId);
3587
3588 constexpr int32_t newDisplayId = 2;
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07003589 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00003590 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003591 UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 1);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003594 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UP, 0);
Siarhei Vishniakoua62a8dd2018-06-08 21:17:33 +01003595 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3596 ASSERT_EQ(newDisplayId, args.displayId);
3597}
3598
Michael Wrightd02c5b62014-02-10 15:10:22 -08003599TEST_F(KeyboardInputMapperTest, GetKeyCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003600 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003601 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003602 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003603
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003604 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003605 ASSERT_EQ(1, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003606
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003607 mFakeEventHub->setKeyCodeState(EVENTHUB_ID, AKEYCODE_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003608 ASSERT_EQ(0, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003609}
3610
Philip Junker4af3b3d2021-12-14 10:36:55 +01003611TEST_F(KeyboardInputMapperTest, GetKeyCodeForKeyLocation) {
3612 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003613 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Philip Junker4af3b3d2021-12-14 10:36:55 +01003614 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3615
3616 mFakeEventHub->addKeyCodeMapping(EVENTHUB_ID, AKEYCODE_Y, AKEYCODE_Z);
3617 ASSERT_EQ(AKEYCODE_Z, mapper.getKeyCodeForKeyLocation(AKEYCODE_Y))
3618 << "If a mapping is available, the result is equal to the mapping";
3619
3620 ASSERT_EQ(AKEYCODE_A, mapper.getKeyCodeForKeyLocation(AKEYCODE_A))
3621 << "If no mapping is available, the result is the key location";
3622}
3623
Michael Wrightd02c5b62014-02-10 15:10:22 -08003624TEST_F(KeyboardInputMapperTest, GetScanCodeState) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003625 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003626 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003627 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003628
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003629 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 1);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003630 ASSERT_EQ(1, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003631
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003632 mFakeEventHub->setScanCodeState(EVENTHUB_ID, KEY_A, 0);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003633 ASSERT_EQ(0, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003634}
3635
3636TEST_F(KeyboardInputMapperTest, MarkSupportedKeyCodes) {
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003637 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003638 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003639 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003640
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003641 mFakeEventHub->addKey(EVENTHUB_ID, KEY_A, 0, AKEYCODE_A, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003642
Michael Wrightd02c5b62014-02-10 15:10:22 -08003643 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07003644 ASSERT_TRUE(mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_A, AKEYCODE_B}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003645 ASSERT_TRUE(flags[0]);
3646 ASSERT_FALSE(flags[1]);
3647}
3648
3649TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleMetaStateAndLeds) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003650 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3651 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3652 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3653 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3654 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3655 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08003656
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003657 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003658 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003659 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003660 // Initial metastate is AMETA_NONE.
3661 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003662
3663 // Initialization should have turned all of the lights off.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003664 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3665 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3666 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Michael Wrightd02c5b62014-02-10 15:10:22 -08003667
3668 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003669 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3670 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003671 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3672 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3673 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003674 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003675
3676 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003677 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3678 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003679 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3680 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3681 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003682 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003683
3684 // Toggle caps lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003685 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3686 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003687 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3688 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3689 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003690 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003691
3692 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003693 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3694 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003695 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3696 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3697 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003698 ASSERT_EQ(AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003699
3700 // Toggle num lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003701 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3702 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003703 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3704 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3705 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003706 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003707
3708 // Toggle scroll lock off.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003711 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3712 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3713 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003714 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Michael Wrightd02c5b62014-02-10 15:10:22 -08003715}
3716
Chris Yea52ade12020-08-27 16:49:20 -07003717TEST_F(KeyboardInputMapperTest, NoMetaStateWhenMetaKeysNotPresent) {
3718 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_BUTTON_A, 0);
3719 mFakeEventHub->addKey(EVENTHUB_ID, BTN_B, 0, AKEYCODE_BUTTON_B, 0);
3720 mFakeEventHub->addKey(EVENTHUB_ID, BTN_X, 0, AKEYCODE_BUTTON_X, 0);
3721 mFakeEventHub->addKey(EVENTHUB_ID, BTN_Y, 0, AKEYCODE_BUTTON_Y, 0);
3722
3723 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003724 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Chris Yea52ade12020-08-27 16:49:20 -07003725 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3726
Chris Yea52ade12020-08-27 16:49:20 -07003727 // Meta state should be AMETA_NONE after reset
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003728 std::list<NotifyArgs> unused = mapper.reset(ARBITRARY_TIME);
Chris Yea52ade12020-08-27 16:49:20 -07003729 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3730 // Meta state should be AMETA_NONE with update, as device doesn't have the keys.
3731 mapper.updateMetaState(AKEYCODE_NUM_LOCK);
3732 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3733
3734 NotifyKeyArgs args;
3735 // Press button "A"
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003736 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_A, 1);
Chris Yea52ade12020-08-27 16:49:20 -07003737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3738 ASSERT_EQ(AMETA_NONE, args.metaState);
3739 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3740 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
3741 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3742
3743 // Button up.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003744 process(mapper, ARBITRARY_TIME + 2, READ_TIME, EV_KEY, BTN_A, 0);
Chris Yea52ade12020-08-27 16:49:20 -07003745 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
3746 ASSERT_EQ(AMETA_NONE, args.metaState);
3747 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
3748 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
3749 ASSERT_EQ(AKEYCODE_BUTTON_A, args.keyCode);
3750}
3751
Arthur Hung2c9a3342019-07-23 14:18:59 +08003752TEST_F(KeyboardInputMapperTest, Configure_AssignsDisplayPort) {
3753 // keyboard 1.
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003754 mFakeEventHub->addKey(EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3755 mFakeEventHub->addKey(EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3756 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3757 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003758
3759 // keyboard 2.
3760 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +08003761 const std::string DEVICE_NAME2 = "KEYBOARD2";
Arthur Hung2c9a3342019-07-23 14:18:59 +08003762 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003763 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +08003764 std::shared_ptr<InputDevice> device2 =
3765 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003766 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +08003767
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08003768 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_UP, 0, AKEYCODE_DPAD_UP, 0);
3769 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_RIGHT, 0, AKEYCODE_DPAD_RIGHT, 0);
3770 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
3771 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, 0);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003772
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003773 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003774 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003775 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003776
Arpit Singh67ca6842023-04-26 14:43:16 +00003777 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08003778 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003779 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3780 mFakePolicy
3781 ->getReaderConfiguration(),
3782 AINPUT_SOURCE_KEYBOARD,
3783 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003784 std::list<NotifyArgs> unused =
3785 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003786 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003787 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003788
3789 // Prepared displays and associated info.
3790 constexpr uint8_t hdmi1 = 0;
3791 constexpr uint8_t hdmi2 = 1;
3792 const std::string SECONDARY_UNIQUE_ID = "local:1";
3793
3794 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
3795 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
3796
3797 // No associated display viewport found, should disable the device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003798 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003799 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003800 ASSERT_FALSE(device2->isEnabled());
3801
3802 // Prepare second display.
3803 constexpr int32_t newDisplayId = 2;
Michael Wrighta9cf4192022-12-01 23:46:39 +00003804 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003805 UNIQUE_ID, hdmi1, ViewportType::INTERNAL);
Michael Wrighta9cf4192022-12-01 23:46:39 +00003806 setDisplayInfoAndReconfigure(newDisplayId, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Michael Wrightfe3de7d2020-07-02 19:05:30 +01003807 SECONDARY_UNIQUE_ID, hdmi2, ViewportType::EXTERNAL);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003808 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003809 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003810 InputReaderConfiguration::Change::DISPLAY_INFO);
Arthur Hung2c9a3342019-07-23 14:18:59 +08003811
3812 // Device should be enabled after the associated display is found.
3813 ASSERT_TRUE(mDevice->isEnabled());
3814 ASSERT_TRUE(device2->isEnabled());
3815
3816 // Test pad key events
3817 ASSERT_NO_FATAL_FAILURE(
3818 testDPadKeyRotation(mapper, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, DISPLAY_ID));
3819 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3820 AKEYCODE_DPAD_RIGHT, DISPLAY_ID));
3821 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3822 AKEYCODE_DPAD_DOWN, DISPLAY_ID));
3823 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3824 AKEYCODE_DPAD_LEFT, DISPLAY_ID));
3825
3826 ASSERT_NO_FATAL_FAILURE(
3827 testDPadKeyRotation(mapper2, KEY_UP, AKEYCODE_DPAD_UP, AKEYCODE_DPAD_UP, newDisplayId));
3828 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_RIGHT, AKEYCODE_DPAD_RIGHT,
3829 AKEYCODE_DPAD_RIGHT, newDisplayId));
3830 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_DOWN, AKEYCODE_DPAD_DOWN,
3831 AKEYCODE_DPAD_DOWN, newDisplayId));
3832 ASSERT_NO_FATAL_FAILURE(testDPadKeyRotation(mapper2, KEY_LEFT, AKEYCODE_DPAD_LEFT,
3833 AKEYCODE_DPAD_LEFT, newDisplayId));
3834}
Michael Wrightd02c5b62014-02-10 15:10:22 -08003835
arthurhungc903df12020-08-11 15:08:42 +08003836TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleAfterReattach) {
3837 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3838 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3839 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3840 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3841 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3842 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3843
3844 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003845 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
arthurhungc903df12020-08-11 15:08:42 +08003846 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003847 // Initial metastate is AMETA_NONE.
3848 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003849
3850 // Initialization should have turned all of the lights off.
3851 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3852 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3853 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3854
3855 // Toggle caps lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003856 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3857 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003858 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3859 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3860
3861 // Toggle num lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003862 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3863 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003864 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3865 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON, mapper.getMetaState());
3866
3867 // Toggle scroll lock on.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00003868 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
3869 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
arthurhungc903df12020-08-11 15:08:42 +08003870 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
3871 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON, mapper.getMetaState());
3872
3873 mFakeEventHub->removeDevice(EVENTHUB_ID);
3874 mReader->loopOnce();
3875
3876 // keyboard 2 should default toggle keys.
3877 const std::string USB2 = "USB2";
3878 const std::string DEVICE_NAME2 = "KEYBOARD2";
3879 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3880 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3881 std::shared_ptr<InputDevice> device2 =
3882 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -07003883 ftl::Flags<InputDeviceClass>(0));
arthurhungc903df12020-08-11 15:08:42 +08003884 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3885 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3886 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3887 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3888 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3889 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3890
Arpit Singh67ca6842023-04-26 14:43:16 +00003891 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
arthurhung6fe95782020-10-05 22:41:16 +08003892 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003893 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3894 mFakePolicy
3895 ->getReaderConfiguration(),
3896 AINPUT_SOURCE_KEYBOARD,
3897 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003898 std::list<NotifyArgs> unused =
3899 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003900 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003901 unused += device2->reset(ARBITRARY_TIME);
arthurhungc903df12020-08-11 15:08:42 +08003902
3903 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_CAPSL));
3904 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_NUML));
3905 ASSERT_TRUE(mFakeEventHub->getLedState(SECOND_EVENTHUB_ID, LED_SCROLLL));
arthurhung6fe95782020-10-05 22:41:16 +08003906 ASSERT_EQ(AMETA_CAPS_LOCK_ON | AMETA_NUM_LOCK_ON | AMETA_SCROLL_LOCK_ON,
3907 mapper2.getMetaState());
arthurhungc903df12020-08-11 15:08:42 +08003908}
3909
Arthur Hungcb40a002021-08-03 14:31:01 +00003910TEST_F(KeyboardInputMapperTest, Process_toggleCapsLockState) {
3911 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3912 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3913 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3914
3915 // Suppose we have two mappers. (DPAD + KEYBOARD)
Arpit Singh67ca6842023-04-26 14:43:16 +00003916 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_DPAD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003917 AINPUT_KEYBOARD_TYPE_NON_ALPHABETIC);
3918 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00003919 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungcb40a002021-08-03 14:31:01 +00003920 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Arthur Hung95f68612022-04-07 14:08:22 +08003921 // Initial metastate is AMETA_NONE.
3922 ASSERT_EQ(AMETA_NONE, mapper.getMetaState());
Arthur Hungcb40a002021-08-03 14:31:01 +00003923
3924 mReader->toggleCapsLockState(DEVICE_ID);
3925 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper.getMetaState());
3926}
3927
Arthur Hungfb3cc112022-04-13 07:39:50 +00003928TEST_F(KeyboardInputMapperTest, Process_LockedKeysShouldToggleInMultiDevices) {
3929 // keyboard 1.
3930 mFakeEventHub->addLed(EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3931 mFakeEventHub->addLed(EVENTHUB_ID, LED_NUML, false /*initially off*/);
3932 mFakeEventHub->addLed(EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3933 mFakeEventHub->addKey(EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3934 mFakeEventHub->addKey(EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3935 mFakeEventHub->addKey(EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3936
3937 KeyboardInputMapper& mapper1 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003938 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hungfb3cc112022-04-13 07:39:50 +00003939 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
3940
3941 // keyboard 2.
3942 const std::string USB2 = "USB2";
3943 const std::string DEVICE_NAME2 = "KEYBOARD2";
3944 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
3945 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
3946 std::shared_ptr<InputDevice> device2 =
3947 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
3948 ftl::Flags<InputDeviceClass>(0));
3949 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_CAPSL, true /*initially on*/);
3950 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_NUML, false /*initially off*/);
3951 mFakeEventHub->addLed(SECOND_EVENTHUB_ID, LED_SCROLLL, false /*initially off*/);
3952 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_CAPSLOCK, 0, AKEYCODE_CAPS_LOCK, 0);
3953 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_NUMLOCK, 0, AKEYCODE_NUM_LOCK, 0);
3954 mFakeEventHub->addKey(SECOND_EVENTHUB_ID, KEY_SCROLLLOCK, 0, AKEYCODE_SCROLL_LOCK, 0);
3955
Arpit Singh67ca6842023-04-26 14:43:16 +00003956 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003957 KeyboardInputMapper& mapper2 =
Arpit Singh67ca6842023-04-26 14:43:16 +00003958 device2->constructAndAddMapper<KeyboardInputMapper>(SECOND_EVENTHUB_ID,
3959 mFakePolicy
3960 ->getReaderConfiguration(),
3961 AINPUT_SOURCE_KEYBOARD,
3962 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003963 std::list<NotifyArgs> unused =
3964 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00003965 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -07003966 unused += device2->reset(ARBITRARY_TIME);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003967
Arthur Hung95f68612022-04-07 14:08:22 +08003968 // Initial metastate is AMETA_NONE.
3969 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3970 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3971
3972 // Toggle num lock on and off.
3973 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3974 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
Arthur Hungfb3cc112022-04-13 07:39:50 +00003975 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3976 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper1.getMetaState());
3977 ASSERT_EQ(AMETA_NUM_LOCK_ON, mapper2.getMetaState());
3978
3979 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 1);
3980 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_NUMLOCK, 0);
3981 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_NUML));
3982 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3983 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3984
3985 // Toggle caps lock on and off.
3986 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3987 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3988 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3989 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper1.getMetaState());
3990 ASSERT_EQ(AMETA_CAPS_LOCK_ON, mapper2.getMetaState());
3991
3992 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 1);
3993 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_CAPSLOCK, 0);
3994 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_CAPSL));
3995 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
3996 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
3997
3998 // Toggle scroll lock on and off.
3999 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4000 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4001 ASSERT_TRUE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4002 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper1.getMetaState());
4003 ASSERT_EQ(AMETA_SCROLL_LOCK_ON, mapper2.getMetaState());
4004
4005 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 1);
4006 process(mapper1, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_SCROLLLOCK, 0);
4007 ASSERT_FALSE(mFakeEventHub->getLedState(EVENTHUB_ID, LED_SCROLLL));
4008 ASSERT_EQ(AMETA_NONE, mapper1.getMetaState());
4009 ASSERT_EQ(AMETA_NONE, mapper2.getMetaState());
4010}
4011
Arthur Hung2141d542022-08-23 07:45:21 +00004012TEST_F(KeyboardInputMapperTest, Process_DisabledDevice) {
4013 const int32_t USAGE_A = 0x070004;
4014 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4015 mFakeEventHub->addKey(EVENTHUB_ID, 0, USAGE_A, AKEYCODE_A, POLICY_FLAG_WAKE);
4016
4017 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004018 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Arthur Hung2141d542022-08-23 07:45:21 +00004019 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4020 // Key down by scan code.
4021 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
4022 NotifyKeyArgs args;
4023 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4024 ASSERT_EQ(DEVICE_ID, args.deviceId);
4025 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
4026 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4027 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
4028 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4029 ASSERT_EQ(KEY_HOME, args.scanCode);
4030 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM, args.flags);
4031
4032 // Disable device, it should synthesize cancellation events for down events.
4033 mFakePolicy->addDisabledDevice(DEVICE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004034 configureDevice(InputReaderConfiguration::Change::ENABLED_STATE);
Arthur Hung2141d542022-08-23 07:45:21 +00004035
4036 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4037 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
4038 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
4039 ASSERT_EQ(KEY_HOME, args.scanCode);
4040 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_CANCELED, args.flags);
4041}
4042
Zixuan Qufecb6062022-11-12 04:44:31 +00004043TEST_F(KeyboardInputMapperTest, Configure_AssignKeyboardLayoutInfo) {
Arpit Singh67ca6842023-04-26 14:43:16 +00004044 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
4045 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Zixuan Qufecb6062022-11-12 04:44:31 +00004046 std::list<NotifyArgs> unused =
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004047 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4048 /*changes=*/{});
Zixuan Qufecb6062022-11-12 04:44:31 +00004049
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004050 uint32_t generation = mReader->getContext()->getGeneration();
Zixuan Qufecb6062022-11-12 04:44:31 +00004051 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4052
4053 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004054 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
Zixuan Qufecb6062022-11-12 04:44:31 +00004055
4056 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
4057 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.languageTag,
4058 deviceInfo.getKeyboardLayoutInfo()->languageTag);
4059 ASSERT_EQ(DEVICE_KEYBOARD_LAYOUT_INFO.layoutType,
4060 deviceInfo.getKeyboardLayoutInfo()->layoutType);
Vaibhav Devmurari0a6fee82023-04-11 18:53:04 +00004061 ASSERT_TRUE(mReader->getContext()->getGeneration() != generation);
4062
4063 // Call change layout association with the same values: Generation shouldn't change
4064 generation = mReader->getContext()->getGeneration();
4065 mFakePolicy->addKeyboardLayoutAssociation(DEVICE_LOCATION, DEVICE_KEYBOARD_LAYOUT_INFO);
4066 unused += mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
4067 InputReaderConfiguration::Change::KEYBOARD_LAYOUT_ASSOCIATION);
4068 ASSERT_TRUE(mReader->getContext()->getGeneration() == generation);
Zixuan Qufecb6062022-11-12 04:44:31 +00004069}
4070
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004071TEST_F(KeyboardInputMapperTest, LayoutInfoCorrectlyMapped) {
4072 mFakeEventHub->setRawLayoutInfo(EVENTHUB_ID,
4073 RawLayoutInfo{.languageTag = "en", .layoutType = "extended"});
4074
4075 // Configuration
Arpit Singh67ca6842023-04-26 14:43:16 +00004076 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004077 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4078 InputReaderConfiguration config;
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004079 std::list<NotifyArgs> unused = mDevice->configure(ARBITRARY_TIME, config, /*changes=*/{});
Vaibhav Devmurari7fb41132023-01-02 13:30:26 +00004080
4081 ASSERT_EQ("en", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->languageTag);
4082 ASSERT_EQ("extended", mDevice->getDeviceInfo().getKeyboardLayoutInfo()->layoutType);
4083}
4084
Justin Chung71ddb432023-03-27 04:29:07 +00004085TEST_F(KeyboardInputMapperTest, Process_GesureEventToSetFlagKeepTouchMode) {
4086 mFakeEventHub->addKey(EVENTHUB_ID, KEY_LEFT, 0, AKEYCODE_DPAD_LEFT, POLICY_FLAG_GESTURE);
4087 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004088 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Justin Chung71ddb432023-03-27 04:29:07 +00004089 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
4090 NotifyKeyArgs args;
4091
4092 // Key down
4093 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_LEFT, 1);
4094 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4095 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_KEEP_TOUCH_MODE, args.flags);
4096}
4097
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004098// --- KeyboardInputMapperTest_ExternalDevice ---
4099
4100class KeyboardInputMapperTest_ExternalDevice : public InputMapperTest {
4101protected:
Chris Yea52ade12020-08-27 16:49:20 -07004102 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004103};
4104
4105TEST_F(KeyboardInputMapperTest_ExternalDevice, WakeBehavior) {
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004106 // For external devices, keys will trigger wake on key down. Media keys should also trigger
4107 // wake if triggered from external devices.
Powei Fengd041c5d2019-05-03 17:11:33 -07004108
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004109 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, 0);
4110 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, 0);
4111 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAYPAUSE, 0, AKEYCODE_MEDIA_PLAY_PAUSE,
4112 POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004113
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004114 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004115 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004116 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004117
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004119 NotifyKeyArgs args;
4120 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4121 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4122
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004123 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4125 ASSERT_EQ(uint32_t(0), args.policyFlags);
4126
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004127 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
Vaibhav Devmurari16257862023-03-06 10:06:32 +00004129 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
Powei Fengd041c5d2019-05-03 17:11:33 -07004130
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004131 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004132 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4133 ASSERT_EQ(uint32_t(0), args.policyFlags);
4134
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004135 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4137 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4138
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004139 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAYPAUSE, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004140 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4141 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4142}
4143
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004144TEST_F(KeyboardInputMapperTest_ExternalDevice, DoNotWakeByDefaultBehavior) {
Powei Fengd041c5d2019-05-03 17:11:33 -07004145 // Tv Remote key's wake behavior is prescribed by the keylayout file.
Powei Fengd041c5d2019-05-03 17:11:33 -07004146
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08004147 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
4148 mFakeEventHub->addKey(EVENTHUB_ID, KEY_DOWN, 0, AKEYCODE_DPAD_DOWN, 0);
4149 mFakeEventHub->addKey(EVENTHUB_ID, KEY_PLAY, 0, AKEYCODE_MEDIA_PLAY, POLICY_FLAG_WAKE);
Powei Fengd041c5d2019-05-03 17:11:33 -07004150
Powei Fengd041c5d2019-05-03 17:11:33 -07004151 addConfigurationProperty("keyboard.doNotWakeByDefault", "1");
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004152 KeyboardInputMapper& mapper =
Arpit Singh67ca6842023-04-26 14:43:16 +00004153 constructAndAddMapper<KeyboardInputMapper>(AINPUT_SOURCE_KEYBOARD,
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004154 AINPUT_KEYBOARD_TYPE_ALPHABETIC);
Powei Fengd041c5d2019-05-03 17:11:33 -07004155
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004156 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_HOME, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004157 NotifyKeyArgs args;
4158 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4159 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4160
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004161 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_HOME, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004162 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4163 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4164
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004165 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_DOWN, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4167 ASSERT_EQ(uint32_t(0), args.policyFlags);
4168
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004169 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_DOWN, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004170 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4171 ASSERT_EQ(uint32_t(0), args.policyFlags);
4172
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_PLAY, 1);
Powei Fengd041c5d2019-05-03 17:11:33 -07004174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4175 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4176
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004177 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, KEY_PLAY, 0);
Powei Fengd041c5d2019-05-03 17:11:33 -07004178 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
4179 ASSERT_EQ(POLICY_FLAG_WAKE, args.policyFlags);
4180}
4181
Byoungho Jungda10dd32023-10-06 17:03:45 +09004182// --- CursorInputMapperTestBase ---
Michael Wrightd02c5b62014-02-10 15:10:22 -08004183
Byoungho Jungda10dd32023-10-06 17:03:45 +09004184class CursorInputMapperTestBase : public InputMapperTest {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004185protected:
4186 static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
4187
Michael Wright17db18e2020-06-26 20:51:44 +01004188 std::shared_ptr<FakePointerController> mFakePointerController;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004189
Chris Yea52ade12020-08-27 16:49:20 -07004190 void SetUp() override {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004191 InputMapperTest::SetUp();
4192
Michael Wright17db18e2020-06-26 20:51:44 +01004193 mFakePointerController = std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +00004194 mFakePolicy->setPointerController(mFakePointerController);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004195 }
4196
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004197 void testMotionRotation(CursorInputMapper& mapper, int32_t originalX, int32_t originalY,
4198 int32_t rotatedX, int32_t rotatedY);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004199
Michael Wrighta9cf4192022-12-01 23:46:39 +00004200 void prepareDisplay(ui::Rotation orientation) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004201 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation,
4202 DISPLAY_UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
4203 }
4204
4205 void prepareSecondaryDisplay() {
4206 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00004207 ui::ROTATION_0, SECONDARY_DISPLAY_UNIQUE_ID, NO_PORT,
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004208 ViewportType::EXTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07004209 }
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004210
4211 static void assertCursorPointerCoords(const PointerCoords& coords, float x, float y,
4212 float pressure) {
4213 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(coords, x, y, pressure, 0.0f, 0.0f, 0.0f, 0.0f,
4214 0.0f, 0.0f, 0.0f, EPSILON));
4215 }
Michael Wrightd02c5b62014-02-10 15:10:22 -08004216};
4217
Byoungho Jungda10dd32023-10-06 17:03:45 +09004218const int32_t CursorInputMapperTestBase::TRACKBALL_MOVEMENT_THRESHOLD = 6;
Michael Wrightd02c5b62014-02-10 15:10:22 -08004219
Byoungho Jungda10dd32023-10-06 17:03:45 +09004220void CursorInputMapperTestBase::testMotionRotation(CursorInputMapper& mapper, int32_t originalX,
4221 int32_t originalY, int32_t rotatedX,
4222 int32_t rotatedY) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004223 NotifyMotionArgs args;
4224
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004225 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, originalX);
4226 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, originalY);
4227 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4229 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004230 ASSERT_NO_FATAL_FAILURE(
4231 assertCursorPointerCoords(args.pointerCoords[0],
4232 float(rotatedX) / TRACKBALL_MOVEMENT_THRESHOLD,
4233 float(rotatedY) / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004234}
4235
Byoungho Jungda10dd32023-10-06 17:03:45 +09004236// --- CursorInputMapperTest ---
4237
4238class CursorInputMapperTest : public CursorInputMapperTestBase {
4239protected:
4240 void SetUp() override {
4241 input_flags::enable_pointer_choreographer(false);
4242 CursorInputMapperTestBase::SetUp();
4243 }
4244};
4245
Michael Wrightd02c5b62014-02-10 15:10:22 -08004246TEST_F(CursorInputMapperTest, WhenModeIsPointer_GetSources_ReturnsMouse) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004247 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004248 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004249
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004250 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004251}
4252
4253TEST_F(CursorInputMapperTest, WhenModeIsNavigation_GetSources_ReturnsTrackball) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004254 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004255 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004256
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08004257 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004258}
4259
4260TEST_F(CursorInputMapperTest, WhenModeIsPointer_PopulateDeviceInfo_ReturnsRangeFromPointerController) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004261 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004262 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004263
4264 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004265 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004266
4267 // Initially there may not be a valid motion range.
Yi Kong9b14ac62018-07-17 13:48:38 -07004268 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
4269 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004270 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4271 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
4272
4273 // When the bounds are set, then there should be a valid motion range.
4274 mFakePointerController->setBounds(1, 2, 800 - 1, 480 - 1);
Byoungho Jungda10dd32023-10-06 17:03:45 +09004275 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004276
4277 InputDeviceInfo info2;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004278 mapper.populateDeviceInfo(info2);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004279
4280 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4281 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE,
4282 1, 800 - 1, 0.0f, 0.0f));
4283 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4284 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE,
4285 2, 480 - 1, 0.0f, 0.0f));
4286 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2,
4287 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_MOUSE,
4288 0.0f, 1.0f, 0.0f, 0.0f));
4289}
4290
4291TEST_F(CursorInputMapperTest, WhenModeIsNavigation_PopulateDeviceInfo_ReturnsScaledRange) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004292 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004293 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004294
4295 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00004296 mapper.populateDeviceInfo(info);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004297
4298 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4299 AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_TRACKBALL,
4300 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4301 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4302 AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_TRACKBALL,
4303 -1.0f, 1.0f, 0.0f, 1.0f / TRACKBALL_MOVEMENT_THRESHOLD));
4304 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
4305 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TRACKBALL,
4306 0.0f, 1.0f, 0.0f, 0.0f));
4307}
4308
4309TEST_F(CursorInputMapperTest, Process_ShouldSetAllFieldsAndIncludeGlobalMetaState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004310 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004311 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004312
arthurhungdcef2dc2020-08-11 14:47:50 +08004313 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004314
4315 NotifyMotionArgs args;
4316
4317 // Button press.
4318 // Mostly testing non x/y behavior here so we don't need to check again elsewhere.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004319 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4320 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004321 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4322 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4323 ASSERT_EQ(DEVICE_ID, args.deviceId);
4324 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4325 ASSERT_EQ(uint32_t(0), args.policyFlags);
4326 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
4327 ASSERT_EQ(0, args.flags);
4328 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4329 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4330 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004331 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004332 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004333 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004334 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004335 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4336 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4337 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4338
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4340 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
4341 ASSERT_EQ(DEVICE_ID, args.deviceId);
4342 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4343 ASSERT_EQ(uint32_t(0), args.policyFlags);
4344 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
4345 ASSERT_EQ(0, args.flags);
4346 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4347 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, args.buttonState);
4348 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004349 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004350 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004351 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004352 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004353 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4354 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4355 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4356
Michael Wrightd02c5b62014-02-10 15:10:22 -08004357 // Button release. Should have same down time.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004358 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4359 process(mapper, ARBITRARY_TIME + 1, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4361 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4362 ASSERT_EQ(DEVICE_ID, args.deviceId);
4363 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4364 ASSERT_EQ(uint32_t(0), args.policyFlags);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004365 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
4366 ASSERT_EQ(0, args.flags);
4367 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4368 ASSERT_EQ(0, args.buttonState);
4369 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004370 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004371 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004372 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004373 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004374 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4375 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4376 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4377
4378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4379 ASSERT_EQ(ARBITRARY_TIME + 1, args.eventTime);
4380 ASSERT_EQ(DEVICE_ID, args.deviceId);
4381 ASSERT_EQ(AINPUT_SOURCE_TRACKBALL, args.source);
4382 ASSERT_EQ(uint32_t(0), args.policyFlags);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004383 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
4384 ASSERT_EQ(0, args.flags);
4385 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
4386 ASSERT_EQ(0, args.buttonState);
4387 ASSERT_EQ(0, args.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07004388 ASSERT_EQ(uint32_t(1), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08004389 ASSERT_EQ(0, args.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07004390 ASSERT_EQ(ToolType::MOUSE, args.pointerProperties[0].toolType);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004391 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004392 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.xPrecision);
4393 ASSERT_EQ(TRACKBALL_MOVEMENT_THRESHOLD, args.yPrecision);
4394 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
4395}
4396
4397TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentXYUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004398 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004399 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004400
4401 NotifyMotionArgs args;
4402
4403 // Motion in X but not Y.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004404 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4405 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4407 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004408 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4409 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f,
4410 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004411
4412 // Motion in Y but not X.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004413 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4414 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004415 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4416 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004417 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f,
4418 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004419}
4420
4421TEST_F(CursorInputMapperTest, Process_ShouldHandleIndependentButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004422 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004423 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004424
4425 NotifyMotionArgs args;
4426
4427 // Button press.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004428 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4429 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4431 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004432 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004433
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004434 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4435 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004436 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004437
Michael Wrightd02c5b62014-02-10 15:10:22 -08004438 // Button release.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004439 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4440 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004441 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004442 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004443 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004444
4445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004446 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004447 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004448}
4449
4450TEST_F(CursorInputMapperTest, Process_ShouldHandleCombinedXYAndButtonUpdates) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004451 addConfigurationProperty("cursor.mode", "navigation");
Arpit Singhe036ad72023-04-27 12:48:15 +00004452 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004453
4454 NotifyMotionArgs args;
4455
4456 // Combined X, Y and Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004457 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
4458 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, -2);
4459 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 1);
4460 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4462 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004463 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4464 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4465 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004466
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4468 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004469 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4470 1.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4471 -2.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004472
Michael Wrightd02c5b62014-02-10 15:10:22 -08004473 // Move X, Y a bit while pressed.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004474 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 2);
4475 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 1);
4476 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4478 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004479 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0],
4480 2.0f / TRACKBALL_MOVEMENT_THRESHOLD,
4481 1.0f / TRACKBALL_MOVEMENT_THRESHOLD, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004482
4483 // Release Button.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004484 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MOUSE, 0);
4485 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004486 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004487 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004488 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004489
4490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004491 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004492 ASSERT_NO_FATAL_FAILURE(assertCursorPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004493}
4494
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004495TEST_F(CursorInputMapperTest, Process_WhenOrientationAware_ShouldNotRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004496 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004497 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004498 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
4499 // need to be rotated.
4500 addConfigurationProperty("cursor.orientationAware", "1");
Arpit Singhe036ad72023-04-27 12:48:15 +00004501 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004502
Michael Wrighta9cf4192022-12-01 23:46:39 +00004503 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004504 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4505 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4506 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4507 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4508 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4509 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4510 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4511 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4512}
4513
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004514TEST_F(CursorInputMapperTest, Process_WhenNotOrientationAware_ShouldRotateMotions) {
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004515 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, DISPLAY_UNIQUE_ID);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004516 addConfigurationProperty("cursor.mode", "navigation");
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004517 // Since InputReader works in the un-rotated coordinate space, only devices that are not
4518 // orientation-aware are affected by display rotation.
Arpit Singhe036ad72023-04-27 12:48:15 +00004519 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004520
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004521 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004522 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004523 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, 1));
4524 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, 1));
4525 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 1, 0));
4526 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, -1));
4527 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, -1));
4528 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, -1));
4529 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, -1, 0));
4530 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, 1));
4531
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004532 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004533 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004534 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, -1, 0));
4535 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, 1));
4536 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, 1));
4537 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, 1, 1));
4538 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 1, 0));
4539 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, -1));
4540 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, -1));
4541 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, -1, -1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004542
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004543 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004544 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004545 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 0, -1));
4546 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, -1, -1));
4547 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, -1, 0));
4548 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, 1));
4549 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, 0, 1));
4550 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, 1, 1));
4551 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 1, 0));
4552 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, -1));
4553
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004554 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00004555 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07004556 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, 1, 1, 0));
4557 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 1, 1, -1));
4558 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, 0, 0, -1));
4559 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 1, -1, -1, -1));
4560 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, 0, -1, -1, 0));
4561 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, -1, -1, 1));
4562 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 0, 0, 1));
4563 ASSERT_NO_FATAL_FAILURE(testMotionRotation(mapper, -1, 1, 1, 1));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004564}
4565
4566TEST_F(CursorInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004567 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004568 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004569
4570 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4571 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004572
4573 NotifyMotionArgs motionArgs;
4574 NotifyKeyArgs keyArgs;
4575
4576 // press BTN_LEFT, release BTN_LEFT
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004577 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
4578 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004579 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4580 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4581 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004582 ASSERT_NO_FATAL_FAILURE(
4583 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004584
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004585 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4586 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4587 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004588 ASSERT_NO_FATAL_FAILURE(
4589 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004590
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
4592 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004594 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004595 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004596 ASSERT_NO_FATAL_FAILURE(
4597 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004598
4599 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004600 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004601 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004602 ASSERT_NO_FATAL_FAILURE(
4603 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004604
4605 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004606 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004607 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004608 ASSERT_NO_FATAL_FAILURE(
4609 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004610
4611 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004612 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
4613 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
4614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4616 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
4617 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004618 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004619 ASSERT_NO_FATAL_FAILURE(
4620 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004621
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4623 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4624 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004625 ASSERT_NO_FATAL_FAILURE(
4626 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004627
4628 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4629 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4630 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
Siarhei Vishniakou10ce1572023-03-15 09:15:21 -07004631 motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004632 ASSERT_NO_FATAL_FAILURE(
4633 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004634
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004635 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
4636 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004637 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004638 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004639 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004640 ASSERT_NO_FATAL_FAILURE(
4641 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004642
4643 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004644 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004645 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004646 ASSERT_NO_FATAL_FAILURE(
4647 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 1.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004648
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004649 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4650 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004652 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
4653 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004654 ASSERT_NO_FATAL_FAILURE(
4655 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004656 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
4657 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004658
4659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004660 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004661 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004662 ASSERT_NO_FATAL_FAILURE(
4663 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004664
Michael Wrightd02c5b62014-02-10 15:10:22 -08004665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4666 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004667 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004668 ASSERT_NO_FATAL_FAILURE(
4669 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004670
4671 // press BTN_BACK, release BTN_BACK
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004672 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
4673 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004674 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4675 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4676 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004677
Michael Wrightd02c5b62014-02-10 15:10:22 -08004678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004679 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004680 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004681 ASSERT_NO_FATAL_FAILURE(
4682 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004683
4684 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4685 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4686 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004687 ASSERT_NO_FATAL_FAILURE(
4688 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004689
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004690 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
4691 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004694 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004695 ASSERT_NO_FATAL_FAILURE(
4696 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004697
4698 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004699 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004700 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004701
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004702 ASSERT_NO_FATAL_FAILURE(
4703 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004704 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4705 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4706 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4707
4708 // press BTN_SIDE, release BTN_SIDE
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004709 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
4710 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004711 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4712 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4713 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004714
Michael Wrightd02c5b62014-02-10 15:10:22 -08004715 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004716 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004717 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004718 ASSERT_NO_FATAL_FAILURE(
4719 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004720
4721 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4722 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4723 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004724 ASSERT_NO_FATAL_FAILURE(
4725 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004726
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004727 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
4728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004729 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004730 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004731 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004732 ASSERT_NO_FATAL_FAILURE(
4733 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004734
4735 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4736 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4737 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004738 ASSERT_NO_FATAL_FAILURE(
4739 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004740
Michael Wrightd02c5b62014-02-10 15:10:22 -08004741 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4742 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4743 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
4744
4745 // press BTN_FORWARD, release BTN_FORWARD
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004746 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
4747 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4749 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4750 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004751
Michael Wrightd02c5b62014-02-10 15:10:22 -08004752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004753 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004754 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004755 ASSERT_NO_FATAL_FAILURE(
4756 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004757
4758 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4759 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4760 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004761 ASSERT_NO_FATAL_FAILURE(
4762 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004763
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004764 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
4765 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004766 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004767 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004768 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004769 ASSERT_NO_FATAL_FAILURE(
4770 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004771
4772 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4773 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4774 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004775 ASSERT_NO_FATAL_FAILURE(
4776 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004777
Michael Wrightd02c5b62014-02-10 15:10:22 -08004778 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4779 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4780 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4781
4782 // press BTN_EXTRA, release BTN_EXTRA
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004783 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
4784 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004785 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4786 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
4787 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004788
Michael Wrightd02c5b62014-02-10 15:10:22 -08004789 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004790 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004791 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004792 ASSERT_NO_FATAL_FAILURE(
4793 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004794
4795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4796 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
4797 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004798 ASSERT_NO_FATAL_FAILURE(
4799 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Michael Wrightd02c5b62014-02-10 15:10:22 -08004800
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004801 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
4802 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004804 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004805 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004806 ASSERT_NO_FATAL_FAILURE(
4807 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004808
4809 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
4810 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
4811 ASSERT_EQ(0, motionArgs.buttonState);
Prabir Pradhanf5334b82021-05-13 14:00:39 -07004812 ASSERT_NO_FATAL_FAILURE(
4813 assertCursorPointerCoords(motionArgs.pointerCoords[0], 100.0f, 200.0f, 0.0f));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08004814
Michael Wrightd02c5b62014-02-10 15:10:22 -08004815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
4816 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
4817 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
4818}
4819
4820TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerAround) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08004821 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004822 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08004823
4824 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
4825 mFakePointerController->setPosition(100, 200);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004826
4827 NotifyMotionArgs args;
4828
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004829 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4830 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4831 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08004832 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004833 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4834 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4835 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
4836 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004837 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Vladislav Kaznacheev78f97b32016-12-15 18:14:58 -08004838}
4839
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004840/**
4841 * When Pointer Capture is enabled, we expect to report unprocessed relative movements, so any
4842 * pointer acceleration or speed processing should not be applied.
4843 */
4844TEST_F(CursorInputMapperTest, PointerCaptureDisablesVelocityProcessing) {
4845 addConfigurationProperty("cursor.mode", "pointer");
Harry Cutts33476232023-01-30 19:57:29 +00004846 const VelocityControlParameters testParams(/*scale=*/5.f, /*low threshold=*/0.f,
4847 /*high threshold=*/100.f, /*acceleration=*/10.f);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004848 mFakePolicy->setVelocityControlParams(testParams);
Arpit Singhe036ad72023-04-27 12:48:15 +00004849 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004850
4851 NotifyDeviceResetArgs resetArgs;
4852 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4853 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4854 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4855
4856 NotifyMotionArgs args;
4857
4858 // Move and verify scale is applied.
4859 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4860 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4861 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4862 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4863 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4864 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4865 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
4866 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
4867 ASSERT_GT(relX, 10);
4868 ASSERT_GT(relY, 20);
4869
4870 // Enable Pointer Capture
4871 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004872 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhanf99d6e72022-04-21 15:28:35 +00004873 NotifyPointerCaptureChangedArgs captureArgs;
4874 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4875 ASSERT_TRUE(captureArgs.request.enable);
4876
4877 // Move and verify scale is not applied.
4878 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4879 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4880 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4881 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4882 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4883 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4884 ASSERT_EQ(10, args.pointerCoords[0].getX());
4885 ASSERT_EQ(20, args.pointerCoords[0].getY());
4886}
4887
Prabir Pradhan208360b2022-06-24 18:37:04 +00004888TEST_F(CursorInputMapperTest, PointerCaptureDisablesOrientationChanges) {
4889 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00004890 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan208360b2022-06-24 18:37:04 +00004891
4892 NotifyDeviceResetArgs resetArgs;
4893 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
4894 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
4895 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
4896
4897 // Ensure the display is rotated.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004898 prepareDisplay(ui::ROTATION_90);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004899
4900 NotifyMotionArgs args;
4901
4902 // Verify that the coordinates are rotated.
4903 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4904 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4905 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4907 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
4908 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
4909 ASSERT_EQ(-20, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X));
4910 ASSERT_EQ(10, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y));
4911
4912 // Enable Pointer Capture.
4913 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004914 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Prabir Pradhan208360b2022-06-24 18:37:04 +00004915 NotifyPointerCaptureChangedArgs captureArgs;
4916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
4917 ASSERT_TRUE(captureArgs.request.enable);
4918
4919 // Move and verify rotation is not applied.
4920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4922 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
4924 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
4925 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
4926 ASSERT_EQ(10, args.pointerCoords[0].getX());
4927 ASSERT_EQ(20, args.pointerCoords[0].getY());
4928}
4929
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004930TEST_F(CursorInputMapperTest, ConfigureDisplayId_NoAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004931 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004932
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004933 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004934 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004935
4936 // Set up the secondary display as the display on which the pointer should be shown.
4937 // The InputDevice is not associated with any display.
4938 prepareSecondaryDisplay();
4939 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004940 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tan888a6a42020-01-09 11:39:16 -08004941
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004942 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004943 mFakePointerController->setPosition(100, 200);
Arthur Hungc7ad2d02018-12-18 17:41:29 +08004944
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004945 // Ensure input events are generated for the secondary display.
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00004946 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4947 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4948 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004950 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4951 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4952 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004953 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004954}
4955
4956TEST_F(CursorInputMapperTest, ConfigureDisplayId_WithAssociatedViewport) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004957 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004958
4959 // Set up the default display.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004960 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004961
4962 // Set up the secondary display as the display on which the pointer should be shown,
4963 // and associate the InputDevice with the secondary display.
4964 prepareSecondaryDisplay();
4965 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
4966 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004967 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004968
4969 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
4970 mFakePointerController->setPosition(100, 200);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004971
4972 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4973 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4974 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan739dca42022-09-09 20:12:01 +00004976 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
4977 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
4978 WithCoords(110.0f, 220.0f))));
Harry Cuttsce86cc32022-12-14 20:36:33 +00004979 ASSERT_NO_FATAL_FAILURE(mFakePointerController->assertPosition(110.0f, 220.0f));
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004980}
4981
4982TEST_F(CursorInputMapperTest, ConfigureDisplayId_IgnoresEventsForMismatchedPointerDisplay) {
Arpit Singhe036ad72023-04-27 12:48:15 +00004983 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004984
4985 // Set up the default display as the display on which the pointer should be shown.
Michael Wrighta9cf4192022-12-01 23:46:39 +00004986 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004987 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
4988
4989 // Associate the InputDevice with the secondary display.
4990 prepareSecondaryDisplay();
4991 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00004992 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc13ff082022-09-08 22:03:30 +00004993
4994 // The mapper should not generate any events because it is associated with a display that is
4995 // different from the pointer display.
4996 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
4997 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
4998 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
4999 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Arthur Hungc7ad2d02018-12-18 17:41:29 +08005000}
5001
Byoungho Jungda10dd32023-10-06 17:03:45 +09005002// --- CursorInputMapperTestWithChoreographer ---
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005003
Prabir Pradhancc7268a2023-11-16 18:54:13 +00005004// TODO(b/311416205): De-duplicate the test cases after the refactoring is complete and the flagging
5005// logic can be removed.
Byoungho Jungda10dd32023-10-06 17:03:45 +09005006class CursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005007protected:
5008 void SetUp() override {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005009 input_flags::enable_pointer_choreographer(true);
5010 CursorInputMapperTestBase::SetUp();
5011 }
5012};
5013
Prabir Pradhan19767602023-11-03 16:53:31 +00005014TEST_F(CursorInputMapperTestWithChoreographer, PopulateDeviceInfoReturnsRangeFromPolicy) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005015 addConfigurationProperty("cursor.mode", "pointer");
5016 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5017
5018 InputDeviceInfo info;
5019 mapper.populateDeviceInfo(info);
5020
5021 // Initially there may not be a valid motion range.
5022 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE));
5023 ASSERT_EQ(nullptr, info.getMotionRange(AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE));
5024 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info, AINPUT_MOTION_RANGE_PRESSURE,
5025 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
5026
5027 // When the viewport and the default pointer display ID is set, then there should be a valid
5028 // motion range.
5029 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5030 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
5031 /*isActive=*/true, "local:0", NO_PORT, ViewportType::INTERNAL);
5032 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5033
5034 InputDeviceInfo info2;
5035 mapper.populateDeviceInfo(info2);
5036
5037 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_X, AINPUT_SOURCE_MOUSE, 0,
5038 DISPLAY_WIDTH - 1, 0.0f, 0.0f));
5039 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_Y, AINPUT_SOURCE_MOUSE, 0,
5040 DISPLAY_HEIGHT - 1, 0.0f, 0.0f));
5041 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info2, AINPUT_MOTION_RANGE_PRESSURE,
5042 AINPUT_SOURCE_MOUSE, 0.0f, 1.0f, 0.0f, 0.0f));
5043}
5044
Prabir Pradhan19767602023-11-03 16:53:31 +00005045TEST_F(CursorInputMapperTestWithChoreographer, ProcessShouldHandleAllButtonsWithZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005046 addConfigurationProperty("cursor.mode", "pointer");
5047 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5048
5049 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5050 prepareDisplay(ui::ROTATION_0);
5051
5052 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5053 mFakePointerController->setPosition(100, 200);
5054
5055 NotifyMotionArgs motionArgs;
5056 NotifyKeyArgs keyArgs;
5057
5058 // press BTN_LEFT, release BTN_LEFT
5059 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 1);
5060 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5061 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5062 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5063 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5064 ASSERT_NO_FATAL_FAILURE(
5065 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5066
5067 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5068 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5069 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
5070 ASSERT_NO_FATAL_FAILURE(
5071 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5072
5073 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_LEFT, 0);
5074 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5076 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5077 ASSERT_EQ(0, motionArgs.buttonState);
5078 ASSERT_NO_FATAL_FAILURE(
5079 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5080
5081 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5082 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5083 ASSERT_EQ(0, motionArgs.buttonState);
5084 ASSERT_NO_FATAL_FAILURE(
5085 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5086
5087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5088 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5089 ASSERT_EQ(0, motionArgs.buttonState);
5090 ASSERT_NO_FATAL_FAILURE(
5091 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5092
5093 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
5094 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 1);
5095 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 1);
5096 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5098 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
5099 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5100 motionArgs.buttonState);
5101 ASSERT_NO_FATAL_FAILURE(
5102 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5103
5104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5105 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5106 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5107 ASSERT_NO_FATAL_FAILURE(
5108 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5109
5110 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5111 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5112 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
5113 motionArgs.buttonState);
5114 ASSERT_NO_FATAL_FAILURE(
5115 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5116
5117 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_RIGHT, 0);
5118 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5119 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5120 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5121 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5122 ASSERT_NO_FATAL_FAILURE(
5123 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5124
5125 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5126 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
5127 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
5128 ASSERT_NO_FATAL_FAILURE(
5129 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 1.0f));
5130
5131 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5132 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5133 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5134 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5135 ASSERT_EQ(0, motionArgs.buttonState);
5136 ASSERT_NO_FATAL_FAILURE(
5137 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5138 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_MIDDLE, 0);
5139 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5140
5141 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5142 ASSERT_EQ(0, motionArgs.buttonState);
5143 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
5144 ASSERT_NO_FATAL_FAILURE(
5145 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5146
5147 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5148 ASSERT_EQ(0, motionArgs.buttonState);
5149 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5150 ASSERT_NO_FATAL_FAILURE(
5151 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5152
5153 // press BTN_BACK, release BTN_BACK
5154 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 1);
5155 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5156 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5157 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5158 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5159
5160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5162 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5163 ASSERT_NO_FATAL_FAILURE(
5164 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5165
5166 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5167 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5168 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5169 ASSERT_NO_FATAL_FAILURE(
5170 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5171
5172 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_BACK, 0);
5173 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5174 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5175 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5176 ASSERT_EQ(0, motionArgs.buttonState);
5177 ASSERT_NO_FATAL_FAILURE(
5178 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5179
5180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5181 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5182 ASSERT_EQ(0, motionArgs.buttonState);
5183
5184 ASSERT_NO_FATAL_FAILURE(
5185 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5186 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5187 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5188 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5189
5190 // press BTN_SIDE, release BTN_SIDE
5191 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 1);
5192 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5193 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5194 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5195 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5196
5197 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5198 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5199 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5200 ASSERT_NO_FATAL_FAILURE(
5201 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5202
5203 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5204 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5205 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
5206 ASSERT_NO_FATAL_FAILURE(
5207 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5208
5209 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_SIDE, 0);
5210 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5212 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5213 ASSERT_EQ(0, motionArgs.buttonState);
5214 ASSERT_NO_FATAL_FAILURE(
5215 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5216
5217 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5218 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5219 ASSERT_EQ(0, motionArgs.buttonState);
5220 ASSERT_NO_FATAL_FAILURE(
5221 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5222
5223 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5224 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5225 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
5226
5227 // press BTN_FORWARD, release BTN_FORWARD
5228 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 1);
5229 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5230 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5231 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5232 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5233
5234 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5235 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5236 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5237 ASSERT_NO_FATAL_FAILURE(
5238 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5239
5240 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5241 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5242 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5243 ASSERT_NO_FATAL_FAILURE(
5244 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5245
5246 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_FORWARD, 0);
5247 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5248 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5249 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5250 ASSERT_EQ(0, motionArgs.buttonState);
5251 ASSERT_NO_FATAL_FAILURE(
5252 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5253
5254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5255 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5256 ASSERT_EQ(0, motionArgs.buttonState);
5257 ASSERT_NO_FATAL_FAILURE(
5258 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5259
5260 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5261 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5262 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5263
5264 // press BTN_EXTRA, release BTN_EXTRA
5265 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 1);
5266 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5267 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5268 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
5269 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5270
5271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5273 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5274 ASSERT_NO_FATAL_FAILURE(
5275 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5276
5277 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5278 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
5279 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
5280 ASSERT_NO_FATAL_FAILURE(
5281 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5282
5283 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_EXTRA, 0);
5284 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5286 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
5287 ASSERT_EQ(0, motionArgs.buttonState);
5288 ASSERT_NO_FATAL_FAILURE(
5289 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5290
5291 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
5292 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
5293 ASSERT_EQ(0, motionArgs.buttonState);
5294 ASSERT_NO_FATAL_FAILURE(
5295 assertCursorPointerCoords(motionArgs.pointerCoords[0], 0.0f, 0.0f, 0.0f));
5296
5297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
5298 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
5299 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
5300}
5301
Prabir Pradhan19767602023-11-03 16:53:31 +00005302TEST_F(CursorInputMapperTestWithChoreographer, ProcessWhenModeIsPointerShouldKeepZeroCoords) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005303 addConfigurationProperty("cursor.mode", "pointer");
5304 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5305
5306 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5307 prepareDisplay(ui::ROTATION_0);
5308
5309 mFakePointerController->setBounds(0, 0, 800 - 1, 480 - 1);
5310 mFakePointerController->setPosition(100, 200);
5311
5312 NotifyMotionArgs args;
5313
5314 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5315 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5316 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5317 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5318 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5319 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5320 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 0.0f, 0.0f, 0.0f, 0.0f, 0.0f,
5321 0.0f, 0.0f, 0.0f, 0.0f, 0.0f));
5322}
5323
5324TEST_F(CursorInputMapperTestWithChoreographer, PointerCaptureDisablesVelocityProcessing) {
5325 addConfigurationProperty("cursor.mode", "pointer");
Prabir Pradhan19767602023-11-03 16:53:31 +00005326 const VelocityControlParameters testParams(/*scale=*/5.f, /*lowThreshold=*/0.f,
5327 /*highThreshold=*/100.f, /*acceleration=*/10.f);
Byoungho Jungda10dd32023-10-06 17:03:45 +09005328 mFakePolicy->setVelocityControlParams(testParams);
5329 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5330
5331 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5332 prepareDisplay(ui::ROTATION_0);
5333
5334 NotifyDeviceResetArgs resetArgs;
5335 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
5336 ASSERT_EQ(ARBITRARY_TIME, resetArgs.eventTime);
5337 ASSERT_EQ(DEVICE_ID, resetArgs.deviceId);
5338
5339 NotifyMotionArgs args;
5340
5341 // Move and verify scale is applied.
5342 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5343 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5344 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5345 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5346 ASSERT_EQ(AINPUT_SOURCE_MOUSE, args.source);
5347 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
5348 const float relX = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5349 const float relY = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5350 ASSERT_GT(relX, 10);
5351 ASSERT_GT(relY, 20);
5352
5353 // Enable Pointer Capture
5354 mFakePolicy->setPointerCapture(true);
5355 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
5356 NotifyPointerCaptureChangedArgs captureArgs;
5357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyCaptureWasCalled(&captureArgs));
5358 ASSERT_TRUE(captureArgs.request.enable);
5359
5360 // Move and verify scale is not applied.
5361 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5362 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5363 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5364 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
5365 ASSERT_EQ(AINPUT_SOURCE_MOUSE_RELATIVE, args.source);
5366 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
5367 const float relX2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X);
5368 const float relY2 = args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y);
5369 ASSERT_EQ(10, relX2);
5370 ASSERT_EQ(20, relY2);
5371}
5372
Prabir Pradhan19767602023-11-03 16:53:31 +00005373TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdNoAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005374 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5375
5376 // Set up the default display.
5377 prepareDisplay(ui::ROTATION_90);
5378
5379 // Set up the secondary display as the display on which the pointer should be shown.
5380 // The InputDevice is not associated with any display.
5381 prepareSecondaryDisplay();
5382 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5383 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5384
5385 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5386 mFakePointerController->setPosition(100, 200);
5387
5388 // Ensure input events are generated without display ID and coords,
5389 // because they will be decided later by PointerChoreographer.
5390 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5391 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5392 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5394 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5395 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(ADISPLAY_ID_NONE),
5396 WithCoords(0.0f, 0.0f))));
5397}
5398
Prabir Pradhan19767602023-11-03 16:53:31 +00005399TEST_F(CursorInputMapperTestWithChoreographer, ConfigureDisplayIdWithAssociatedViewport) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005400 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5401
5402 // Set up the default display.
5403 prepareDisplay(ui::ROTATION_90);
5404
5405 // Set up the secondary display as the display on which the pointer should be shown,
5406 // and associate the InputDevice with the secondary display.
5407 prepareSecondaryDisplay();
5408 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
5409 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5410 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5411
5412 mFakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
5413 mFakePointerController->setPosition(100, 200);
5414
5415 // Ensure input events are generated with associated display ID but not with coords,
5416 // because the coords will be decided later by PointerChoreographer.
5417 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5418 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5419 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5421 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5422 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5423 WithCoords(0.0f, 0.0f))));
5424}
5425
5426TEST_F(CursorInputMapperTestWithChoreographer,
Prabir Pradhan19767602023-11-03 16:53:31 +00005427 ConfigureDisplayIdShouldGenerateEventWithMismatchedPointerDisplay) {
Byoungho Jungda10dd32023-10-06 17:03:45 +09005428 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5429
5430 // Set up the default display as the display on which the pointer should be shown.
5431 prepareDisplay(ui::ROTATION_90);
5432 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5433
5434 // Associate the InputDevice with the secondary display.
5435 prepareSecondaryDisplay();
5436 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, SECONDARY_DISPLAY_UNIQUE_ID);
5437 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5438
5439 // With PointerChoreographer enabled, there could be a PointerController for the associated
5440 // display even if it is different from the pointer display. So the mapper should generate an
5441 // event.
5442 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 10);
5443 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_Y, 20);
5444 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5445 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5446 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5447 WithSource(AINPUT_SOURCE_MOUSE), WithDisplayId(SECONDARY_DISPLAY_ID),
5448 WithCoords(0.0f, 0.0f))));
5449}
5450
5451// --- BluetoothCursorInputMapperTest ---
5452
5453class BluetoothCursorInputMapperTest : public CursorInputMapperTestBase {
5454protected:
5455 void SetUp() override {
5456 input_flags::enable_pointer_choreographer(false);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005457 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5458
5459 mFakePointerController = std::make_shared<FakePointerController>();
5460 mFakePolicy->setPointerController(mFakePointerController);
5461 }
5462};
5463
5464TEST_F(BluetoothCursorInputMapperTest, TimestampSmoothening) {
5465 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005466 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005467
5468 nsecs_t kernelEventTime = ARBITRARY_TIME;
5469 nsecs_t expectedEventTime = ARBITRARY_TIME;
5470 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5471 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5472 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5473 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5474 WithEventTime(expectedEventTime))));
5475
5476 // Process several events that come in quick succession, according to their timestamps.
5477 for (int i = 0; i < 3; i++) {
5478 constexpr static nsecs_t delta = ms2ns(1);
5479 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5480 kernelEventTime += delta;
5481 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5482
5483 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5484 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5485 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5486 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5487 WithEventTime(expectedEventTime))));
5488 }
5489}
5490
5491TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningIsCapped) {
5492 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005493 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005494
5495 nsecs_t expectedEventTime = ARBITRARY_TIME;
5496 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5497 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5499 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5500 WithEventTime(expectedEventTime))));
5501
5502 // Process several events with the same timestamp from the kernel.
5503 // Ensure that we do not generate events too far into the future.
5504 constexpr static int32_t numEvents =
5505 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5506 for (int i = 0; i < numEvents; i++) {
5507 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5508
5509 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5510 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5511 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5512 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5513 WithEventTime(expectedEventTime))));
5514 }
5515
5516 // By processing more events with the same timestamp, we should not generate events with a
5517 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5518 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5519 for (int i = 0; i < 3; i++) {
5520 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5521 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5523 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5524 WithEventTime(cappedEventTime))));
5525 }
5526}
5527
5528TEST_F(BluetoothCursorInputMapperTest, TimestampSmootheningNotUsed) {
5529 addConfigurationProperty("cursor.mode", "pointer");
Arpit Singhe036ad72023-04-27 12:48:15 +00005530 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00005531
5532 nsecs_t kernelEventTime = ARBITRARY_TIME;
5533 nsecs_t expectedEventTime = ARBITRARY_TIME;
5534 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5535 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5537 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5538 WithEventTime(expectedEventTime))));
5539
5540 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5541 // smoothening is not needed, its timestamp is not affected.
5542 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5543 expectedEventTime = kernelEventTime;
5544
5545 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5546 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5548 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5549 WithEventTime(expectedEventTime))));
5550}
5551
Byoungho Jungda10dd32023-10-06 17:03:45 +09005552// --- BluetoothCursorInputMapperTestWithChoreographer ---
5553
5554class BluetoothCursorInputMapperTestWithChoreographer : public CursorInputMapperTestBase {
5555protected:
5556 void SetUp() override {
5557 input_flags::enable_pointer_choreographer(true);
5558 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
5559
5560 mFakePointerController = std::make_shared<FakePointerController>();
5561 mFakePolicy->setPointerController(mFakePointerController);
5562 }
5563};
5564
5565TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmoothening) {
5566 addConfigurationProperty("cursor.mode", "pointer");
5567 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5568
5569 // Set up the default display.
5570 prepareDisplay(ui::ROTATION_0);
5571 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5572 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5573
5574 nsecs_t kernelEventTime = ARBITRARY_TIME;
5575 nsecs_t expectedEventTime = ARBITRARY_TIME;
5576 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5577 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5579 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5580 WithEventTime(expectedEventTime))));
5581
5582 // Process several events that come in quick succession, according to their timestamps.
5583 for (int i = 0; i < 3; i++) {
5584 constexpr static nsecs_t delta = ms2ns(1);
5585 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
5586 kernelEventTime += delta;
5587 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5588
5589 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5590 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5592 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5593 WithEventTime(expectedEventTime))));
5594 }
5595}
5596
5597TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningIsCapped) {
5598 addConfigurationProperty("cursor.mode", "pointer");
5599 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5600
5601 // Set up the default display.
5602 prepareDisplay(ui::ROTATION_0);
5603 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5604 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5605
5606 nsecs_t expectedEventTime = ARBITRARY_TIME;
5607 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5608 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5610 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5611 WithEventTime(expectedEventTime))));
5612
5613 // Process several events with the same timestamp from the kernel.
5614 // Ensure that we do not generate events too far into the future.
5615 constexpr static int32_t numEvents =
5616 MAX_BLUETOOTH_SMOOTHING_DELTA / MIN_BLUETOOTH_TIMESTAMP_DELTA;
5617 for (int i = 0; i < numEvents; i++) {
5618 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
5619
5620 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5621 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5623 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5624 WithEventTime(expectedEventTime))));
5625 }
5626
5627 // By processing more events with the same timestamp, we should not generate events with a
5628 // timestamp that is more than the specified max time delta from the timestamp at its injection.
5629 const nsecs_t cappedEventTime = ARBITRARY_TIME + MAX_BLUETOOTH_SMOOTHING_DELTA;
5630 for (int i = 0; i < 3; i++) {
5631 process(mapper, ARBITRARY_TIME, READ_TIME, EV_REL, REL_X, 1);
5632 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
5633 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5634 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5635 WithEventTime(cappedEventTime))));
5636 }
5637}
5638
5639TEST_F(BluetoothCursorInputMapperTestWithChoreographer, TimestampSmootheningNotUsed) {
5640 addConfigurationProperty("cursor.mode", "pointer");
5641 CursorInputMapper& mapper = constructAndAddMapper<CursorInputMapper>();
5642
5643 // Set up the default display.
5644 prepareDisplay(ui::ROTATION_0);
5645 mFakePolicy->setDefaultPointerDisplayId(DISPLAY_ID);
5646 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
5647
5648 nsecs_t kernelEventTime = ARBITRARY_TIME;
5649 nsecs_t expectedEventTime = ARBITRARY_TIME;
5650 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5651 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5653 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5654 WithEventTime(expectedEventTime))));
5655
5656 // If the next event has a timestamp that is sufficiently spaced out so that Bluetooth timestamp
5657 // smoothening is not needed, its timestamp is not affected.
5658 kernelEventTime += MAX_BLUETOOTH_SMOOTHING_DELTA + ms2ns(1);
5659 expectedEventTime = kernelEventTime;
5660
5661 process(mapper, kernelEventTime, READ_TIME, EV_REL, REL_X, 1);
5662 process(mapper, kernelEventTime, READ_TIME, EV_SYN, SYN_REPORT, 0);
5663 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
5664 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE),
5665 WithEventTime(expectedEventTime))));
5666}
5667
Michael Wrightd02c5b62014-02-10 15:10:22 -08005668// --- TouchInputMapperTest ---
5669
5670class TouchInputMapperTest : public InputMapperTest {
5671protected:
5672 static const int32_t RAW_X_MIN;
5673 static const int32_t RAW_X_MAX;
5674 static const int32_t RAW_Y_MIN;
5675 static const int32_t RAW_Y_MAX;
5676 static const int32_t RAW_TOUCH_MIN;
5677 static const int32_t RAW_TOUCH_MAX;
5678 static const int32_t RAW_TOOL_MIN;
5679 static const int32_t RAW_TOOL_MAX;
5680 static const int32_t RAW_PRESSURE_MIN;
5681 static const int32_t RAW_PRESSURE_MAX;
5682 static const int32_t RAW_ORIENTATION_MIN;
5683 static const int32_t RAW_ORIENTATION_MAX;
5684 static const int32_t RAW_DISTANCE_MIN;
5685 static const int32_t RAW_DISTANCE_MAX;
5686 static const int32_t RAW_TILT_MIN;
5687 static const int32_t RAW_TILT_MAX;
5688 static const int32_t RAW_ID_MIN;
5689 static const int32_t RAW_ID_MAX;
5690 static const int32_t RAW_SLOT_MIN;
5691 static const int32_t RAW_SLOT_MAX;
5692 static const float X_PRECISION;
5693 static const float Y_PRECISION;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005694 static const float X_PRECISION_VIRTUAL;
5695 static const float Y_PRECISION_VIRTUAL;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005696
5697 static const float GEOMETRIC_SCALE;
Jason Gerecke489fda82012-09-07 17:19:40 -07005698 static const TouchAffineTransformation AFFINE_TRANSFORM;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005699
5700 static const VirtualKeyDefinition VIRTUAL_KEYS[2];
5701
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005702 const std::string UNIQUE_ID = "local:0";
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005703 const std::string SECONDARY_UNIQUE_ID = "local:1";
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07005704
Michael Wrightd02c5b62014-02-10 15:10:22 -08005705 enum Axes {
5706 POSITION = 1 << 0,
5707 TOUCH = 1 << 1,
5708 TOOL = 1 << 2,
5709 PRESSURE = 1 << 3,
5710 ORIENTATION = 1 << 4,
5711 MINOR = 1 << 5,
5712 ID = 1 << 6,
5713 DISTANCE = 1 << 7,
5714 TILT = 1 << 8,
5715 SLOT = 1 << 9,
5716 TOOL_TYPE = 1 << 10,
5717 };
5718
Michael Wrighta9cf4192022-12-01 23:46:39 +00005719 void prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port = NO_PORT);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005720 void prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port = NO_PORT);
Michael Wrighta9cf4192022-12-01 23:46:39 +00005721 void prepareVirtualDisplay(ui::Rotation orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005722 void prepareVirtualKeys();
Jason Gerecke489fda82012-09-07 17:19:40 -07005723 void prepareLocationCalibration();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005724 int32_t toRawX(float displayX);
5725 int32_t toRawY(float displayY);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005726 int32_t toRotatedRawX(float displayX);
5727 int32_t toRotatedRawY(float displayY);
Jason Gerecke489fda82012-09-07 17:19:40 -07005728 float toCookedX(float rawX, float rawY);
5729 float toCookedY(float rawX, float rawY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005730 float toDisplayX(int32_t rawX);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005731 float toDisplayX(int32_t rawX, int32_t displayWidth);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005732 float toDisplayY(int32_t rawY);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005733 float toDisplayY(int32_t rawY, int32_t displayHeight);
5734
Michael Wrightd02c5b62014-02-10 15:10:22 -08005735};
5736
5737const int32_t TouchInputMapperTest::RAW_X_MIN = 25;
5738const int32_t TouchInputMapperTest::RAW_X_MAX = 1019;
5739const int32_t TouchInputMapperTest::RAW_Y_MIN = 30;
5740const int32_t TouchInputMapperTest::RAW_Y_MAX = 1009;
5741const int32_t TouchInputMapperTest::RAW_TOUCH_MIN = 0;
5742const int32_t TouchInputMapperTest::RAW_TOUCH_MAX = 31;
5743const int32_t TouchInputMapperTest::RAW_TOOL_MIN = 0;
5744const int32_t TouchInputMapperTest::RAW_TOOL_MAX = 15;
Michael Wrightaa449c92017-12-13 21:21:43 +00005745const int32_t TouchInputMapperTest::RAW_PRESSURE_MIN = 0;
5746const int32_t TouchInputMapperTest::RAW_PRESSURE_MAX = 255;
Michael Wrightd02c5b62014-02-10 15:10:22 -08005747const int32_t TouchInputMapperTest::RAW_ORIENTATION_MIN = -7;
5748const int32_t TouchInputMapperTest::RAW_ORIENTATION_MAX = 7;
5749const int32_t TouchInputMapperTest::RAW_DISTANCE_MIN = 0;
5750const int32_t TouchInputMapperTest::RAW_DISTANCE_MAX = 7;
5751const int32_t TouchInputMapperTest::RAW_TILT_MIN = 0;
5752const int32_t TouchInputMapperTest::RAW_TILT_MAX = 150;
5753const int32_t TouchInputMapperTest::RAW_ID_MIN = 0;
5754const int32_t TouchInputMapperTest::RAW_ID_MAX = 9;
5755const int32_t TouchInputMapperTest::RAW_SLOT_MIN = 0;
5756const int32_t TouchInputMapperTest::RAW_SLOT_MAX = 9;
5757const float TouchInputMapperTest::X_PRECISION = float(RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH;
5758const float TouchInputMapperTest::Y_PRECISION = float(RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT;
Santos Cordonfa5cf462017-04-05 10:37:00 -07005759const float TouchInputMapperTest::X_PRECISION_VIRTUAL =
5760 float(RAW_X_MAX - RAW_X_MIN + 1) / VIRTUAL_DISPLAY_WIDTH;
5761const float TouchInputMapperTest::Y_PRECISION_VIRTUAL =
5762 float(RAW_Y_MAX - RAW_Y_MIN + 1) / VIRTUAL_DISPLAY_HEIGHT;
Jason Gerecke489fda82012-09-07 17:19:40 -07005763const TouchAffineTransformation TouchInputMapperTest::AFFINE_TRANSFORM =
5764 TouchAffineTransformation(1, -2, 3, -4, 5, -6);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005765
5766const float TouchInputMapperTest::GEOMETRIC_SCALE =
5767 avg(float(DISPLAY_WIDTH) / (RAW_X_MAX - RAW_X_MIN + 1),
5768 float(DISPLAY_HEIGHT) / (RAW_Y_MAX - RAW_Y_MIN + 1));
5769
5770const VirtualKeyDefinition TouchInputMapperTest::VIRTUAL_KEYS[2] = {
5771 { KEY_HOME, 60, DISPLAY_HEIGHT + 15, 20, 20 },
5772 { KEY_MENU, DISPLAY_HEIGHT - 60, DISPLAY_WIDTH + 15, 20, 20 },
5773};
5774
Michael Wrighta9cf4192022-12-01 23:46:39 +00005775void TouchInputMapperTest::prepareDisplay(ui::Rotation orientation, std::optional<uint8_t> port) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005776 setDisplayInfoAndReconfigure(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, orientation, UNIQUE_ID,
5777 port, ViewportType::INTERNAL);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -07005778}
5779
5780void TouchInputMapperTest::prepareSecondaryDisplay(ViewportType type, std::optional<uint8_t> port) {
5781 setDisplayInfoAndReconfigure(SECONDARY_DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT,
Michael Wrighta9cf4192022-12-01 23:46:39 +00005782 ui::ROTATION_0, SECONDARY_UNIQUE_ID, port, type);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005783}
5784
Michael Wrighta9cf4192022-12-01 23:46:39 +00005785void TouchInputMapperTest::prepareVirtualDisplay(ui::Rotation orientation) {
Michael Wrightfe3de7d2020-07-02 19:05:30 +01005786 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH, VIRTUAL_DISPLAY_HEIGHT,
5787 orientation, VIRTUAL_DISPLAY_UNIQUE_ID, NO_PORT,
5788 ViewportType::VIRTUAL);
Santos Cordonfa5cf462017-04-05 10:37:00 -07005789}
5790
Michael Wrightd02c5b62014-02-10 15:10:22 -08005791void TouchInputMapperTest::prepareVirtualKeys() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005792 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[0]);
5793 mFakeEventHub->addVirtualKeyDefinition(EVENTHUB_ID, VIRTUAL_KEYS[1]);
5794 mFakeEventHub->addKey(EVENTHUB_ID, KEY_HOME, 0, AKEYCODE_HOME, POLICY_FLAG_WAKE);
5795 mFakeEventHub->addKey(EVENTHUB_ID, KEY_MENU, 0, AKEYCODE_MENU, POLICY_FLAG_WAKE);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005796}
5797
Jason Gerecke489fda82012-09-07 17:19:40 -07005798void TouchInputMapperTest::prepareLocationCalibration() {
5799 mFakePolicy->setTouchAffineTransformation(AFFINE_TRANSFORM);
5800}
5801
Michael Wrightd02c5b62014-02-10 15:10:22 -08005802int32_t TouchInputMapperTest::toRawX(float displayX) {
5803 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_WIDTH + RAW_X_MIN);
5804}
5805
5806int32_t TouchInputMapperTest::toRawY(float displayY) {
5807 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_HEIGHT + RAW_Y_MIN);
5808}
5809
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07005810int32_t TouchInputMapperTest::toRotatedRawX(float displayX) {
5811 return int32_t(displayX * (RAW_X_MAX - RAW_X_MIN + 1) / DISPLAY_HEIGHT + RAW_X_MIN);
5812}
5813
5814int32_t TouchInputMapperTest::toRotatedRawY(float displayY) {
5815 return int32_t(displayY * (RAW_Y_MAX - RAW_Y_MIN + 1) / DISPLAY_WIDTH + RAW_Y_MIN);
5816}
5817
Jason Gerecke489fda82012-09-07 17:19:40 -07005818float TouchInputMapperTest::toCookedX(float rawX, float rawY) {
5819 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5820 return rawX;
5821}
5822
5823float TouchInputMapperTest::toCookedY(float rawX, float rawY) {
5824 AFFINE_TRANSFORM.applyTo(rawX, rawY);
5825 return rawY;
5826}
5827
Michael Wrightd02c5b62014-02-10 15:10:22 -08005828float TouchInputMapperTest::toDisplayX(int32_t rawX) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005829 return toDisplayX(rawX, DISPLAY_WIDTH);
5830}
5831
5832float TouchInputMapperTest::toDisplayX(int32_t rawX, int32_t displayWidth) {
5833 return float(rawX - RAW_X_MIN) * displayWidth / (RAW_X_MAX - RAW_X_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005834}
5835
5836float TouchInputMapperTest::toDisplayY(int32_t rawY) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07005837 return toDisplayY(rawY, DISPLAY_HEIGHT);
5838}
5839
5840float TouchInputMapperTest::toDisplayY(int32_t rawY, int32_t displayHeight) {
5841 return float(rawY - RAW_Y_MIN) * displayHeight / (RAW_Y_MAX - RAW_Y_MIN + 1);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005842}
5843
5844
5845// --- SingleTouchInputMapperTest ---
5846
5847class SingleTouchInputMapperTest : public TouchInputMapperTest {
5848protected:
5849 void prepareButtons();
5850 void prepareAxes(int axes);
5851
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005852 void processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5853 void processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y);
5854 void processUp(SingleTouchInputMapper& mappery);
5855 void processPressure(SingleTouchInputMapper& mapper, int32_t pressure);
5856 void processToolMajor(SingleTouchInputMapper& mapper, int32_t toolMajor);
5857 void processDistance(SingleTouchInputMapper& mapper, int32_t distance);
5858 void processTilt(SingleTouchInputMapper& mapper, int32_t tiltX, int32_t tiltY);
5859 void processKey(SingleTouchInputMapper& mapper, int32_t code, int32_t value);
5860 void processSync(SingleTouchInputMapper& mapper);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005861};
5862
5863void SingleTouchInputMapperTest::prepareButtons() {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005864 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005865}
5866
5867void SingleTouchInputMapperTest::prepareAxes(int axes) {
5868 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005869 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
5870 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005871 }
5872 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005873 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MIN,
5874 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005875 }
5876 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005877 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TOOL_WIDTH, RAW_TOOL_MIN, RAW_TOOL_MAX, 0,
5878 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005879 }
5880 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005881 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_DISTANCE, RAW_DISTANCE_MIN,
5882 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005883 }
5884 if (axes & TILT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08005885 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_X, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
5886 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_TILT_Y, RAW_TILT_MIN, RAW_TILT_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005887 }
5888}
5889
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005890void SingleTouchInputMapperTest::processDown(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005891 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
5892 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5893 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005894}
5895
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005896void SingleTouchInputMapperTest::processMove(SingleTouchInputMapper& mapper, int32_t x, int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005897 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_X, x);
5898 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005899}
5900
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005901void SingleTouchInputMapperTest::processUp(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005902 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005903}
5904
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005905void SingleTouchInputMapperTest::processPressure(SingleTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005906 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005907}
5908
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005909void SingleTouchInputMapperTest::processToolMajor(SingleTouchInputMapper& mapper,
5910 int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005911 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TOOL_WIDTH, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005912}
5913
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005914void SingleTouchInputMapperTest::processDistance(SingleTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005915 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005916}
5917
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005918void SingleTouchInputMapperTest::processTilt(SingleTouchInputMapper& mapper, int32_t tiltX,
5919 int32_t tiltY) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005920 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_X, tiltX);
5921 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_TILT_Y, tiltY);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005922}
5923
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005924void SingleTouchInputMapperTest::processKey(SingleTouchInputMapper& mapper, int32_t code,
5925 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005926 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005927}
5928
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005929void SingleTouchInputMapperTest::processSync(SingleTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00005930 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005931}
5932
Michael Wrightd02c5b62014-02-10 15:10:22 -08005933TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsNotSpecifiedAndNotACursor_ReturnsPointer) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005934 prepareButtons();
5935 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00005936 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005937
Josep del Río2d8c79a2023-01-23 19:33:50 +00005938 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005939}
5940
Michael Wrightd02c5b62014-02-10 15:10:22 -08005941TEST_F(SingleTouchInputMapperTest, GetSources_WhenDeviceTypeIsTouchScreen_ReturnsTouchScreen) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005942 prepareButtons();
5943 prepareAxes(POSITION);
5944 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +00005945 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005946
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005947 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
Michael Wrightd02c5b62014-02-10 15:10:22 -08005948}
5949
5950TEST_F(SingleTouchInputMapperTest, GetKeyCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005951 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005952 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005953 prepareButtons();
5954 prepareAxes(POSITION);
5955 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005956 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005957
5958 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005959 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005960
5961 // Virtual key is down.
5962 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5963 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5964 processDown(mapper, x, y);
5965 processSync(mapper);
5966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5967
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005968 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005969
5970 // Virtual key is up.
5971 processUp(mapper);
5972 processSync(mapper);
5973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5974
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005975 ASSERT_EQ(AKEY_STATE_UP, mapper.getKeyCodeState(AINPUT_SOURCE_ANY, AKEYCODE_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005976}
5977
5978TEST_F(SingleTouchInputMapperTest, GetScanCodeState) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08005979 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00005980 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08005981 prepareButtons();
5982 prepareAxes(POSITION);
5983 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00005984 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08005985
5986 // Unknown key.
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005987 ASSERT_EQ(AKEY_STATE_UNKNOWN, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_A));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005988
5989 // Virtual key is down.
5990 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
5991 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
5992 processDown(mapper, x, y);
5993 processSync(mapper);
5994 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
5995
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08005996 ASSERT_EQ(AKEY_STATE_VIRTUAL, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08005997
5998 // Virtual key is up.
5999 processUp(mapper);
6000 processSync(mapper);
6001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled());
6002
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08006003 ASSERT_EQ(AKEY_STATE_UP, mapper.getScanCodeState(AINPUT_SOURCE_ANY, KEY_HOME));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006004}
6005
6006TEST_F(SingleTouchInputMapperTest, MarkSupportedKeyCodes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006007 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006008 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006009 prepareButtons();
6010 prepareAxes(POSITION);
6011 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006012 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006013
Michael Wrightd02c5b62014-02-10 15:10:22 -08006014 uint8_t flags[2] = { 0, 0 };
Siarhei Vishniakou74007942022-06-13 13:57:47 -07006015 ASSERT_TRUE(
6016 mapper.markSupportedKeyCodes(AINPUT_SOURCE_ANY, {AKEYCODE_HOME, AKEYCODE_A}, flags));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006017 ASSERT_TRUE(flags[0]);
6018 ASSERT_FALSE(flags[1]);
6019}
6020
6021TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndReleasedNormally_SendsKeyDownAndKeyUp) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006022 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006023 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006024 prepareButtons();
6025 prepareAxes(POSITION);
6026 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006027 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006028
arthurhungdcef2dc2020-08-11 14:47:50 +08006029 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006030
6031 NotifyKeyArgs args;
6032
6033 // Press virtual key.
6034 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6035 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6036 processDown(mapper, x, y);
6037 processSync(mapper);
6038
6039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6040 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6041 ASSERT_EQ(DEVICE_ID, args.deviceId);
6042 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6043 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6044 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, args.action);
6045 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6046 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6047 ASSERT_EQ(KEY_HOME, args.scanCode);
6048 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6049 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6050
6051 // Release virtual key.
6052 processUp(mapper);
6053 processSync(mapper);
6054
6055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&args));
6056 ASSERT_EQ(ARBITRARY_TIME, args.eventTime);
6057 ASSERT_EQ(DEVICE_ID, args.deviceId);
6058 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, args.source);
6059 ASSERT_EQ(POLICY_FLAG_VIRTUAL, args.policyFlags);
6060 ASSERT_EQ(AKEY_EVENT_ACTION_UP, args.action);
6061 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, args.flags);
6062 ASSERT_EQ(AKEYCODE_HOME, args.keyCode);
6063 ASSERT_EQ(KEY_HOME, args.scanCode);
6064 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, args.metaState);
6065 ASSERT_EQ(ARBITRARY_TIME, args.downTime);
6066
6067 // Should not have sent any motions.
6068 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6069}
6070
6071TEST_F(SingleTouchInputMapperTest, Process_WhenVirtualKeyIsPressedAndMovedOutOfBounds_SendsKeyDownAndKeyCancel) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006072 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006073 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006074 prepareButtons();
6075 prepareAxes(POSITION);
6076 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006077 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006078
arthurhungdcef2dc2020-08-11 14:47:50 +08006079 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006080
6081 NotifyKeyArgs keyArgs;
6082
6083 // Press virtual key.
6084 int32_t x = toRawX(VIRTUAL_KEYS[0].centerX);
6085 int32_t y = toRawY(VIRTUAL_KEYS[0].centerY);
6086 processDown(mapper, x, y);
6087 processSync(mapper);
6088
6089 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6090 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6091 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6092 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6093 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6094 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6095 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY, keyArgs.flags);
6096 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6097 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6098 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6099 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6100
6101 // Move out of bounds. This should generate a cancel and a pointer down since we moved
6102 // into the display area.
6103 y -= 100;
6104 processMove(mapper, x, y);
6105 processSync(mapper);
6106
6107 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6108 ASSERT_EQ(ARBITRARY_TIME, keyArgs.eventTime);
6109 ASSERT_EQ(DEVICE_ID, keyArgs.deviceId);
6110 ASSERT_EQ(AINPUT_SOURCE_KEYBOARD, keyArgs.source);
6111 ASSERT_EQ(POLICY_FLAG_VIRTUAL, keyArgs.policyFlags);
6112 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6113 ASSERT_EQ(AKEY_EVENT_FLAG_FROM_SYSTEM | AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY
6114 | AKEY_EVENT_FLAG_CANCELED, keyArgs.flags);
6115 ASSERT_EQ(AKEYCODE_HOME, keyArgs.keyCode);
6116 ASSERT_EQ(KEY_HOME, keyArgs.scanCode);
6117 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, keyArgs.metaState);
6118 ASSERT_EQ(ARBITRARY_TIME, keyArgs.downTime);
6119
6120 NotifyMotionArgs motionArgs;
6121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6122 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6123 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6124 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6125 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6126 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6127 ASSERT_EQ(0, motionArgs.flags);
6128 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6129 ASSERT_EQ(0, motionArgs.buttonState);
6130 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006131 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006132 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006133 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006134 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6135 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6136 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6137 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6138 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6139
6140 // Keep moving out of bounds. Should generate a pointer move.
6141 y -= 50;
6142 processMove(mapper, x, y);
6143 processSync(mapper);
6144
6145 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6146 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6147 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6148 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6149 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6150 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6151 ASSERT_EQ(0, motionArgs.flags);
6152 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6153 ASSERT_EQ(0, motionArgs.buttonState);
6154 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006155 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006156 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006157 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006158 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6159 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6160 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6161 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6162 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6163
6164 // Release out of bounds. Should generate a pointer up.
6165 processUp(mapper);
6166 processSync(mapper);
6167
6168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6169 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6170 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6171 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6172 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6173 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6174 ASSERT_EQ(0, motionArgs.flags);
6175 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6176 ASSERT_EQ(0, motionArgs.buttonState);
6177 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006178 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006179 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006180 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006181 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6182 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6183 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6184 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6185 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6186
6187 // Should not have sent any more keys or motions.
6188 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6190}
6191
6192TEST_F(SingleTouchInputMapperTest, Process_WhenTouchStartsOutsideDisplayAndMovesIn_SendsDownAsTouchEntersDisplay) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006193 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006194 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006195 prepareButtons();
6196 prepareAxes(POSITION);
6197 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006198 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006199
arthurhungdcef2dc2020-08-11 14:47:50 +08006200 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006201
6202 NotifyMotionArgs motionArgs;
6203
6204 // Initially go down out of bounds.
6205 int32_t x = -10;
6206 int32_t y = -10;
6207 processDown(mapper, x, y);
6208 processSync(mapper);
6209
6210 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6211
6212 // Move into the display area. Should generate a pointer down.
6213 x = 50;
6214 y = 75;
6215 processMove(mapper, x, y);
6216 processSync(mapper);
6217
6218 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6219 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6220 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6221 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6222 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6223 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6224 ASSERT_EQ(0, motionArgs.flags);
6225 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6226 ASSERT_EQ(0, motionArgs.buttonState);
6227 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006228 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006229 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006230 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006231 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6232 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6233 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6234 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6235 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6236
6237 // Release. Should generate a pointer up.
6238 processUp(mapper);
6239 processSync(mapper);
6240
6241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6242 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6243 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6244 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6245 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6246 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6247 ASSERT_EQ(0, motionArgs.flags);
6248 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6249 ASSERT_EQ(0, motionArgs.buttonState);
6250 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006251 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006252 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006253 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6255 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6256 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6257 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6258 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6259
6260 // Should not have sent any more keys or motions.
6261 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6262 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6263}
6264
Santos Cordonfa5cf462017-04-05 10:37:00 -07006265TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture_VirtualDisplay) {
Santos Cordonfa5cf462017-04-05 10:37:00 -07006266 addConfigurationProperty("touch.deviceType", "touchScreen");
6267 addConfigurationProperty("touch.displayId", VIRTUAL_DISPLAY_UNIQUE_ID);
6268
Michael Wrighta9cf4192022-12-01 23:46:39 +00006269 prepareVirtualDisplay(ui::ROTATION_0);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006270 prepareButtons();
6271 prepareAxes(POSITION);
6272 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006273 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Santos Cordonfa5cf462017-04-05 10:37:00 -07006274
arthurhungdcef2dc2020-08-11 14:47:50 +08006275 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006276
6277 NotifyMotionArgs motionArgs;
6278
6279 // Down.
6280 int32_t x = 100;
6281 int32_t y = 125;
6282 processDown(mapper, x, y);
6283 processSync(mapper);
6284
6285 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6286 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6287 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6288 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6289 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6290 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6291 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6292 ASSERT_EQ(0, motionArgs.flags);
6293 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6294 ASSERT_EQ(0, motionArgs.buttonState);
6295 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006296 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006297 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006298 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6300 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6301 1, 0, 0, 0, 0, 0, 0, 0));
6302 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6303 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6304 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6305
6306 // Move.
6307 x += 50;
6308 y += 75;
6309 processMove(mapper, x, y);
6310 processSync(mapper);
6311
6312 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6313 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6314 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6315 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6316 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6317 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6318 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6319 ASSERT_EQ(0, motionArgs.flags);
6320 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6321 ASSERT_EQ(0, motionArgs.buttonState);
6322 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006323 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006324 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006325 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6327 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6328 1, 0, 0, 0, 0, 0, 0, 0));
6329 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6330 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6331 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6332
6333 // Up.
6334 processUp(mapper);
6335 processSync(mapper);
6336
6337 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6338 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6339 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6340 ASSERT_EQ(VIRTUAL_DISPLAY_ID, motionArgs.displayId);
6341 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6342 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6343 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6344 ASSERT_EQ(0, motionArgs.flags);
6345 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6346 ASSERT_EQ(0, motionArgs.buttonState);
6347 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006348 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Santos Cordonfa5cf462017-04-05 10:37:00 -07006349 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006350 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Santos Cordonfa5cf462017-04-05 10:37:00 -07006351 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6352 toDisplayX(x, VIRTUAL_DISPLAY_WIDTH), toDisplayY(y, VIRTUAL_DISPLAY_HEIGHT),
6353 1, 0, 0, 0, 0, 0, 0, 0));
6354 ASSERT_NEAR(X_PRECISION_VIRTUAL, motionArgs.xPrecision, EPSILON);
6355 ASSERT_NEAR(Y_PRECISION_VIRTUAL, motionArgs.yPrecision, EPSILON);
6356 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6357
6358 // Should not have sent any more keys or motions.
6359 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6360 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6361}
6362
Michael Wrightd02c5b62014-02-10 15:10:22 -08006363TEST_F(SingleTouchInputMapperTest, Process_NormalSingleTouchGesture) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006364 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006365 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006366 prepareButtons();
6367 prepareAxes(POSITION);
6368 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00006369 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006370
arthurhungdcef2dc2020-08-11 14:47:50 +08006371 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006372
6373 NotifyMotionArgs motionArgs;
6374
6375 // Down.
6376 int32_t x = 100;
6377 int32_t y = 125;
6378 processDown(mapper, x, y);
6379 processSync(mapper);
6380
6381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6382 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6383 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6384 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6385 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6386 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6387 ASSERT_EQ(0, motionArgs.flags);
6388 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6389 ASSERT_EQ(0, motionArgs.buttonState);
6390 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006391 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006392 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006393 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006394 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6395 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6396 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6397 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6398 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6399
6400 // Move.
6401 x += 50;
6402 y += 75;
6403 processMove(mapper, x, y);
6404 processSync(mapper);
6405
6406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6407 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6408 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6409 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6410 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6411 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6412 ASSERT_EQ(0, motionArgs.flags);
6413 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6414 ASSERT_EQ(0, motionArgs.buttonState);
6415 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006416 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006417 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006418 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006419 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6420 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6421 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6422 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6423 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6424
6425 // Up.
6426 processUp(mapper);
6427 processSync(mapper);
6428
6429 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6430 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
6431 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
6432 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
6433 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
6434 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
6435 ASSERT_EQ(0, motionArgs.flags);
6436 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
6437 ASSERT_EQ(0, motionArgs.buttonState);
6438 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07006439 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08006440 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07006441 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006442 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
6443 toDisplayX(x), toDisplayY(y), 1, 0, 0, 0, 0, 0, 0, 0));
6444 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
6445 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
6446 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
6447
6448 // Should not have sent any more keys or motions.
6449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
6450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6451}
6452
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006453TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationAware_DoesNotRotateMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006454 addConfigurationProperty("touch.deviceType", "touchScreen");
6455 prepareButtons();
6456 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006457 // InputReader works in the un-rotated coordinate space, so orientation-aware devices do not
6458 // need to be rotated. Touchscreens are orientation-aware by default.
Arpit Singha8c236b2023-04-25 13:56:05 +00006459 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006460
6461 NotifyMotionArgs args;
6462
6463 // Rotation 90.
Michael Wrighta9cf4192022-12-01 23:46:39 +00006464 prepareDisplay(ui::ROTATION_90);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006465 processDown(mapper, toRawX(50), toRawY(75));
6466 processSync(mapper);
6467
6468 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6469 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6470 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6471
6472 processUp(mapper);
6473 processSync(mapper);
6474 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6475}
6476
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006477TEST_F(SingleTouchInputMapperTest, Process_WhenNotOrientationAware_RotatesMotions) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006478 addConfigurationProperty("touch.deviceType", "touchScreen");
6479 prepareButtons();
6480 prepareAxes(POSITION);
Prabir Pradhanc14266f2021-05-12 15:56:24 -07006481 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6482 // orientation-aware are affected by display rotation.
6483 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +00006484 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006485
6486 NotifyMotionArgs args;
6487
6488 // Rotation 0.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006489 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006490 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006491 processDown(mapper, toRawX(50), toRawY(75));
6492 processSync(mapper);
6493
6494 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6495 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6496 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6497
6498 processUp(mapper);
6499 processSync(mapper);
6500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6501
6502 // Rotation 90.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006503 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006504 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006505 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006506 processSync(mapper);
6507
6508 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6509 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6510 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6511
6512 processUp(mapper);
6513 processSync(mapper);
6514 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6515
6516 // Rotation 180.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006517 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006518 prepareDisplay(ui::ROTATION_180);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006519 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6520 processSync(mapper);
6521
6522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6523 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6524 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6525
6526 processUp(mapper);
6527 processSync(mapper);
6528 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6529
6530 // Rotation 270.
Siarhei Vishniakou05a8fe22018-10-03 16:38:28 -07006531 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006532 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006533 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006534 processSync(mapper);
6535
6536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6537 ASSERT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6538 ASSERT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6539
6540 processUp(mapper);
6541 processSync(mapper);
6542 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6543}
6544
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006545TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation0_RotatesMotions) {
6546 addConfigurationProperty("touch.deviceType", "touchScreen");
6547 prepareButtons();
6548 prepareAxes(POSITION);
6549 addConfigurationProperty("touch.orientationAware", "1");
6550 addConfigurationProperty("touch.orientation", "ORIENTATION_0");
6551 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006552 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006553 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006554 NotifyMotionArgs args;
6555
6556 // Orientation 0.
6557 processDown(mapper, toRawX(50), toRawY(75));
6558 processSync(mapper);
6559
6560 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6561 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6562 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6563
6564 processUp(mapper);
6565 processSync(mapper);
6566 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6567}
6568
6569TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation90_RotatesMotions) {
6570 addConfigurationProperty("touch.deviceType", "touchScreen");
6571 prepareButtons();
6572 prepareAxes(POSITION);
6573 addConfigurationProperty("touch.orientationAware", "1");
6574 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
6575 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006576 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006577 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006578 NotifyMotionArgs args;
6579
6580 // Orientation 90.
6581 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6582 processSync(mapper);
6583
6584 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6585 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6586 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6587
6588 processUp(mapper);
6589 processSync(mapper);
6590 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6591}
6592
6593TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation180_RotatesMotions) {
6594 addConfigurationProperty("touch.deviceType", "touchScreen");
6595 prepareButtons();
6596 prepareAxes(POSITION);
6597 addConfigurationProperty("touch.orientationAware", "1");
6598 addConfigurationProperty("touch.orientation", "ORIENTATION_180");
6599 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006600 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006601 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006602 NotifyMotionArgs args;
6603
6604 // Orientation 180.
6605 processDown(mapper, RAW_X_MAX - toRawX(50) + RAW_X_MIN, RAW_Y_MAX - toRawY(75) + RAW_Y_MIN);
6606 processSync(mapper);
6607
6608 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6609 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6610 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6611
6612 processUp(mapper);
6613 processSync(mapper);
6614 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6615}
6616
6617TEST_F(SingleTouchInputMapperTest, Process_WhenOrientation270_RotatesMotions) {
6618 addConfigurationProperty("touch.deviceType", "touchScreen");
6619 prepareButtons();
6620 prepareAxes(POSITION);
6621 addConfigurationProperty("touch.orientationAware", "1");
6622 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
6623 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006624 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006625 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006626 NotifyMotionArgs args;
6627
6628 // Orientation 270.
6629 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6630 processSync(mapper);
6631
6632 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6633 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6634 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6635
6636 processUp(mapper);
6637 processSync(mapper);
6638 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6639}
6640
6641TEST_F(SingleTouchInputMapperTest, Process_WhenOrientationSpecified_RotatesMotionWithDisplay) {
6642 addConfigurationProperty("touch.deviceType", "touchScreen");
6643 prepareButtons();
6644 prepareAxes(POSITION);
6645 // Since InputReader works in the un-rotated coordinate space, only devices that are not
6646 // orientation-aware are affected by display rotation.
6647 addConfigurationProperty("touch.orientationAware", "0");
6648 addConfigurationProperty("touch.orientation", "ORIENTATION_90");
Arpit Singha8c236b2023-04-25 13:56:05 +00006649 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006650
6651 NotifyMotionArgs args;
6652
6653 // Orientation 90, Rotation 0.
6654 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006655 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006656 processDown(mapper, RAW_X_MAX - toRotatedRawX(75) + RAW_X_MIN, toRotatedRawY(50));
6657 processSync(mapper);
6658
6659 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6660 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6661 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6662
6663 processUp(mapper);
6664 processSync(mapper);
6665 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6666
6667 // Orientation 90, Rotation 90.
6668 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006669 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006670 processDown(mapper, toRawX(50), toRawY(75));
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006671 processSync(mapper);
6672
6673 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6674 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6675 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6676
6677 processUp(mapper);
6678 processSync(mapper);
6679 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6680
6681 // Orientation 90, Rotation 180.
6682 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006683 prepareDisplay(ui::ROTATION_180);
Prabir Pradhanac1c74f2021-08-20 16:09:32 -07006684 processDown(mapper, toRotatedRawX(75), RAW_Y_MAX - toRotatedRawY(50) + RAW_Y_MIN);
6685 processSync(mapper);
6686
6687 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6688 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6689 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6690
6691 processUp(mapper);
6692 processSync(mapper);
6693 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6694
6695 // Orientation 90, Rotation 270.
6696 clearViewports();
Michael Wrighta9cf4192022-12-01 23:46:39 +00006697 prepareDisplay(ui::ROTATION_270);
Prabir Pradhanea31d4f2022-11-10 20:48:01 +00006698 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 -07006699 processSync(mapper);
6700
6701 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6702 EXPECT_NEAR(50, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6703 EXPECT_NEAR(75, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6704
6705 processUp(mapper);
6706 processSync(mapper);
6707 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6708}
6709
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006710TEST_F(SingleTouchInputMapperTest, Process_IgnoresTouchesOutsidePhysicalFrame) {
6711 addConfigurationProperty("touch.deviceType", "touchScreen");
6712 prepareButtons();
6713 prepareAxes(POSITION);
6714 addConfigurationProperty("touch.orientationAware", "1");
6715 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +00006716 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006717
6718 // Set a physical frame in the display viewport.
6719 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6720 viewport->physicalLeft = 20;
6721 viewport->physicalTop = 600;
6722 viewport->physicalRight = 30;
6723 viewport->physicalBottom = 610;
6724 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00006725 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan675f25a2022-11-10 22:04:07 +00006726
6727 // Start the touch.
6728 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6729 processSync(mapper);
6730
6731 // Expect all input starting outside the physical frame to be ignored.
6732 const std::array<Point, 6> outsidePoints = {
6733 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6734 for (const auto& p : outsidePoints) {
6735 processMove(mapper, toRawX(p.x), toRawY(p.y));
6736 processSync(mapper);
6737 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
6738 }
6739
6740 // Move the touch into the physical frame.
6741 processMove(mapper, toRawX(25), toRawY(605));
6742 processSync(mapper);
6743 NotifyMotionArgs args;
6744 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6745 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
6746 EXPECT_NEAR(25, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6747 EXPECT_NEAR(605, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6748
6749 // Once the touch down is reported, continue reporting input, even if it is outside the frame.
6750 for (const auto& p : outsidePoints) {
6751 processMove(mapper, toRawX(p.x), toRawY(p.y));
6752 processSync(mapper);
6753 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6754 EXPECT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
6755 EXPECT_NEAR(p.x, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_X), 1);
6756 EXPECT_NEAR(p.y, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_Y), 1);
6757 }
6758
6759 processUp(mapper);
6760 processSync(mapper);
6761 EXPECT_NO_FATAL_FAILURE(
6762 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
6763}
6764
Harry Cutts1db43992023-06-19 17:05:07 +00006765TEST_F(SingleTouchInputMapperTest, Process_DoesntCheckPhysicalFrameForTouchpads) {
6766 std::shared_ptr<FakePointerController> fakePointerController =
6767 std::make_shared<FakePointerController>();
6768 mFakePolicy->setPointerController(fakePointerController);
6769
6770 addConfigurationProperty("touch.deviceType", "pointer");
6771 prepareAxes(POSITION);
6772 prepareDisplay(ui::ROTATION_0);
6773 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
6774
6775 // Set a physical frame in the display viewport.
6776 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
6777 viewport->physicalLeft = 20;
6778 viewport->physicalTop = 600;
6779 viewport->physicalRight = 30;
6780 viewport->physicalBottom = 610;
6781 mFakePolicy->updateViewport(*viewport);
6782 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
6783
6784 // Start the touch.
6785 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, BTN_TOUCH, 1);
6786 processSync(mapper);
6787
6788 // Expect all input starting outside the physical frame to result in NotifyMotionArgs being
6789 // produced.
6790 const std::array<Point, 6> outsidePoints = {
6791 {{0, 0}, {19, 605}, {31, 605}, {25, 599}, {25, 611}, {DISPLAY_WIDTH, DISPLAY_HEIGHT}}};
6792 for (const auto& p : outsidePoints) {
6793 processMove(mapper, toRawX(p.x), toRawY(p.y));
6794 processSync(mapper);
6795 EXPECT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled());
6796 }
6797}
6798
Michael Wrightd02c5b62014-02-10 15:10:22 -08006799TEST_F(SingleTouchInputMapperTest, Process_AllAxes_DefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006800 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006801 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006802 prepareButtons();
6803 prepareAxes(POSITION | PRESSURE | TOOL | DISTANCE | TILT);
Arpit Singha8c236b2023-04-25 13:56:05 +00006804 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006805
6806 // These calculations are based on the input device calibration documentation.
6807 int32_t rawX = 100;
6808 int32_t rawY = 200;
6809 int32_t rawPressure = 10;
6810 int32_t rawToolMajor = 12;
6811 int32_t rawDistance = 2;
6812 int32_t rawTiltX = 30;
6813 int32_t rawTiltY = 110;
6814
6815 float x = toDisplayX(rawX);
6816 float y = toDisplayY(rawY);
6817 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
6818 float size = float(rawToolMajor) / RAW_TOOL_MAX;
6819 float tool = float(rawToolMajor) * GEOMETRIC_SCALE;
6820 float distance = float(rawDistance);
6821
6822 float tiltCenter = (RAW_TILT_MAX + RAW_TILT_MIN) * 0.5f;
6823 float tiltScale = M_PI / 180;
6824 float tiltXAngle = (rawTiltX - tiltCenter) * tiltScale;
6825 float tiltYAngle = (rawTiltY - tiltCenter) * tiltScale;
6826 float orientation = atan2f(-sinf(tiltXAngle), sinf(tiltYAngle));
6827 float tilt = acosf(cosf(tiltXAngle) * cosf(tiltYAngle));
6828
6829 processDown(mapper, rawX, rawY);
6830 processPressure(mapper, rawPressure);
6831 processToolMajor(mapper, rawToolMajor);
6832 processDistance(mapper, rawDistance);
6833 processTilt(mapper, rawTiltX, rawTiltY);
6834 processSync(mapper);
6835
6836 NotifyMotionArgs args;
6837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6838 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6839 x, y, pressure, size, tool, tool, tool, tool, orientation, distance));
6840 ASSERT_EQ(tilt, args.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_TILT));
6841}
6842
Jason Gerecke489fda82012-09-07 17:19:40 -07006843TEST_F(SingleTouchInputMapperTest, Process_XYAxes_AffineCalibration) {
Jason Gerecke489fda82012-09-07 17:19:40 -07006844 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006845 prepareDisplay(ui::ROTATION_0);
Jason Gerecke489fda82012-09-07 17:19:40 -07006846 prepareLocationCalibration();
6847 prepareButtons();
6848 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006849 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Jason Gerecke489fda82012-09-07 17:19:40 -07006850
6851 int32_t rawX = 100;
6852 int32_t rawY = 200;
6853
6854 float x = toDisplayX(toCookedX(rawX, rawY));
6855 float y = toDisplayY(toCookedY(rawX, rawY));
6856
6857 processDown(mapper, rawX, rawY);
6858 processSync(mapper);
6859
6860 NotifyMotionArgs args;
6861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
6862 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
6863 x, y, 1, 0, 0, 0, 0, 0, 0, 0));
6864}
6865
Michael Wrightd02c5b62014-02-10 15:10:22 -08006866TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08006867 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00006868 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006869 prepareButtons();
6870 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00006871 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08006872
6873 NotifyMotionArgs motionArgs;
6874 NotifyKeyArgs keyArgs;
6875
6876 processDown(mapper, 100, 200);
6877 processSync(mapper);
6878 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6879 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
6880 ASSERT_EQ(0, motionArgs.buttonState);
6881
6882 // press BTN_LEFT, release BTN_LEFT
6883 processKey(mapper, BTN_LEFT, 1);
6884 processSync(mapper);
6885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6887 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6888
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006889 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6890 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6891 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
6892
Michael Wrightd02c5b62014-02-10 15:10:22 -08006893 processKey(mapper, BTN_LEFT, 0);
6894 processSync(mapper);
6895 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006896 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006897 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006898
6899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006901 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006902
6903 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
6904 processKey(mapper, BTN_RIGHT, 1);
6905 processKey(mapper, BTN_MIDDLE, 1);
6906 processSync(mapper);
6907 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6908 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
6909 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6910 motionArgs.buttonState);
6911
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006912 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6913 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6914 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
6915
6916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6917 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6918 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
6919 motionArgs.buttonState);
6920
Michael Wrightd02c5b62014-02-10 15:10:22 -08006921 processKey(mapper, BTN_RIGHT, 0);
6922 processSync(mapper);
6923 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006924 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006925 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006926
6927 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006928 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006929 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006930
6931 processKey(mapper, BTN_MIDDLE, 0);
6932 processSync(mapper);
6933 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006934 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006935 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006936
6937 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006938 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006939 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006940
6941 // press BTN_BACK, release BTN_BACK
6942 processKey(mapper, BTN_BACK, 1);
6943 processSync(mapper);
6944 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6945 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6946 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006947
Michael Wrightd02c5b62014-02-10 15:10:22 -08006948 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006949 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006950 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6951
6952 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6953 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6954 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006955
6956 processKey(mapper, BTN_BACK, 0);
6957 processSync(mapper);
6958 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006959 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006960 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006961
6962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006963 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006964 ASSERT_EQ(0, motionArgs.buttonState);
6965
Michael Wrightd02c5b62014-02-10 15:10:22 -08006966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6967 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6968 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6969
6970 // press BTN_SIDE, release BTN_SIDE
6971 processKey(mapper, BTN_SIDE, 1);
6972 processSync(mapper);
6973 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6974 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
6975 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006976
Michael Wrightd02c5b62014-02-10 15:10:22 -08006977 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006978 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006979 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
6980
6981 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
6982 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
6983 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006984
6985 processKey(mapper, BTN_SIDE, 0);
6986 processSync(mapper);
6987 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006988 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08006989 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006990
6991 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08006992 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08006993 ASSERT_EQ(0, motionArgs.buttonState);
6994
Michael Wrightd02c5b62014-02-10 15:10:22 -08006995 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
6996 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
6997 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
6998
6999 // press BTN_FORWARD, release BTN_FORWARD
7000 processKey(mapper, BTN_FORWARD, 1);
7001 processSync(mapper);
7002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7003 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7004 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007005
Michael Wrightd02c5b62014-02-10 15:10:22 -08007006 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007007 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007008 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7009
7010 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7011 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7012 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007013
7014 processKey(mapper, BTN_FORWARD, 0);
7015 processSync(mapper);
7016 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007017 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007018 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007019
7020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007021 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007022 ASSERT_EQ(0, motionArgs.buttonState);
7023
Michael Wrightd02c5b62014-02-10 15:10:22 -08007024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7025 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7026 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7027
7028 // press BTN_EXTRA, release BTN_EXTRA
7029 processKey(mapper, BTN_EXTRA, 1);
7030 processSync(mapper);
7031 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7032 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
7033 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007034
Michael Wrightd02c5b62014-02-10 15:10:22 -08007035 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007036 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007037 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
7038
7039 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7040 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7041 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007042
7043 processKey(mapper, BTN_EXTRA, 0);
7044 processSync(mapper);
7045 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007046 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007047 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007048
7049 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007050 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007051 ASSERT_EQ(0, motionArgs.buttonState);
7052
Michael Wrightd02c5b62014-02-10 15:10:22 -08007053 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
7054 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
7055 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
7056
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007057 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7058
Michael Wrightd02c5b62014-02-10 15:10:22 -08007059 // press BTN_STYLUS, release BTN_STYLUS
7060 processKey(mapper, BTN_STYLUS, 1);
7061 processSync(mapper);
7062 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7063 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007064 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
7065
7066 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7067 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7068 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007069
7070 processKey(mapper, BTN_STYLUS, 0);
7071 processSync(mapper);
7072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007073 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007074 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007075
7076 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007077 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007078 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007079
7080 // press BTN_STYLUS2, release BTN_STYLUS2
7081 processKey(mapper, BTN_STYLUS2, 1);
7082 processSync(mapper);
7083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7084 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007085 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
7086
7087 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7088 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
7089 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007090
7091 processKey(mapper, BTN_STYLUS2, 0);
7092 processSync(mapper);
7093 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007094 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007095 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007096
7097 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08007098 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08007099 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007100
7101 // release touch
7102 processUp(mapper);
7103 processSync(mapper);
7104 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7105 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7106 ASSERT_EQ(0, motionArgs.buttonState);
7107}
7108
7109TEST_F(SingleTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007110 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007111 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007112 prepareButtons();
7113 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007114 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007115
7116 NotifyMotionArgs motionArgs;
7117
7118 // default tool type is finger
7119 processDown(mapper, 100, 200);
7120 processSync(mapper);
7121 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7122 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007123 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007124
7125 // eraser
7126 processKey(mapper, BTN_TOOL_RUBBER, 1);
7127 processSync(mapper);
7128 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7129 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007130 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007131
7132 // stylus
7133 processKey(mapper, BTN_TOOL_RUBBER, 0);
7134 processKey(mapper, BTN_TOOL_PEN, 1);
7135 processSync(mapper);
7136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7137 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007138 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007139
7140 // brush
7141 processKey(mapper, BTN_TOOL_PEN, 0);
7142 processKey(mapper, BTN_TOOL_BRUSH, 1);
7143 processSync(mapper);
7144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7145 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007146 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007147
7148 // pencil
7149 processKey(mapper, BTN_TOOL_BRUSH, 0);
7150 processKey(mapper, BTN_TOOL_PENCIL, 1);
7151 processSync(mapper);
7152 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7153 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007154 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007155
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08007156 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08007157 processKey(mapper, BTN_TOOL_PENCIL, 0);
7158 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
7159 processSync(mapper);
7160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7161 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007162 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007163
7164 // mouse
7165 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
7166 processKey(mapper, BTN_TOOL_MOUSE, 1);
7167 processSync(mapper);
7168 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7169 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007170 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007171
7172 // lens
7173 processKey(mapper, BTN_TOOL_MOUSE, 0);
7174 processKey(mapper, BTN_TOOL_LENS, 1);
7175 processSync(mapper);
7176 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7177 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007178 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007179
7180 // double-tap
7181 processKey(mapper, BTN_TOOL_LENS, 0);
7182 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
7183 processSync(mapper);
7184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7185 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007186 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007187
7188 // triple-tap
7189 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
7190 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
7191 processSync(mapper);
7192 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7193 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007194 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007195
7196 // quad-tap
7197 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
7198 processKey(mapper, BTN_TOOL_QUADTAP, 1);
7199 processSync(mapper);
7200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7201 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007202 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007203
7204 // finger
7205 processKey(mapper, BTN_TOOL_QUADTAP, 0);
7206 processKey(mapper, BTN_TOOL_FINGER, 1);
7207 processSync(mapper);
7208 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7209 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007210 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007211
7212 // stylus trumps finger
7213 processKey(mapper, BTN_TOOL_PEN, 1);
7214 processSync(mapper);
7215 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7216 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007217 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007218
7219 // eraser trumps stylus
7220 processKey(mapper, BTN_TOOL_RUBBER, 1);
7221 processSync(mapper);
7222 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7223 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007224 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007225
7226 // mouse trumps eraser
7227 processKey(mapper, BTN_TOOL_MOUSE, 1);
7228 processSync(mapper);
7229 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7230 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007231 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007232
7233 // back to default tool type
7234 processKey(mapper, BTN_TOOL_MOUSE, 0);
7235 processKey(mapper, BTN_TOOL_RUBBER, 0);
7236 processKey(mapper, BTN_TOOL_PEN, 0);
7237 processKey(mapper, BTN_TOOL_FINGER, 0);
7238 processSync(mapper);
7239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7240 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007241 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007242}
7243
7244TEST_F(SingleTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007245 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007246 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007247 prepareButtons();
7248 prepareAxes(POSITION);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08007249 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_FINGER, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00007250 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007251
7252 NotifyMotionArgs motionArgs;
7253
7254 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
7255 processKey(mapper, BTN_TOOL_FINGER, 1);
7256 processMove(mapper, 100, 200);
7257 processSync(mapper);
7258 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7259 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7260 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7261 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7262
7263 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7264 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7265 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7266 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7267
7268 // move a little
7269 processMove(mapper, 150, 250);
7270 processSync(mapper);
7271 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7272 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7273 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7274 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7275
7276 // down when BTN_TOUCH is pressed, pressure defaults to 1
7277 processKey(mapper, BTN_TOUCH, 1);
7278 processSync(mapper);
7279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7280 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7281 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7282 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7283
7284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7285 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7286 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7287 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7288
7289 // up when BTN_TOUCH is released, hover restored
7290 processKey(mapper, BTN_TOUCH, 0);
7291 processSync(mapper);
7292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7293 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7294 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7295 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7296
7297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7298 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7299 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7300 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7301
7302 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7303 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7304 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7305 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7306
7307 // exit hover when pointer goes away
7308 processKey(mapper, BTN_TOOL_FINGER, 0);
7309 processSync(mapper);
7310 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7311 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7312 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7313 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7314}
7315
7316TEST_F(SingleTouchInputMapperTest, Process_WhenAbsPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08007317 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007318 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08007319 prepareButtons();
7320 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007321 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08007322
7323 NotifyMotionArgs motionArgs;
7324
7325 // initially hovering because pressure is 0
7326 processDown(mapper, 100, 200);
7327 processPressure(mapper, 0);
7328 processSync(mapper);
7329 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7330 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7331 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7332 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7333
7334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7335 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7336 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7337 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
7338
7339 // move a little
7340 processMove(mapper, 150, 250);
7341 processSync(mapper);
7342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7343 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7344 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7345 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7346
7347 // down when pressure is non-zero
7348 processPressure(mapper, RAW_PRESSURE_MAX);
7349 processSync(mapper);
7350 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7351 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7352 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7353 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7354
7355 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7356 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7357 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7358 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7359
7360 // up when pressure becomes 0, hover restored
7361 processPressure(mapper, 0);
7362 processSync(mapper);
7363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7364 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7365 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7366 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
7367
7368 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7369 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
7370 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7371 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7372
7373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7374 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
7375 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7376 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7377
7378 // exit hover when pointer goes away
7379 processUp(mapper);
7380 processSync(mapper);
7381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
7383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
7384 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
7385}
7386
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007387TEST_F(SingleTouchInputMapperTest, Reset_CancelsOngoingGesture) {
7388 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007389 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007390 prepareButtons();
7391 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007392 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007393
7394 // Touch down.
7395 processDown(mapper, 100, 200);
7396 processPressure(mapper, 1);
7397 processSync(mapper);
7398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7399 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
7400
7401 // Reset the mapper. This should cancel the ongoing gesture.
7402 resetMapper(mapper, ARBITRARY_TIME);
7403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7404 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
7405
7406 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7407}
7408
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007409TEST_F(SingleTouchInputMapperTest, Reset_RecreatesTouchState) {
7410 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007411 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007412 prepareButtons();
7413 prepareAxes(POSITION | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +00007414 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007415
7416 // Set the initial state for the touch pointer.
7417 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 100);
7418 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 200);
7419 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_PRESSURE, RAW_PRESSURE_MAX);
7420 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7421
7422 // Reset the mapper. When the mapper is reset, we expect it to attempt to recreate the touch
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +00007423 // state by reading the current axis values. Since there was no ongoing gesture, calling reset
7424 // does not generate any events.
7425 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007426
7427 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
7428 // the recreated touch state to generate a down event.
7429 processSync(mapper);
7430 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7431 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
7432
7433 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7434}
7435
lilinnan687e58f2022-07-19 16:00:50 +08007436TEST_F(SingleTouchInputMapperTest,
7437 Process_WhenViewportDisplayIdChanged_TouchIsCanceledAndDeviceIsReset) {
7438 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007439 prepareDisplay(ui::ROTATION_0);
lilinnan687e58f2022-07-19 16:00:50 +08007440 prepareButtons();
7441 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007442 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
lilinnan687e58f2022-07-19 16:00:50 +08007443 NotifyMotionArgs motionArgs;
7444
7445 // Down.
Prabir Pradhan3e5ec702022-07-29 16:26:24 +00007446 processDown(mapper, 100, 200);
lilinnan687e58f2022-07-19 16:00:50 +08007447 processSync(mapper);
7448
7449 // We should receive a down event
7450 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7451 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7452
7453 // Change display id
7454 clearViewports();
7455 prepareSecondaryDisplay(ViewportType::INTERNAL);
7456
7457 // We should receive a cancel event
7458 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7459 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7460 // Then receive reset called
7461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7462}
7463
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007464TEST_F(SingleTouchInputMapperTest,
7465 Process_WhenViewportActiveStatusChanged_TouchIsCanceledAndDeviceIsReset) {
7466 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007467 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007468 prepareButtons();
7469 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007470 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007471 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7472 NotifyMotionArgs motionArgs;
7473
7474 // Start a new gesture.
7475 processDown(mapper, 100, 200);
7476 processSync(mapper);
7477 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7478 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
7479
7480 // Make the viewport inactive. This will put the device in disabled mode.
7481 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7482 viewport->isActive = false;
7483 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007484 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007485
7486 // We should receive a cancel event for the ongoing gesture.
7487 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7488 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
7489 // Then we should be notified that the device was reset.
7490 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7491
7492 // No events are generated while the viewport is inactive.
7493 processMove(mapper, 101, 201);
7494 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007495 processUp(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007496 processSync(mapper);
7497 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7498
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007499 // Start a new gesture while the viewport is still inactive.
7500 processDown(mapper, 300, 400);
7501 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_X, 300);
7502 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_Y, 400);
7503 mFakeEventHub->setScanCodeState(EVENTHUB_ID, BTN_TOUCH, 1);
7504 processSync(mapper);
7505
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007506 // Make the viewport active again. The device should resume processing events.
7507 viewport->isActive = true;
7508 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007509 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007510
7511 // The device is reset because it changes back to direct mode, without generating any events.
7512 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7513 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7514
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007515 // In the next sync, the touch state that was recreated when the device was reset is reported.
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007516 processSync(mapper);
Prabir Pradhanafabcde2022-09-27 19:32:43 +00007517 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7518 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +00007519
7520 // No more events.
7521 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7522 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
7523}
7524
Prabir Pradhan211ba622022-10-31 21:09:21 +00007525TEST_F(SingleTouchInputMapperTest, ButtonIsReleasedOnTouchUp) {
7526 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007527 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan211ba622022-10-31 21:09:21 +00007528 prepareButtons();
7529 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007530 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan211ba622022-10-31 21:09:21 +00007531 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7532
7533 // Press a stylus button.
7534 processKey(mapper, BTN_STYLUS, 1);
7535 processSync(mapper);
7536
7537 // Start a touch gesture and ensure the BUTTON_PRESS event is generated.
7538 processDown(mapper, 100, 200);
7539 processSync(mapper);
7540 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7541 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7542 WithCoords(toDisplayX(100), toDisplayY(200)),
7543 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7544 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7545 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
7546 WithCoords(toDisplayX(100), toDisplayY(200)),
7547 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
7548
7549 // Release the touch gesture. Ensure that the BUTTON_RELEASE event is generated even though
7550 // the button has not actually been released, since there will be no pointers through which the
7551 // button state can be reported. The event is generated at the location of the pointer before
7552 // it went up.
7553 processUp(mapper);
7554 processSync(mapper);
7555 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7556 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
7557 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7558 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7559 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
7560 WithCoords(toDisplayX(100), toDisplayY(200)), WithButtonState(0))));
7561}
7562
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007563TEST_F(SingleTouchInputMapperTest, StylusButtonMotionEventsDisabled) {
7564 addConfigurationProperty("touch.deviceType", "touchScreen");
7565 prepareDisplay(ui::ROTATION_0);
7566 prepareButtons();
7567 prepareAxes(POSITION);
7568
7569 mFakePolicy->setStylusButtonMotionEventsEnabled(false);
7570
Arpit Singha8c236b2023-04-25 13:56:05 +00007571 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7aa7ff02022-12-21 21:05:38 +00007572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7573
7574 // Press a stylus button.
7575 processKey(mapper, BTN_STYLUS, 1);
7576 processSync(mapper);
7577
7578 // Start a touch gesture and ensure that the stylus button is not reported.
7579 processDown(mapper, 100, 200);
7580 processSync(mapper);
7581 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7582 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
7583
7584 // Release and press the stylus button again.
7585 processKey(mapper, BTN_STYLUS, 0);
7586 processSync(mapper);
7587 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7588 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7589 processKey(mapper, BTN_STYLUS, 1);
7590 processSync(mapper);
7591 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7592 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
7593
7594 // Release the touch gesture.
7595 processUp(mapper);
7596 processSync(mapper);
7597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7598 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
7599
7600 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7601}
7602
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007603TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsSetToTouchNavigation_setsCorrectType) {
7604 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7605 prepareDisplay(ui::ROTATION_0);
7606 prepareButtons();
7607 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007608 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7bc23bf2022-10-04 13:13:07 +00007609 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
7610
7611 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mapper.getSources());
7612}
7613
Seunghwan Choi356026c2023-02-01 14:37:25 +09007614TEST_F(SingleTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
7615 std::shared_ptr<FakePointerController> fakePointerController =
7616 std::make_shared<FakePointerController>();
7617 addConfigurationProperty("touch.deviceType", "touchScreen");
7618 prepareDisplay(ui::ROTATION_0);
7619 prepareButtons();
7620 prepareAxes(POSITION);
7621 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7622 mFakePolicy->setPointerController(fakePointerController);
7623 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +00007624 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007625
7626 processKey(mapper, BTN_TOOL_PEN, 1);
7627 processMove(mapper, 100, 200);
7628 processSync(mapper);
7629 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7630 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007631 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007632 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7633 ASSERT_TRUE(fakePointerController->isPointerShown());
7634 ASSERT_NO_FATAL_FAILURE(
7635 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
7636}
7637
7638TEST_F(SingleTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
7639 std::shared_ptr<FakePointerController> fakePointerController =
7640 std::make_shared<FakePointerController>();
7641 addConfigurationProperty("touch.deviceType", "touchScreen");
7642 prepareDisplay(ui::ROTATION_0);
7643 prepareButtons();
7644 prepareAxes(POSITION);
7645 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7646 mFakePolicy->setPointerController(fakePointerController);
7647 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +00007648 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +09007649
7650 processKey(mapper, BTN_TOOL_PEN, 1);
7651 processMove(mapper, 100, 200);
7652 processSync(mapper);
7653 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7654 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07007655 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +09007656 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
7657 ASSERT_FALSE(fakePointerController->isPointerShown());
7658}
7659
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007660TEST_F(SingleTouchInputMapperTest, WhenDeviceTypeIsChangedToTouchNavigation_updatesDeviceType) {
7661 // Initialize the device without setting device source to touch navigation.
7662 addConfigurationProperty("touch.deviceType", "touchScreen");
7663 prepareDisplay(ui::ROTATION_0);
7664 prepareButtons();
7665 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007666 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007667
7668 // Ensure that the device is created as a touchscreen, not touch navigation.
7669 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
7670
7671 // Add device type association after the device was created.
7672 mFakePolicy->addDeviceTypeAssociation(DEVICE_LOCATION, "touchNavigation");
7673
7674 // Send update to the mapper.
7675 std::list<NotifyArgs> unused2 =
7676 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007677 InputReaderConfiguration::Change::DEVICE_TYPE /*changes*/);
Ambrus Weisz7b6e16b2022-12-16 17:54:57 +00007678
7679 // Check whether device type update was successful.
7680 ASSERT_EQ(AINPUT_SOURCE_TOUCH_NAVIGATION, mDevice->getSources());
7681}
7682
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007683TEST_F(SingleTouchInputMapperTest, HoverEventsOutsidePhysicalFrameAreIgnored) {
7684 // Initialize the device without setting device source to touch navigation.
7685 addConfigurationProperty("touch.deviceType", "touchScreen");
7686 prepareDisplay(ui::ROTATION_0);
7687 prepareButtons();
7688 prepareAxes(POSITION);
7689 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
7690
7691 // Set a physical frame in the display viewport.
7692 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7693 viewport->physicalLeft = 0;
7694 viewport->physicalTop = 0;
7695 viewport->physicalRight = DISPLAY_WIDTH / 2;
7696 viewport->physicalBottom = DISPLAY_HEIGHT / 2;
7697 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007698 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007699
Arpit Singha8c236b2023-04-25 13:56:05 +00007700 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhane1e309a2022-11-29 02:54:27 +00007701
7702 // Hovering inside the physical frame produces events.
7703 processKey(mapper, BTN_TOOL_PEN, 1);
7704 processMove(mapper, RAW_X_MIN + 1, RAW_Y_MIN + 1);
7705 processSync(mapper);
7706 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7707 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7708 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7709 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7710
7711 // Leaving the physical frame ends the hovering gesture.
7712 processMove(mapper, RAW_X_MAX - 1, RAW_Y_MAX - 1);
7713 processSync(mapper);
7714 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7715 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_EXIT)));
7716
7717 // Moving outside the physical frame does not produce events.
7718 processMove(mapper, RAW_X_MAX - 2, RAW_Y_MAX - 2);
7719 processSync(mapper);
7720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7721
7722 // Re-entering the physical frame produces events.
7723 processMove(mapper, RAW_X_MIN, RAW_Y_MIN);
7724 processSync(mapper);
7725 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7726 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER)));
7727 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7728 WithMotionAction(AMOTION_EVENT_ACTION_HOVER_MOVE)));
7729}
7730
Prabir Pradhan5632d622021-09-06 07:57:20 -07007731// --- TouchDisplayProjectionTest ---
7732
7733class TouchDisplayProjectionTest : public SingleTouchInputMapperTest {
7734public:
7735 // The values inside DisplayViewport are expected to be pre-rotated. This updates the current
7736 // DisplayViewport to pre-rotate the values. The viewport's physical display will be set to the
7737 // rotated equivalent of the given un-rotated physical display bounds.
Prabir Pradhana9df3162022-12-05 23:57:27 +00007738 void configurePhysicalDisplay(ui::Rotation orientation, Rect naturalPhysicalDisplay,
7739 int32_t naturalDisplayWidth = DISPLAY_WIDTH,
7740 int32_t naturalDisplayHeight = DISPLAY_HEIGHT) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007741 uint32_t inverseRotationFlags;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007742 auto rotatedWidth = naturalDisplayWidth;
7743 auto rotatedHeight = naturalDisplayHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007744 switch (orientation) {
Michael Wrighta9cf4192022-12-01 23:46:39 +00007745 case ui::ROTATION_90:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007746 inverseRotationFlags = ui::Transform::ROT_270;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007747 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007748 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007749 case ui::ROTATION_180:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007750 inverseRotationFlags = ui::Transform::ROT_180;
7751 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007752 case ui::ROTATION_270:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007753 inverseRotationFlags = ui::Transform::ROT_90;
Prabir Pradhana9df3162022-12-05 23:57:27 +00007754 std::swap(rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007755 break;
Michael Wrighta9cf4192022-12-01 23:46:39 +00007756 case ui::ROTATION_0:
Prabir Pradhan5632d622021-09-06 07:57:20 -07007757 inverseRotationFlags = ui::Transform::ROT_0;
7758 break;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007759 }
7760
Prabir Pradhana9df3162022-12-05 23:57:27 +00007761 const ui::Transform rotation(inverseRotationFlags, rotatedWidth, rotatedHeight);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007762 const Rect rotatedPhysicalDisplay = rotation.transform(naturalPhysicalDisplay);
7763
7764 std::optional<DisplayViewport> internalViewport =
7765 *mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
7766 DisplayViewport& v = *internalViewport;
7767 v.displayId = DISPLAY_ID;
7768 v.orientation = orientation;
7769
7770 v.logicalLeft = 0;
7771 v.logicalTop = 0;
7772 v.logicalRight = 100;
7773 v.logicalBottom = 100;
7774
7775 v.physicalLeft = rotatedPhysicalDisplay.left;
7776 v.physicalTop = rotatedPhysicalDisplay.top;
7777 v.physicalRight = rotatedPhysicalDisplay.right;
7778 v.physicalBottom = rotatedPhysicalDisplay.bottom;
7779
Prabir Pradhana9df3162022-12-05 23:57:27 +00007780 v.deviceWidth = rotatedWidth;
7781 v.deviceHeight = rotatedHeight;
Prabir Pradhan5632d622021-09-06 07:57:20 -07007782
7783 v.isActive = true;
7784 v.uniqueId = UNIQUE_ID;
7785 v.type = ViewportType::INTERNAL;
7786 mFakePolicy->updateViewport(v);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00007787 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007788 }
7789
7790 void assertReceivedMove(const Point& point) {
7791 NotifyMotionArgs motionArgs;
7792 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7793 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007794 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007795 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], point.x, point.y,
7796 1, 0, 0, 0, 0, 0, 0, 0));
7797 }
7798};
7799
7800TEST_F(TouchDisplayProjectionTest, IgnoresTouchesOutsidePhysicalDisplay) {
7801 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007802 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007803
7804 prepareButtons();
7805 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007806 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007807
7808 NotifyMotionArgs motionArgs;
7809
7810 // Configure the DisplayViewport such that the logical display maps to a subsection of
7811 // the display panel called the physical display. Here, the physical display is bounded by the
7812 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7813 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7814 static const std::array<Point, 6> kPointsOutsidePhysicalDisplay{
7815 {{-10, -10}, {0, 0}, {5, 100}, {50, 15}, {75, 100}, {50, 165}}};
7816
Michael Wrighta9cf4192022-12-01 23:46:39 +00007817 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007818 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7819
7820 // Touches outside the physical display should be ignored, and should not generate any
7821 // events. Ensure touches at the following points that lie outside of the physical display
7822 // area do not generate any events.
7823 for (const auto& point : kPointsOutsidePhysicalDisplay) {
7824 processDown(mapper, toRawX(point.x), toRawY(point.y));
7825 processSync(mapper);
7826 processUp(mapper);
7827 processSync(mapper);
7828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled())
7829 << "Unexpected event generated for touch outside physical display at point: "
7830 << point.x << ", " << point.y;
7831 }
7832 }
7833}
7834
7835TEST_F(TouchDisplayProjectionTest, EmitsTouchDownAfterEnteringPhysicalDisplay) {
7836 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00007837 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan5632d622021-09-06 07:57:20 -07007838
7839 prepareButtons();
7840 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00007841 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan5632d622021-09-06 07:57:20 -07007842
7843 NotifyMotionArgs motionArgs;
7844
7845 // Configure the DisplayViewport such that the logical display maps to a subsection of
7846 // the display panel called the physical display. Here, the physical display is bounded by the
7847 // points (10, 20) and (70, 160) inside the display space, which is of the size 400 x 800.
7848 static const Rect kPhysicalDisplay{10, 20, 70, 160};
7849
Michael Wrighta9cf4192022-12-01 23:46:39 +00007850 for (auto orientation : {ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180, ui::ROTATION_270}) {
Prabir Pradhan5632d622021-09-06 07:57:20 -07007851 configurePhysicalDisplay(orientation, kPhysicalDisplay);
7852
7853 // Touches that start outside the physical display should be ignored until it enters the
7854 // physical display bounds, at which point it should generate a down event. Start a touch at
7855 // the point (5, 100), which is outside the physical display bounds.
7856 static const Point kOutsidePoint{5, 100};
7857 processDown(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7858 processSync(mapper);
7859 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7860
7861 // Move the touch into the physical display area. This should generate a pointer down.
7862 processMove(mapper, toRawX(11), toRawY(21));
7863 processSync(mapper);
7864 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7865 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07007866 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Prabir Pradhan5632d622021-09-06 07:57:20 -07007867 ASSERT_NO_FATAL_FAILURE(
7868 assertPointerCoords(motionArgs.pointerCoords[0], 11, 21, 1, 0, 0, 0, 0, 0, 0, 0));
7869
7870 // Move the touch inside the physical display area. This should generate a pointer move.
7871 processMove(mapper, toRawX(69), toRawY(159));
7872 processSync(mapper);
7873 assertReceivedMove({69, 159});
7874
7875 // Move outside the physical display area. Since the pointer is already down, this should
7876 // now continue generating events.
7877 processMove(mapper, toRawX(kOutsidePoint.x), toRawY(kOutsidePoint.y));
7878 processSync(mapper);
7879 assertReceivedMove(kOutsidePoint);
7880
7881 // Release. This should generate a pointer up.
7882 processUp(mapper);
7883 processSync(mapper);
7884 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
7885 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
7886 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], kOutsidePoint.x,
7887 kOutsidePoint.y, 1, 0, 0, 0, 0, 0, 0, 0));
7888
7889 // Ensure no more events were generated.
7890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
7891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
7892 }
7893}
7894
Prabir Pradhana9df3162022-12-05 23:57:27 +00007895// --- TouchscreenPrecisionTests ---
7896
7897// This test suite is used to ensure that touchscreen devices are scaled and configured correctly
7898// in various orientations and with different display rotations. We configure the touchscreen to
7899// have a higher resolution than that of the display by an integer scale factor in each axis so that
7900// we can enforce that coordinates match precisely as expected.
7901class TouchscreenPrecisionTestsFixture : public TouchDisplayProjectionTest,
7902 public ::testing::WithParamInterface<ui::Rotation> {
7903public:
7904 void SetUp() override {
7905 SingleTouchInputMapperTest::SetUp();
7906
7907 // Prepare the raw axes to have twice the resolution of the display in the X axis and
7908 // four times the resolution of the display in the Y axis.
7909 prepareButtons();
7910 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, PRECISION_RAW_X_MIN, PRECISION_RAW_X_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007911 PRECISION_RAW_X_FLAT, PRECISION_RAW_X_FUZZ,
7912 PRECISION_RAW_X_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007913 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, PRECISION_RAW_Y_MIN, PRECISION_RAW_Y_MAX,
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007914 PRECISION_RAW_Y_FLAT, PRECISION_RAW_Y_FUZZ,
7915 PRECISION_RAW_Y_RES);
Prabir Pradhana9df3162022-12-05 23:57:27 +00007916 }
7917
7918 static const int32_t PRECISION_RAW_X_MIN = TouchInputMapperTest::RAW_X_MIN;
7919 static const int32_t PRECISION_RAW_X_MAX = PRECISION_RAW_X_MIN + DISPLAY_WIDTH * 2 - 1;
7920 static const int32_t PRECISION_RAW_Y_MIN = TouchInputMapperTest::RAW_Y_MIN;
7921 static const int32_t PRECISION_RAW_Y_MAX = PRECISION_RAW_Y_MIN + DISPLAY_HEIGHT * 4 - 1;
7922
Prabir Pradhan46211fb2022-12-17 00:30:39 +00007923 static const int32_t PRECISION_RAW_X_RES = 50; // units per millimeter
7924 static const int32_t PRECISION_RAW_Y_RES = 100; // units per millimeter
7925
7926 static const int32_t PRECISION_RAW_X_FLAT = 16;
7927 static const int32_t PRECISION_RAW_Y_FLAT = 32;
7928
7929 static const int32_t PRECISION_RAW_X_FUZZ = 4;
7930 static const int32_t PRECISION_RAW_Y_FUZZ = 8;
7931
Prabir Pradhana9df3162022-12-05 23:57:27 +00007932 static const std::array<Point, 4> kRawCorners;
7933};
7934
7935const std::array<Point, 4> TouchscreenPrecisionTestsFixture::kRawCorners = {{
7936 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MIN}, // left-top
7937 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MIN}, // right-top
7938 {PRECISION_RAW_X_MAX, PRECISION_RAW_Y_MAX}, // right-bottom
7939 {PRECISION_RAW_X_MIN, PRECISION_RAW_Y_MAX}, // left-bottom
7940}};
7941
7942// Tests for how the touchscreen is oriented relative to the natural orientation of the display.
7943// For example, if a touchscreen is configured with an orientation of 90 degrees, it is a portrait
7944// touchscreen panel that is used on a device whose natural display orientation is in landscape.
7945TEST_P(TouchscreenPrecisionTestsFixture, OrientationPrecision) {
7946 enum class Orientation {
7947 ORIENTATION_0 = ui::toRotationInt(ui::ROTATION_0),
7948 ORIENTATION_90 = ui::toRotationInt(ui::ROTATION_90),
7949 ORIENTATION_180 = ui::toRotationInt(ui::ROTATION_180),
7950 ORIENTATION_270 = ui::toRotationInt(ui::ROTATION_270),
7951 ftl_last = ORIENTATION_270,
7952 };
7953 using Orientation::ORIENTATION_0, Orientation::ORIENTATION_90, Orientation::ORIENTATION_180,
7954 Orientation::ORIENTATION_270;
7955 static const std::map<Orientation, std::array<vec2, 4> /*mappedCorners*/> kMappedCorners = {
7956 {ORIENTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
7957 {ORIENTATION_90, {{{0, 479.5}, {0, 0}, {799.75, 0}, {799.75, 479.5}}}},
7958 {ORIENTATION_180, {{{479.5, 799.75}, {0, 799.75}, {0, 0}, {479.5, 0}}}},
7959 {ORIENTATION_270, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
7960 };
7961
7962 const auto touchscreenOrientation = static_cast<Orientation>(ui::toRotationInt(GetParam()));
7963
7964 // Configure the touchscreen as being installed in the one of the four different orientations
7965 // relative to the display.
7966 addConfigurationProperty("touch.deviceType", "touchScreen");
7967 addConfigurationProperty("touch.orientation", ftl::enum_string(touchscreenOrientation).c_str());
7968 prepareDisplay(ui::ROTATION_0);
7969
Arpit Singha8c236b2023-04-25 13:56:05 +00007970 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhana9df3162022-12-05 23:57:27 +00007971
7972 // If the touchscreen is installed in a rotated orientation relative to the display (i.e. in
7973 // orientations of either 90 or 270) this means the display's natural resolution will be
7974 // flipped.
7975 const bool displayRotated =
7976 touchscreenOrientation == ORIENTATION_90 || touchscreenOrientation == ORIENTATION_270;
7977 const int32_t width = displayRotated ? DISPLAY_HEIGHT : DISPLAY_WIDTH;
7978 const int32_t height = displayRotated ? DISPLAY_WIDTH : DISPLAY_HEIGHT;
7979 const Rect physicalFrame{0, 0, width, height};
7980 configurePhysicalDisplay(ui::ROTATION_0, physicalFrame, width, height);
7981
7982 const auto& expectedPoints = kMappedCorners.at(touchscreenOrientation);
7983 const float expectedPrecisionX = displayRotated ? 4 : 2;
7984 const float expectedPrecisionY = displayRotated ? 2 : 4;
7985
7986 // Test all four corners.
7987 for (int i = 0; i < 4; i++) {
7988 const auto& raw = kRawCorners[i];
7989 processDown(mapper, raw.x, raw.y);
7990 processSync(mapper);
7991 const auto& expected = expectedPoints[i];
7992 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
7993 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
7994 WithCoords(expected.x, expected.y),
7995 WithPrecision(expectedPrecisionX, expectedPrecisionY))))
7996 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
7997 << "with touchscreen orientation "
7998 << ftl::enum_string(touchscreenOrientation).c_str() << ", expected point ("
7999 << expected.x << ", " << expected.y << ").";
8000 processUp(mapper);
8001 processSync(mapper);
8002 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8003 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
8004 WithCoords(expected.x, expected.y))));
8005 }
8006}
8007
Prabir Pradhan82687402022-12-06 01:32:53 +00008008TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionWhenOrientationAware) {
8009 static const std::map<ui::Rotation /*rotation*/, std::array<vec2, 4> /*mappedCorners*/>
8010 kMappedCorners = {
8011 {ui::ROTATION_0, {{{0, 0}, {479.5, 0}, {479.5, 799.75}, {0, 799.75}}}},
8012 {ui::ROTATION_90, {{{0.5, 0}, {480, 0}, {480, 799.75}, {0.5, 799.75}}}},
8013 {ui::ROTATION_180, {{{0.5, 0.25}, {480, 0.25}, {480, 800}, {0.5, 800}}}},
8014 {ui::ROTATION_270, {{{0, 0.25}, {479.5, 0.25}, {479.5, 800}, {0, 800}}}},
8015 };
8016
8017 const ui::Rotation displayRotation = GetParam();
8018
8019 addConfigurationProperty("touch.deviceType", "touchScreen");
8020 prepareDisplay(displayRotation);
8021
Arpit Singha8c236b2023-04-25 13:56:05 +00008022 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan82687402022-12-06 01:32:53 +00008023
8024 const auto& expectedPoints = kMappedCorners.at(displayRotation);
8025
8026 // Test all four corners.
8027 for (int i = 0; i < 4; i++) {
8028 const auto& expected = expectedPoints[i];
8029 const auto& raw = kRawCorners[i];
8030 processDown(mapper, raw.x, raw.y);
8031 processSync(mapper);
8032 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8033 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8034 WithCoords(expected.x, expected.y), WithPrecision(2, 4))))
8035 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
8036 << "with display rotation " << ui::toCString(displayRotation)
8037 << ", expected point (" << expected.x << ", " << expected.y << ").";
8038 processUp(mapper);
8039 processSync(mapper);
8040 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8041 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
8042 WithCoords(expected.x, expected.y))));
8043 }
8044}
8045
Prabir Pradhan3e798762022-12-02 21:02:11 +00008046TEST_P(TouchscreenPrecisionTestsFixture, RotationPrecisionOrientationAwareInOri270) {
8047 static const std::map<ui::Rotation /*orientation*/, std::array<vec2, 4> /*mappedCorners*/>
8048 kMappedCorners = {
8049 {ui::ROTATION_0, {{{799.75, 0}, {799.75, 479.5}, {0, 479.5}, {0, 0}}}},
8050 {ui::ROTATION_90, {{{800, 0}, {800, 479.5}, {0.25, 479.5}, {0.25, 0}}}},
8051 {ui::ROTATION_180, {{{800, 0.5}, {800, 480}, {0.25, 480}, {0.25, 0.5}}}},
8052 {ui::ROTATION_270, {{{799.75, 0.5}, {799.75, 480}, {0, 480}, {0, 0.5}}}},
8053 };
8054
8055 const ui::Rotation displayRotation = GetParam();
8056
8057 addConfigurationProperty("touch.deviceType", "touchScreen");
8058 addConfigurationProperty("touch.orientation", "ORIENTATION_270");
8059
Arpit Singha8c236b2023-04-25 13:56:05 +00008060 SingleTouchInputMapper& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan3e798762022-12-02 21:02:11 +00008061
8062 // Ori 270, so width and height swapped
8063 const Rect physicalFrame{0, 0, DISPLAY_HEIGHT, DISPLAY_WIDTH};
8064 prepareDisplay(displayRotation);
8065 configurePhysicalDisplay(displayRotation, physicalFrame, DISPLAY_HEIGHT, DISPLAY_WIDTH);
8066
8067 const auto& expectedPoints = kMappedCorners.at(displayRotation);
8068
8069 // Test all four corners.
8070 for (int i = 0; i < 4; i++) {
8071 const auto& expected = expectedPoints[i];
8072 const auto& raw = kRawCorners[i];
8073 processDown(mapper, raw.x, raw.y);
8074 processSync(mapper);
8075 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8076 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8077 WithCoords(expected.x, expected.y), WithPrecision(4, 2))))
8078 << "Failed to process raw point (" << raw.x << ", " << raw.y << ") "
8079 << "with display rotation " << ui::toCString(displayRotation)
8080 << ", expected point (" << expected.x << ", " << expected.y << ").";
8081 processUp(mapper);
8082 processSync(mapper);
8083 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8084 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
8085 WithCoords(expected.x, expected.y))));
8086 }
8087}
8088
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008089TEST_P(TouchscreenPrecisionTestsFixture, MotionRangesAreOrientedInRotatedDisplay) {
8090 const ui::Rotation displayRotation = GetParam();
8091
8092 addConfigurationProperty("touch.deviceType", "touchScreen");
8093 prepareDisplay(displayRotation);
8094
8095 __attribute__((unused)) SingleTouchInputMapper& mapper =
Arpit Singha8c236b2023-04-25 13:56:05 +00008096 constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan46211fb2022-12-17 00:30:39 +00008097
8098 const InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
8099 // MotionRanges use display pixels as their units
8100 const auto* xRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_X, AINPUT_SOURCE_TOUCHSCREEN);
8101 const auto* yRange = deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_Y, AINPUT_SOURCE_TOUCHSCREEN);
8102
8103 // The MotionRanges should be oriented in the rotated display's coordinate space
8104 const bool displayRotated =
8105 displayRotation == ui::ROTATION_90 || displayRotation == ui::ROTATION_270;
8106
8107 constexpr float MAX_X = 479.5;
8108 constexpr float MAX_Y = 799.75;
8109 EXPECT_EQ(xRange->min, 0.f);
8110 EXPECT_EQ(yRange->min, 0.f);
8111 EXPECT_EQ(xRange->max, displayRotated ? MAX_Y : MAX_X);
8112 EXPECT_EQ(yRange->max, displayRotated ? MAX_X : MAX_Y);
8113
8114 EXPECT_EQ(xRange->flat, 8.f);
8115 EXPECT_EQ(yRange->flat, 8.f);
8116
8117 EXPECT_EQ(xRange->fuzz, 2.f);
8118 EXPECT_EQ(yRange->fuzz, 2.f);
8119
8120 EXPECT_EQ(xRange->resolution, 25.f); // pixels per millimeter
8121 EXPECT_EQ(yRange->resolution, 25.f); // pixels per millimeter
8122}
8123
Prabir Pradhana9df3162022-12-05 23:57:27 +00008124// Run the precision tests for all rotations.
8125INSTANTIATE_TEST_SUITE_P(TouchscreenPrecisionTests, TouchscreenPrecisionTestsFixture,
8126 ::testing::Values(ui::ROTATION_0, ui::ROTATION_90, ui::ROTATION_180,
8127 ui::ROTATION_270),
8128 [](const testing::TestParamInfo<ui::Rotation>& testParamInfo) {
8129 return ftl::enum_string(testParamInfo.param);
8130 });
8131
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008132// --- ExternalStylusFusionTest ---
8133
8134class ExternalStylusFusionTest : public SingleTouchInputMapperTest {
8135public:
8136 SingleTouchInputMapper& initializeInputMapperWithExternalStylus() {
8137 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008138 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008139 prepareButtons();
8140 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +00008141 auto& mapper = constructAndAddMapper<SingleTouchInputMapper>();
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008142
8143 mStylusState.when = ARBITRARY_TIME;
8144 mStylusState.pressure = 0.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008145 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008146 mReader->getContext()->setExternalStylusDevices({mExternalStylusDeviceInfo});
Prabir Pradhan4bf6d452023-04-18 21:26:56 +00008147 configureDevice(InputReaderConfiguration::Change::EXTERNAL_STYLUS_PRESENCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008148 processExternalStylusState(mapper);
8149 return mapper;
8150 }
8151
8152 std::list<NotifyArgs> processExternalStylusState(InputMapper& mapper) {
8153 std::list<NotifyArgs> generatedArgs = mapper.updateExternalStylusState(mStylusState);
8154 for (const NotifyArgs& args : generatedArgs) {
8155 mFakeListener->notify(args);
8156 }
8157 // Loop the reader to flush the input listener queue.
8158 mReader->loopOnce();
8159 return generatedArgs;
8160 }
8161
8162protected:
8163 StylusState mStylusState{};
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008164
8165 void testStartFusedStylusGesture(SingleTouchInputMapper& mapper) {
8166 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008167 AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008168
8169 // The first pointer is withheld.
8170 processDown(mapper, 100, 200);
8171 processSync(mapper);
8172 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8173 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8174 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8175
8176 // The external stylus reports pressure. The withheld finger pointer is released as a
8177 // stylus.
8178 mStylusState.pressure = 1.f;
8179 processExternalStylusState(mapper);
8180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8181 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8182 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8183
8184 // Subsequent pointer events are not withheld.
8185 processMove(mapper, 101, 201);
8186 processSync(mapper);
8187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8188 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8189
8190 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8191 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8192 }
8193
8194 void testSuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
8195 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8196
8197 // Releasing the touch pointer ends the gesture.
8198 processUp(mapper);
8199 processSync(mapper);
8200 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008201 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008202 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008203
8204 mStylusState.pressure = 0.f;
8205 processExternalStylusState(mapper);
8206 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8207 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8208 }
8209
8210 void testUnsuccessfulFusionGesture(SingleTouchInputMapper& mapper) {
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008211 // When stylus fusion is not successful, events should be reported with the original source.
8212 // In this case, it is from a touchscreen.
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008213 auto toolTypeSource =
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008214 AllOf(WithSource(AINPUT_SOURCE_TOUCHSCREEN), WithToolType(ToolType::FINGER));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008215
8216 // The first pointer is withheld when an external stylus is connected,
8217 // and a timeout is requested.
8218 processDown(mapper, 100, 200);
8219 processSync(mapper);
8220 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8221 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8222 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8223
8224 // If the timeout expires early, it is requested again.
8225 handleTimeout(mapper, ARBITRARY_TIME + 1);
8226 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasRequested(
8227 ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT));
8228
8229 // When the timeout expires, the withheld touch is released as a finger pointer.
8230 handleTimeout(mapper, ARBITRARY_TIME + EXTERNAL_STYLUS_DATA_TIMEOUT);
8231 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8232 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8233
8234 // Subsequent pointer events are not withheld.
8235 processMove(mapper, 101, 201);
8236 processSync(mapper);
8237 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8238 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8239 processUp(mapper);
8240 processSync(mapper);
8241 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8242 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8243
8244 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8246 }
8247
8248private:
8249 InputDeviceInfo mExternalStylusDeviceInfo{};
8250};
8251
8252TEST_F(ExternalStylusFusionTest, UsesBluetoothStylusSource) {
8253 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008254 ASSERT_EQ(STYLUS_FUSION_SOURCE, mapper.getSources());
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008255}
8256
8257TEST_F(ExternalStylusFusionTest, UnsuccessfulFusion) {
8258 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8259 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8260}
8261
8262TEST_F(ExternalStylusFusionTest, SuccessfulFusion_TouchFirst) {
8263 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8264 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8265}
8266
8267// Test a successful stylus fusion gesture where the pressure is reported by the external
8268// before the touch is reported by the touchscreen.
8269TEST_F(ExternalStylusFusionTest, SuccessfulFusion_PressureFirst) {
8270 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008271 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008272
8273 // The external stylus reports pressure first. It is ignored for now.
8274 mStylusState.pressure = 1.f;
8275 processExternalStylusState(mapper);
8276 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8277 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8278
8279 // When the touch goes down afterwards, it is reported as a stylus pointer.
8280 processDown(mapper, 100, 200);
8281 processSync(mapper);
8282 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8283 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN))));
8284 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8285
8286 processMove(mapper, 101, 201);
8287 processSync(mapper);
8288 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8289 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE))));
8290 processUp(mapper);
8291 processSync(mapper);
8292 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8293 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP))));
8294
8295 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8296 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8297}
8298
8299TEST_F(ExternalStylusFusionTest, FusionIsRepeatedForEachNewGesture) {
8300 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
8301
8302 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8303 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8304
8305 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8306 ASSERT_NO_FATAL_FAILURE(testSuccessfulFusionGesture(mapper));
8307 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8308 ASSERT_NO_FATAL_FAILURE(testUnsuccessfulFusionGesture(mapper));
8309}
8310
8311TEST_F(ExternalStylusFusionTest, FusedPointerReportsPressureChanges) {
8312 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008313 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008314
8315 mStylusState.pressure = 0.8f;
8316 processExternalStylusState(mapper);
8317 processDown(mapper, 100, 200);
8318 processSync(mapper);
8319 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8320 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
8321 WithPressure(0.8f))));
8322 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8323
8324 // The external stylus reports a pressure change. We wait for some time for a touch event.
8325 mStylusState.pressure = 0.6f;
8326 processExternalStylusState(mapper);
8327 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8328 ASSERT_NO_FATAL_FAILURE(
8329 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8330
8331 // If a touch is reported within the timeout, it reports the updated pressure.
8332 processMove(mapper, 101, 201);
8333 processSync(mapper);
8334 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8335 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8336 WithPressure(0.6f))));
8337 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8338
8339 // There is another pressure change.
8340 mStylusState.pressure = 0.5f;
8341 processExternalStylusState(mapper);
8342 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8343 ASSERT_NO_FATAL_FAILURE(
8344 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8345
8346 // If a touch is not reported within the timeout, a move event is generated to report
8347 // the new pressure.
8348 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8349 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8350 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8351 WithPressure(0.5f))));
8352
8353 // If a zero pressure is reported before the touch goes up, the previous pressure value is
8354 // repeated indefinitely.
8355 mStylusState.pressure = 0.0f;
8356 processExternalStylusState(mapper);
8357 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8358 ASSERT_NO_FATAL_FAILURE(
8359 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8360 processMove(mapper, 102, 202);
8361 processSync(mapper);
8362 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8363 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8364 WithPressure(0.5f))));
8365 processMove(mapper, 103, 203);
8366 processSync(mapper);
8367 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8368 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8369 WithPressure(0.5f))));
8370
8371 processUp(mapper);
8372 processSync(mapper);
8373 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008374 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithSource(STYLUS_FUSION_SOURCE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008375 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008376
8377 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8378 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8379}
8380
8381TEST_F(ExternalStylusFusionTest, FusedPointerReportsToolTypeChanges) {
8382 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008383 auto source = WithSource(STYLUS_FUSION_SOURCE);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008384
8385 mStylusState.pressure = 1.f;
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008386 mStylusState.toolType = ToolType::ERASER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008387 processExternalStylusState(mapper);
8388 processDown(mapper, 100, 200);
8389 processSync(mapper);
8390 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8391 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008392 WithToolType(ToolType::ERASER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008393 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8394
8395 // The external stylus reports a tool change. We wait for some time for a touch event.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008396 mStylusState.toolType = ToolType::STYLUS;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008397 processExternalStylusState(mapper);
8398 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8399 ASSERT_NO_FATAL_FAILURE(
8400 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8401
8402 // If a touch is reported within the timeout, it reports the updated pressure.
8403 processMove(mapper, 101, 201);
8404 processSync(mapper);
8405 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8406 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008407 WithToolType(ToolType::STYLUS))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008408 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8409
8410 // There is another tool type change.
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008411 mStylusState.toolType = ToolType::FINGER;
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008412 processExternalStylusState(mapper);
8413 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8414 ASSERT_NO_FATAL_FAILURE(
8415 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8416
8417 // If a touch is not reported within the timeout, a move event is generated to report
8418 // the new tool type.
8419 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
8420 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8421 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008422 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008423
8424 processUp(mapper);
8425 processSync(mapper);
8426 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8427 AllOf(source, WithMotionAction(AMOTION_EVENT_ACTION_UP),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008428 WithToolType(ToolType::FINGER))));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008429
8430 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8431 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8432}
8433
8434TEST_F(ExternalStylusFusionTest, FusedPointerReportsButtons) {
8435 SingleTouchInputMapper& mapper = initializeInputMapperWithExternalStylus();
Prabir Pradhanb08a0e82023-09-14 22:28:32 +00008436 auto toolTypeSource = AllOf(WithSource(STYLUS_FUSION_SOURCE), WithToolType(ToolType::STYLUS));
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008437
8438 ASSERT_NO_FATAL_FAILURE(testStartFusedStylusGesture(mapper));
8439
8440 // The external stylus reports a button change. We wait for some time for a touch event.
8441 mStylusState.buttons = AMOTION_EVENT_BUTTON_STYLUS_PRIMARY;
8442 processExternalStylusState(mapper);
8443 ASSERT_NO_FATAL_FAILURE(
8444 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8445
8446 // If a touch is reported within the timeout, it reports the updated button state.
8447 processMove(mapper, 101, 201);
8448 processSync(mapper);
8449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8450 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8451 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8452 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8453 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
8454 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
8455 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8456
8457 // The button is now released.
8458 mStylusState.buttons = 0;
8459 processExternalStylusState(mapper);
8460 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8461 ASSERT_NO_FATAL_FAILURE(
8462 mReader->getContext()->assertTimeoutWasRequested(ARBITRARY_TIME + TOUCH_DATA_TIMEOUT));
8463
8464 // If a touch is not reported within the timeout, a move event is generated to report
8465 // the new button state.
8466 handleTimeout(mapper, ARBITRARY_TIME + TOUCH_DATA_TIMEOUT);
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008467 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
8468 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE),
8469 WithButtonState(0))));
8470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan124ea442022-10-28 20:27:44 +00008471 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
8472 WithButtonState(0))));
8473
8474 processUp(mapper);
8475 processSync(mapper);
8476 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
Prabir Pradhan7d04c4b2022-10-28 19:23:26 +00008477 AllOf(toolTypeSource, WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
8478
8479 ASSERT_NO_FATAL_FAILURE(mReader->getContext()->assertTimeoutWasNotRequested());
8480 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8481}
8482
Michael Wrightd02c5b62014-02-10 15:10:22 -08008483// --- MultiTouchInputMapperTest ---
8484
8485class MultiTouchInputMapperTest : public TouchInputMapperTest {
8486protected:
8487 void prepareAxes(int axes);
8488
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008489 void processPosition(MultiTouchInputMapper& mapper, int32_t x, int32_t y);
8490 void processTouchMajor(MultiTouchInputMapper& mapper, int32_t touchMajor);
8491 void processTouchMinor(MultiTouchInputMapper& mapper, int32_t touchMinor);
8492 void processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor);
8493 void processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor);
8494 void processOrientation(MultiTouchInputMapper& mapper, int32_t orientation);
8495 void processPressure(MultiTouchInputMapper& mapper, int32_t pressure);
8496 void processDistance(MultiTouchInputMapper& mapper, int32_t distance);
8497 void processId(MultiTouchInputMapper& mapper, int32_t id);
8498 void processSlot(MultiTouchInputMapper& mapper, int32_t slot);
8499 void processToolType(MultiTouchInputMapper& mapper, int32_t toolType);
8500 void processKey(MultiTouchInputMapper& mapper, int32_t code, int32_t value);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008501 void processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode, int32_t value);
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008502 void processMTSync(MultiTouchInputMapper& mapper);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008503 void processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime = ARBITRARY_TIME,
8504 nsecs_t readTime = READ_TIME);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008505};
8506
8507void MultiTouchInputMapperTest::prepareAxes(int axes) {
8508 if (axes & POSITION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
8510 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008511 }
8512 if (axes & TOUCH) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008513 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN,
8514 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008515 if (axes & MINOR) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008516 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN,
8517 RAW_TOUCH_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008518 }
8519 }
8520 if (axes & TOOL) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008521 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8522 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008523 if (axes & MINOR) {
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008524 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN,
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008525 RAW_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008526 }
8527 }
8528 if (axes & ORIENTATION) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008529 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_ORIENTATION, RAW_ORIENTATION_MIN,
8530 RAW_ORIENTATION_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008531 }
8532 if (axes & PRESSURE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008533 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_PRESSURE, RAW_PRESSURE_MIN,
8534 RAW_PRESSURE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008535 }
8536 if (axes & DISTANCE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008537 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_DISTANCE, RAW_DISTANCE_MIN,
8538 RAW_DISTANCE_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008539 }
8540 if (axes & ID) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008541 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX, 0,
8542 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008543 }
8544 if (axes & SLOT) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008545 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX, 0, 0);
8546 mFakeEventHub->setAbsoluteAxisValue(EVENTHUB_ID, ABS_MT_SLOT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008547 }
8548 if (axes & TOOL_TYPE) {
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08008549 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOOL_TYPE, 0, MT_TOOL_MAX, 0, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008550 }
8551}
8552
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008553void MultiTouchInputMapperTest::processPosition(MultiTouchInputMapper& mapper, int32_t x,
8554 int32_t y) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008555 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_X, x);
8556 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_POSITION_Y, y);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008557}
8558
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008559void MultiTouchInputMapperTest::processTouchMajor(MultiTouchInputMapper& mapper,
8560 int32_t touchMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008561 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MAJOR, touchMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008562}
8563
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008564void MultiTouchInputMapperTest::processTouchMinor(MultiTouchInputMapper& mapper,
8565 int32_t touchMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008566 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOUCH_MINOR, touchMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008567}
8568
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008569void MultiTouchInputMapperTest::processToolMajor(MultiTouchInputMapper& mapper, int32_t toolMajor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008570 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MAJOR, toolMajor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008571}
8572
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008573void MultiTouchInputMapperTest::processToolMinor(MultiTouchInputMapper& mapper, int32_t toolMinor) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008574 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_WIDTH_MINOR, toolMinor);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008575}
8576
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008577void MultiTouchInputMapperTest::processOrientation(MultiTouchInputMapper& mapper,
8578 int32_t orientation) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008579 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_ORIENTATION, orientation);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008580}
8581
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008582void MultiTouchInputMapperTest::processPressure(MultiTouchInputMapper& mapper, int32_t pressure) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008583 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_PRESSURE, pressure);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008584}
8585
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008586void MultiTouchInputMapperTest::processDistance(MultiTouchInputMapper& mapper, int32_t distance) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008587 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_DISTANCE, distance);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008588}
8589
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008590void MultiTouchInputMapperTest::processId(MultiTouchInputMapper& mapper, int32_t id) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008591 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TRACKING_ID, id);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008592}
8593
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008594void MultiTouchInputMapperTest::processSlot(MultiTouchInputMapper& mapper, int32_t slot) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008595 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_SLOT, slot);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008596}
8597
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008598void MultiTouchInputMapperTest::processToolType(MultiTouchInputMapper& mapper, int32_t toolType) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008599 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, ABS_MT_TOOL_TYPE, toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008600}
8601
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008602void MultiTouchInputMapperTest::processKey(MultiTouchInputMapper& mapper, int32_t code,
8603 int32_t value) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008604 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, code, value);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008605}
8606
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00008607void MultiTouchInputMapperTest::processHidUsage(MultiTouchInputMapper& mapper, int32_t usageCode,
8608 int32_t value) {
8609 process(mapper, ARBITRARY_TIME, READ_TIME, EV_MSC, MSC_SCAN, usageCode);
8610 process(mapper, ARBITRARY_TIME, READ_TIME, EV_KEY, KEY_UNKNOWN, value);
8611}
8612
Nathaniel R. Lewisf4916ef2020-01-14 11:57:18 -08008613void MultiTouchInputMapperTest::processMTSync(MultiTouchInputMapper& mapper) {
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +00008614 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_MT_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008615}
8616
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +00008617void MultiTouchInputMapperTest::processSync(MultiTouchInputMapper& mapper, nsecs_t eventTime,
8618 nsecs_t readTime) {
8619 process(mapper, eventTime, readTime, EV_SYN, SYN_REPORT, 0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008620}
8621
Michael Wrightd02c5b62014-02-10 15:10:22 -08008622TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithoutTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008623 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008624 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008625 prepareAxes(POSITION);
8626 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008627 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008628
arthurhungdcef2dc2020-08-11 14:47:50 +08008629 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008630
8631 NotifyMotionArgs motionArgs;
8632
8633 // Two fingers down at once.
8634 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8635 processPosition(mapper, x1, y1);
8636 processMTSync(mapper);
8637 processPosition(mapper, x2, y2);
8638 processMTSync(mapper);
8639 processSync(mapper);
8640
8641 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8642 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8643 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8644 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8645 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8646 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
8647 ASSERT_EQ(0, motionArgs.flags);
8648 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8649 ASSERT_EQ(0, motionArgs.buttonState);
8650 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008651 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008652 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008653 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008654 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8655 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8656 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8657 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8658 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8659
8660 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8661 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8662 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8663 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8664 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008665 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008666 ASSERT_EQ(0, motionArgs.flags);
8667 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8668 ASSERT_EQ(0, motionArgs.buttonState);
8669 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008670 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008671 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008672 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008673 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008674 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008675 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8676 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8677 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8678 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8679 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8680 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8681 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8682
8683 // Move.
8684 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8685 processPosition(mapper, x1, y1);
8686 processMTSync(mapper);
8687 processPosition(mapper, x2, y2);
8688 processMTSync(mapper);
8689 processSync(mapper);
8690
8691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8692 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8693 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8694 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8695 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8696 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8697 ASSERT_EQ(0, motionArgs.flags);
8698 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8699 ASSERT_EQ(0, motionArgs.buttonState);
8700 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008701 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008702 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008703 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008704 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008705 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008706 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8707 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8708 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8709 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8710 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8711 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8712 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8713
8714 // First finger up.
8715 x2 += 15; y2 -= 20;
8716 processPosition(mapper, x2, y2);
8717 processMTSync(mapper);
8718 processSync(mapper);
8719
8720 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8721 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8722 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8723 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8724 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008725 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008726 ASSERT_EQ(0, motionArgs.flags);
8727 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8728 ASSERT_EQ(0, motionArgs.buttonState);
8729 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008730 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008731 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008732 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008733 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008734 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008735 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8736 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8737 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8738 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8739 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8740 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8741 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8742
8743 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8744 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8745 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8746 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8747 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8748 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8749 ASSERT_EQ(0, motionArgs.flags);
8750 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8751 ASSERT_EQ(0, motionArgs.buttonState);
8752 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008753 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008754 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008755 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008756 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8757 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8758 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8759 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8760 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8761
8762 // Move.
8763 x2 += 20; y2 -= 25;
8764 processPosition(mapper, x2, y2);
8765 processMTSync(mapper);
8766 processSync(mapper);
8767
8768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8769 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8770 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8771 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8772 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8773 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8774 ASSERT_EQ(0, motionArgs.flags);
8775 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8776 ASSERT_EQ(0, motionArgs.buttonState);
8777 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008778 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008779 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008780 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008781 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8782 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8783 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8784 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8785 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8786
8787 // New finger down.
8788 int32_t x3 = 700, y3 = 300;
8789 processPosition(mapper, x2, y2);
8790 processMTSync(mapper);
8791 processPosition(mapper, x3, y3);
8792 processMTSync(mapper);
8793 processSync(mapper);
8794
8795 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8796 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8797 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8798 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8799 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008800 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008801 ASSERT_EQ(0, motionArgs.flags);
8802 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8803 ASSERT_EQ(0, motionArgs.buttonState);
8804 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008805 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008806 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008807 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008808 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008809 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008810 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8811 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8812 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8813 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8814 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8815 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8816 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8817
8818 // Second finger up.
8819 x3 += 30; y3 -= 20;
8820 processPosition(mapper, x3, y3);
8821 processMTSync(mapper);
8822 processSync(mapper);
8823
8824 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8825 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8826 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8827 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8828 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008829 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008830 ASSERT_EQ(0, motionArgs.flags);
8831 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8832 ASSERT_EQ(0, motionArgs.buttonState);
8833 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008834 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008835 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008836 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008837 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008838 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008839 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8840 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8841 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8842 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8843 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8844 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8845 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8846
8847 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8848 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8849 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8850 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8851 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8852 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
8853 ASSERT_EQ(0, motionArgs.flags);
8854 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8855 ASSERT_EQ(0, motionArgs.buttonState);
8856 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008857 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008858 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008859 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008860 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8861 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8862 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8863 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8864 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8865
8866 // Last finger up.
8867 processMTSync(mapper);
8868 processSync(mapper);
8869
8870 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8871 ASSERT_EQ(ARBITRARY_TIME, motionArgs.eventTime);
8872 ASSERT_EQ(DEVICE_ID, motionArgs.deviceId);
8873 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, motionArgs.source);
8874 ASSERT_EQ(uint32_t(0), motionArgs.policyFlags);
8875 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
8876 ASSERT_EQ(0, motionArgs.flags);
8877 ASSERT_EQ(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON, motionArgs.metaState);
8878 ASSERT_EQ(0, motionArgs.buttonState);
8879 ASSERT_EQ(0, motionArgs.edgeFlags);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008880 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008881 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008882 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008883 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8884 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
8885 ASSERT_NEAR(X_PRECISION, motionArgs.xPrecision, EPSILON);
8886 ASSERT_NEAR(Y_PRECISION, motionArgs.yPrecision, EPSILON);
8887 ASSERT_EQ(ARBITRARY_TIME, motionArgs.downTime);
8888
8889 // Should not have sent any more keys or motions.
8890 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
8891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
8892}
8893
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008894TEST_F(MultiTouchInputMapperTest, AxisResolution_IsPopulated) {
8895 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008896 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008897
8898 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8899 /*fuzz*/ 0, /*resolution*/ 10);
8900 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8901 /*fuzz*/ 0, /*resolution*/ 11);
8902 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MAJOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8903 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 12);
8904 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_TOUCH_MINOR, RAW_TOUCH_MIN, RAW_TOUCH_MAX,
8905 /*flat*/ 0, /*fuzz*/ 0, /*resolution*/ 13);
8906 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MAJOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8907 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 14);
8908 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_WIDTH_MINOR, RAW_TOOL_MIN, RAW_TOOL_MAX,
8909 /*flat*/ 0, /*flat*/ 0, /*resolution*/ 15);
8910
Arpit Singha8c236b2023-04-25 13:56:05 +00008911 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008912
8913 // X and Y axes
8914 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_X, 10 / X_PRECISION);
8915 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_Y, 11 / Y_PRECISION);
8916 // Touch major and minor
8917 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR, 12 * GEOMETRIC_SCALE);
8918 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR, 13 * GEOMETRIC_SCALE);
8919 // Tool major and minor
8920 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR, 14 * GEOMETRIC_SCALE);
8921 assertAxisResolution(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR, 15 * GEOMETRIC_SCALE);
8922}
8923
8924TEST_F(MultiTouchInputMapperTest, TouchMajorAndMinorAxes_DoNotAppearIfNotSupported) {
8925 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008926 prepareDisplay(ui::ROTATION_0);
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008927
8928 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX, /*flat*/ 0,
8929 /*fuzz*/ 0, /*resolution*/ 10);
8930 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX, /*flat*/ 0,
8931 /*fuzz*/ 0, /*resolution*/ 11);
8932
8933 // We do not add ABS_MT_TOUCH_MAJOR / MINOR or ABS_MT_WIDTH_MAJOR / MINOR axes
8934
Arpit Singha8c236b2023-04-25 13:56:05 +00008935 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -08008936
8937 // Touch major and minor
8938 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MAJOR);
8939 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOUCH_MINOR);
8940 // Tool major and minor
8941 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MAJOR);
8942 assertAxisNotPresent(mapper, AMOTION_EVENT_AXIS_TOOL_MINOR);
8943}
8944
Michael Wrightd02c5b62014-02-10 15:10:22 -08008945TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithTrackingIds) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08008946 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00008947 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008948 prepareAxes(POSITION | ID);
8949 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00008950 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08008951
arthurhungdcef2dc2020-08-11 14:47:50 +08008952 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008953
8954 NotifyMotionArgs motionArgs;
8955
8956 // Two fingers down at once.
8957 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
8958 processPosition(mapper, x1, y1);
8959 processId(mapper, 1);
8960 processMTSync(mapper);
8961 processPosition(mapper, x2, y2);
8962 processId(mapper, 2);
8963 processMTSync(mapper);
8964 processSync(mapper);
8965
8966 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8967 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008968 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008969 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008970 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008971 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8972 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8973
8974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08008975 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008976 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008977 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008978 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008979 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07008980 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08008981 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
8982 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
8983 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
8984 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
8985
8986 // Move.
8987 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
8988 processPosition(mapper, x1, y1);
8989 processId(mapper, 1);
8990 processMTSync(mapper);
8991 processPosition(mapper, x2, y2);
8992 processId(mapper, 2);
8993 processMTSync(mapper);
8994 processSync(mapper);
8995
8996 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
8997 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07008998 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08008999 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009000 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009001 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009002 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9004 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9005 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9006 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9007
9008 // First finger up.
9009 x2 += 15; y2 -= 20;
9010 processPosition(mapper, x2, y2);
9011 processId(mapper, 2);
9012 processMTSync(mapper);
9013 processSync(mapper);
9014
9015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009016 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009017 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009018 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009019 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009020 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009021 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9023 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9024 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9025 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9026
9027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9028 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009029 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009030 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009031 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009032 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9033 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9034
9035 // Move.
9036 x2 += 20; y2 -= 25;
9037 processPosition(mapper, x2, y2);
9038 processId(mapper, 2);
9039 processMTSync(mapper);
9040 processSync(mapper);
9041
9042 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9043 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009044 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009045 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009046 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009047 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9048 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9049
9050 // New finger down.
9051 int32_t x3 = 700, y3 = 300;
9052 processPosition(mapper, x2, y2);
9053 processId(mapper, 2);
9054 processMTSync(mapper);
9055 processPosition(mapper, x3, y3);
9056 processId(mapper, 3);
9057 processMTSync(mapper);
9058 processSync(mapper);
9059
9060 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009061 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009062 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009063 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009064 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009065 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009066 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009067 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9068 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9069 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9070 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9071
9072 // Second finger up.
9073 x3 += 30; y3 -= 20;
9074 processPosition(mapper, x3, y3);
9075 processId(mapper, 3);
9076 processMTSync(mapper);
9077 processSync(mapper);
9078
9079 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009080 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009081 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009082 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009083 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009084 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009085 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009086 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9087 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9088 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9089 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9090
9091 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9092 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009093 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009094 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009095 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009096 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9097 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9098
9099 // Last finger up.
9100 processMTSync(mapper);
9101 processSync(mapper);
9102
9103 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9104 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009105 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009106 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009107 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009108 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9109 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9110
9111 // Should not have sent any more keys or motions.
9112 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9113 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9114}
9115
9116TEST_F(MultiTouchInputMapperTest, Process_NormalMultiTouchGesture_WithSlots) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009117 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009118 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009119 prepareAxes(POSITION | ID | SLOT);
9120 prepareVirtualKeys();
Arpit Singha8c236b2023-04-25 13:56:05 +00009121 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009122
arthurhungdcef2dc2020-08-11 14:47:50 +08009123 mReader->getContext()->setGlobalMetaState(AMETA_SHIFT_LEFT_ON | AMETA_SHIFT_ON);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009124
9125 NotifyMotionArgs motionArgs;
9126
9127 // Two fingers down at once.
9128 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
9129 processPosition(mapper, x1, y1);
9130 processId(mapper, 1);
9131 processSlot(mapper, 1);
9132 processPosition(mapper, x2, y2);
9133 processId(mapper, 2);
9134 processSync(mapper);
9135
9136 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9137 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009138 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009139 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009140 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009141 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9142 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9143
9144 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009145 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009146 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009147 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009148 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009149 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009150 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009151 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9152 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9153 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9154 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9155
9156 // Move.
9157 x1 += 10; y1 += 15; x2 += 5; y2 -= 10;
9158 processSlot(mapper, 0);
9159 processPosition(mapper, x1, y1);
9160 processSlot(mapper, 1);
9161 processPosition(mapper, x2, y2);
9162 processSync(mapper);
9163
9164 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9165 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009166 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009167 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009168 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009169 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009170 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009171 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9172 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9173 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9174 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9175
9176 // First finger up.
9177 x2 += 15; y2 -= 20;
9178 processSlot(mapper, 0);
9179 processId(mapper, -1);
9180 processSlot(mapper, 1);
9181 processPosition(mapper, x2, y2);
9182 processSync(mapper);
9183
9184 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009185 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009186 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009187 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009188 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009189 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009190 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009191 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9192 toDisplayX(x1), toDisplayY(y1), 1, 0, 0, 0, 0, 0, 0, 0));
9193 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9194 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9195
9196 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9197 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009198 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009199 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009200 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009201 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9202 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9203
9204 // Move.
9205 x2 += 20; y2 -= 25;
9206 processPosition(mapper, x2, y2);
9207 processSync(mapper);
9208
9209 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9210 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009211 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009212 ASSERT_EQ(1, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009213 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009214 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9215 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9216
9217 // New finger down.
9218 int32_t x3 = 700, y3 = 300;
9219 processPosition(mapper, x2, y2);
9220 processSlot(mapper, 0);
9221 processId(mapper, 3);
9222 processPosition(mapper, x3, y3);
9223 processSync(mapper);
9224
9225 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009226 ASSERT_EQ(ACTION_POINTER_0_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009227 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009228 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009229 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009230 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009231 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009232 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9233 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9234 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9235 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9236
9237 // Second finger up.
9238 x3 += 30; y3 -= 20;
9239 processSlot(mapper, 1);
9240 processId(mapper, -1);
9241 processSlot(mapper, 0);
9242 processPosition(mapper, x3, y3);
9243 processSync(mapper);
9244
9245 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009246 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009247 ASSERT_EQ(size_t(2), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009248 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009249 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009250 ASSERT_EQ(1, motionArgs.pointerProperties[1].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009251 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009252 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9253 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9254 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1],
9255 toDisplayX(x2), toDisplayY(y2), 1, 0, 0, 0, 0, 0, 0, 0));
9256
9257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9258 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009259 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009260 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009261 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009262 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9263 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9264
9265 // Last finger up.
9266 processId(mapper, -1);
9267 processSync(mapper);
9268
9269 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9270 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009271 ASSERT_EQ(size_t(1), motionArgs.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009272 ASSERT_EQ(0, motionArgs.pointerProperties[0].id);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009273 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009274 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9275 toDisplayX(x3), toDisplayY(y3), 1, 0, 0, 0, 0, 0, 0, 0));
9276
9277 // Should not have sent any more keys or motions.
9278 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9279 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
9280}
9281
9282TEST_F(MultiTouchInputMapperTest, Process_AllAxes_WithDefaultCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009283 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009284 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009285 prepareAxes(POSITION | TOUCH | TOOL | PRESSURE | ORIENTATION | ID | MINOR | DISTANCE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009286 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009287
9288 // These calculations are based on the input device calibration documentation.
9289 int32_t rawX = 100;
9290 int32_t rawY = 200;
9291 int32_t rawTouchMajor = 7;
9292 int32_t rawTouchMinor = 6;
9293 int32_t rawToolMajor = 9;
9294 int32_t rawToolMinor = 8;
9295 int32_t rawPressure = 11;
9296 int32_t rawDistance = 0;
9297 int32_t rawOrientation = 3;
9298 int32_t id = 5;
9299
9300 float x = toDisplayX(rawX);
9301 float y = toDisplayY(rawY);
9302 float pressure = float(rawPressure) / RAW_PRESSURE_MAX;
9303 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9304 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9305 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9306 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9307 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9308 float orientation = float(rawOrientation) / RAW_ORIENTATION_MAX * M_PI_2;
9309 float distance = float(rawDistance);
9310
9311 processPosition(mapper, rawX, rawY);
9312 processTouchMajor(mapper, rawTouchMajor);
9313 processTouchMinor(mapper, rawTouchMinor);
9314 processToolMajor(mapper, rawToolMajor);
9315 processToolMinor(mapper, rawToolMinor);
9316 processPressure(mapper, rawPressure);
9317 processOrientation(mapper, rawOrientation);
9318 processDistance(mapper, rawDistance);
9319 processId(mapper, id);
9320 processMTSync(mapper);
9321 processSync(mapper);
9322
9323 NotifyMotionArgs args;
9324 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9325 ASSERT_EQ(0, args.pointerProperties[0].id);
9326 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9327 x, y, pressure, size, touchMajor, touchMinor, toolMajor, toolMinor,
9328 orientation, distance));
9329}
9330
9331TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_GeometricCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009332 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009333 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009334 prepareAxes(POSITION | TOUCH | TOOL | MINOR);
9335 addConfigurationProperty("touch.size.calibration", "geometric");
Arpit Singha8c236b2023-04-25 13:56:05 +00009336 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009337
9338 // These calculations are based on the input device calibration documentation.
9339 int32_t rawX = 100;
9340 int32_t rawY = 200;
9341 int32_t rawTouchMajor = 140;
9342 int32_t rawTouchMinor = 120;
9343 int32_t rawToolMajor = 180;
9344 int32_t rawToolMinor = 160;
9345
9346 float x = toDisplayX(rawX);
9347 float y = toDisplayY(rawY);
9348 float size = avg(rawTouchMajor, rawTouchMinor) / RAW_TOUCH_MAX;
9349 float toolMajor = float(rawToolMajor) * GEOMETRIC_SCALE;
9350 float toolMinor = float(rawToolMinor) * GEOMETRIC_SCALE;
9351 float touchMajor = float(rawTouchMajor) * GEOMETRIC_SCALE;
9352 float touchMinor = float(rawTouchMinor) * GEOMETRIC_SCALE;
9353
9354 processPosition(mapper, rawX, rawY);
9355 processTouchMajor(mapper, rawTouchMajor);
9356 processTouchMinor(mapper, rawTouchMinor);
9357 processToolMajor(mapper, rawToolMajor);
9358 processToolMinor(mapper, rawToolMinor);
9359 processMTSync(mapper);
9360 processSync(mapper);
9361
9362 NotifyMotionArgs args;
9363 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9364 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9365 x, y, 1.0f, size, touchMajor, touchMinor, toolMajor, toolMinor, 0, 0));
9366}
9367
9368TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_SummedLinearCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009369 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009370 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009371 prepareAxes(POSITION | TOUCH | TOOL);
9372 addConfigurationProperty("touch.size.calibration", "diameter");
9373 addConfigurationProperty("touch.size.scale", "10");
9374 addConfigurationProperty("touch.size.bias", "160");
9375 addConfigurationProperty("touch.size.isSummed", "1");
Arpit Singha8c236b2023-04-25 13:56:05 +00009376 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009377
9378 // These calculations are based on the input device calibration documentation.
9379 // Note: We only provide a single common touch/tool value because the device is assumed
9380 // not to emit separate values for each pointer (isSummed = 1).
9381 int32_t rawX = 100;
9382 int32_t rawY = 200;
9383 int32_t rawX2 = 150;
9384 int32_t rawY2 = 250;
9385 int32_t rawTouchMajor = 5;
9386 int32_t rawToolMajor = 8;
9387
9388 float x = toDisplayX(rawX);
9389 float y = toDisplayY(rawY);
9390 float x2 = toDisplayX(rawX2);
9391 float y2 = toDisplayY(rawY2);
9392 float size = float(rawTouchMajor) / 2 / RAW_TOUCH_MAX;
9393 float touch = float(rawTouchMajor) / 2 * 10.0f + 160.0f;
9394 float tool = float(rawToolMajor) / 2 * 10.0f + 160.0f;
9395
9396 processPosition(mapper, rawX, rawY);
9397 processTouchMajor(mapper, rawTouchMajor);
9398 processToolMajor(mapper, rawToolMajor);
9399 processMTSync(mapper);
9400 processPosition(mapper, rawX2, rawY2);
9401 processTouchMajor(mapper, rawTouchMajor);
9402 processToolMajor(mapper, rawToolMajor);
9403 processMTSync(mapper);
9404 processSync(mapper);
9405
9406 NotifyMotionArgs args;
9407 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9408 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
9409
9410 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -08009411 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -07009412 ASSERT_EQ(size_t(2), args.getPointerCount());
Michael Wrightd02c5b62014-02-10 15:10:22 -08009413 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9414 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9415 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[1],
9416 x2, y2, 1.0f, size, touch, touch, tool, tool, 0, 0));
9417}
9418
9419TEST_F(MultiTouchInputMapperTest, Process_TouchAndToolAxes_AreaCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009420 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009421 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009422 prepareAxes(POSITION | TOUCH | TOOL);
9423 addConfigurationProperty("touch.size.calibration", "area");
9424 addConfigurationProperty("touch.size.scale", "43");
9425 addConfigurationProperty("touch.size.bias", "3");
Arpit Singha8c236b2023-04-25 13:56:05 +00009426 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009427
9428 // These calculations are based on the input device calibration documentation.
9429 int32_t rawX = 100;
9430 int32_t rawY = 200;
9431 int32_t rawTouchMajor = 5;
9432 int32_t rawToolMajor = 8;
9433
9434 float x = toDisplayX(rawX);
9435 float y = toDisplayY(rawY);
9436 float size = float(rawTouchMajor) / RAW_TOUCH_MAX;
9437 float touch = sqrtf(rawTouchMajor) * 43.0f + 3.0f;
9438 float tool = sqrtf(rawToolMajor) * 43.0f + 3.0f;
9439
9440 processPosition(mapper, rawX, rawY);
9441 processTouchMajor(mapper, rawTouchMajor);
9442 processToolMajor(mapper, rawToolMajor);
9443 processMTSync(mapper);
9444 processSync(mapper);
9445
9446 NotifyMotionArgs args;
9447 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9448 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9449 x, y, 1.0f, size, touch, touch, tool, tool, 0, 0));
9450}
9451
9452TEST_F(MultiTouchInputMapperTest, Process_PressureAxis_AmplitudeCalibration) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009453 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009454 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009455 prepareAxes(POSITION | PRESSURE);
9456 addConfigurationProperty("touch.pressure.calibration", "amplitude");
9457 addConfigurationProperty("touch.pressure.scale", "0.01");
Arpit Singha8c236b2023-04-25 13:56:05 +00009458 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009459
Michael Wrightaa449c92017-12-13 21:21:43 +00009460 InputDeviceInfo info;
Harry Cuttsd02ea102023-03-17 18:21:30 +00009461 mapper.populateDeviceInfo(info);
Michael Wrightaa449c92017-12-13 21:21:43 +00009462 ASSERT_NO_FATAL_FAILURE(assertMotionRange(info,
9463 AINPUT_MOTION_RANGE_PRESSURE, AINPUT_SOURCE_TOUCHSCREEN,
9464 0.0f, RAW_PRESSURE_MAX * 0.01, 0.0f, 0.0f));
9465
Michael Wrightd02c5b62014-02-10 15:10:22 -08009466 // These calculations are based on the input device calibration documentation.
9467 int32_t rawX = 100;
9468 int32_t rawY = 200;
9469 int32_t rawPressure = 60;
9470
9471 float x = toDisplayX(rawX);
9472 float y = toDisplayY(rawY);
9473 float pressure = float(rawPressure) * 0.01f;
9474
9475 processPosition(mapper, rawX, rawY);
9476 processPressure(mapper, rawPressure);
9477 processMTSync(mapper);
9478 processSync(mapper);
9479
9480 NotifyMotionArgs args;
9481 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
9482 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0],
9483 x, y, pressure, 0, 0, 0, 0, 0, 0, 0));
9484}
9485
9486TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllButtons) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009487 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009488 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009489 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009490 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009491
9492 NotifyMotionArgs motionArgs;
9493 NotifyKeyArgs keyArgs;
9494
9495 processId(mapper, 1);
9496 processPosition(mapper, 100, 200);
9497 processSync(mapper);
9498 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9499 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9500 ASSERT_EQ(0, motionArgs.buttonState);
9501
9502 // press BTN_LEFT, release BTN_LEFT
9503 processKey(mapper, BTN_LEFT, 1);
9504 processSync(mapper);
9505 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9506 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9507 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9508
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009509 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9510 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9511 ASSERT_EQ(AMOTION_EVENT_BUTTON_PRIMARY, motionArgs.buttonState);
9512
Michael Wrightd02c5b62014-02-10 15:10:22 -08009513 processKey(mapper, BTN_LEFT, 0);
9514 processSync(mapper);
9515 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009516 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009517 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009518
9519 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009520 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009521 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009522
9523 // press BTN_RIGHT + BTN_MIDDLE, release BTN_RIGHT, release BTN_MIDDLE
9524 processKey(mapper, BTN_RIGHT, 1);
9525 processKey(mapper, BTN_MIDDLE, 1);
9526 processSync(mapper);
9527 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9528 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
9529 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9530 motionArgs.buttonState);
9531
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009532 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9533 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9534 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
9535
9536 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9537 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9538 ASSERT_EQ(AMOTION_EVENT_BUTTON_SECONDARY | AMOTION_EVENT_BUTTON_TERTIARY,
9539 motionArgs.buttonState);
9540
Michael Wrightd02c5b62014-02-10 15:10:22 -08009541 processKey(mapper, BTN_RIGHT, 0);
9542 processSync(mapper);
9543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009544 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009545 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009546
9547 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009548 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009549 ASSERT_EQ(AMOTION_EVENT_BUTTON_TERTIARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009550
9551 processKey(mapper, BTN_MIDDLE, 0);
9552 processSync(mapper);
9553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009554 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009555 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009556
9557 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009558 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009559 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009560
9561 // press BTN_BACK, release BTN_BACK
9562 processKey(mapper, BTN_BACK, 1);
9563 processSync(mapper);
9564 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9565 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9566 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009567
Michael Wrightd02c5b62014-02-10 15:10:22 -08009568 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009569 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009570 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9571
9572 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9573 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9574 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009575
9576 processKey(mapper, BTN_BACK, 0);
9577 processSync(mapper);
9578 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009579 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009580 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009581
9582 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009583 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009584 ASSERT_EQ(0, motionArgs.buttonState);
9585
Michael Wrightd02c5b62014-02-10 15:10:22 -08009586 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9587 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9588 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9589
9590 // press BTN_SIDE, release BTN_SIDE
9591 processKey(mapper, BTN_SIDE, 1);
9592 processSync(mapper);
9593 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9594 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9595 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009596
Michael Wrightd02c5b62014-02-10 15:10:22 -08009597 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009598 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009599 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
9600
9601 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9602 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9603 ASSERT_EQ(AMOTION_EVENT_BUTTON_BACK, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009604
9605 processKey(mapper, BTN_SIDE, 0);
9606 processSync(mapper);
9607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009608 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009609 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009610
9611 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009612 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009613 ASSERT_EQ(0, motionArgs.buttonState);
9614
Michael Wrightd02c5b62014-02-10 15:10:22 -08009615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9616 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9617 ASSERT_EQ(AKEYCODE_BACK, keyArgs.keyCode);
9618
9619 // press BTN_FORWARD, release BTN_FORWARD
9620 processKey(mapper, BTN_FORWARD, 1);
9621 processSync(mapper);
9622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9623 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9624 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009625
Michael Wrightd02c5b62014-02-10 15:10:22 -08009626 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009628 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9629
9630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9631 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9632 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009633
9634 processKey(mapper, BTN_FORWARD, 0);
9635 processSync(mapper);
9636 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009637 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009638 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009639
9640 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009641 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009642 ASSERT_EQ(0, motionArgs.buttonState);
9643
Michael Wrightd02c5b62014-02-10 15:10:22 -08009644 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9645 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9646 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9647
9648 // press BTN_EXTRA, release BTN_EXTRA
9649 processKey(mapper, BTN_EXTRA, 1);
9650 processSync(mapper);
9651 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9652 ASSERT_EQ(AKEY_EVENT_ACTION_DOWN, keyArgs.action);
9653 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009654
Michael Wrightd02c5b62014-02-10 15:10:22 -08009655 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009656 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009657 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
9658
9659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9660 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9661 ASSERT_EQ(AMOTION_EVENT_BUTTON_FORWARD, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009662
9663 processKey(mapper, BTN_EXTRA, 0);
9664 processSync(mapper);
9665 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009666 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009667 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009668
9669 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009670 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009671 ASSERT_EQ(0, motionArgs.buttonState);
9672
Michael Wrightd02c5b62014-02-10 15:10:22 -08009673 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasCalled(&keyArgs));
9674 ASSERT_EQ(AKEY_EVENT_ACTION_UP, keyArgs.action);
9675 ASSERT_EQ(AKEYCODE_FORWARD, keyArgs.keyCode);
9676
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009677 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyKeyWasNotCalled());
9678
Michael Wrightd02c5b62014-02-10 15:10:22 -08009679 // press BTN_STYLUS, release BTN_STYLUS
9680 processKey(mapper, BTN_STYLUS, 1);
9681 processSync(mapper);
9682 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9683 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009684 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
9685
9686 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9687 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9688 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009689
9690 processKey(mapper, BTN_STYLUS, 0);
9691 processSync(mapper);
9692 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009693 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009694 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009695
9696 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009697 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009698 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009699
9700 // press BTN_STYLUS2, release BTN_STYLUS2
9701 processKey(mapper, BTN_STYLUS2, 1);
9702 processSync(mapper);
9703 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9704 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009705 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
9706
9707 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9708 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, motionArgs.action);
9709 ASSERT_EQ(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009710
9711 processKey(mapper, BTN_STYLUS2, 0);
9712 processSync(mapper);
9713 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009714 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, motionArgs.action);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009715 ASSERT_EQ(0, motionArgs.buttonState);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009716
9717 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Michael Wrightd02c5b62014-02-10 15:10:22 -08009718 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Vladislav Kaznacheevfb752582016-12-16 14:17:06 -08009719 ASSERT_EQ(0, motionArgs.buttonState);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009720
9721 // release touch
9722 processId(mapper, -1);
9723 processSync(mapper);
9724 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9725 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9726 ASSERT_EQ(0, motionArgs.buttonState);
9727}
9728
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009729TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleMappedStylusButtons) {
9730 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009731 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009732 prepareAxes(POSITION | ID | SLOT);
Arpit Singha8c236b2023-04-25 13:56:05 +00009733 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan4f05b5f2022-10-11 21:24:07 +00009734
9735 mFakeEventHub->addKey(EVENTHUB_ID, BTN_A, 0, AKEYCODE_STYLUS_BUTTON_PRIMARY, 0);
9736 mFakeEventHub->addKey(EVENTHUB_ID, 0, 0xabcd, AKEYCODE_STYLUS_BUTTON_SECONDARY, 0);
9737
9738 // Touch down.
9739 processId(mapper, 1);
9740 processPosition(mapper, 100, 200);
9741 processSync(mapper);
9742 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9743 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithButtonState(0))));
9744
9745 // Press and release button mapped to the primary stylus button.
9746 processKey(mapper, BTN_A, 1);
9747 processSync(mapper);
9748 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9749 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9750 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9751 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9752 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9753 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_PRIMARY))));
9754
9755 processKey(mapper, BTN_A, 0);
9756 processSync(mapper);
9757 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9758 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9759 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9760 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9761
9762 // Press and release the HID usage mapped to the secondary stylus button.
9763 processHidUsage(mapper, 0xabcd, 1);
9764 processSync(mapper);
9765 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9766 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
9767 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9768 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9769 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_PRESS),
9770 WithButtonState(AMOTION_EVENT_BUTTON_STYLUS_SECONDARY))));
9771
9772 processHidUsage(mapper, 0xabcd, 0);
9773 processSync(mapper);
9774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9775 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_BUTTON_RELEASE), WithButtonState(0))));
9776 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9777 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE), WithButtonState(0))));
9778
9779 // Release touch.
9780 processId(mapper, -1);
9781 processSync(mapper);
9782 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
9783 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP), WithButtonState(0))));
9784}
9785
Michael Wrightd02c5b62014-02-10 15:10:22 -08009786TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleAllToolTypes) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009787 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009788 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009789 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +00009790 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009791
9792 NotifyMotionArgs motionArgs;
9793
9794 // default tool type is finger
9795 processId(mapper, 1);
9796 processPosition(mapper, 100, 200);
9797 processSync(mapper);
9798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9799 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009800 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009801
9802 // eraser
9803 processKey(mapper, BTN_TOOL_RUBBER, 1);
9804 processSync(mapper);
9805 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9806 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009807 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009808
9809 // stylus
9810 processKey(mapper, BTN_TOOL_RUBBER, 0);
9811 processKey(mapper, BTN_TOOL_PEN, 1);
9812 processSync(mapper);
9813 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9814 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009815 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009816
9817 // brush
9818 processKey(mapper, BTN_TOOL_PEN, 0);
9819 processKey(mapper, BTN_TOOL_BRUSH, 1);
9820 processSync(mapper);
9821 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9822 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009823 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009824
9825 // pencil
9826 processKey(mapper, BTN_TOOL_BRUSH, 0);
9827 processKey(mapper, BTN_TOOL_PENCIL, 1);
9828 processSync(mapper);
9829 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9830 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009831 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009832
Arthur Hung7c3ae9c2019-03-11 11:23:03 +08009833 // air-brush
Michael Wrightd02c5b62014-02-10 15:10:22 -08009834 processKey(mapper, BTN_TOOL_PENCIL, 0);
9835 processKey(mapper, BTN_TOOL_AIRBRUSH, 1);
9836 processSync(mapper);
9837 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9838 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009839 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009840
9841 // mouse
9842 processKey(mapper, BTN_TOOL_AIRBRUSH, 0);
9843 processKey(mapper, BTN_TOOL_MOUSE, 1);
9844 processSync(mapper);
9845 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9846 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009847 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009848
9849 // lens
9850 processKey(mapper, BTN_TOOL_MOUSE, 0);
9851 processKey(mapper, BTN_TOOL_LENS, 1);
9852 processSync(mapper);
9853 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9854 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009855 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009856
9857 // double-tap
9858 processKey(mapper, BTN_TOOL_LENS, 0);
9859 processKey(mapper, BTN_TOOL_DOUBLETAP, 1);
9860 processSync(mapper);
9861 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9862 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009863 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009864
9865 // triple-tap
9866 processKey(mapper, BTN_TOOL_DOUBLETAP, 0);
9867 processKey(mapper, BTN_TOOL_TRIPLETAP, 1);
9868 processSync(mapper);
9869 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9870 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009871 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009872
9873 // quad-tap
9874 processKey(mapper, BTN_TOOL_TRIPLETAP, 0);
9875 processKey(mapper, BTN_TOOL_QUADTAP, 1);
9876 processSync(mapper);
9877 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9878 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009879 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009880
9881 // finger
9882 processKey(mapper, BTN_TOOL_QUADTAP, 0);
9883 processKey(mapper, BTN_TOOL_FINGER, 1);
9884 processSync(mapper);
9885 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9886 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009887 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009888
9889 // stylus trumps finger
9890 processKey(mapper, BTN_TOOL_PEN, 1);
9891 processSync(mapper);
9892 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9893 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009894 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009895
9896 // eraser trumps stylus
9897 processKey(mapper, BTN_TOOL_RUBBER, 1);
9898 processSync(mapper);
9899 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9900 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009901 ASSERT_EQ(ToolType::ERASER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009902
9903 // mouse trumps eraser
9904 processKey(mapper, BTN_TOOL_MOUSE, 1);
9905 processSync(mapper);
9906 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9907 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009908 ASSERT_EQ(ToolType::MOUSE, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009909
9910 // MT tool type trumps BTN tool types: MT_TOOL_FINGER
9911 processToolType(mapper, MT_TOOL_FINGER); // this is the first time we send MT_TOOL_TYPE
9912 processSync(mapper);
9913 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9914 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009915 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009916
9917 // MT tool type trumps BTN tool types: MT_TOOL_PEN
9918 processToolType(mapper, MT_TOOL_PEN);
9919 processSync(mapper);
9920 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9921 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009922 ASSERT_EQ(ToolType::STYLUS, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009923
9924 // back to default tool type
9925 processToolType(mapper, -1); // use a deliberately undefined tool type, for testing
9926 processKey(mapper, BTN_TOOL_MOUSE, 0);
9927 processKey(mapper, BTN_TOOL_RUBBER, 0);
9928 processKey(mapper, BTN_TOOL_PEN, 0);
9929 processKey(mapper, BTN_TOOL_FINGER, 0);
9930 processSync(mapper);
9931 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9932 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -07009933 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009934}
9935
9936TEST_F(MultiTouchInputMapperTest, Process_WhenBtnTouchPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -08009937 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +00009938 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -08009939 prepareAxes(POSITION | ID | SLOT);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -08009940 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +00009941 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -08009942
9943 NotifyMotionArgs motionArgs;
9944
9945 // initially hovering because BTN_TOUCH not sent yet, pressure defaults to 0
9946 processId(mapper, 1);
9947 processPosition(mapper, 100, 200);
9948 processSync(mapper);
9949 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9950 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9951 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9952 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9953
9954 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9955 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9956 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9957 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
9958
9959 // move a little
9960 processPosition(mapper, 150, 250);
9961 processSync(mapper);
9962 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9963 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9964 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9965 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9966
9967 // down when BTN_TOUCH is pressed, pressure defaults to 1
9968 processKey(mapper, BTN_TOUCH, 1);
9969 processSync(mapper);
9970 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9971 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
9972 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9973 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9974
9975 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9976 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
9977 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9978 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9979
9980 // up when BTN_TOUCH is released, hover restored
9981 processKey(mapper, BTN_TOUCH, 0);
9982 processSync(mapper);
9983 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9984 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
9985 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9986 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
9987
9988 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9989 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
9990 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9991 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9992
9993 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
9994 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
9995 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
9996 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
9997
9998 // exit hover when pointer goes away
9999 processId(mapper, -1);
10000 processSync(mapper);
10001 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10002 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
10003 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10004 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10005}
10006
10007TEST_F(MultiTouchInputMapperTest, Process_WhenAbsMTPressureIsPresent_HoversIfItsValueIsZero) {
Michael Wrightd02c5b62014-02-10 15:10:22 -080010008 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010009 prepareDisplay(ui::ROTATION_0);
Michael Wrightd02c5b62014-02-10 15:10:22 -080010010 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010011 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Michael Wrightd02c5b62014-02-10 15:10:22 -080010012
10013 NotifyMotionArgs motionArgs;
10014
10015 // initially hovering because pressure is 0
10016 processId(mapper, 1);
10017 processPosition(mapper, 100, 200);
10018 processPressure(mapper, 0);
10019 processSync(mapper);
10020 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10021 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
10022 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10023 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
10024
10025 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10026 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10027 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10028 toDisplayX(100), toDisplayY(200), 0, 0, 0, 0, 0, 0, 0, 0));
10029
10030 // move a little
10031 processPosition(mapper, 150, 250);
10032 processSync(mapper);
10033 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10034 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10035 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10036 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10037
10038 // down when pressure becomes non-zero
10039 processPressure(mapper, RAW_PRESSURE_MAX);
10040 processSync(mapper);
10041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10042 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
10043 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10044 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10045
10046 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10047 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10048 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10049 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
10050
10051 // up when pressure becomes 0, hover restored
10052 processPressure(mapper, 0);
10053 processSync(mapper);
10054 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10055 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10056 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10057 toDisplayX(150), toDisplayY(250), 1, 0, 0, 0, 0, 0, 0, 0));
10058
10059 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10060 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_ENTER, motionArgs.action);
10061 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10062 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10063
10064 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10065 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10066 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10067 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10068
10069 // exit hover when pointer goes away
10070 processId(mapper, -1);
10071 processSync(mapper);
10072 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10073 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_EXIT, motionArgs.action);
10074 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0],
10075 toDisplayX(150), toDisplayY(250), 0, 0, 0, 0, 0, 0, 0, 0));
10076}
10077
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010078/**
10079 * Set the input device port <--> display port associations, and check that the
10080 * events are routed to the display that matches the display port.
10081 * This can be checked by looking at the displayId of the resulting NotifyMotionArgs.
10082 */
10083TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayPort) {
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010084 const std::string usb2 = "USB2";
10085 const uint8_t hdmi1 = 0;
10086 const uint8_t hdmi2 = 1;
10087 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010088 constexpr ViewportType type = ViewportType::EXTERNAL;
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010089
10090 addConfigurationProperty("touch.deviceType", "touchScreen");
10091 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010092 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010093
10094 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10095 mFakePolicy->addInputPortAssociation(usb2, hdmi2);
10096
10097 // We are intentionally not adding the viewport for display 1 yet. Since the port association
10098 // for this input device is specified, and the matching viewport is not present,
10099 // the input device should be disabled (at the mapper level).
10100
10101 // Add viewport for display 2 on hdmi2
10102 prepareSecondaryDisplay(type, hdmi2);
10103 // Send a touch event
10104 processPosition(mapper, 100, 100);
10105 processSync(mapper);
10106 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10107
10108 // Add viewport for display 1 on hdmi1
Michael Wrighta9cf4192022-12-01 23:46:39 +000010109 prepareDisplay(ui::ROTATION_0, hdmi1);
Siarhei Vishniakou8158e7e2018-10-15 14:28:20 -070010110 // Send a touch event again
10111 processPosition(mapper, 100, 100);
10112 processSync(mapper);
10113
10114 NotifyMotionArgs args;
10115 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10116 ASSERT_EQ(DISPLAY_ID, args.displayId);
10117}
Michael Wrightd02c5b62014-02-10 15:10:22 -080010118
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010119TEST_F(MultiTouchInputMapperTest, Configure_AssignsDisplayUniqueId) {
10120 addConfigurationProperty("touch.deviceType", "touchScreen");
10121 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010122 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010123
10124 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
10125
Michael Wrighta9cf4192022-12-01 23:46:39 +000010126 prepareDisplay(ui::ROTATION_0);
10127 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000010128
10129 // Send a touch event
10130 processPosition(mapper, 100, 100);
10131 processSync(mapper);
10132
10133 NotifyMotionArgs args;
10134 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10135 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
10136}
10137
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010138TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
Garfield Tan888a6a42020-01-09 11:39:16 -080010139 // Setup for second display.
Michael Wright17db18e2020-06-26 20:51:44 +010010140 std::shared_ptr<FakePointerController> fakePointerController =
10141 std::make_shared<FakePointerController>();
Garfield Tan888a6a42020-01-09 11:39:16 -080010142 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010143 fakePointerController->setPosition(100, 200);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010144 mFakePolicy->setPointerController(fakePointerController);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010145
Garfield Tan888a6a42020-01-09 11:39:16 -080010146 mFakePolicy->setDefaultPointerDisplayId(SECONDARY_DISPLAY_ID);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010147 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Garfield Tan888a6a42020-01-09 11:39:16 -080010148
Michael Wrighta9cf4192022-12-01 23:46:39 +000010149 prepareDisplay(ui::ROTATION_0);
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010150 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010151 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010152
Josep del Río2d8c79a2023-01-23 19:33:50 +000010153 // Check source is mouse that would obtain the PointerController.
10154 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Arthur Hungc7ad2d02018-12-18 17:41:29 +080010155
10156 NotifyMotionArgs motionArgs;
10157 processPosition(mapper, 100, 100);
10158 processSync(mapper);
10159
10160 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10161 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, motionArgs.action);
10162 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
10163}
10164
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010165/**
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010166 * Ensure that the readTime is set to the SYN_REPORT value when processing touch events.
10167 */
10168TEST_F(MultiTouchInputMapperTest, Process_SendsReadTime) {
10169 addConfigurationProperty("touch.deviceType", "touchScreen");
10170 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010171 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010172
Michael Wrighta9cf4192022-12-01 23:46:39 +000010173 prepareDisplay(ui::ROTATION_0);
Harry Cutts33476232023-01-30 19:57:29 +000010174 process(mapper, 10, /*readTime=*/11, EV_ABS, ABS_MT_TRACKING_ID, 1);
10175 process(mapper, 15, /*readTime=*/16, EV_ABS, ABS_MT_POSITION_X, 100);
10176 process(mapper, 20, /*readTime=*/21, EV_ABS, ABS_MT_POSITION_Y, 100);
10177 process(mapper, 25, /*readTime=*/26, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010178
10179 NotifyMotionArgs args;
10180 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10181 ASSERT_EQ(26, args.readTime);
10182
Harry Cutts33476232023-01-30 19:57:29 +000010183 process(mapper, 30, /*readTime=*/31, EV_ABS, ABS_MT_POSITION_X, 110);
10184 process(mapper, 30, /*readTime=*/32, EV_ABS, ABS_MT_POSITION_Y, 220);
10185 process(mapper, 30, /*readTime=*/33, EV_SYN, SYN_REPORT, 0);
Siarhei Vishniakou58ba3d12021-02-11 01:31:07 +000010186
10187 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10188 ASSERT_EQ(33, args.readTime);
10189}
10190
10191/**
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010192 * When the viewport is not active (isActive=false), the touch mapper should be disabled and the
10193 * events should not be delivered to the listener.
10194 */
10195TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreDropped) {
10196 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010197 // Don't set touch.enableForInactiveViewport to verify the default behavior.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010198 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010199 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010200 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010201 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010202 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6f778462020-12-09 23:39:07 +000010203
10204 NotifyMotionArgs motionArgs;
10205 processPosition(mapper, 100, 100);
10206 processSync(mapper);
10207
10208 mFakeListener->assertNotifyMotionWasNotCalled();
10209}
10210
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010211/**
10212 * When the viewport is not active (isActive=false) and touch.enableForInactiveViewport is true,
10213 * the touch mapper can process the events and the events can be delivered to the listener.
10214 */
10215TEST_F(MultiTouchInputMapperTest, WhenViewportIsNotActive_TouchesAreProcessed) {
10216 addConfigurationProperty("touch.deviceType", "touchScreen");
10217 addConfigurationProperty("touch.enableForInactiveViewport", "1");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010218 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010219 /*isActive=*/false, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010220 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010221 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010222 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010223
10224 NotifyMotionArgs motionArgs;
10225 processPosition(mapper, 100, 100);
10226 processSync(mapper);
10227
10228 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10229 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10230}
10231
Josh Thielene986aed2023-06-01 14:17:30 +000010232/**
10233 * When the viewport is deactivated (isActive transitions from true to false),
10234 * and touch.enableForInactiveViewport is false, touches prior to the transition
10235 * should be cancelled.
10236 */
Garfield Tanc734e4f2021-01-15 20:01:39 -080010237TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_AbortTouches) {
10238 addConfigurationProperty("touch.deviceType", "touchScreen");
Yuncheol Heo50c19b12022-11-02 20:33:08 -070010239 addConfigurationProperty("touch.enableForInactiveViewport", "0");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010240 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
Harry Cutts33476232023-01-30 19:57:29 +000010241 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010242 std::optional<DisplayViewport> optionalDisplayViewport =
10243 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10244 ASSERT_TRUE(optionalDisplayViewport.has_value());
10245 DisplayViewport displayViewport = *optionalDisplayViewport;
10246
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010247 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010248 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010249 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Garfield Tanc734e4f2021-01-15 20:01:39 -080010250
10251 // Finger down
10252 int32_t x = 100, y = 100;
10253 processPosition(mapper, x, y);
10254 processSync(mapper);
10255
10256 NotifyMotionArgs motionArgs;
10257 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10258 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
10259
10260 // Deactivate display viewport
10261 displayViewport.isActive = false;
10262 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010263 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010264
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010265 // The ongoing touch should be canceled immediately
10266 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10267 EXPECT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10268
10269 // Finger move is ignored
Garfield Tanc734e4f2021-01-15 20:01:39 -080010270 x += 10, y += 10;
10271 processPosition(mapper, x, y);
10272 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010273 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
Garfield Tanc734e4f2021-01-15 20:01:39 -080010274
10275 // Reactivate display viewport
10276 displayViewport.isActive = true;
10277 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010278 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010279
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010280 // Finger move again starts new gesture
Garfield Tanc734e4f2021-01-15 20:01:39 -080010281 x += 10, y += 10;
10282 processPosition(mapper, x, y);
10283 processSync(mapper);
Prabir Pradhanc0bdeef2022-08-05 22:32:11 +000010284 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10285 EXPECT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Garfield Tanc734e4f2021-01-15 20:01:39 -080010286}
10287
Josh Thielene986aed2023-06-01 14:17:30 +000010288/**
10289 * When the viewport is deactivated (isActive transitions from true to false),
10290 * and touch.enableForInactiveViewport is true, touches prior to the transition
10291 * should not be cancelled.
10292 */
10293TEST_F(MultiTouchInputMapperTest, Process_DeactivateViewport_TouchesNotAborted) {
10294 addConfigurationProperty("touch.deviceType", "touchScreen");
10295 addConfigurationProperty("touch.enableForInactiveViewport", "1");
10296 mFakePolicy->addDisplayViewport(DISPLAY_ID, DISPLAY_WIDTH, DISPLAY_HEIGHT, ui::ROTATION_0,
10297 /*isActive=*/true, UNIQUE_ID, NO_PORT, ViewportType::INTERNAL);
10298 std::optional<DisplayViewport> optionalDisplayViewport =
10299 mFakePolicy->getDisplayViewportByUniqueId(UNIQUE_ID);
10300 ASSERT_TRUE(optionalDisplayViewport.has_value());
10301 DisplayViewport displayViewport = *optionalDisplayViewport;
10302
10303 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10304 prepareAxes(POSITION);
10305 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
10306
10307 // Finger down
10308 int32_t x = 100, y = 100;
10309 processPosition(mapper, x, y);
10310 processSync(mapper);
10311 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10312 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
10313
10314 // Deactivate display viewport
10315 displayViewport.isActive = false;
10316 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10317 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10318
10319 // The ongoing touch should not be canceled
10320 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10321
10322 // Finger move is not ignored
10323 x += 10, y += 10;
10324 processPosition(mapper, x, y);
10325 processSync(mapper);
10326 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10327 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10328
10329 // Reactivate display viewport
10330 displayViewport.isActive = true;
10331 ASSERT_TRUE(mFakePolicy->updateViewport(displayViewport));
10332 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
10333
10334 // Finger move continues and does not start new gesture
10335 x += 10, y += 10;
10336 processPosition(mapper, x, y);
10337 processSync(mapper);
10338 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
10339 WithMotionAction(AMOTION_EVENT_ACTION_MOVE)));
10340}
10341
Arthur Hung7c645402019-01-25 17:45:42 +080010342TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) {
10343 // Setup the first touch screen device.
Arthur Hung7c645402019-01-25 17:45:42 +080010344 prepareAxes(POSITION | ID | SLOT);
10345 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010346 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung7c645402019-01-25 17:45:42 +080010347
10348 // Create the second touch screen device, and enable multi fingers.
10349 const std::string USB2 = "USB2";
arthurhungdcef2dc2020-08-11 14:47:50 +080010350 const std::string DEVICE_NAME2 = "TOUCHSCREEN2";
Arthur Hung2c9a3342019-07-23 14:18:59 +080010351 constexpr int32_t SECOND_DEVICE_ID = DEVICE_ID + 1;
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010352 constexpr int32_t SECOND_EVENTHUB_ID = EVENTHUB_ID + 1;
arthurhungdcef2dc2020-08-11 14:47:50 +080010353 std::shared_ptr<InputDevice> device2 =
10354 newDevice(SECOND_DEVICE_ID, DEVICE_NAME2, USB2, SECOND_EVENTHUB_ID,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070010355 ftl::Flags<InputDeviceClass>(0));
arthurhungdcef2dc2020-08-11 14:47:50 +080010356
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010357 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010358 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010359 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010360 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010361 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_TRACKING_ID, RAW_ID_MIN, RAW_ID_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010362 /*flat=*/0, /*fuzz=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010363 mFakeEventHub->addAbsoluteAxis(SECOND_EVENTHUB_ID, ABS_MT_SLOT, RAW_SLOT_MIN, RAW_SLOT_MAX,
Harry Cutts33476232023-01-30 19:57:29 +000010364 /*flat=*/0, /*fuzz=*/0);
10365 mFakeEventHub->setAbsoluteAxisValue(SECOND_EVENTHUB_ID, ABS_MT_SLOT, /*value=*/0);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010366 mFakeEventHub->addConfigurationProperty(SECOND_EVENTHUB_ID, String8("touch.deviceType"),
10367 String8("touchScreen"));
Arthur Hung7c645402019-01-25 17:45:42 +080010368
10369 // Setup the second touch screen device.
Arpit Singha8c236b2023-04-25 13:56:05 +000010370 device2->addEmptyEventHubDevice(SECOND_EVENTHUB_ID);
10371 MultiTouchInputMapper& mapper2 = device2->constructAndAddMapper<
10372 MultiTouchInputMapper>(SECOND_EVENTHUB_ID, mFakePolicy->getReaderConfiguration());
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010373 std::list<NotifyArgs> unused =
10374 device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010375 /*changes=*/{});
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010376 unused += device2->reset(ARBITRARY_TIME);
Arthur Hung7c645402019-01-25 17:45:42 +080010377
10378 // Setup PointerController.
Michael Wright17db18e2020-06-26 20:51:44 +010010379 std::shared_ptr<FakePointerController> fakePointerController =
10380 std::make_shared<FakePointerController>();
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000010381 mFakePolicy->setPointerController(fakePointerController);
Arthur Hung7c645402019-01-25 17:45:42 +080010382
10383 // Setup policy for associated displays and show touches.
10384 const uint8_t hdmi1 = 0;
10385 const uint8_t hdmi2 = 1;
10386 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi1);
10387 mFakePolicy->addInputPortAssociation(USB2, hdmi2);
10388 mFakePolicy->setShowTouches(true);
10389
10390 // Create displays.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010391 prepareDisplay(ui::ROTATION_0, hdmi1);
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010392 prepareSecondaryDisplay(ViewportType::EXTERNAL, hdmi2);
Arthur Hung7c645402019-01-25 17:45:42 +080010393
10394 // Default device will reconfigure above, need additional reconfiguration for another device.
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010395 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010396 InputReaderConfiguration::Change::DISPLAY_INFO |
10397 InputReaderConfiguration::Change::SHOW_TOUCHES);
Arthur Hung7c645402019-01-25 17:45:42 +080010398
10399 // Two fingers down at default display.
10400 int32_t x1 = 100, y1 = 125, x2 = 300, y2 = 500;
10401 processPosition(mapper, x1, y1);
10402 processId(mapper, 1);
10403 processSlot(mapper, 1);
10404 processPosition(mapper, x2, y2);
10405 processId(mapper, 2);
10406 processSync(mapper);
10407
10408 std::map<int32_t, std::vector<int32_t>>::const_iterator iter =
10409 fakePointerController->getSpots().find(DISPLAY_ID);
10410 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10411 ASSERT_EQ(size_t(2), iter->second.size());
10412
10413 // Two fingers down at second display.
10414 processPosition(mapper2, x1, y1);
10415 processId(mapper2, 1);
10416 processSlot(mapper2, 1);
10417 processPosition(mapper2, x2, y2);
10418 processId(mapper2, 2);
10419 processSync(mapper2);
10420
10421 iter = fakePointerController->getSpots().find(SECONDARY_DISPLAY_ID);
10422 ASSERT_TRUE(iter != fakePointerController->getSpots().end());
10423 ASSERT_EQ(size_t(2), iter->second.size());
Prabir Pradhan197e0862022-07-01 14:28:00 +000010424
10425 // Disable the show touches configuration and ensure the spots are cleared.
10426 mFakePolicy->setShowTouches(false);
Siarhei Vishniakou2935db72022-09-22 13:35:22 -070010427 unused += device2->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000010428 InputReaderConfiguration::Change::SHOW_TOUCHES);
Prabir Pradhan197e0862022-07-01 14:28:00 +000010429
10430 ASSERT_TRUE(fakePointerController->getSpots().empty());
Arthur Hung7c645402019-01-25 17:45:42 +080010431}
10432
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010433TEST_F(MultiTouchInputMapperTest, VideoFrames_ReceivedByListener) {
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010434 prepareAxes(POSITION);
10435 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010436 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000010437 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010438
10439 NotifyMotionArgs motionArgs;
10440 // Unrotated video frame
10441 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10442 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010443 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou6b76bdf2019-02-15 20:01:35 -060010444 processPosition(mapper, 100, 200);
10445 processSync(mapper);
10446 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10447 ASSERT_EQ(frames, motionArgs.videoFrames);
10448
10449 // Subsequent touch events should not have any videoframes
10450 // This is implemented separately in FakeEventHub,
10451 // but that should match the behaviour of TouchVideoDevice.
10452 processPosition(mapper, 200, 200);
10453 processSync(mapper);
10454 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10455 ASSERT_EQ(std::vector<TouchVideoFrame>(), motionArgs.videoFrames);
10456}
10457
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010458TEST_F(MultiTouchInputMapperTest, VideoFrames_AreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010459 prepareAxes(POSITION);
10460 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010461 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010462 // Unrotated video frame
10463 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10464 NotifyMotionArgs motionArgs;
10465
10466 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010467 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010468 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10469 clearViewports();
10470 prepareDisplay(orientation);
10471 std::vector<TouchVideoFrame> frames{frame};
10472 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10473 processPosition(mapper, 100, 200);
10474 processSync(mapper);
10475 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10476 ASSERT_EQ(frames, motionArgs.videoFrames);
10477 }
10478}
10479
10480TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_AreRotated) {
10481 prepareAxes(POSITION);
10482 addConfigurationProperty("touch.deviceType", "touchScreen");
10483 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10484 // orientation-aware are affected by display rotation.
10485 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010486 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010487 // Unrotated video frame
10488 TouchVideoFrame frame(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10489 NotifyMotionArgs motionArgs;
10490
10491 // Test all 4 orientations
Michael Wrighta9cf4192022-12-01 23:46:39 +000010492 for (ui::Rotation orientation : ftl::enum_range<ui::Rotation>()) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010493 SCOPED_TRACE("Orientation " + StringPrintf("%i", orientation));
10494 clearViewports();
10495 prepareDisplay(orientation);
10496 std::vector<TouchVideoFrame> frames{frame};
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010497 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010498 processPosition(mapper, 100, 200);
10499 processSync(mapper);
10500 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010501 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10502 // compared to the display. This is so that when the window transform (which contains the
10503 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10504 // window's coordinate space.
10505 frames[0].rotate(getInverseRotation(orientation));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010506 ASSERT_EQ(frames, motionArgs.videoFrames);
lilinnan687e58f2022-07-19 16:00:50 +080010507
10508 // Release finger.
10509 processSync(mapper);
10510 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010511 }
10512}
10513
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010514TEST_F(MultiTouchInputMapperTest, VideoFrames_MultipleFramesAreNotRotated) {
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010515 prepareAxes(POSITION);
10516 addConfigurationProperty("touch.deviceType", "touchScreen");
Arpit Singha8c236b2023-04-25 13:56:05 +000010517 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010518 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10519 // so mix these.
10520 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10521 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10522 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10523 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10524 NotifyMotionArgs motionArgs;
10525
Michael Wrighta9cf4192022-12-01 23:46:39 +000010526 prepareDisplay(ui::ROTATION_90);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080010527 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010528 processPosition(mapper, 100, 200);
10529 processSync(mapper);
10530 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010531 ASSERT_EQ(frames, motionArgs.videoFrames);
10532}
10533
10534TEST_F(MultiTouchInputMapperTest, VideoFrames_WhenNotOrientationAware_MultipleFramesAreRotated) {
10535 prepareAxes(POSITION);
10536 addConfigurationProperty("touch.deviceType", "touchScreen");
10537 // Since InputReader works in the un-rotated coordinate space, only devices that are not
10538 // orientation-aware are affected by display rotation.
10539 addConfigurationProperty("touch.orientationAware", "0");
Arpit Singha8c236b2023-04-25 13:56:05 +000010540 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010541 // Unrotated video frames. There's no rule that they must all have the same dimensions,
10542 // so mix these.
10543 TouchVideoFrame frame1(3, 2, {1, 2, 3, 4, 5, 6}, {1, 2});
10544 TouchVideoFrame frame2(3, 3, {0, 1, 2, 3, 4, 5, 6, 7, 8}, {1, 3});
10545 TouchVideoFrame frame3(2, 2, {10, 20, 10, 0}, {1, 4});
10546 std::vector<TouchVideoFrame> frames{frame1, frame2, frame3};
10547 NotifyMotionArgs motionArgs;
10548
Michael Wrighta9cf4192022-12-01 23:46:39 +000010549 prepareDisplay(ui::ROTATION_90);
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010550 mFakeEventHub->setVideoFrames({{EVENTHUB_ID, frames}});
10551 processPosition(mapper, 100, 200);
10552 processSync(mapper);
10553 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10554 std::for_each(frames.begin(), frames.end(), [](TouchVideoFrame& frame) {
10555 // We expect the raw coordinates of the MotionEvent to be rotated in the inverse direction
10556 // compared to the display. This is so that when the window transform (which contains the
10557 // display rotation) is applied later by InputDispatcher, the coordinates end up in the
10558 // window's coordinate space.
Michael Wrighta9cf4192022-12-01 23:46:39 +000010559 frame.rotate(getInverseRotation(ui::ROTATION_90));
Prabir Pradhanc14266f2021-05-12 15:56:24 -070010560 });
Siarhei Vishniakou8154bbd2019-02-15 17:21:03 -060010561 ASSERT_EQ(frames, motionArgs.videoFrames);
10562}
10563
Arthur Hung9da14732019-09-02 16:16:58 +080010564/**
10565 * If we had defined port associations, but the viewport is not ready, the touch device would be
10566 * expected to be disabled, and it should be enabled after the viewport has found.
10567 */
10568TEST_F(MultiTouchInputMapperTest, Configure_EnabledForAssociatedDisplay) {
Arthur Hung9da14732019-09-02 16:16:58 +080010569 constexpr uint8_t hdmi2 = 1;
10570 const std::string secondaryUniqueId = "uniqueId2";
Michael Wrightfe3de7d2020-07-02 19:05:30 +010010571 constexpr ViewportType type = ViewportType::EXTERNAL;
Arthur Hung9da14732019-09-02 16:16:58 +080010572
10573 mFakePolicy->addInputPortAssociation(DEVICE_LOCATION, hdmi2);
10574
10575 addConfigurationProperty("touch.deviceType", "touchScreen");
10576 prepareAxes(POSITION);
Arpit Singha8c236b2023-04-25 13:56:05 +000010577 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9da14732019-09-02 16:16:58 +080010578
10579 ASSERT_EQ(mDevice->isEnabled(), false);
10580
10581 // Add display on hdmi2, the device should be enabled and can receive touch event.
10582 prepareSecondaryDisplay(type, hdmi2);
10583 ASSERT_EQ(mDevice->isEnabled(), true);
10584
10585 // Send a touch event.
10586 processPosition(mapper, 100, 100);
10587 processSync(mapper);
10588
10589 NotifyMotionArgs args;
10590 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
10591 ASSERT_EQ(SECONDARY_DISPLAY_ID, args.displayId);
10592}
10593
Arthur Hung421eb1c2020-01-16 00:09:42 +080010594TEST_F(MultiTouchInputMapperTest, Process_ShouldHandleSingleTouch) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010595 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010596 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010597 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010598 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010599
10600 NotifyMotionArgs motionArgs;
10601
10602 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10603 // finger down
10604 processId(mapper, 1);
10605 processPosition(mapper, x1, y1);
10606 processSync(mapper);
10607 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10608 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010609 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010610
10611 // finger move
10612 processId(mapper, 1);
10613 processPosition(mapper, x2, y2);
10614 processSync(mapper);
10615 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10616 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010617 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010618
10619 // finger up.
10620 processId(mapper, -1);
10621 processSync(mapper);
10622 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10623 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010624 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010625
10626 // new finger down
10627 processId(mapper, 1);
10628 processPosition(mapper, x3, y3);
10629 processSync(mapper);
10630 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10631 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010632 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010633}
10634
10635/**
arthurhungcc7f9802020-04-30 17:55:40 +080010636 * Test single touch should be canceled when received the MT_TOOL_PALM event, and the following
10637 * MOVE and UP events should be ignored.
Arthur Hung421eb1c2020-01-16 00:09:42 +080010638 */
arthurhungcc7f9802020-04-30 17:55:40 +080010639TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_SinglePointer) {
Arthur Hung421eb1c2020-01-16 00:09:42 +080010640 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010641 prepareDisplay(ui::ROTATION_0);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010642 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010643 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung421eb1c2020-01-16 00:09:42 +080010644
10645 NotifyMotionArgs motionArgs;
10646
10647 // default tool type is finger
10648 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
arthurhungcc7f9802020-04-30 17:55:40 +080010649 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010650 processPosition(mapper, x1, y1);
10651 processSync(mapper);
10652 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10653 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010654 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010655
10656 // Tool changed to MT_TOOL_PALM expect sending the cancel event.
10657 processToolType(mapper, MT_TOOL_PALM);
10658 processSync(mapper);
10659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10660 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10661
10662 // Ignore the following MOVE and UP events if had detect a palm event.
arthurhungcc7f9802020-04-30 17:55:40 +080010663 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010664 processPosition(mapper, x2, y2);
10665 processSync(mapper);
10666 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10667
10668 // finger up.
arthurhungcc7f9802020-04-30 17:55:40 +080010669 processId(mapper, INVALID_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010670 processSync(mapper);
10671 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
10672
10673 // new finger down
arthurhungcc7f9802020-04-30 17:55:40 +080010674 processId(mapper, FIRST_TRACKING_ID);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010675 processToolType(mapper, MT_TOOL_FINGER);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010676 processPosition(mapper, x3, y3);
10677 processSync(mapper);
10678 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10679 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010680 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Arthur Hung421eb1c2020-01-16 00:09:42 +080010681}
10682
arthurhungbf89a482020-04-17 17:37:55 +080010683/**
arthurhungcc7f9802020-04-30 17:55:40 +080010684 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10685 * and the rest active fingers could still be allowed to receive the events
arthurhungbf89a482020-04-17 17:37:55 +080010686 */
arthurhungcc7f9802020-04-30 17:55:40 +080010687TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_TwoPointers) {
arthurhungbf89a482020-04-17 17:37:55 +080010688 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010689 prepareDisplay(ui::ROTATION_0);
arthurhungbf89a482020-04-17 17:37:55 +080010690 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010691 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungbf89a482020-04-17 17:37:55 +080010692
10693 NotifyMotionArgs motionArgs;
10694
10695 // default tool type is finger
arthurhungcc7f9802020-04-30 17:55:40 +080010696 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10697 processId(mapper, FIRST_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010698 processPosition(mapper, x1, y1);
10699 processSync(mapper);
10700 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10701 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010702 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010703
10704 // Second finger down.
arthurhungcc7f9802020-04-30 17:55:40 +080010705 processSlot(mapper, SECOND_SLOT);
10706 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010707 processPosition(mapper, x2, y2);
arthurhungcc7f9802020-04-30 17:55:40 +080010708 processSync(mapper);
10709 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010710 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010711 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[1].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010712
10713 // If the tool type of the first finger changes to MT_TOOL_PALM,
10714 // we expect to receive ACTION_POINTER_UP with cancel flag.
10715 processSlot(mapper, FIRST_SLOT);
10716 processId(mapper, FIRST_TRACKING_ID);
10717 processToolType(mapper, MT_TOOL_PALM);
10718 processSync(mapper);
10719 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010720 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010721 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10722
10723 // The following MOVE events of second finger should be processed.
10724 processSlot(mapper, SECOND_SLOT);
10725 processId(mapper, SECOND_TRACKING_ID);
10726 processPosition(mapper, x2 + 1, y2 + 1);
10727 processSync(mapper);
10728 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10729 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010730 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010731
10732 // First finger up. It used to be in palm mode, and we already generated ACTION_POINTER_UP for
10733 // it. Second finger receive move.
10734 processSlot(mapper, FIRST_SLOT);
10735 processId(mapper, INVALID_TRACKING_ID);
10736 processSync(mapper);
10737 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10738 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010739 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010740
10741 // Second finger keeps moving.
10742 processSlot(mapper, SECOND_SLOT);
10743 processId(mapper, SECOND_TRACKING_ID);
10744 processPosition(mapper, x2 + 2, y2 + 2);
10745 processSync(mapper);
10746 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10747 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010748 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010749
10750 // Second finger up.
10751 processId(mapper, INVALID_TRACKING_ID);
10752 processSync(mapper);
10753 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10754 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10755 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10756}
10757
10758/**
10759 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event, if only 1 finger
10760 * is active, it should send CANCEL after receiving the MT_TOOL_PALM event.
10761 */
10762TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_ShouldCancelWhenAllTouchIsPalm) {
10763 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010764 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010765 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010766 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010767
10768 NotifyMotionArgs motionArgs;
10769
10770 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220, x3 = 140, y3 = 240;
10771 // First finger down.
10772 processId(mapper, FIRST_TRACKING_ID);
10773 processPosition(mapper, x1, y1);
10774 processSync(mapper);
10775 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10776 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010777 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010778
10779 // Second finger down.
10780 processSlot(mapper, SECOND_SLOT);
10781 processId(mapper, SECOND_TRACKING_ID);
10782 processPosition(mapper, x2, y2);
arthurhungbf89a482020-04-17 17:37:55 +080010783 processSync(mapper);
10784 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010785 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010786 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungbf89a482020-04-17 17:37:55 +080010787
arthurhungcc7f9802020-04-30 17:55:40 +080010788 // If the tool type of the first finger changes to MT_TOOL_PALM,
10789 // we expect to receive ACTION_POINTER_UP with cancel flag.
10790 processSlot(mapper, FIRST_SLOT);
10791 processId(mapper, FIRST_TRACKING_ID);
10792 processToolType(mapper, MT_TOOL_PALM);
10793 processSync(mapper);
10794 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010795 ASSERT_EQ(ACTION_POINTER_0_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010796 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10797
10798 // Second finger keeps moving.
10799 processSlot(mapper, SECOND_SLOT);
10800 processId(mapper, SECOND_TRACKING_ID);
10801 processPosition(mapper, x2 + 1, y2 + 1);
10802 processSync(mapper);
10803 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10804 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10805
10806 // second finger becomes palm, receive cancel due to only 1 finger is active.
10807 processId(mapper, SECOND_TRACKING_ID);
arthurhungbf89a482020-04-17 17:37:55 +080010808 processToolType(mapper, MT_TOOL_PALM);
10809 processSync(mapper);
10810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10811 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
10812
arthurhungcc7f9802020-04-30 17:55:40 +080010813 // third finger down.
10814 processSlot(mapper, THIRD_SLOT);
10815 processId(mapper, THIRD_TRACKING_ID);
10816 processToolType(mapper, MT_TOOL_FINGER);
arthurhungbf89a482020-04-17 17:37:55 +080010817 processPosition(mapper, x3, y3);
10818 processSync(mapper);
arthurhungbf89a482020-04-17 17:37:55 +080010819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10820 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010821 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010822 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010823
10824 // third finger move
10825 processId(mapper, THIRD_TRACKING_ID);
10826 processPosition(mapper, x3 + 1, y3 + 1);
10827 processSync(mapper);
10828 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10829 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10830
10831 // first finger up, third finger receive move.
10832 processSlot(mapper, FIRST_SLOT);
10833 processId(mapper, INVALID_TRACKING_ID);
10834 processSync(mapper);
10835 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10836 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010837 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010838
10839 // second finger up, third finger receive move.
10840 processSlot(mapper, SECOND_SLOT);
10841 processId(mapper, INVALID_TRACKING_ID);
10842 processSync(mapper);
10843 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10844 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010845 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010846
10847 // third finger up.
10848 processSlot(mapper, THIRD_SLOT);
10849 processId(mapper, INVALID_TRACKING_ID);
10850 processSync(mapper);
10851 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10852 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10853 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10854}
10855
10856/**
10857 * Test multi-touch should sent POINTER_UP when received the MT_TOOL_PALM event from some finger,
10858 * and the active finger could still be allowed to receive the events
10859 */
10860TEST_F(MultiTouchInputMapperTest, Process_ShouldHandlePalmToolType_KeepFirstPointer) {
10861 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010862 prepareDisplay(ui::ROTATION_0);
arthurhungcc7f9802020-04-30 17:55:40 +080010863 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010864 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
arthurhungcc7f9802020-04-30 17:55:40 +080010865
10866 NotifyMotionArgs motionArgs;
10867
10868 // default tool type is finger
10869 constexpr int32_t x1 = 100, y1 = 200, x2 = 120, y2 = 220;
10870 processId(mapper, FIRST_TRACKING_ID);
10871 processPosition(mapper, x1, y1);
10872 processSync(mapper);
10873 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10874 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010875 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010876
10877 // Second finger down.
10878 processSlot(mapper, SECOND_SLOT);
10879 processId(mapper, SECOND_TRACKING_ID);
10880 processPosition(mapper, x2, y2);
10881 processSync(mapper);
10882 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010883 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070010884 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
arthurhungcc7f9802020-04-30 17:55:40 +080010885
10886 // If the tool type of the second finger changes to MT_TOOL_PALM,
10887 // we expect to receive ACTION_POINTER_UP with cancel flag.
10888 processId(mapper, SECOND_TRACKING_ID);
10889 processToolType(mapper, MT_TOOL_PALM);
10890 processSync(mapper);
10891 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010892 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
arthurhungcc7f9802020-04-30 17:55:40 +080010893 ASSERT_EQ(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
10894
10895 // The following MOVE event should be processed.
10896 processSlot(mapper, FIRST_SLOT);
10897 processId(mapper, FIRST_TRACKING_ID);
10898 processPosition(mapper, x1 + 1, y1 + 1);
10899 processSync(mapper);
10900 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10901 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010902 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
arthurhungcc7f9802020-04-30 17:55:40 +080010903
10904 // second finger up.
10905 processSlot(mapper, SECOND_SLOT);
10906 processId(mapper, INVALID_TRACKING_ID);
10907 processSync(mapper);
10908 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10909 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10910
10911 // first finger keep moving
10912 processSlot(mapper, FIRST_SLOT);
10913 processId(mapper, FIRST_TRACKING_ID);
10914 processPosition(mapper, x1 + 2, y1 + 2);
10915 processSync(mapper);
10916 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10917 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
10918
10919 // first finger up.
10920 processId(mapper, INVALID_TRACKING_ID);
10921 processSync(mapper);
10922 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10923 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
10924 ASSERT_NE(AMOTION_EVENT_FLAG_CANCELED, motionArgs.flags);
arthurhungbf89a482020-04-17 17:37:55 +080010925}
10926
Arthur Hung9ad18942021-06-19 02:04:46 +000010927/**
10928 * Test multi-touch should sent ACTION_POINTER_UP/ACTION_UP when received the INVALID_TRACKING_ID,
10929 * to prevent the driver side may send unexpected data after set tracking id as INVALID_TRACKING_ID
10930 * cause slot be valid again.
10931 */
10932TEST_F(MultiTouchInputMapperTest, Process_MultiTouch_WithInvalidTrackingId) {
10933 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010934 prepareDisplay(ui::ROTATION_0);
Arthur Hung9ad18942021-06-19 02:04:46 +000010935 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010936 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Arthur Hung9ad18942021-06-19 02:04:46 +000010937
10938 NotifyMotionArgs motionArgs;
10939
10940 constexpr int32_t x1 = 100, y1 = 200, x2 = 0, y2 = 0;
10941 // First finger down.
10942 processId(mapper, FIRST_TRACKING_ID);
10943 processPosition(mapper, x1, y1);
10944 processPressure(mapper, RAW_PRESSURE_MAX);
10945 processSync(mapper);
10946 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10947 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010948 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010949
10950 // First finger move.
10951 processId(mapper, FIRST_TRACKING_ID);
10952 processPosition(mapper, x1 + 1, y1 + 1);
10953 processPressure(mapper, RAW_PRESSURE_MAX);
10954 processSync(mapper);
10955 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10956 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010957 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010958
10959 // Second finger down.
10960 processSlot(mapper, SECOND_SLOT);
10961 processId(mapper, SECOND_TRACKING_ID);
10962 processPosition(mapper, x2, y2);
10963 processPressure(mapper, RAW_PRESSURE_MAX);
10964 processSync(mapper);
10965 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010966 ASSERT_EQ(ACTION_POINTER_1_DOWN, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010967 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010968
10969 // second finger up with some unexpected data.
10970 processSlot(mapper, SECOND_SLOT);
10971 processId(mapper, INVALID_TRACKING_ID);
10972 processPosition(mapper, x2, y2);
10973 processSync(mapper);
10974 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080010975 ASSERT_EQ(ACTION_POINTER_1_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010976 ASSERT_EQ(uint32_t(2), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010977
10978 // first finger up with some unexpected data.
10979 processSlot(mapper, FIRST_SLOT);
10980 processId(mapper, INVALID_TRACKING_ID);
10981 processPosition(mapper, x2, y2);
10982 processPressure(mapper, RAW_PRESSURE_MAX);
10983 processSync(mapper);
10984 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
10985 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, motionArgs.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070010986 ASSERT_EQ(uint32_t(1), motionArgs.getPointerCount());
Arthur Hung9ad18942021-06-19 02:04:46 +000010987}
10988
Arpit Singh4bb0bd52023-12-20 14:41:10 +000010989TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010990 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000010991 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010992 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000010993 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000010994
10995 // First finger down.
10996 processId(mapper, FIRST_TRACKING_ID);
10997 processPosition(mapper, 100, 200);
10998 processPressure(mapper, RAW_PRESSURE_MAX);
10999 processSync(mapper);
11000 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11001 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
11002
11003 // Second finger down.
11004 processSlot(mapper, SECOND_SLOT);
11005 processId(mapper, SECOND_TRACKING_ID);
11006 processPosition(mapper, 300, 400);
11007 processPressure(mapper, RAW_PRESSURE_MAX);
11008 processSync(mapper);
11009 ASSERT_NO_FATAL_FAILURE(
11010 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(ACTION_POINTER_1_DOWN)));
11011
Arpit Singh4bb0bd52023-12-20 14:41:10 +000011012 // Reset the mapper. When the mapper is reset, we expect the current multi-touch state to be
11013 // preserved. Resetting should cancel the ongoing gesture.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000011014 resetMapper(mapper, ARBITRARY_TIME);
11015 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11016 WithMotionAction(AMOTION_EVENT_ACTION_CANCEL)));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011017
Arpit Singh4bb0bd52023-12-20 14:41:10 +000011018 // Send a sync to simulate an empty touch frame where nothing changes. The mapper should use
11019 // the existing touch state to generate a down event.
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011020 processPosition(mapper, 301, 302);
11021 processSync(mapper);
11022 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11023 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithPressure(1.f))));
Arpit Singh4bb0bd52023-12-20 14:41:10 +000011024 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11025 AllOf(WithMotionAction(ACTION_POINTER_1_DOWN), WithPressure(1.f))));
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011026
11027 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11028}
11029
Arpit Singh4bb0bd52023-12-20 14:41:10 +000011030TEST_F(MultiTouchInputMapperTest, Reset_PreservesLastTouchState_NoPointersDown) {
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011031 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011032 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011033 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000011034 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011035
11036 // First finger touches down and releases.
11037 processId(mapper, FIRST_TRACKING_ID);
11038 processPosition(mapper, 100, 200);
11039 processPressure(mapper, RAW_PRESSURE_MAX);
11040 processSync(mapper);
11041 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11042 WithMotionAction(AMOTION_EVENT_ACTION_DOWN)));
11043 processId(mapper, INVALID_TRACKING_ID);
11044 processSync(mapper);
11045 ASSERT_NO_FATAL_FAILURE(
11046 mFakeListener->assertNotifyMotionWasCalled(WithMotionAction(AMOTION_EVENT_ACTION_UP)));
11047
11048 // Reset the mapper. When the mapper is reset, we expect it to restore the latest
11049 // raw state where no pointers are down.
Prabir Pradhanf5b4d7a2022-10-03 15:45:50 +000011050 resetMapper(mapper, ARBITRARY_TIME);
Prabir Pradhanafabcde2022-09-27 19:32:43 +000011051 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11052
11053 // Send an empty sync frame. Since there are no pointers, no events are generated.
11054 processSync(mapper);
11055 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11056}
11057
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011058TEST_F(MultiTouchInputMapperTest, StylusSourceIsAddedDynamicallyFromToolType) {
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011059 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011060 prepareDisplay(ui::ROTATION_0);
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011061 prepareAxes(POSITION | ID | SLOT | PRESSURE | TOOL_TYPE);
Arpit Singha8c236b2023-04-25 13:56:05 +000011062 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011063 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011064
11065 // Even if the device supports reporting the ABS_MT_TOOL_TYPE axis, which could give it the
11066 // ability to report MT_TOOL_PEN, we do not report the device as coming from a stylus source.
11067 // Due to limitations in the evdev protocol, we cannot say for certain that a device is capable
11068 // of reporting stylus events just because it supports ABS_MT_TOOL_TYPE.
11069 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
11070
11071 // However, if the device ever ends up reporting an event with MT_TOOL_PEN, it should be
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011072 // reported with the stylus source.
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011073 processId(mapper, FIRST_TRACKING_ID);
11074 processToolType(mapper, MT_TOOL_PEN);
11075 processPosition(mapper, 100, 200);
11076 processPressure(mapper, RAW_PRESSURE_MAX);
11077 processSync(mapper);
11078 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11079 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11080 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011081 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011082
Prabir Pradhan5d0d97d2022-11-17 01:06:01 +000011083 // Now that we know the device supports styluses, ensure that the device is re-configured with
11084 // the stylus source.
11085 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, mapper.getSources());
11086 {
11087 const auto& devices = mReader->getInputDevices();
11088 auto deviceInfo =
11089 std::find_if(devices.begin(), devices.end(),
11090 [](const InputDeviceInfo& info) { return info.getId() == DEVICE_ID; });
11091 LOG_ALWAYS_FATAL_IF(deviceInfo == devices.end(), "Cannot find InputDevice");
11092 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS, deviceInfo->getSources());
11093 }
11094
11095 // Ensure the device was not reset to prevent interruptions of any ongoing gestures.
11096 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasNotCalled());
11097
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011098 processId(mapper, INVALID_TRACKING_ID);
11099 processSync(mapper);
11100 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11101 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11102 WithSource(AINPUT_SOURCE_TOUCHSCREEN | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011103 WithToolType(ToolType::STYLUS))));
Prabir Pradhanf9a41282022-10-25 17:15:50 +000011104}
11105
Seunghwan Choi356026c2023-02-01 14:37:25 +090011106TEST_F(MultiTouchInputMapperTest, Process_WhenConfigEnabled_ShouldShowDirectStylusPointer) {
11107 addConfigurationProperty("touch.deviceType", "touchScreen");
11108 prepareDisplay(ui::ROTATION_0);
11109 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11110 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11111 // indicate stylus presence dynamically.
11112 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11113 std::shared_ptr<FakePointerController> fakePointerController =
11114 std::make_shared<FakePointerController>();
11115 mFakePolicy->setPointerController(fakePointerController);
11116 mFakePolicy->setStylusPointerIconEnabled(true);
Arpit Singha8c236b2023-04-25 13:56:05 +000011117 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011118
11119 processId(mapper, FIRST_TRACKING_ID);
11120 processPressure(mapper, RAW_PRESSURE_MIN);
11121 processPosition(mapper, 100, 200);
11122 processToolType(mapper, MT_TOOL_PEN);
11123 processSync(mapper);
11124 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11125 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011126 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011127 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11128 ASSERT_TRUE(fakePointerController->isPointerShown());
11129 ASSERT_NO_FATAL_FAILURE(
11130 fakePointerController->assertPosition(toDisplayX(100), toDisplayY(200)));
11131}
11132
11133TEST_F(MultiTouchInputMapperTest, Process_WhenConfigDisabled_ShouldNotShowDirectStylusPointer) {
11134 addConfigurationProperty("touch.deviceType", "touchScreen");
11135 prepareDisplay(ui::ROTATION_0);
11136 prepareAxes(POSITION | ID | SLOT | TOOL_TYPE | PRESSURE);
11137 // Add BTN_TOOL_PEN to statically show stylus support, since using ABS_MT_TOOL_TYPE can only
11138 // indicate stylus presence dynamically.
11139 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
11140 std::shared_ptr<FakePointerController> fakePointerController =
11141 std::make_shared<FakePointerController>();
11142 mFakePolicy->setPointerController(fakePointerController);
11143 mFakePolicy->setStylusPointerIconEnabled(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011144 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Seunghwan Choi356026c2023-02-01 14:37:25 +090011145
11146 processId(mapper, FIRST_TRACKING_ID);
11147 processPressure(mapper, RAW_PRESSURE_MIN);
11148 processPosition(mapper, 100, 200);
11149 processToolType(mapper, MT_TOOL_PEN);
11150 processSync(mapper);
11151 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11152 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_HOVER_ENTER),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011153 WithToolType(ToolType::STYLUS),
Seunghwan Choi356026c2023-02-01 14:37:25 +090011154 WithPointerCoords(0, toDisplayX(100), toDisplayY(200)))));
11155 ASSERT_FALSE(fakePointerController->isPointerShown());
11156}
11157
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011158// --- MultiTouchInputMapperTest_ExternalDevice ---
11159
11160class MultiTouchInputMapperTest_ExternalDevice : public MultiTouchInputMapperTest {
11161protected:
Chris Yea52ade12020-08-27 16:49:20 -070011162 void SetUp() override { InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL); }
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011163};
11164
11165/**
11166 * Expect fallback to internal viewport if device is external and external viewport is not present.
11167 */
11168TEST_F(MultiTouchInputMapperTest_ExternalDevice, Viewports_Fallback) {
11169 prepareAxes(POSITION);
11170 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011171 prepareDisplay(ui::ROTATION_0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011172 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011173
11174 ASSERT_EQ(AINPUT_SOURCE_TOUCHSCREEN, mapper.getSources());
11175
11176 NotifyMotionArgs motionArgs;
11177
11178 // Expect the event to be sent to the internal viewport,
11179 // because an external viewport is not present.
11180 processPosition(mapper, 100, 100);
11181 processSync(mapper);
11182 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11183 ASSERT_EQ(ADISPLAY_ID_DEFAULT, motionArgs.displayId);
11184
11185 // Expect the event to be sent to the external viewport if it is present.
Michael Wrightfe3de7d2020-07-02 19:05:30 +010011186 prepareSecondaryDisplay(ViewportType::EXTERNAL);
Nathaniel R. Lewisa7b82e12020-02-12 15:40:45 -080011187 processPosition(mapper, 100, 100);
11188 processSync(mapper);
11189 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11190 ASSERT_EQ(SECONDARY_DISPLAY_ID, motionArgs.displayId);
11191}
Arthur Hung4197f6b2020-03-16 15:39:59 +080011192
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011193TEST_F(MultiTouchInputMapperTest, Process_TouchpadCapture) {
11194 // we need a pointer controller for mouse mode of touchpad (start pointer at 0,0)
11195 std::shared_ptr<FakePointerController> fakePointerController =
11196 std::make_shared<FakePointerController>();
11197 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11198 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011199
11200 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011201 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011202 prepareAxes(POSITION | ID | SLOT);
11203 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11204 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
11205 mFakePolicy->setPointerCapture(true);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011206 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011207 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011208
11209 // captured touchpad should be a touchpad source
11210 NotifyDeviceResetArgs resetArgs;
11211 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
11212 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11213
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011214 InputDeviceInfo deviceInfo = mDevice->getDeviceInfo();
Chris Yef74dc422020-09-02 22:41:50 -070011215
11216 const InputDeviceInfo::MotionRange* relRangeX =
11217 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_X, AINPUT_SOURCE_TOUCHPAD);
11218 ASSERT_NE(relRangeX, nullptr);
11219 ASSERT_EQ(relRangeX->min, -(RAW_X_MAX - RAW_X_MIN));
11220 ASSERT_EQ(relRangeX->max, RAW_X_MAX - RAW_X_MIN);
11221 const InputDeviceInfo::MotionRange* relRangeY =
11222 deviceInfo.getMotionRange(AMOTION_EVENT_AXIS_RELATIVE_Y, AINPUT_SOURCE_TOUCHPAD);
11223 ASSERT_NE(relRangeY, nullptr);
11224 ASSERT_EQ(relRangeY->min, -(RAW_Y_MAX - RAW_Y_MIN));
11225 ASSERT_EQ(relRangeY->max, RAW_Y_MAX - RAW_Y_MIN);
11226
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011227 // run captured pointer tests - note that this is unscaled, so input listener events should be
11228 // identical to what the hardware sends (accounting for any
11229 // calibration).
11230 // FINGER 0 DOWN
Chris Ye364fdb52020-08-05 15:07:56 -070011231 processSlot(mapper, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011232 processId(mapper, 1);
11233 processPosition(mapper, 100 + RAW_X_MIN, 100 + RAW_Y_MIN);
11234 processKey(mapper, BTN_TOUCH, 1);
11235 processSync(mapper);
11236
11237 // expect coord[0] to contain initial location of touch 0
11238 NotifyMotionArgs args;
11239 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11240 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011241 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011242 ASSERT_EQ(0, args.pointerProperties[0].id);
11243 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, args.source);
11244 ASSERT_NO_FATAL_FAILURE(
11245 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11246
11247 // FINGER 1 DOWN
11248 processSlot(mapper, 1);
11249 processId(mapper, 2);
11250 processPosition(mapper, 560 + RAW_X_MIN, 154 + RAW_Y_MIN);
11251 processSync(mapper);
11252
11253 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11254 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
Siarhei Vishniakou5fd3e012021-12-30 15:20:32 -080011255 ASSERT_EQ(ACTION_POINTER_1_DOWN, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011256 ASSERT_EQ(2U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011257 ASSERT_EQ(0, args.pointerProperties[0].id);
11258 ASSERT_EQ(1, args.pointerProperties[1].id);
11259 ASSERT_NO_FATAL_FAILURE(
11260 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11261 ASSERT_NO_FATAL_FAILURE(
11262 assertPointerCoords(args.pointerCoords[1], 560, 154, 1, 0, 0, 0, 0, 0, 0, 0));
11263
11264 // FINGER 1 MOVE
11265 processPosition(mapper, 540 + RAW_X_MIN, 690 + RAW_Y_MIN);
11266 processSync(mapper);
11267
11268 // expect coord[0] to contain previous location, coord[1] to contain new touch 1 location
11269 // from move
11270 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11271 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11272 ASSERT_NO_FATAL_FAILURE(
11273 assertPointerCoords(args.pointerCoords[0], 100, 100, 1, 0, 0, 0, 0, 0, 0, 0));
11274 ASSERT_NO_FATAL_FAILURE(
11275 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11276
11277 // FINGER 0 MOVE
11278 processSlot(mapper, 0);
11279 processPosition(mapper, 50 + RAW_X_MIN, 800 + RAW_Y_MIN);
11280 processSync(mapper);
11281
11282 // expect coord[0] to contain new touch 0 location, coord[1] to contain previous location
11283 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11284 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11285 ASSERT_NO_FATAL_FAILURE(
11286 assertPointerCoords(args.pointerCoords[0], 50, 800, 1, 0, 0, 0, 0, 0, 0, 0));
11287 ASSERT_NO_FATAL_FAILURE(
11288 assertPointerCoords(args.pointerCoords[1], 540, 690, 1, 0, 0, 0, 0, 0, 0, 0));
11289
11290 // BUTTON DOWN
11291 processKey(mapper, BTN_LEFT, 1);
11292 processSync(mapper);
11293
11294 // touchinputmapper design sends a move before button press
11295 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11296 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11297 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11298 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11299
11300 // BUTTON UP
11301 processKey(mapper, BTN_LEFT, 0);
11302 processSync(mapper);
11303
11304 // touchinputmapper design sends a move after button release
11305 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11306 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11307 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11308 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
11309
11310 // FINGER 0 UP
11311 processId(mapper, -1);
11312 processSync(mapper);
11313 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11314 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_UP | 0x0000, args.action);
11315
11316 // FINGER 1 MOVE
11317 processSlot(mapper, 1);
11318 processPosition(mapper, 320 + RAW_X_MIN, 900 + RAW_Y_MIN);
11319 processSync(mapper);
11320
11321 // expect coord[0] to contain new location of touch 1, and properties[0].id to contain 1
11322 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11323 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011324 ASSERT_EQ(1U, args.getPointerCount());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011325 ASSERT_EQ(1, args.pointerProperties[0].id);
11326 ASSERT_NO_FATAL_FAILURE(
11327 assertPointerCoords(args.pointerCoords[0], 320, 900, 1, 0, 0, 0, 0, 0, 0, 0));
11328
11329 // FINGER 1 UP
11330 processId(mapper, -1);
11331 processKey(mapper, BTN_TOUCH, 0);
11332 processSync(mapper);
11333 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11334 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
11335
Josep del Río2d8c79a2023-01-23 19:33:50 +000011336 // non captured touchpad should be a mouse source
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011337 mFakePolicy->setPointerCapture(false);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011338 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011339 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled(&resetArgs));
Josep del Río2d8c79a2023-01-23 19:33:50 +000011340 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011341}
11342
11343TEST_F(MultiTouchInputMapperTest, Process_UnCapturedTouchpadPointer) {
11344 std::shared_ptr<FakePointerController> fakePointerController =
11345 std::make_shared<FakePointerController>();
11346 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11347 fakePointerController->setPosition(0, 0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011348
11349 // prepare device and capture
Michael Wrighta9cf4192022-12-01 23:46:39 +000011350 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011351 prepareAxes(POSITION | ID | SLOT);
11352 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
11353 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOUCH, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011354 mFakePolicy->setPointerController(fakePointerController);
Arpit Singha8c236b2023-04-25 13:56:05 +000011355 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011356 // run uncaptured pointer tests - pushes out generic events
11357 // FINGER 0 DOWN
11358 processId(mapper, 3);
11359 processPosition(mapper, 100, 100);
11360 processKey(mapper, BTN_TOUCH, 1);
11361 processSync(mapper);
11362
11363 // start at (100,100), cursor should be at (0,0) * scale
11364 NotifyMotionArgs args;
11365 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11366 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11367 ASSERT_NO_FATAL_FAILURE(
11368 assertPointerCoords(args.pointerCoords[0], 0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
11369
11370 // FINGER 0 MOVE
11371 processPosition(mapper, 200, 200);
11372 processSync(mapper);
11373
11374 // compute scaling to help with touch position checking
11375 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11376 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11377 float scale =
11378 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11379
11380 // translate from (100,100) -> (200,200), cursor should have changed to (100,100) * scale)
11381 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11382 ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action);
11383 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 100 * scale, 100 * scale, 0,
11384 0, 0, 0, 0, 0, 0, 0));
LiZhihong758eb562022-11-03 15:28:29 +080011385
11386 // BUTTON DOWN
11387 processKey(mapper, BTN_LEFT, 1);
11388 processSync(mapper);
11389
11390 // touchinputmapper design sends a move before button press
11391 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11392 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, args.action);
11393 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11394 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_PRESS, args.action);
11395
11396 // BUTTON UP
11397 processKey(mapper, BTN_LEFT, 0);
11398 processSync(mapper);
11399
11400 // touchinputmapper design sends a move after button release
11401 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11402 ASSERT_EQ(AMOTION_EVENT_ACTION_BUTTON_RELEASE, args.action);
11403 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11404 ASSERT_EQ(AMOTION_EVENT_ACTION_UP, args.action);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011405}
11406
11407TEST_F(MultiTouchInputMapperTest, WhenCapturedAndNotCaptured_GetSources) {
11408 std::shared_ptr<FakePointerController> fakePointerController =
11409 std::make_shared<FakePointerController>();
11410
Michael Wrighta9cf4192022-12-01 23:46:39 +000011411 prepareDisplay(ui::ROTATION_0);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011412 prepareAxes(POSITION | ID | SLOT);
11413 mFakeEventHub->addKey(EVENTHUB_ID, BTN_LEFT, 0, AKEYCODE_UNKNOWN, 0);
Prabir Pradhan2853b7a2021-08-23 14:08:51 +000011414 mFakePolicy->setPointerController(fakePointerController);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011415 mFakePolicy->setPointerCapture(false);
Arpit Singha8c236b2023-04-25 13:56:05 +000011416 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011417
Josep del Río2d8c79a2023-01-23 19:33:50 +000011418 // uncaptured touchpad should be a pointer device
11419 ASSERT_EQ(AINPUT_SOURCE_MOUSE, mapper.getSources());
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011420
Josep del Río2d8c79a2023-01-23 19:33:50 +000011421 // captured touchpad should be a touchpad device
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011422 mFakePolicy->setPointerCapture(true);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011423 configureDevice(InputReaderConfiguration::Change::POINTER_CAPTURE);
Nathaniel R. Lewisd5665332018-02-22 13:31:42 -080011424 ASSERT_EQ(AINPUT_SOURCE_TOUCHPAD, mapper.getSources());
11425}
11426
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011427// --- BluetoothMultiTouchInputMapperTest ---
11428
11429class BluetoothMultiTouchInputMapperTest : public MultiTouchInputMapperTest {
11430protected:
11431 void SetUp() override {
11432 InputMapperTest::SetUp(DEVICE_CLASSES | InputDeviceClass::EXTERNAL, BUS_BLUETOOTH);
11433 }
11434};
11435
11436TEST_F(BluetoothMultiTouchInputMapperTest, TimestampSmoothening) {
11437 addConfigurationProperty("touch.deviceType", "touchScreen");
Michael Wrighta9cf4192022-12-01 23:46:39 +000011438 prepareDisplay(ui::ROTATION_0);
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011439 prepareAxes(POSITION | ID | SLOT | PRESSURE);
Arpit Singha8c236b2023-04-25 13:56:05 +000011440 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhan2f37bcb2022-11-08 20:41:28 +000011441
11442 nsecs_t kernelEventTime = ARBITRARY_TIME;
11443 nsecs_t expectedEventTime = ARBITRARY_TIME;
11444 // Touch down.
11445 processId(mapper, FIRST_TRACKING_ID);
11446 processPosition(mapper, 100, 200);
11447 processPressure(mapper, RAW_PRESSURE_MAX);
11448 processSync(mapper, ARBITRARY_TIME);
11449 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11450 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN), WithEventTime(ARBITRARY_TIME))));
11451
11452 // Process several events that come in quick succession, according to their timestamps.
11453 for (int i = 0; i < 3; i++) {
11454 constexpr static nsecs_t delta = ms2ns(1);
11455 static_assert(delta < MIN_BLUETOOTH_TIMESTAMP_DELTA);
11456 kernelEventTime += delta;
11457 expectedEventTime += MIN_BLUETOOTH_TIMESTAMP_DELTA;
11458
11459 processPosition(mapper, 101 + i, 201 + i);
11460 processSync(mapper, kernelEventTime);
11461 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11462 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11463 WithEventTime(expectedEventTime))));
11464 }
11465
11466 // Release the touch.
11467 processId(mapper, INVALID_TRACKING_ID);
11468 processPressure(mapper, RAW_PRESSURE_MIN);
11469 processSync(mapper, ARBITRARY_TIME + ms2ns(50));
11470 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11471 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_UP),
11472 WithEventTime(ARBITRARY_TIME + ms2ns(50)))));
11473}
11474
11475// --- MultiTouchPointerModeTest ---
11476
HQ Liue6983c72022-04-19 22:14:56 +000011477class MultiTouchPointerModeTest : public MultiTouchInputMapperTest {
11478protected:
11479 float mPointerMovementScale;
11480 float mPointerXZoomScale;
11481 void preparePointerMode(int xAxisResolution, int yAxisResolution) {
11482 addConfigurationProperty("touch.deviceType", "pointer");
11483 std::shared_ptr<FakePointerController> fakePointerController =
11484 std::make_shared<FakePointerController>();
11485 fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
11486 fakePointerController->setPosition(0, 0);
Michael Wrighta9cf4192022-12-01 23:46:39 +000011487 prepareDisplay(ui::ROTATION_0);
HQ Liue6983c72022-04-19 22:14:56 +000011488
11489 prepareAxes(POSITION);
11490 prepareAbsoluteAxisResolution(xAxisResolution, yAxisResolution);
11491 // In order to enable swipe and freeform gesture in pointer mode, pointer capture
11492 // needs to be disabled, and the pointer gesture needs to be enabled.
11493 mFakePolicy->setPointerCapture(false);
11494 mFakePolicy->setPointerGestureEnabled(true);
11495 mFakePolicy->setPointerController(fakePointerController);
11496
11497 float rawDiagonal = hypotf(RAW_X_MAX - RAW_X_MIN, RAW_Y_MAX - RAW_Y_MIN);
11498 float displayDiagonal = hypotf(DISPLAY_WIDTH, DISPLAY_HEIGHT);
11499 mPointerMovementScale =
11500 mFakePolicy->getPointerGestureMovementSpeedRatio() * displayDiagonal / rawDiagonal;
11501 mPointerXZoomScale =
11502 mFakePolicy->getPointerGestureZoomSpeedRatio() * displayDiagonal / rawDiagonal;
11503 }
11504
11505 void prepareAbsoluteAxisResolution(int xAxisResolution, int yAxisResolution) {
11506 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_X, RAW_X_MIN, RAW_X_MAX,
11507 /*flat*/ 0,
11508 /*fuzz*/ 0, /*resolution*/ xAxisResolution);
11509 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_MT_POSITION_Y, RAW_Y_MIN, RAW_Y_MAX,
11510 /*flat*/ 0,
11511 /*fuzz*/ 0, /*resolution*/ yAxisResolution);
11512 }
11513};
11514
11515/**
11516 * Two fingers down on a pointer mode touch pad. The width
11517 * of the two finger is larger than 1/4 of the touch pack diagnal length. However, it
11518 * is smaller than the fixed min physical length 30mm. Two fingers' distance must
11519 * be greater than the both value to be freeform gesture, so that after two
11520 * fingers start to move downwards, the gesture should be swipe.
11521 */
11522TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthSwipe) {
11523 // The min freeform gesture width is 25units/mm x 30mm = 750
11524 // which is greater than fraction of the diagnal length of the touchpad (349).
11525 // Thus, MaxSwipWidth is 750.
Harry Cutts33476232023-01-30 19:57:29 +000011526 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011527 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011528 NotifyMotionArgs motionArgs;
11529
11530 // Two fingers down at once.
11531 // The two fingers are 450 units apart, expects the current gesture to be PRESS
11532 // Pointer's initial position is used the [0,0] coordinate.
11533 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11534
11535 processId(mapper, FIRST_TRACKING_ID);
11536 processPosition(mapper, x1, y1);
11537 processMTSync(mapper);
11538 processId(mapper, SECOND_TRACKING_ID);
11539 processPosition(mapper, x2, y2);
11540 processMTSync(mapper);
11541 processSync(mapper);
11542
11543 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011544 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011545 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011546 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011547 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011548 ASSERT_NO_FATAL_FAILURE(
11549 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11550
11551 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11552 // that there should be 1 pointer.
11553 int32_t movingDistance = 200;
11554 y1 += movingDistance;
11555 y2 += movingDistance;
11556
11557 processId(mapper, FIRST_TRACKING_ID);
11558 processPosition(mapper, x1, y1);
11559 processMTSync(mapper);
11560 processId(mapper, SECOND_TRACKING_ID);
11561 processPosition(mapper, x2, y2);
11562 processMTSync(mapper);
11563 processSync(mapper);
11564
11565 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011566 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011567 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011568 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011569 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011570 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11571 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11572 0, 0, 0, 0));
11573}
11574
11575/**
11576 * Two fingers down on a pointer mode touch pad. The width of the two finger is larger
11577 * than the minimum freeform gesture width, 30mm. However, it is smaller than 1/4 of
11578 * the touch pack diagnal length. Two fingers' distance must be greater than the both
11579 * value to be freeform gesture, so that after two fingers start to move downwards,
11580 * the gesture should be swipe.
11581 */
11582TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthLowResolutionSwipe) {
11583 // The min freeform gesture width is 5units/mm x 30mm = 150
11584 // which is greater than fraction of the diagnal length of the touchpad (349).
11585 // Thus, MaxSwipWidth is the fraction of the diagnal length, 349.
Harry Cutts33476232023-01-30 19:57:29 +000011586 preparePointerMode(/*xResolution=*/5, /*yResolution=*/5);
Arpit Singha8c236b2023-04-25 13:56:05 +000011587 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011588 NotifyMotionArgs motionArgs;
11589
11590 // Two fingers down at once.
11591 // The two fingers are 250 units apart, expects the current gesture to be PRESS
11592 // Pointer's initial position is used the [0,0] coordinate.
11593 int32_t x1 = 100, y1 = 125, x2 = 350, y2 = 125;
11594
11595 processId(mapper, FIRST_TRACKING_ID);
11596 processPosition(mapper, x1, y1);
11597 processMTSync(mapper);
11598 processId(mapper, SECOND_TRACKING_ID);
11599 processPosition(mapper, x2, y2);
11600 processMTSync(mapper);
11601 processSync(mapper);
11602
11603 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011604 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011605 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011606 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011607 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011608 ASSERT_NO_FATAL_FAILURE(
11609 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11610
11611 // It should be recognized as a SWIPE gesture when two fingers start to move down,
11612 // and there should be 1 pointer.
11613 int32_t movingDistance = 200;
11614 y1 += movingDistance;
11615 y2 += movingDistance;
11616
11617 processId(mapper, FIRST_TRACKING_ID);
11618 processPosition(mapper, x1, y1);
11619 processMTSync(mapper);
11620 processId(mapper, SECOND_TRACKING_ID);
11621 processPosition(mapper, x2, y2);
11622 processMTSync(mapper);
11623 processSync(mapper);
11624
11625 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011626 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011627 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011628 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011629 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011630 // New coordinate is the scaled relative coordinate from the initial coordinate.
11631 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], 0,
11632 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11633 0, 0, 0, 0));
11634}
11635
11636/**
11637 * Touch the touch pad with two fingers with a distance wider than the minimum freeform
11638 * gesture width and 1/4 of the diagnal length of the touchpad. Expect to receive
11639 * freeform gestures after two fingers start to move downwards.
11640 */
11641TEST_F(MultiTouchPointerModeTest, PointerGestureMaxSwipeWidthFreeform) {
Harry Cutts33476232023-01-30 19:57:29 +000011642 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011643 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
HQ Liue6983c72022-04-19 22:14:56 +000011644
11645 NotifyMotionArgs motionArgs;
11646
11647 // Two fingers down at once. Wider than the max swipe width.
11648 // The gesture is expected to be PRESS, then transformed to FREEFORM
11649 int32_t x1 = 100, y1 = 125, x2 = 900, y2 = 125;
11650
11651 processId(mapper, FIRST_TRACKING_ID);
11652 processPosition(mapper, x1, y1);
11653 processMTSync(mapper);
11654 processId(mapper, SECOND_TRACKING_ID);
11655 processPosition(mapper, x2, y2);
11656 processMTSync(mapper);
11657 processSync(mapper);
11658
11659 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011660 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011661 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011662 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011663 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011664 // One pointer for PRESS, and its coordinate is used as the origin for pointer coordinates.
11665 ASSERT_NO_FATAL_FAILURE(
11666 assertPointerCoords(motionArgs.pointerCoords[0], 0, 0, 1, 0, 0, 0, 0, 0, 0, 0));
11667
11668 int32_t movingDistance = 200;
11669
11670 // Move two fingers down, expect a cancel event because gesture is changing to freeform,
11671 // then two down events for two pointers.
11672 y1 += movingDistance;
11673 y2 += movingDistance;
11674
11675 processId(mapper, FIRST_TRACKING_ID);
11676 processPosition(mapper, x1, y1);
11677 processMTSync(mapper);
11678 processId(mapper, SECOND_TRACKING_ID);
11679 processPosition(mapper, x2, y2);
11680 processMTSync(mapper);
11681 processSync(mapper);
11682
11683 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
11684 // The previous PRESS gesture is cancelled, because it is transformed to freeform
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011685 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011686 ASSERT_EQ(AMOTION_EVENT_ACTION_CANCEL, motionArgs.action);
11687 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011688 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011689 ASSERT_EQ(1U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011690 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11691 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011692 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011693 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011694 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011695 ASSERT_EQ(AMOTION_EVENT_ACTION_POINTER_DOWN, motionArgs.action & AMOTION_EVENT_ACTION_MASK);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011696 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011697 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011698 // Two pointers' scaled relative coordinates from their initial centroid.
11699 // Initial y coordinates are 0 as y1 and y2 have the same value.
11700 float cookedX1 = (x1 - x2) / 2 * mPointerXZoomScale;
11701 float cookedX2 = (x2 - x1) / 2 * mPointerXZoomScale;
11702 // When pointers move, the new coordinates equal to the initial coordinates plus
11703 // scaled moving distance.
11704 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11705 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11706 0, 0, 0, 0));
11707 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11708 movingDistance * mPointerMovementScale, 1, 0, 0, 0,
11709 0, 0, 0, 0));
11710
11711 // Move two fingers down again, expect one MOVE motion event.
11712 y1 += movingDistance;
11713 y2 += movingDistance;
11714
11715 processId(mapper, FIRST_TRACKING_ID);
11716 processPosition(mapper, x1, y1);
11717 processMTSync(mapper);
11718 processId(mapper, SECOND_TRACKING_ID);
11719 processPosition(mapper, x2, y2);
11720 processMTSync(mapper);
11721 processSync(mapper);
11722
11723 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011724 ASSERT_EQ(2U, motionArgs.getPointerCount());
HQ Liue6983c72022-04-19 22:14:56 +000011725 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011726 ASSERT_EQ(ToolType::FINGER, motionArgs.pointerProperties[0].toolType);
Harry Cutts2800fb02022-09-15 13:49:23 +000011727 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
HQ Liue6983c72022-04-19 22:14:56 +000011728 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[0], cookedX1,
11729 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11730 0, 0, 0, 0, 0));
11731 ASSERT_NO_FATAL_FAILURE(assertPointerCoords(motionArgs.pointerCoords[1], cookedX2,
11732 movingDistance * 2 * mPointerMovementScale, 1, 0, 0,
11733 0, 0, 0, 0, 0));
11734}
11735
Harry Cutts39b7ca22022-10-05 15:55:48 +000011736TEST_F(MultiTouchPointerModeTest, TwoFingerSwipeOffsets) {
Harry Cutts33476232023-01-30 19:57:29 +000011737 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Arpit Singha8c236b2023-04-25 13:56:05 +000011738 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Harry Cutts39b7ca22022-10-05 15:55:48 +000011739 NotifyMotionArgs motionArgs;
11740
11741 // Place two fingers down.
11742 int32_t x1 = 100, y1 = 125, x2 = 550, y2 = 125;
11743
11744 processId(mapper, FIRST_TRACKING_ID);
11745 processPosition(mapper, x1, y1);
11746 processMTSync(mapper);
11747 processId(mapper, SECOND_TRACKING_ID);
11748 processPosition(mapper, x2, y2);
11749 processMTSync(mapper);
11750 processSync(mapper);
11751
11752 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011753 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011754 ASSERT_EQ(AMOTION_EVENT_ACTION_DOWN, motionArgs.action);
11755 ASSERT_EQ(MotionClassification::NONE, motionArgs.classification);
11756 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET));
11757 ASSERT_EQ(0, motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET));
11758
11759 // Move the two fingers down and to the left.
11760 int32_t movingDistance = 200;
11761 x1 -= movingDistance;
11762 y1 += movingDistance;
11763 x2 -= movingDistance;
11764 y2 += movingDistance;
11765
11766 processId(mapper, FIRST_TRACKING_ID);
11767 processPosition(mapper, x1, y1);
11768 processMTSync(mapper);
11769 processId(mapper, SECOND_TRACKING_ID);
11770 processPosition(mapper, x2, y2);
11771 processMTSync(mapper);
11772 processSync(mapper);
11773
11774 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&motionArgs));
Siarhei Vishniakou3218fc02023-06-15 20:41:02 -070011775 ASSERT_EQ(1U, motionArgs.getPointerCount());
Harry Cutts39b7ca22022-10-05 15:55:48 +000011776 ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, motionArgs.action);
11777 ASSERT_EQ(MotionClassification::TWO_FINGER_SWIPE, motionArgs.classification);
11778 ASSERT_LT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_X_OFFSET), 0);
11779 ASSERT_GT(motionArgs.pointerCoords[0].getAxisValue(AMOTION_EVENT_AXIS_GESTURE_Y_OFFSET), 0);
11780}
11781
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011782TEST_F(MultiTouchPointerModeTest, WhenViewportActiveStatusChanged_PointerGestureIsReset) {
Harry Cutts33476232023-01-30 19:57:29 +000011783 preparePointerMode(/*xResolution=*/25, /*yResolution=*/25);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011784 mFakeEventHub->addKey(EVENTHUB_ID, BTN_TOOL_PEN, 0, AKEYCODE_UNKNOWN, 0);
Arpit Singha8c236b2023-04-25 13:56:05 +000011785 MultiTouchInputMapper& mapper = constructAndAddMapper<MultiTouchInputMapper>();
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011786 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyDeviceResetWasCalled());
11787
11788 // Start a stylus gesture.
11789 processKey(mapper, BTN_TOOL_PEN, 1);
11790 processId(mapper, FIRST_TRACKING_ID);
11791 processPosition(mapper, 100, 200);
11792 processSync(mapper);
11793 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11794 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_DOWN),
11795 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011796 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011797 // TODO(b/257078296): Pointer mode generates extra event.
11798 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11799 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_MOVE),
11800 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011801 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011802 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11803
11804 // Make the viewport inactive. This will put the device in disabled mode, and the ongoing stylus
11805 // gesture should be disabled.
11806 auto viewport = mFakePolicy->getDisplayViewportByType(ViewportType::INTERNAL);
11807 viewport->isActive = false;
11808 mFakePolicy->updateViewport(*viewport);
Prabir Pradhan4bf6d452023-04-18 21:26:56 +000011809 configureDevice(InputReaderConfiguration::Change::DISPLAY_INFO);
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011810 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11811 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11812 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011813 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011814 // TODO(b/257078296): Pointer mode generates extra event.
11815 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(
11816 AllOf(WithMotionAction(AMOTION_EVENT_ACTION_CANCEL),
11817 WithSource(AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_STYLUS),
Siarhei Vishniakou6d73f832022-07-21 17:27:03 -070011818 WithToolType(ToolType::STYLUS))));
Prabir Pradhanb80b6c02022-11-02 20:05:13 +000011819 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasNotCalled());
11820}
11821
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011822// --- JoystickInputMapperTest ---
11823
11824class JoystickInputMapperTest : public InputMapperTest {
11825protected:
11826 static const int32_t RAW_X_MIN;
11827 static const int32_t RAW_X_MAX;
11828 static const int32_t RAW_Y_MIN;
11829 static const int32_t RAW_Y_MAX;
11830
11831 void SetUp() override {
11832 InputMapperTest::SetUp(InputDeviceClass::JOYSTICK | InputDeviceClass::EXTERNAL);
11833 }
11834 void prepareAxes() {
11835 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_X, RAW_X_MIN, RAW_X_MAX, 0, 0);
11836 mFakeEventHub->addAbsoluteAxis(EVENTHUB_ID, ABS_Y, RAW_Y_MIN, RAW_Y_MAX, 0, 0);
11837 }
11838
11839 void processAxis(JoystickInputMapper& mapper, int32_t axis, int32_t value) {
11840 process(mapper, ARBITRARY_TIME, READ_TIME, EV_ABS, axis, value);
11841 }
11842
11843 void processSync(JoystickInputMapper& mapper) {
11844 process(mapper, ARBITRARY_TIME, READ_TIME, EV_SYN, SYN_REPORT, 0);
11845 }
11846
Michael Wrighta9cf4192022-12-01 23:46:39 +000011847 void prepareVirtualDisplay(ui::Rotation orientation) {
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011848 setDisplayInfoAndReconfigure(VIRTUAL_DISPLAY_ID, VIRTUAL_DISPLAY_WIDTH,
11849 VIRTUAL_DISPLAY_HEIGHT, orientation, VIRTUAL_DISPLAY_UNIQUE_ID,
11850 NO_PORT, ViewportType::VIRTUAL);
11851 }
11852};
11853
11854const int32_t JoystickInputMapperTest::RAW_X_MIN = -32767;
11855const int32_t JoystickInputMapperTest::RAW_X_MAX = 32767;
11856const int32_t JoystickInputMapperTest::RAW_Y_MIN = -32767;
11857const int32_t JoystickInputMapperTest::RAW_Y_MAX = 32767;
11858
11859TEST_F(JoystickInputMapperTest, Configure_AssignsDisplayUniqueId) {
11860 prepareAxes();
Arpit Singhae876352023-04-26 14:16:50 +000011861 JoystickInputMapper& mapper = constructAndAddMapper<JoystickInputMapper>();
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011862
11863 mFakePolicy->addInputUniqueIdAssociation(DEVICE_LOCATION, VIRTUAL_DISPLAY_UNIQUE_ID);
11864
Michael Wrighta9cf4192022-12-01 23:46:39 +000011865 prepareVirtualDisplay(ui::ROTATION_0);
Arthur Hung6d5b4b22022-01-21 07:21:10 +000011866
11867 // Send an axis event
11868 processAxis(mapper, ABS_X, 100);
11869 processSync(mapper);
11870
11871 NotifyMotionArgs args;
11872 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11873 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11874
11875 // Send another axis event
11876 processAxis(mapper, ABS_Y, 100);
11877 processSync(mapper);
11878
11879 ASSERT_NO_FATAL_FAILURE(mFakeListener->assertNotifyMotionWasCalled(&args));
11880 ASSERT_EQ(VIRTUAL_DISPLAY_ID, args.displayId);
11881}
11882
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011883// --- PeripheralControllerTest ---
Chris Yee2b1e5c2021-03-10 22:45:12 -080011884
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011885class PeripheralControllerTest : public testing::Test {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011886protected:
11887 static const char* DEVICE_NAME;
11888 static const char* DEVICE_LOCATION;
11889 static const int32_t DEVICE_ID;
11890 static const int32_t DEVICE_GENERATION;
11891 static const int32_t DEVICE_CONTROLLER_NUMBER;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011892 static const ftl::Flags<InputDeviceClass> DEVICE_CLASSES;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011893 static const int32_t EVENTHUB_ID;
11894
11895 std::shared_ptr<FakeEventHub> mFakeEventHub;
11896 sp<FakeInputReaderPolicy> mFakePolicy;
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011897 std::unique_ptr<TestInputListener> mFakeListener;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011898 std::unique_ptr<InstrumentedInputReader> mReader;
11899 std::shared_ptr<InputDevice> mDevice;
11900
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011901 virtual void SetUp(ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011902 mFakeEventHub = std::make_unique<FakeEventHub>();
Siarhei Vishniakouaed7ad02022-08-03 15:04:33 -070011903 mFakePolicy = sp<FakeInputReaderPolicy>::make();
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011904 mFakeListener = std::make_unique<TestInputListener>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011905 mReader = std::make_unique<InstrumentedInputReader>(mFakeEventHub, mFakePolicy,
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011906 *mFakeListener);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011907 mDevice = newDevice(DEVICE_ID, DEVICE_NAME, DEVICE_LOCATION, EVENTHUB_ID, classes);
11908 }
11909
11910 void SetUp() override { SetUp(DEVICE_CLASSES); }
11911
11912 void TearDown() override {
Siarhei Vishniakou18050092021-09-01 13:32:49 -070011913 mFakeListener.reset();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011914 mFakePolicy.clear();
11915 }
11916
Chris Yee2b1e5c2021-03-10 22:45:12 -080011917 std::shared_ptr<InputDevice> newDevice(int32_t deviceId, const std::string& name,
11918 const std::string& location, int32_t eventHubId,
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011919 ftl::Flags<InputDeviceClass> classes) {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011920 InputDeviceIdentifier identifier;
11921 identifier.name = name;
11922 identifier.location = location;
11923 std::shared_ptr<InputDevice> device =
11924 std::make_shared<InputDevice>(mReader->getContext(), deviceId, DEVICE_GENERATION,
11925 identifier);
11926 mReader->pushNextDevice(device);
11927 mFakeEventHub->addDevice(eventHubId, name, classes);
11928 mReader->loopOnce();
11929 return device;
11930 }
11931
11932 template <class T, typename... Args>
11933 T& addControllerAndConfigure(Args... args) {
11934 T& controller = mDevice->addController<T>(EVENTHUB_ID, args...);
11935
11936 return controller;
11937 }
11938};
11939
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011940const char* PeripheralControllerTest::DEVICE_NAME = "device";
11941const char* PeripheralControllerTest::DEVICE_LOCATION = "BLUETOOTH";
11942const int32_t PeripheralControllerTest::DEVICE_ID = END_RESERVED_ID + 1000;
11943const int32_t PeripheralControllerTest::DEVICE_GENERATION = 2;
11944const int32_t PeripheralControllerTest::DEVICE_CONTROLLER_NUMBER = 0;
Dominik Laskowski2f01d772022-03-23 16:01:29 -070011945const ftl::Flags<InputDeviceClass> PeripheralControllerTest::DEVICE_CLASSES =
11946 ftl::Flags<InputDeviceClass>(0); // not needed for current tests
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011947const int32_t PeripheralControllerTest::EVENTHUB_ID = 1;
Chris Yee2b1e5c2021-03-10 22:45:12 -080011948
11949// --- BatteryControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011950class BatteryControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011951protected:
11952 void SetUp() override {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011953 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::BATTERY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011954 }
11955};
11956
11957TEST_F(BatteryControllerTest, GetBatteryCapacity) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011958 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011959
Harry Cuttsa5b71292022-11-28 12:56:17 +000011960 ASSERT_TRUE(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY));
11961 ASSERT_EQ(controller.getBatteryCapacity(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11962 FakeEventHub::BATTERY_CAPACITY);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011963}
11964
11965TEST_F(BatteryControllerTest, GetBatteryStatus) {
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011966 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011967
Harry Cuttsa5b71292022-11-28 12:56:17 +000011968 ASSERT_TRUE(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY));
11969 ASSERT_EQ(controller.getBatteryStatus(FakeEventHub::DEFAULT_BATTERY).value_or(-1),
11970 FakeEventHub::BATTERY_STATUS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080011971}
11972
11973// --- LightControllerTest ---
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011974class LightControllerTest : public PeripheralControllerTest {
Chris Yee2b1e5c2021-03-10 22:45:12 -080011975protected:
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011976 void SetUp() override {
11977 PeripheralControllerTest::SetUp(DEVICE_CLASSES | InputDeviceClass::LIGHT);
11978 }
Chris Yee2b1e5c2021-03-10 22:45:12 -080011979};
11980
Chris Ye85758332021-05-16 23:05:17 -070011981TEST_F(LightControllerTest, MonoLight) {
11982 RawLightInfo infoMono = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011983 .name = "mono_light",
Chris Ye85758332021-05-16 23:05:17 -070011984 .maxBrightness = 255,
11985 .flags = InputLightClass::BRIGHTNESS,
11986 .path = ""};
11987 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
Chris Yee2b1e5c2021-03-10 22:45:12 -080011988
Chris Ye1dd2e5c2021-04-04 23:12:41 -070011989 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080011990 InputDeviceInfo info;
11991 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000011992 std::vector<InputDeviceLightInfo> lights = info.getLights();
11993 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000011994 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
11995 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
11996
11997 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
11998 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
11999}
12000
12001TEST_F(LightControllerTest, MonoKeyboardBacklight) {
12002 RawLightInfo infoMono = {.id = 1,
12003 .name = "mono_keyboard_backlight",
12004 .maxBrightness = 255,
12005 .flags = InputLightClass::BRIGHTNESS |
12006 InputLightClass::KEYBOARD_BACKLIGHT,
12007 .path = ""};
12008 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12009
12010 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12011 InputDeviceInfo info;
12012 controller.populateDeviceInfo(&info);
12013 std::vector<InputDeviceLightInfo> lights = info.getLights();
12014 ASSERT_EQ(1U, lights.size());
12015 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12016 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012017
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012018 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_BRIGHTNESS));
12019 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_BRIGHTNESS);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012020}
12021
Vaibhav Devmurari16c24192023-05-04 15:20:12 +000012022TEST_F(LightControllerTest, Ignore_MonoLight_WithPreferredBacklightLevels) {
12023 RawLightInfo infoMono = {.id = 1,
12024 .name = "mono_light",
12025 .maxBrightness = 255,
12026 .flags = InputLightClass::BRIGHTNESS,
12027 .path = ""};
12028 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12029 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
12030 "0,100,200");
12031
12032 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12033 std::list<NotifyArgs> unused =
12034 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12035 /*changes=*/{});
12036
12037 InputDeviceInfo info;
12038 controller.populateDeviceInfo(&info);
12039 std::vector<InputDeviceLightInfo> lights = info.getLights();
12040 ASSERT_EQ(1U, lights.size());
12041 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
12042}
12043
12044TEST_F(LightControllerTest, KeyboardBacklight_WithNoPreferredBacklightLevels) {
12045 RawLightInfo infoMono = {.id = 1,
12046 .name = "mono_keyboard_backlight",
12047 .maxBrightness = 255,
12048 .flags = InputLightClass::BRIGHTNESS |
12049 InputLightClass::KEYBOARD_BACKLIGHT,
12050 .path = ""};
12051 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12052
12053 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12054 std::list<NotifyArgs> unused =
12055 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12056 /*changes=*/{});
12057
12058 InputDeviceInfo info;
12059 controller.populateDeviceInfo(&info);
12060 std::vector<InputDeviceLightInfo> lights = info.getLights();
12061 ASSERT_EQ(1U, lights.size());
12062 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
12063}
12064
12065TEST_F(LightControllerTest, KeyboardBacklight_WithPreferredBacklightLevels) {
12066 RawLightInfo infoMono = {.id = 1,
12067 .name = "mono_keyboard_backlight",
12068 .maxBrightness = 255,
12069 .flags = InputLightClass::BRIGHTNESS |
12070 InputLightClass::KEYBOARD_BACKLIGHT,
12071 .path = ""};
12072 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12073 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
12074 "0,100,200");
12075
12076 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12077 std::list<NotifyArgs> unused =
12078 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12079 /*changes=*/{});
12080
12081 InputDeviceInfo info;
12082 controller.populateDeviceInfo(&info);
12083 std::vector<InputDeviceLightInfo> lights = info.getLights();
12084 ASSERT_EQ(1U, lights.size());
12085 ASSERT_EQ(3U, lights[0].preferredBrightnessLevels.size());
12086 std::set<BrightnessLevel>::iterator it = lights[0].preferredBrightnessLevels.begin();
12087 ASSERT_EQ(BrightnessLevel(0), *it);
12088 std::advance(it, 1);
12089 ASSERT_EQ(BrightnessLevel(100), *it);
12090 std::advance(it, 1);
12091 ASSERT_EQ(BrightnessLevel(200), *it);
12092}
12093
12094TEST_F(LightControllerTest, KeyboardBacklight_WithWrongPreferredBacklightLevels) {
12095 RawLightInfo infoMono = {.id = 1,
12096 .name = "mono_keyboard_backlight",
12097 .maxBrightness = 255,
12098 .flags = InputLightClass::BRIGHTNESS |
12099 InputLightClass::KEYBOARD_BACKLIGHT,
12100 .path = ""};
12101 mFakeEventHub->addRawLightInfo(infoMono.id, std::move(infoMono));
12102 mFakeEventHub->addConfigurationProperty(EVENTHUB_ID, "keyboard.backlight.brightnessLevels",
12103 "0,100,200,300,400,500");
12104
12105 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12106 std::list<NotifyArgs> unused =
12107 mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(),
12108 /*changes=*/{});
12109
12110 InputDeviceInfo info;
12111 controller.populateDeviceInfo(&info);
12112 std::vector<InputDeviceLightInfo> lights = info.getLights();
12113 ASSERT_EQ(1U, lights.size());
12114 ASSERT_EQ(0U, lights[0].preferredBrightnessLevels.size());
12115}
12116
Chris Yee2b1e5c2021-03-10 22:45:12 -080012117TEST_F(LightControllerTest, RGBLight) {
12118 RawLightInfo infoRed = {.id = 1,
12119 .name = "red",
12120 .maxBrightness = 255,
12121 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12122 .path = ""};
12123 RawLightInfo infoGreen = {.id = 2,
12124 .name = "green",
12125 .maxBrightness = 255,
12126 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12127 .path = ""};
12128 RawLightInfo infoBlue = {.id = 3,
12129 .name = "blue",
12130 .maxBrightness = 255,
12131 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12132 .path = ""};
12133 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12134 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12135 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12136
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012137 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012138 InputDeviceInfo info;
12139 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012140 std::vector<InputDeviceLightInfo> lights = info.getLights();
12141 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012142 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12143 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12144 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12145
12146 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12147 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12148}
12149
12150TEST_F(LightControllerTest, CorrectRGBKeyboardBacklight) {
12151 RawLightInfo infoRed = {.id = 1,
12152 .name = "red_keyboard_backlight",
12153 .maxBrightness = 255,
12154 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED |
12155 InputLightClass::KEYBOARD_BACKLIGHT,
12156 .path = ""};
12157 RawLightInfo infoGreen = {.id = 2,
12158 .name = "green_keyboard_backlight",
12159 .maxBrightness = 255,
12160 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN |
12161 InputLightClass::KEYBOARD_BACKLIGHT,
12162 .path = ""};
12163 RawLightInfo infoBlue = {.id = 3,
12164 .name = "blue_keyboard_backlight",
12165 .maxBrightness = 255,
12166 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE |
12167 InputLightClass::KEYBOARD_BACKLIGHT,
12168 .path = ""};
12169 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12170 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12171 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12172
12173 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12174 InputDeviceInfo info;
12175 controller.populateDeviceInfo(&info);
12176 std::vector<InputDeviceLightInfo> lights = info.getLights();
12177 ASSERT_EQ(1U, lights.size());
12178 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12179 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12180 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12181
12182 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12183 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12184}
12185
12186TEST_F(LightControllerTest, IncorrectRGBKeyboardBacklight) {
12187 RawLightInfo infoRed = {.id = 1,
12188 .name = "red",
12189 .maxBrightness = 255,
12190 .flags = InputLightClass::BRIGHTNESS | InputLightClass::RED,
12191 .path = ""};
12192 RawLightInfo infoGreen = {.id = 2,
12193 .name = "green",
12194 .maxBrightness = 255,
12195 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GREEN,
12196 .path = ""};
12197 RawLightInfo infoBlue = {.id = 3,
12198 .name = "blue",
12199 .maxBrightness = 255,
12200 .flags = InputLightClass::BRIGHTNESS | InputLightClass::BLUE,
12201 .path = ""};
12202 RawLightInfo infoGlobal = {.id = 3,
12203 .name = "global_keyboard_backlight",
12204 .maxBrightness = 255,
12205 .flags = InputLightClass::BRIGHTNESS | InputLightClass::GLOBAL |
12206 InputLightClass::KEYBOARD_BACKLIGHT,
12207 .path = ""};
12208 mFakeEventHub->addRawLightInfo(infoRed.id, std::move(infoRed));
12209 mFakeEventHub->addRawLightInfo(infoGreen.id, std::move(infoGreen));
12210 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoBlue));
12211 mFakeEventHub->addRawLightInfo(infoBlue.id, std::move(infoGlobal));
12212
12213 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12214 InputDeviceInfo info;
12215 controller.populateDeviceInfo(&info);
12216 std::vector<InputDeviceLightInfo> lights = info.getLights();
12217 ASSERT_EQ(1U, lights.size());
12218 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12219 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12220 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012221
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012222 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12223 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012224}
12225
12226TEST_F(LightControllerTest, MultiColorRGBLight) {
12227 RawLightInfo infoColor = {.id = 1,
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012228 .name = "multi_color",
Chris Yee2b1e5c2021-03-10 22:45:12 -080012229 .maxBrightness = 255,
12230 .flags = InputLightClass::BRIGHTNESS |
12231 InputLightClass::MULTI_INTENSITY |
12232 InputLightClass::MULTI_INDEX,
12233 .path = ""};
12234
12235 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12236
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012237 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012238 InputDeviceInfo info;
12239 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012240 std::vector<InputDeviceLightInfo> lights = info.getLights();
12241 ASSERT_EQ(1U, lights.size());
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012242 ASSERT_EQ(InputDeviceLightType::INPUT, lights[0].type);
12243 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12244 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
12245
12246 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12247 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
12248}
12249
12250TEST_F(LightControllerTest, MultiColorRGBKeyboardBacklight) {
12251 RawLightInfo infoColor = {.id = 1,
12252 .name = "multi_color_keyboard_backlight",
12253 .maxBrightness = 255,
12254 .flags = InputLightClass::BRIGHTNESS |
12255 InputLightClass::MULTI_INTENSITY |
12256 InputLightClass::MULTI_INDEX |
12257 InputLightClass::KEYBOARD_BACKLIGHT,
12258 .path = ""};
12259
12260 mFakeEventHub->addRawLightInfo(infoColor.id, std::move(infoColor));
12261
12262 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
12263 InputDeviceInfo info;
12264 controller.populateDeviceInfo(&info);
12265 std::vector<InputDeviceLightInfo> lights = info.getLights();
12266 ASSERT_EQ(1U, lights.size());
12267 ASSERT_EQ(InputDeviceLightType::KEYBOARD_BACKLIGHT, lights[0].type);
12268 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12269 ASSERT_TRUE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012270
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012271 ASSERT_TRUE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12272 ASSERT_EQ(controller.getLightColor(lights[0].id).value_or(-1), LIGHT_COLOR);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012273}
12274
12275TEST_F(LightControllerTest, PlayerIdLight) {
12276 RawLightInfo info1 = {.id = 1,
12277 .name = "player1",
12278 .maxBrightness = 255,
12279 .flags = InputLightClass::BRIGHTNESS,
12280 .path = ""};
12281 RawLightInfo info2 = {.id = 2,
12282 .name = "player2",
12283 .maxBrightness = 255,
12284 .flags = InputLightClass::BRIGHTNESS,
12285 .path = ""};
12286 RawLightInfo info3 = {.id = 3,
12287 .name = "player3",
12288 .maxBrightness = 255,
12289 .flags = InputLightClass::BRIGHTNESS,
12290 .path = ""};
12291 RawLightInfo info4 = {.id = 4,
12292 .name = "player4",
12293 .maxBrightness = 255,
12294 .flags = InputLightClass::BRIGHTNESS,
12295 .path = ""};
12296 mFakeEventHub->addRawLightInfo(info1.id, std::move(info1));
12297 mFakeEventHub->addRawLightInfo(info2.id, std::move(info2));
12298 mFakeEventHub->addRawLightInfo(info3.id, std::move(info3));
12299 mFakeEventHub->addRawLightInfo(info4.id, std::move(info4));
12300
Chris Ye1dd2e5c2021-04-04 23:12:41 -070012301 PeripheralController& controller = addControllerAndConfigure<PeripheralController>();
Chris Yee2b1e5c2021-03-10 22:45:12 -080012302 InputDeviceInfo info;
12303 controller.populateDeviceInfo(&info);
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012304 std::vector<InputDeviceLightInfo> lights = info.getLights();
12305 ASSERT_EQ(1U, lights.size());
12306 ASSERT_EQ(InputDeviceLightType::PLAYER_ID, lights[0].type);
Vaibhav Devmurari82b37d62022-09-12 13:36:48 +000012307 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::BRIGHTNESS));
12308 ASSERT_FALSE(lights[0].capabilityFlags.test(InputDeviceLightCapability::RGB));
Chris Yee2b1e5c2021-03-10 22:45:12 -080012309
Siarhei Vishniakou1983a712021-06-04 19:27:09 +000012310 ASSERT_FALSE(controller.setLightColor(lights[0].id, LIGHT_COLOR));
12311 ASSERT_TRUE(controller.setLightPlayerId(lights[0].id, LIGHT_PLAYER_ID));
12312 ASSERT_EQ(controller.getLightPlayerId(lights[0].id).value_or(-1), LIGHT_PLAYER_ID);
Chris Yee2b1e5c2021-03-10 22:45:12 -080012313}
12314
Michael Wrightd02c5b62014-02-10 15:10:22 -080012315} // namespace android